|
|
| version 1.37, 2001/02/25 08:50:13 | version 1.55, 2001/03/11 07:52:42 |
|---|---|
| Line 1 | Line 1 |
| /* | /* |
| $Id$ | Parser |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexander Petrosyan <paf@design.ru> | |
| $Id$ | |
| */ | */ |
| #include "core.h" | |
| #include "_string.h" | |
| #include "_double.h" | |
| #include "_int.h" | |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_wwrapper.h" | |
| #include "pa_common.h" | |
| #include "pa_vclass.h" | |
| #include <stdio.h> | String *unnamed_name; |
| String *empty_string; | |
| String *auto_method_name; | |
| String *main_method_name; | |
| String *run_class_name; | |
| String *root_class_name; | |
| String *env_class_name; | |
| void core() { | void core() { |
| Pool pool; | Pool pool; |
| Request request(pool); | |
| request.core(); | // names |
| } | unnamed_name=new(pool) String(pool); unnamed_name->APPEND_CONST("unnamed"); |
| empty_string=new(pool) String(pool); | |
| void Request::core() { | auto_method_name=new(pool) String(pool); auto_method_name->APPEND_CONST(AUTO_METHOD_NAME); |
| TRY { | main_method_name=new(pool) String(pool); main_method_name->APPEND_CONST(MAIN_METHOD_NAME); |
| char *file="Y:\\parser3\\src\\test.p"; | |
| String RUN(pool()); RUN.APPEND_CONST(NAME_RUN); | run_class_name=new(pool) String(pool); run_class_name->APPEND_CONST(RUN_CLASS_NAME); |
| use(file, &RUN); | root_class_name=new(pool) String(pool); root_class_name->APPEND_CONST(ROOT_CLASS_NAME); |
| env_class_name=new(pool) String(pool); env_class_name->APPEND_CONST(ENV_CLASS_NAME); | |
| char *result=execute_MAIN(); | |
| printf("result-----------------\n%sEOF----------------\n", result); | // classes |
| } | initialize_string_class(pool, *(string_class=new(pool) VClass(pool))); |
| CATCH(e) { | initialize_double_class(pool, *(double_class=new(pool) VClass(pool))); |
| printf("\nERROR: "); | initialize_int_class(pool, *(int_class=new(pool) VClass(pool))); |
| 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(); | |
| if(type) { | |
| printf(" type: %s", type->cstr()); | |
| const String *code=e.code(); | |
| if(code) | |
| printf(", code: %s", code->cstr()); | |
| } | |
| printf("\n"); | |
| } | |
| END_CATCH | |
| } | |
| void Request::use(char *file, String *name) { | // request |
| char *source=file_read(pool(), file); | Request request(pool); |
| if(!source) | request.core(); |
| THROW(0,0, | |
| 0, | |
| "use: can not read '%s' file", file); | |
| VClass& vclass=COMPILE(source, file); | |
| 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); | |
| } | } |
| char *Request::execute_MAIN() { | |
| // locate class with @main & it's code | |
| String name_main(pool()); | |
| name_main.APPEND_CONST(MAIN_METHOD_NAME); | |
| // looking for latest known @main | |
| for(int i=classes_array().size(); --i>=0;) { | |
| VClass *vclass=static_cast<VClass *>(classes_array().get(i)); | |
| Value *main=vclass->get_element(name_main); | |
| if(main) { // found some 'main' element | |
| Junction *junction=main->get_junction(); | |
| if(junction) {// it even has junction! | |
| const Method *method=junction->method; | |
| if(method) { // and junction is method-junction! call it | |
| // initialize contexts | |
| 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; | |
| } |