|
|
| version 1.23, 2001/02/22 09:36:24 | version 1.36, 2001/02/24 15:35:28 |
|---|---|
| Line 3 $Id$ | Line 3 $Id$ |
| */ | */ |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_wcontext.h" | #include "pa_wwrapper.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_vclass.h" | #include "pa_vclass.h" |
| #include <stdio.h> | #include <stdio.h> |
| void core() { | void core() { |
| Exception exception; | Pool pool; |
| if(EXCEPTION_TRY(exception)) { | Request request(pool); |
| Pool pool(exception); | request.core(); |
| Request request(pool); | } |
| request.core(); | |
| } else { | void Request::core() { |
| Exception& e=exception; | TRY { |
| printf("fatal exception occured: %s\n", e.comment()); | char *file="Y:\\parser3\\src\\test.p"; |
| const String *type=e.type(); | String RUN(pool()); RUN.APPEND_CONST(NAME_RUN); |
| if(type) { | use(file, &RUN); |
| printf(" type: %s", type->cstr()); | |
| const String *code=e.code(); | char *result=execute_MAIN(); |
| if(code) | printf("result-----------------\n%sEOF----------------\n", result); |
| printf(", code: %s", code->cstr()); | } |
| printf("\n"); | CATCH(e) { |
| } | printf("\nERROR: "); |
| const String *problem_source=e.problem_source(); | const String *problem_source=e.problem_source(); |
| if(problem_source) { | if(problem_source) { |
| const Origin& origin=problem_source->origin(); | const Origin& origin=problem_source->origin(); |
| printf(" '%s'\n", | |
| problem_source->cstr()); | |
| if(origin.file) | if(origin.file) |
| printf(" [%s:%d]", | printf("%s(%d): ", |
| origin.file, origin.line); | origin.file, 1+origin.line); |
| printf("\n"); | printf("'%s' ", |
| problem_source->cstr()); | |
| } | } |
| } | printf("%s", e.comment()); |
| } | |
| void Request::core() { | |
| Exception local_exception; | |
| Local_request_exception subst(*this, local_exception); | |
| if(EXCEPTION_TRY(local_exception)) { | |
| String name_RUN(pool()); name_RUN.APPEND_CONST("RUN"); | |
| char *result=execute_MAIN(construct_class(name_RUN, load_and_compile_RUN())); | |
| printf("-----------------\n%s\n----------------\n", result); | |
| } else { | |
| Exception& e=exception(); | |
| printf("operator error occured: %s\n", 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="Y:\\parser3\\src\\test.p"; | |
| char *source=file_read(pool(), file); | char *source=file_read(pool(), file); |
| Array& compiled_methods=COMPILE(source, file); | VClass& vclass=COMPILE(source, file); |
| return compiled_methods; | if(name) // they forced some name? |
| vclass.set_name(*name); | |
| name=vclass.name(); | |
| if(!name) | |
| return; //TODO: add operators | |
| classes_array()+=&vclass; | |
| classes().put(*name, &vclass); | |
| } | } |
| VClass *Request::construct_class(String& name, Array& compiled_methods) { | char *Request::execute_MAIN() { |
| // create new 'name' vclass, add it to request's classes | // locate class with @main & it's code |
| Array immediate_parents(pool()); | |
| // TODO: immediate_parents=@PARENTS | |
| VClass *result=new(pool()) VClass(pool(), name, immediate_parents); | |
| classes().put(name, result); | |
| for(int i=0; i<compiled_methods.size(); i++) { | |
| // TODO: filter out @PARENTS & ?co? | |
| Method &method=*static_cast<Method *>(compiled_methods.quick_get(i)); | |
| result->add_method(method.name, method); | |
| } | |
| return result; | |
| } | |
| char *Request::execute_MAIN(VClass *class_RUN) { | |
| // initialize contexts | |
| root=self=rcontext=class_RUN; | |
| wcontext=new(pool()) WContext(pool(), class_RUN); | |
| // locate @main code | |
| String name_main(pool()); | 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;) { |
| 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_NAME"' 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(); | self=root=rcontext=vclass; |
| wcontext=NEW WWrapper(pool(), vclass); | |
| // execute! | |
| execute(method->code); | |
| // return chars | |
| return wcontext->get_string()->cstr(); | |
| } | |
| } | |
| } | |
| } | |
| THROW(0,0, | |
| 0, | |
| "'"MAIN_METHOD_NAME"' method not found"); | |
| return 0; | |
| } | } |