--- parser3/src/main/Attic/core.C 2001/02/11 19:35:39 1.1 +++ parser3/src/main/Attic/core.C 2001/02/23 21:59:07 1.30 @@ -1,122 +1,101 @@ -void process(Pool& pool, - Value& self, - RContext& rcontext, WContext& wcontext, - StringIterator& code, char char_to_stop_before) { - - // $ on code? - process_dollar(pool, rcontext, wcontext, code, char_to_stop_before); - - // ^ on code? - process_bird(pool, rcontext, wcontext, code, char_to_stop_before); +/* +$Id: core.C,v 1.30 2001/02/23 21:59:07 paf Exp $ +*/ + +#include "pa_request.h" +#include "pa_wwrapper.h" +#include "pa_common.h" +#include "pa_vclass.h" + +#include + +void core() { + Pool pool; + Request request(pool); + request.core(); } -void process_dollar(Pool& pool, - Value& THIS, - RContext& rcontext, WContext& wcontext, - StringIterator& iter, char char_to_stop_before) { - - // $name.field.subfield -- read - // $name.field.subfield(constructor code) -- construct - // $name.field.subfield[usage code & if none existed autoconstructed as VHash] -- use OR auto-VHash construct - - Array/**/ names(pool); // what.they.refer.to left-to-right list - char names_ended_before; // the char after long name - get_names( - iter, " ([", - &names, &names_ended_before); // must return count()>0 - - bool read_mode=name_ended_before==' '; - Value *context=read_mode?rcontext:wcontext; - - if(read_mode) { - for(int i=0; iget_element(static_cast(names.get[i])); - if(!context) // no such object field, nothing bad, just ignore that - return; +void Request::core() { + TRY { + 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); + } + CATCH(e) { + printf("error occured: %s\n", 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"); } - wcontext.write(context); - } else { // write mode - iter++; // skip '(' '[' - - bool construct_mode=names_ended_before=='('; - - int steps=names.count(); - if(construct_mode) - steps--; - for(int i=0; i(names.get[i]); - Value *next_current=context->get_element(name); - if(next_current) - next_current=context->put_element(name, new(pool) VHash(pool)); - context=new_current; + 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, 1+origin.line); + printf("\n"); } + } + END_CATCH +} - if(construct_mode) { - // pure side effect, no wcontext.write here - String& name=static_cast(names.get[steps]); - WContext local_wcontext(pool /* empty */); - process(pool, rcontext, local_wcontext, iter, ')'); - context->put_element(name, local_wcontext.value()); - } else { // =='[' - WContext local_context(pool, context); - process(pool, local_context, local_context, iter, ']'); - wcontext.write(local_context); - } +Array& Request::load_and_compile_RUN() { + char *file="Y:\\parser3\\src\\test.p"; + char *source=file_read(pool(), file); + Array& compiled_methods=COMPILE(source, file); + return compiled_methods; +} + +VClass *Request::construct_class(String& name, Array& compiled_methods) { + // create new 'name' vclass, add it to request's classes + Array immediate_parents(pool()); + // TODO: immediate_parents=@PARENTS + + VClass *result=NEW VClass(pool(), name, immediate_parents); + result->set_name(name); + classes().put(name, result); - iter++; // skip ')' ']' + for(int i=0; i(compiled_methods.quick_get(i)); + result->add_method(method.name, method); } + + return result; } -void process_bird(Pool& pool, - Value& self, - RContext& rcontext, WContext& wcontext, - StringIterator& iter, char char_to_stop_before) { - - // ^name.field.subfield.method[..] -- plain call - // ^name.field.subfield.method_ref[..] -- method ref call, when .get_method()!=0 - - Array/**/ names(pool); // what.they.refer.to left-to-right list - char names_ended_before; // the char after long name - get_names( - iter, "[", - &names, &names_ended_before); // must return count()>0 - - Value *context=rcontext; - iter++; // skip '[' - - Value *local_self=context; - int steps=names.count()-1; - for(int i=0; i(names.get[i]); - context=(local_self=context)->get_element(name); - if(!context) // no such object field, sad story: can't call method of void - pool.exception().raise(name, "call: to void.method"); - } - - String& name=static_cast(names.get[steps]); - // first we're trying to locate method with that 'name' - Method *method=context.get_method(name); - if(!method) { // no such method: try to locate method ref field - Value *value=context.get_element(name); - if(!value) // failed: no element of that 'name' - pool.exception().raise(name, "call: no method field found"); - method=value->get_method(); - if(!method) // failed: that field wasn't method_ref - pool.exception().raise(name, "call: this field is not a method reference"); - local_self=value->get_self(); - } +char *Request::execute_MAIN(VClass *class_RUN) { + // initialize contexts + root=self=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(), + "no '"MAIN_METHOD_NAME"' method in class"); + + Junction *junction_main=value_main->get_junction(); + const Method *method_main=junction_main->method; + + if(!method_main) + THROW(0,0, + &class_RUN->name(), + "'"MAIN_METHOD_NAME"' in class is not a method"); + // execute! + execute(method_main->code); - Array/**/ param_values(pool); - get_params( - iter, "]", - ¶m_values); - iter++; // skip ']' - - Method_this_n_params local_rcontext(pool, - local_self, - method->param_names, param_values); - WContext local_wcontext(pool /* empty */); - process(pool, local_rcontext, local_wcontext, iter, ']'); - wcontext.write(local_wcontext); -} \ No newline at end of file + // return chars + return wcontext->get_string()->cstr(); +}