|
|
| version 1.2, 2001/02/21 17:36:58 | version 1.3, 2001/03/10 11:44:42 |
|---|---|
| Line 3 $Id$ | Line 3 $Id$ |
| */ | */ |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "compile.h" | #include "pa_wwrapper.h" |
| #include "pa_context.h" | #include "pa_common.h" |
| #include "pa_vclass.h" | |
| #include "classes/_root.h" | |
| #include "classes/_env.h" | |
| #include <stdio.h> | |
| Request::Request(Pool& apool) : Pooled(apool), | |
| stack(apool), | |
| root_class(apool), | |
| env_class(apool), | |
| fclasses(apool), | |
| fclasses_array(apool), | |
| lang(String::Untaint_lang::HTML_TYPO) | |
| { | |
| // root class | |
| initialize_root_class(pool(), root_class); | |
| // adding root superclass, | |
| // parent of all classes, | |
| // operators holder | |
| String ROOT(pool()); ROOT.APPEND_CONST(ROOT_NAME); | |
| classes().put(ROOT, &root_class); | |
| // env class | |
| initialize_env_class(pool(), env_class); | |
| String ENV(pool()); ENV.APPEND_CONST(ENV_NAME); | |
| classes().put(ENV, &env_class); | |
| } | |
| void Request::core() { | void Request::core() { |
| Exception exception; | TRY { |
| Local_request_exception subst(*this, exception); | char *file="Y:\\parser3\\src\\test.p"; |
| if(EXCEPTION_TRY(exception())) { | String RUN(pool()); RUN.APPEND_CONST(RUN_NAME); |
| String name_RUN(pool()); name_RUN.APPEND_CONST("RUN"); | use(file, &RUN); |
| execute_MAIN(construct_class(name_RUN, load_and_compile_RUN()))); | |
| } else { | char *result=execute_MAIN(); |
| Exception& e=exception(); | printf("result-----------------\n%sEOF----------------\n", result); |
| printf("operator error occured: %s\n", e.comment()); | } |
| CATCH(e) { | |
| printf("\nERROR: "); | |
| const String *problem_source=e.problem_source(); | |
| if(problem_source) { | |
| const Origin& origin=problem_source->origin(); | |
| if(origin.file) | |
| printf("%s(%d): ", | |
| origin.file, 1+origin.line); | |
| printf("'%s' ", | |
| problem_source->cstr()); | |
| } | |
| printf("%s", e.comment()); | |
| const String *type=e.type(); | const String *type=e.type(); |
| if(type) { | if(type) { |
| printf(" type: %s", type->cstr()); | printf(" type: %s", type->cstr()); |
| const String *code=e.code(); | const String *code=e.code(); |
| if(code) | if(code) |
| printf(", code: %s", code->cstr()); | printf(", code: %s", code->cstr()); |
| printf("\n"); | |
| } | |
| const String *problem_source=e.problem_source(); | |
| if(problem_source) { | |
| const Origin& origin=problem_source->origin(); | |
| printf(" '%s'\n", | |
| problem_source->cstr()); | |
| if(origin.file) | |
| printf(" [%s:%d]", | |
| origin.file, origin.line); | |
| printf("\n"); | |
| } | } |
| printf("\n"); | |
| } | } |
| END_CATCH | |
| } | } |
| Array& Request::load_and_compile_RUN() { | void Request::use(char *file, String *name) { |
| char *file="c:\\temp\\test.p"; | // TODO: обнаружить|решить cyclic dependences |
| char *source=file_read(pool(), file); | char *source=file_read(pool(), file); |
| Array& compiled_methods=COMPILE(request, source, file); | if(!source) |
| return compiled_methods; | THROW(0,0, |
| } | 0, |
| "use: can not read '%s' file", file); | |
| VClass *Request::construct_class(String& name, Array& compiled_methods) { | COMPILE(source, name, file); |
| // create new 'name' vclass, add it to request's classes | // TODO: запустить @STATIC[], если есть |
| Array immediate_parents(pool); | |
| // TODO: immediate_parents=@PARENTS | |
| VClass *vclass=new(pool) VClass(pool, name, immediate_parents); | |
| request.classes().put(name, vclass); | |
| for(int i=0; i<compiled_methods.size(); i++) { | |
| // TODO: filter out @PARENTS & ?co? | |
| Method &method=*static_cast<Method *>(compiled_methods.quick_get(i)); | |
| vclass->add_method(method.name, method); | |
| } | |
| } | |
| char *Request::execute_MAIN(Value *class_RUN) { | // if(alias) |
| // initialize contexts | //classes().put(*alias, &vclass); |
| root=self=rcontext=class_RUN; | } |
| wcontext=new(pool()) WContext(pool()); | |
| // locate @main code | char *Request::execute_MAIN() { |
| String name_main(pool); | // locate class with @main & it's code |
| String name_main(pool()); | |
| name_main.APPEND_CONST(MAIN_METHOD_NAME); | name_main.APPEND_CONST(MAIN_METHOD_NAME); |
| Method *method_main=class_RUN->get_method(name_main); | // looking for latest known @main |
| if(!method_main) | for(int i=classes_array().size(); --i>=0;) { |
| request.exception().raise(0,0, | VClass *vclass=static_cast<VClass *>(classes_array().get(i)); |
| &class_RUN->name(), | Value *main=vclass->get_element(name_main); |
| "no 'main' method in class"); | if(main) { // found some 'main' element |
| Junction *junction=main->get_junction(); | |
| // execute! | if(junction) {// it even has junction! |
| execute(method_main->code); | const Method *method=junction->method; |
| if(method) { // and junction is method-junction! call it | |
| // return chars | // initialize contexts |
| return wcontext.get_string()->cstr(); | root=rcontext=self=vclass; |
| wcontext=NEW WWrapper(pool(), vclass, false /* not constructing */); | |
| // execute! | |
| execute(*method->parser_code); | |
| // return chars | |
| return wcontext->get_string()->cstr(); | |
| } | |
| } | |
| } | |
| } | |
| THROW(0,0, | |
| 0, | |
| "'"MAIN_METHOD_NAME"' method not found"); | |
| return 0; | |
| } | } |