|
|
| version 1.33, 2001/02/24 13:21:59 | version 1.65, 2001/03/13 16:38:23 |
|---|---|
| Line 1 | Line 1 |
| /* | /* |
| $Id$ | Parser |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | |
| */ | */ |
| #include "core.h" | |
| #include "_string.h" | |
| #include "_double.h" | |
| #include "_int.h" | |
| #include "_table.h" | |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_wwrapper.h" | #include "_form.h" |
| #include "pa_common.h" | |
| #include "pa_vclass.h" | |
| #include <stdio.h> | #define NEW_STRING(name, value) name=new(pool) String(pool); name->APPEND_CONST(value) |
| #define LOCAL_STRING(name, value) String name(pool); name.APPEND_CONST(value) | |
| void core() { | String *unnamed_name; |
| Pool pool; | String *empty_string; |
| Request request(pool); | |
| request.core(); | |
| } | |
| void Request::core() { | String *auto_method_name; |
| TRY { | String *main_method_name; |
| String name_RUN(pool()); name_RUN.APPEND_CONST("RUN"); | |
| char *result=execute_MAIN(construct_class(name_RUN, load_and_compile_RUN())); | |
| printf("result-----------------\n%s\nEOF----------------\n", result); | |
| } | |
| 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(); | |
| if(type) { | |
| printf(" type: %s", type->cstr()); | |
| const String *code=e.code(); | |
| if(code) | |
| printf(", code: %s", code->cstr()); | |
| } | |
| printf("\n"); | |
| } | |
| END_CATCH | |
| } | |
| Array& Request::load_and_compile_RUN() { | String *root_class_name; |
| char *file="Y:\\parser3\\src\\test.p"; | String *main_class_name; |
| char *source=file_read(pool(), file); | String *env_class_name; |
| Array& compiled_methods=COMPILE(source, file); | String *table_class_name; |
| return compiled_methods; | String *form_class_name; |
| } | |
| VClass *Request::construct_class(String& name, Array& compiled_methods) { | Hash *untaint_lang_name2enum; |
| // create new 'name' vclass, add it to request's classes | |
| Array immediate_parents(pool()); | |
| // TODO: immediate_parents=@PARENTS | |
| VClass *result=NEW VClass(pool(), immediate_parents); | |
| result->set_name(name); | |
| classes().put(name, result); | |
| for(int i=0; i<compiled_methods.size(); i++) { | |
| // TODO: filter out @PARENTS & @CLASS & ?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 | |
| self=root=rcontext=class_RUN; | |
| wcontext=NEW WWrapper(pool(), class_RUN); | |
| // locate @main code | |
| String name_main(pool()); | |
| name_main.APPEND_CONST(MAIN_METHOD_NAME); | |
| Value *value_main=class_RUN->get_element(name_main); | |
| if(!value_main) | |
| THROW(0,0, | |
| class_RUN->name(), "class does not contain method '"MAIN_METHOD_NAME"'"); | |
| Junction *junction_main=value_main->get_junction(); | |
| const Method *method_main=junction_main->method; | |
| if(!method_main) | |
| THROW(0,0, | |
| class_RUN->name(), | |
| "class does contain '"MAIN_METHOD_NAME"' but it is not a method"); | |
| // execute! | void core() { |
| execute(method_main->code); | Pool pool; |
| // names | |
| NEW_STRING(unnamed_name, UNNAMED_NAME); | |
| empty_string=new(pool) String(pool); | |
| NEW_STRING(auto_method_name, AUTO_METHOD_NAME); | |
| NEW_STRING(main_method_name, MAIN_METHOD_NAME); | |
| NEW_STRING(root_class_name, ROOT_CLASS_NAME); | |
| NEW_STRING(main_class_name, MAIN_CLASS_NAME); | |
| NEW_STRING(table_class_name, TABLE_CLASS_NAME); | |
| NEW_STRING(env_class_name, ENV_CLASS_NAME); | |
| NEW_STRING(form_class_name, FORM_CLASS_NAME); | |
| // hashes | |
| untaint_lang_name2enum=new(pool) Hash(pool); | |
| LOCAL_STRING(as_is, "as-is"); | |
| untaint_lang_name2enum->put(as_is, (int)String::Untaint_lang::AS_IS); | |
| LOCAL_STRING(table, "table"); | |
| untaint_lang_name2enum->put(table, (int)String::Untaint_lang::TABLE); | |
| LOCAL_STRING(sql, "sql"); | |
| untaint_lang_name2enum->put(sql, (int)String::Untaint_lang::SQL); | |
| LOCAL_STRING(js, "js"); | |
| untaint_lang_name2enum->put(js, (int)String::Untaint_lang::JS); | |
| LOCAL_STRING(html, "html"); | |
| untaint_lang_name2enum->put(html, (int)String::Untaint_lang::HTML); | |
| LOCAL_STRING(html_typo, "html-typo"); | |
| untaint_lang_name2enum->put(html_typo, (int)String::Untaint_lang::HTML_TYPO); | |
| // read-only stateless classes | |
| initialize_string_class(pool, *(string_class=new(pool) VStateless_class(pool))); string_class->freeze(); | |
| initialize_double_class(pool, *(double_class=new(pool) VStateless_class(pool))); double_class->freeze(); | |
| initialize_int_class(pool, *(int_class=new(pool) VStateless_class(pool))); int_class->freeze(); | |
| initialize_table_class(pool, *(table_class=new(pool) VStateless_class(pool))); table_class->freeze(); | |
| // read-only stateless base classes | |
| initialize_env_base_class(pool, *(env_base_class=new(pool) VStateless_class(pool))); env_base_class->freeze(); | |
| initialize_form_base_class(pool, *(form_base_class=new(pool) VStateless_class(pool))); form_base_class->freeze(); | |
| // return chars | // request |
| return wcontext->get_string()->cstr(); | Request request(pool); |
| request.core(); | |
| } | } |