Diff for /parser3/src/main/execute.C between versions 1.169 and 1.193

version 1.169, 2001/06/28 07:44:17 version 1.193, 2001/10/13 15:12:50
Line 2 Line 2
         Parser: executor part of request class.          Parser: executor part of request class.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
   
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
   
           $Id$
 */  */
 static const char *RCSId="$Id$";   
   
 #include "pa_opcode.h"  #include "pa_opcode.h"
 #include "pa_array.h"   #include "pa_array.h" 
Line 22  static const char *RCSId="$Id$"; Line 22  static const char *RCSId="$Id$";
 #include "pa_vfile.h"  #include "pa_vfile.h"
 #include "pa_vimage.h"  #include "pa_vimage.h"
   
 //#define DEBUG_EXECUTE  #define DEBUG_EXECUTE
   
 const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;  const uint ANTI_ENDLESS_EXECUTE_RECOURSION=400;
   
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
 char *opcode_name[]={  char *opcode_name[]={
Line 32  char *opcode_name[]={ Line 32  char *opcode_name[]={
         "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",          "VALUE",  "CURLY_CODE__STORE_PARAM",  "EXPR_CODE__STORE_PARAM",
   
         // actions          // actions
         "WITH_SELF",    "WITH_ROOT",    "WITH_READ",    "WITH_WRITE",          "WITH_ROOT",    "WITH_SELF",    "WITH_READ",    "WITH_WRITE",
         "GET_CLASS",          "GET_CLASS",
         "CONSTRUCT_VALUE",  "CONSTRUCT_DOUBLE",          "CONSTRUCT_VALUE", "CONSTRUCT_EXPR", "CURLY_CODE__CONSTRUCT",
         "WRITE_VALUE",  "WRITE_EXPR_RESULT",  "STRING__WRITE",          "WRITE_VALUE",  "WRITE_EXPR_RESULT",  "STRING__WRITE",
         "GET_ELEMENT",  "GET_ELEMENT__WRITE",          "GET_ELEMENT",  "GET_ELEMENT__WRITE",
         "CREATE_EWPOOL",        "REDUCE_EWPOOL",          "CREATE_EWPOOL",        "REDUCE_EWPOOL",
         "CREATE_RWPOOL",        "REDUCE_RWPOOL",  
         "CREATE_SWPOOL",        "REDUCE_SWPOOL",          "CREATE_SWPOOL",        "REDUCE_SWPOOL",
         "GET_METHOD_FRAME",          "GET_METHOD_FRAME",
         "STORE_PARAM",          "STORE_PARAM",
         "CALL",          "PREPARE_TO_CONSTRUCT_OBJECT",  "CALL",
   
         // expression ops: unary          // expression ops: unary
         "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",          "NEG", "INV", "NOT", "DEF", "IN", "FEXISTS", "DEXISTS",
Line 69  void debug_printf(Pool& pool, const char Line 68  void debug_printf(Pool& pool, const char
 }  }
   
 void debug_dump(Pool& pool, int level, const Array& ops) {  void debug_dump(Pool& pool, int level, const Array& ops) {
         {          Array_iter i(ops);
                 int size=ops.quick_size();          while(i.has_next()) {
                 //debug_printf(pool, "size=%d\n", size);  
                 for(int i=0; i<size; i++) {  
                         Operation op;  
                         op.cast=ops.quick_get(i);  
                         debug_printf(pool, "%8X\n", op.cast);  
                 }  
         }  
   
         int size=ops.quick_size();  
         //debug_printf(pool, "size=%d\n", size);  
         for(int i=0; i<size; i++) {  
                 Operation op;                  Operation op;
                 op.cast=ops.quick_get(i);                  op.cast=i.next();
   
                 if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {                  if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
                         Value *value=static_cast<Value *>(ops.quick_get(++i));                          Value *value=static_cast<Value *>(i.next());
                         debug_printf(pool,                           debug_printf(pool, 
                                 "%*s%s"                                  "%*s%s"
                                 " \"%s\" %s",                                   " \"%s\" %s", 
Line 96  void debug_dump(Pool& pool, int level, c Line 84  void debug_dump(Pool& pool, int level, c
                 }                  }
                 debug_printf(pool, "%*s%s", level*4, "", opcode_name[op.code]);                  debug_printf(pool, "%*s%s", level*4, "", opcode_name[op.code]);
   
                 if(op.code==OP_CURLY_CODE__STORE_PARAM || op.code==OP_EXPR_CODE__STORE_PARAM) {                  switch(op.code) {
                         const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));                  case OP_CURLY_CODE__STORE_PARAM: 
                   case OP_EXPR_CODE__STORE_PARAM:
                   case OP_CURLY_CODE__CONSTRUCT:
                           const Array *local_ops=reinterpret_cast<const Array *>(i.next());
                         debug_dump(pool, level+1, *local_ops);                          debug_dump(pool, level+1, *local_ops);
                 }                  }
         }          }
Line 115  void Request::execute(const Array& ops) Line 106  void Request::execute(const Array& ops)
         debug_printf(pool(), "execution-------------------------\n");          debug_printf(pool(), "execution-------------------------\n");
 #endif  #endif
   
         int size=ops.quick_size();          Array_iter i(ops);
         //debug_printf(pool(), "size=%d\n", size);          while(i.has_next()) {
         for(int i=0; i<size; i++) {  
                 Operation op;                  Operation op;
                 op.cast=ops.quick_get(i);                  op.cast=i.next();
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
                 debug_printf(pool(), "%d:%s", stack.top_index()+1, opcode_name[op.code]);                  debug_printf(pool(), "%d:%s", stack.top_index()+1, opcode_name[op.code]);
 #endif  #endif
Line 128  void Request::execute(const Array& ops) Line 118  void Request::execute(const Array& ops)
                 // param in next instruction                  // param in next instruction
                 case OP_VALUE:                  case OP_VALUE:
                         {                          {
                                 Value *value=static_cast<Value *>(ops.quick_get(++i));                                  Value *value=static_cast<Value *>(i.next());
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
                                 debug_printf(pool(), " \"%s\" %s", value->get_string()->cstr(), value->type());                                  debug_printf(pool(), " \"%s\" %s", value->get_string()->cstr(), value->type());
 #endif  #endif
Line 140  void Request::execute(const Array& ops) Line 130  void Request::execute(const Array& ops)
                         {                          {
                                 VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());                                  VMethodFrame *frame=static_cast<VMethodFrame *>(stack.top_value());
                                 // code                                  // code
                                 const Array *local_ops=reinterpret_cast<const Array *>(ops.quick_get(++i));                                  const Array *local_ops=reinterpret_cast<const Array *>(i.next());
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
                                 debug_printf(pool(), " (%d)\n", local_ops->size());                                  debug_printf(pool(), " (%d)\n", local_ops->size());
                                 debug_dump(pool(), 1, *local_ops);                                  debug_dump(pool(), 1, *local_ops);
Line 182  void Request::execute(const Array& ops) Line 172  void Request::execute(const Array& ops)
                         }                          }
                                                   
                 // OP_WITH                  // OP_WITH
                 case OP_WITH_SELF:                   case OP_WITH_ROOT:
                         {                          {
                                 PUSH(self);                                  PUSH(root);
                                 break;                                  break;
                         }                          }
                 case OP_WITH_ROOT:                   case OP_WITH_SELF: 
                         {                          {
                                 PUSH(root);                                  PUSH(self);
                                 break;                                  break;
                         }                          }
                 case OP_WITH_READ:                   case OP_WITH_READ: 
Line 222  void Request::execute(const Array& ops) Line 212  void Request::execute(const Array& ops)
                                 value->set_name(name);                                  value->set_name(name);
                                 break;                                  break;
                         }                          }
                   case OP_CURLY_CODE__CONSTRUCT:
                           {
                                   const Array *local_ops=reinterpret_cast<const Array *>(i.next());
   #ifdef DEBUG_EXECUTE
                                   debug_printf(pool(), " (%d)\n", local_ops->size());
                                   debug_dump(pool(), 1, *local_ops);
   #endif                          
                                   Junction& j=*NEW Junction(pool(), 
                                           *self, 0, 0,
                                           root, 
                                           rcontext, 
                                           wcontext, 
                                           local_ops);
                                   
                                   Value *value=NEW VJunction(j);
                                   const String& name=POP_NAME();
                                   Value *ncontext=POP();
                                   ncontext->put_element(name, value);
                                   value->set_name(name);
                                   break;
                           }
                 case OP_WRITE_VALUE:                  case OP_WRITE_VALUE:
                         {                          {
                                 Value *value=POP();                                  Value *value=POP();
Line 240  void Request::execute(const Array& ops) Line 251  void Request::execute(const Array& ops)
                         }                          }
                 case OP_STRING__WRITE:                  case OP_STRING__WRITE:
                         {                          {
                                 VString *vstring=static_cast<VString *>(ops.quick_get(++i));                                  VString *vstring=static_cast<VString *>(i.next());
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
                                 debug_printf(pool(), " \"%s\"", vstring->string().cstr());                                  debug_printf(pool(), " \"%s\"", vstring->string().cstr());
 #endif  #endif
Line 253  void Request::execute(const Array& ops) Line 264  void Request::execute(const Array& ops)
                                 // maybe they do ^object.method[] call, remember the fact                                  // maybe they do ^object.method[] call, remember the fact
                                 wcontext->inc_somebody_entered_some_object();                                  wcontext->inc_somebody_entered_some_object();
   
                                   //_asm int 3;
                                 Value *value=get_element();                                  Value *value=get_element();
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
Line 271  void Request::execute(const Array& ops) Line 283  void Request::execute(const Array& ops)
                                 PUSH(wcontext);                                  PUSH(wcontext);
                                 PUSH((void *)flang);                                  PUSH((void *)flang);
                                 flang=String::UL_PASS_APPENDED;                                  flang=String::UL_PASS_APPENDED;
                                 wcontext=NEW WWrapper(pool(), 0 /*empty*/, true /*constructing*/);                                  wcontext=NEW WWrapper(pool(), 0 /*empty*/);
                                 break;                                  break;
                         }                          }
                 case OP_REDUCE_EWPOOL:                  case OP_REDUCE_EWPOOL:
Line 283  void Request::execute(const Array& ops) Line 295  void Request::execute(const Array& ops)
                                 break;                                  break;
                         }                          }
                                                   
                 case OP_CREATE_RWPOOL:  
                         {  
                                 Value *ncontext=POP();  
                                 PUSH(rcontext);  
                                 PUSH(wcontext);  
                                 rcontext=ncontext;  
                                 wcontext=NEW WWrapper(pool(), ncontext, false /*not constructing*/);  
                                 break;  
                         }  
                 case OP_REDUCE_RWPOOL:  
                         {  
                                 const String *string=wcontext->get_string();  
                                 Value *value;  
                                 if(string)  
                                         value=NEW VString(*string);  
                                 else  
                                         value=NEW VVoid(pool());  
                                 wcontext=static_cast<WContext *>(POP());  
                                 rcontext=POP();  
                                 PUSH(value);  
                                 break;  
                         }  
                 case OP_CREATE_SWPOOL:                  case OP_CREATE_SWPOOL:
                         {                          {
                                 PUSH(wcontext);                                  PUSH(wcontext);
                                 wcontext=NEW WWrapper(pool(), 0 /*empty*/, false /*not constructing*/);                                  wcontext=NEW WWrapper(pool(), 0 /*empty*/);
                                 break;                                  break;
                         }                          }
                 case OP_REDUCE_SWPOOL:                  case OP_REDUCE_SWPOOL:
Line 336  void Request::execute(const Array& ops) Line 326  void Request::execute(const Array& ops)
                                 //      not a code-junction                                  //      not a code-junction
                                 Junction *junction=value->get_junction();                                  Junction *junction=value->get_junction();
                                 if(!junction)                                  if(!junction)
                                         THROW(0,0,                                          THROW(0, 0,
                                                 &value->name(),                                                  &value->name(),
                                                 "(%s) not a method or junction, can not call it",                                                  "(%s) not a method or junction, can not call it",
                                                         value->type());                                                           value->type()); 
   
                                 bool is_constructor=                                  VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction);
                                         wcontext->constructing() && // constructing?  
                                         wcontext->somebody_entered_some_class(); // ^class:method[..]?  
                                 if(is_constructor)  
                                         wcontext->constructing(false);  
   
                                 VMethodFrame *frame=NEW VMethodFrame(pool(), value->name(), *junction, is_constructor);  
                                 PUSH(frame);                                  PUSH(frame);
                                 break;                                  break;
                         }                          }
Line 359  void Request::execute(const Array& ops) Line 343  void Request::execute(const Array& ops)
                                 break;                                  break;
                         }                          }
   
                   case OP_PREPARE_TO_CONSTRUCT_OBJECT:
                           {
                                   wcontext->constructing(true);
                                   break;
                           }
                 case OP_CALL:                  case OP_CALL:
                         {                          {
 #ifdef DEBUG_EXECUTE  #ifdef DEBUG_EXECUTE
Line 372  void Request::execute(const Array& ops) Line 361  void Request::execute(const Array& ops)
                                 PUSH(wcontext);                                   PUSH(wcontext); 
                                                                   
                                 VStateless_class *called_class=frame->junction.self.get_class();                                  VStateless_class *called_class=frame->junction.self.get_class();
                                 // not ^name.method call and                                  if(wcontext->constructing()) {
                                 // is context object or class & is it my class or my parent's class and?                                          wcontext->constructing(false);
                                 VStateless_class *read_class=rcontext->get_class();                                          if(frame->junction.method->call_type!=Method::CT_STATIC) {
                                 if(!wcontext->somebody_entered_some_object() &&   
                                         read_class && read_class->is_or_derived_from(*called_class)) // yes  
                                         self=rcontext; // class dynamic call  
                                 else // no, not me or relative of mine (total stranger)  
                                         // were are constructing something and  
                                         // our constructor is just method call and  
                                         // not static-only-method call  
                                         if(frame->is_constructor &&   
                                                 wcontext->somebody_entered_some_object()==1 &&  
                                                 frame->junction.method->call_type!=Method::CT_STATIC) {  
                                                 // this is a constructor call                                                  // this is a constructor call
                                                 // some stateless_object creatable derivates  
                                                 if(Value *value=called_class->create_new_value(pool()))                                                  if(Value *value=called_class->create_new_value(pool())) {
                                                           // some stateless_object creatable derivates
                                                         self=value;                                                          self=value;
                                                 else // stateful object                                                  } else {
                                                           // stateful object
                                                         self=NEW VObject(pool(), *called_class);                                                          self=NEW VObject(pool(), *called_class);
                                                   }
                                                 frame->write(*self,                                                   frame->write(*self, 
                                                         String::UL_CLEAN  // not used, always an object, not string                                                          String::UL_CLEAN  // not used, always an object, not string
                                                 );                                                  );
                                         } else                                           } else
                                                 self=&frame->junction.self; // no, static or simple dynamic call                                                  THROW(0, 0,
                                                           &frame->name(),
                                                           "method is static and can not be used as constructor");
                                   } else {
                                           // this is not constructor call
   
                                           // not ^name.method call, name:method call; and
                                           // is context object or class & is it my class or my parent's class and?
                                           VStateless_class *read_class=rcontext->get_class();
                                           if(
                                                   !(wcontext->somebody_entered_some_object() &&
                                                   !wcontext->somebody_entered_some_class()) && 
                                                   read_class && read_class->is_or_derived_from(*called_class)) // yes
                                                   self=rcontext; // dynamic call
                                           else // no, not me or relative of mine (=total stranger)
                                                   self=&frame->junction.self; // static call
                                   }
   
                                 frame->set_self(*self);                                  frame->set_self(*self);
                                 root=rcontext=wcontext=frame;                                  root=rcontext=wcontext=frame;
Line 529  void Request::execute(const Array& ops) Line 527  void Request::execute(const Array& ops)
                 case OP_DIV:                   case OP_DIV: 
                         {                          {
                                 Value *b=POP();  Value *a=POP();                                  Value *b=POP();  Value *a=POP();
                                 Value *value=NEW VDouble(pool(), a->as_double() / b->as_double());  
                                   double a_double=a->as_double();
                                   double b_double=b->as_double();
   
                                   if(b_double == 0) {
                                           const String *problem_source=&b->as_string();
   #ifndef NO_STRING_ORIGIN
                                           if(!problem_source->origin().file)
                                                   problem_source=&b->name();
   #endif
                                           THROW(0, 0,
                                                   problem_source,
                                                   "Division by zero");
                                   }
   
                                   Value *value=NEW VDouble(pool(), a_double / b_double);
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
                         }                          }
                 case OP_MOD:                   case OP_MOD: 
                         {                          {
                                 Value *b=POP();  Value *a=POP();                                  Value *b=POP();  Value *a=POP();
                                 Value *value=NEW VDouble(pool(),   
                                         a->as_int() %                                  double a_double=a->as_double();
                                         b->as_int());                                  double b_double=b->as_double();
   
                                   if(b_double == 0) {
                                           const String *problem_source=&b->as_string();
   #ifndef NO_STRING_ORIGIN
                                           if(!problem_source->origin().file)
                                                   problem_source=&b->name();
   #endif
                                           THROW(0, 0,
                                                   problem_source,
                                                   "Modulus by zero");
                                   }
   
                                   Value *value=NEW VDouble(pool(), fmod(a_double, b_double));
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
                         }                          }
Line 676  void Request::execute(const Array& ops) Line 702  void Request::execute(const Array& ops)
                         }                          }
                 case OP_IS:                  case OP_IS:
                         {                          {
                                   //_asm int 3;
                                 Value *b=POP();  Value *a=POP();                                  Value *b=POP();  Value *a=POP();
                                 Value *value=NEW VBool(pool(), b->as_string() == a->type());                                  Value *value=NEW VBool(pool(), b->as_string() == a->type());
                                 PUSH(value);                                  PUSH(value);
Line 693  void Request::execute(const Array& ops) Line 720  void Request::execute(const Array& ops)
 Value *Request::get_element() {  Value *Request::get_element() {
         const String& name=POP_NAME();          const String& name=POP_NAME();
         Value *ncontext=POP();          Value *ncontext=POP();
         Value *value;          Value *value=ncontext->get_element(name);
         if(Method* method=OP.get_method(name)) { // operator?          if(!value)
                 // as if that method were in self and we have normal dynamic method here                  if(Method* method=OP.get_method(name)) { // maybe operator?
                 Junction& junction=*NEW Junction(pool(),                           // as if that method were in self and we have normal dynamic method here
                         *self, self->get_class(), method, 0,0,0,0);                          Junction& junction=*NEW Junction(pool(), 
                 value=NEW VJunction(junction);                                  *root/*self*/, self->get_class(), method, 0,0,0,0);
         } else                          value=NEW VJunction(junction);
                 value=ncontext->get_element(name);                  }
         if(value)          if(value)
                 value=&process(*value, &name); // process possible code-junction                  value=&process(*value, &name); // process possible code-junction
         else {          else {
Line 744  Value& Request::process(Value& value, co Line 771  Value& Request::process(Value& value, co
                         frame=NEW VCodeFrame(pool(), *junction->wcontext);                            frame=NEW VCodeFrame(pool(), *junction->wcontext);  
                 } else {                  } else {
                         // plain wwrapper                          // plain wwrapper
                         frame=NEW WWrapper(pool(), 0/*empty*/, false/*not constructing*/);                          frame=NEW WWrapper(pool(), 0/*empty*/);
                 }                  }
                                   
                   //frame->set_name(value.name());
                 wcontext=frame;                  wcontext=frame;
                 self=&junction->self;                  self=&junction->self;
                 root=junction->root;                  root=junction->root;
Line 776  Value& Request::process(Value& value, co Line 804  Value& Request::process(Value& value, co
         return *result;          return *result;
 }  }
   
 const String *Request::execute_method(Value& aself, const Method& method, bool return_cstr) {  const String *Request::execute_method(Value& aself, const Method& method, 
                                                                             bool return_cstr) {
         PUSH(self);            PUSH(self);  
         PUSH(root);            PUSH(root);  
         PUSH(rcontext);            PUSH(rcontext);  
Line 784  const String *Request::execute_method(Va Line 813  const String *Request::execute_method(Va
                   
         // initialize contexts          // initialize contexts
         root=rcontext=self=&aself;          root=rcontext=self=&aself;
         wcontext=NEW WWrapper(pool(), &aself, false /*not constructing*/);          wcontext=NEW WWrapper(pool(), &aself);
                   
         // execute!               // execute!     
         execute(*method.parser_code);          execute(*method.parser_code);
Line 805  const String *Request::execute_method(Va Line 834  const String *Request::execute_method(Va
         return result;          return result;
 }  }
   
 const String *Request::execute_method(Value& aself,   const String *Request::execute_virtual_method(Value& aself, 
                                                           const String& method_name, bool return_cstr) {                                                                                            const String& method_name, 
                                                                                             bool return_cstr) {
         if(Value *value=aself.get_element(method_name))          if(Value *value=aself.get_element(method_name))
                 if(Junction *junction=value->get_junction())                  if(Junction *junction=value->get_junction())
                         if(const Method *method=junction->method)                           if(const Method *method=junction->method) 
                                 return execute_method(aself, *method, return_cstr);                                  return execute_method(aself, *method, return_cstr);
                           
           return 0;
   }
   
   const String *Request::execute_nonvirtual_method(VStateless_class& aclass, 
                                                                                                    const String& method_name, 
                                                                                                    bool return_cstr) {
           if(const Method *method=aclass.get_method(method_name))
                   return execute_method(aclass, *method, return_cstr);
   
         return 0;          return 0;
 }  }

Removed from v.1.169  
changed lines
  Added in v.1.193


E-mail: