Diff for /parser3/src/main/execute.C between versions 1.75 and 1.90

version 1.75, 2001/03/08 15:20:46 version 1.90, 2001/03/11 09:24:43
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 "pa_array.h"   #include "pa_array.h" 
Line 28  char *opcode_name[]={ Line 32  char *opcode_name[]={
         // actions          // actions
         "WITH_SELF",    "WITH_ROOT",    "WITH_READ",    "WITH_WRITE",          "WITH_SELF",    "WITH_ROOT",    "WITH_READ",    "WITH_WRITE",
         "GET_CLASS",          "GET_CLASS",
         "CONSTRUCT",          "CONSTRUCT_VALUE",  "CONSTRUCT_DOUBLE",
         "WRITE",          "WRITE",  "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_RWPOOL",        "REDUCE_RWPOOL",
Line 66  void dump(int level, const Array& ops) { Line 70  void dump(int level, const Array& ops) {
                 op.cast=ops.quick_get(i);                  op.cast=ops.quick_get(i);
                 fprintf(stderr, "%*s%s", level*4, "", opcode_name[op.code]);                  fprintf(stderr, "%*s%s", level*4, "", opcode_name[op.code]);
   
                 if(op.code==OP_VALUE) {                  if(op.code==OP_VALUE || op.code==OP_STRING__WRITE) {
                         Value *value=static_cast<Value *>(ops.quick_get(++i));                          Value *value=static_cast<Value *>(ops.quick_get(++i));
                         fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());                          fprintf(stderr, " \"%s\" %s", value->get_string()->cstr(), value->type());
                 }                  }
Line 92  void Request::execute(const Array& ops) Line 96  void Request::execute(const Array& ops)
         for(int i=0; i<size; i++) {          for(int i=0; i<size; i++) {
                 Operation op;                  Operation op;
                 op.cast=ops.quick_get(i);                  op.cast=ops.quick_get(i);
                 fprintf(stderr, "%d:%s", stack.top_index(), opcode_name[op.code]); fflush(stderr);                  fprintf(stderr, "%d:%s", stack.top_index()+1, opcode_name[op.code]); fflush(stderr);
   
                 switch(op.code) {                  switch(op.code) {
                 // param in next instruction                  // param in next instruction
Line 116  void Request::execute(const Array& ops) Line 120  void Request::execute(const Array& ops)
                                         root, frame, frame, local_ops);                                          root, frame, frame, local_ops);
                                                                   
                                 Value *value=NEW VJunction(j);                                  Value *value=NEW VJunction(j);
                                 value->set_name(frame->name());  
   
                                 // store param                                  // store param
                                 frame->store_param(value);                                  frame->store_param(value);
Line 124  void Request::execute(const Array& ops) Line 127  void Request::execute(const Array& ops)
                         }                          }
                 case OP_GET_CLASS:                  case OP_GET_CLASS:
                         {                          {
                                 String& name=POP_NAME();                                  const String& name=POP_NAME();
                                 VClass *vclass=static_cast<VClass *>(classes().get(name));                                  VClass *vclass=static_cast<VClass *>(classes().get(name));
                                 if(!vclass)                                   if(!vclass) 
                                         THROW(0,0,                                          THROW(0,0,
Line 158  void Request::execute(const Array& ops) Line 161  void Request::execute(const Array& ops)
                         }                          }
                                                   
                 // OTHER ACTIONS BUT WITHs                  // OTHER ACTIONS BUT WITHs
                 case OP_CONSTRUCT:                  case OP_CONSTRUCT_VALUE:
                         {                          {
                                 Value *value=POP();                                  Value *value=POP();
                                 String& name=POP_NAME();                                  const String& name=POP_NAME();
                                 Value *ncontext=POP();                                  Value *ncontext=POP();
                                 value->set_name(name);  
                                 ncontext->put_element(name, value);                                  ncontext->put_element(name, value);
                                   value->set_name(name);
                                   break;
                           }
                   case OP_CONSTRUCT_EXPR:
                           {
                                   Value *value=POP();
                                   const String& name=POP_NAME();
                                   Value *ncontext=POP();
                                   ncontext->put_element(name, value->get_expr_result());
                                   value->set_name(name);
                                 break;                                  break;
                         }                          }
                 case OP_WRITE:                  case OP_WRITE:
                         {                          {
                                 Value *value=POP();                                  Value *value=POP();
                                 wcontext->write(*value);                                  write(*value);
                                   break;
                           }
                   case OP_STRING__WRITE:
                           {
                                   VString *vstring=static_cast<VString *>(ops.quick_get(++i));
                                   fprintf(stderr, " \"%s\"", vstring->value().cstr());
                                   write(vstring->value());
                                 break;                                  break;
                         }                          }
                                                   
Line 184  void Request::execute(const Array& ops) Line 203  void Request::execute(const Array& ops)
                 case OP_GET_ELEMENT__WRITE:                  case OP_GET_ELEMENT__WRITE:
                         {                          {
                                 Value *value=get_element();                                  Value *value=get_element();
                                 wcontext->write(*value);                                  write(*value);
                                 break;                                  break;
                         }                          }
   
Line 214  void Request::execute(const Array& ops) Line 233  void Request::execute(const Array& ops)
                         }                          }
                 case OP_REDUCE_RWPOOL:                  case OP_REDUCE_RWPOOL:
                         {                          {
                                 String *string=wcontext->get_string();                                  const String *string=wcontext->get_string();
                                 Value *value=string?NEW VString(*string):NEW VString(pool());                                  Value *value;
                                   if(string)
                                           value=NEW VString(*string);
                                   else
                                           value=NEW VUnknown(pool());
                                 wcontext=static_cast<WContext *>(POP());                                  wcontext=static_cast<WContext *>(POP());
                                 rcontext=POP();                                  rcontext=POP();
                                 PUSH(value);                                  PUSH(value);
Line 231  void Request::execute(const Array& ops) Line 254  void Request::execute(const Array& ops)
                         {                          {
                                 // from "$a $b" part of expression taking only string value,                                  // from "$a $b" part of expression taking only string value,
                                 // ignoring any other content of wcontext                                  // ignoring any other content of wcontext
                                 String *string=wcontext->get_string();                                  const String *string=wcontext->get_string();
                                 Value *value=string?NEW VString(*string):NEW VString(pool());                                  Value *value;
                                   if(string)
                                           value=NEW VString(*string);
                                   else
                                           NEW VUnknown(pool());
                                 wcontext=static_cast<WContext *>(POP());                                  wcontext=static_cast<WContext *>(POP());
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
Line 282  void Request::execute(const Array& ops) Line 309  void Request::execute(const Array& ops)
                                         if(wcontext->constructing()) {  // constructing?                                          if(wcontext->constructing()) {  // constructing?
                                                 // yes, constructor call: $some(^class:method(..))                                                  // yes, constructor call: $some(^class:method(..))
                                                 self=NEW VObject(pool(), *called_class);                                                  self=NEW VObject(pool(), *called_class);
                                                 frame->write(*self);                                                  frame->write(*self, 
                                                           String::Untaint_lang::NO  // not used, always an object, not string
                                                   );
                                         } else                                           } else 
                                                 self=&frame->junction.self; // no, static or simple dynamic call                                                  self=&frame->junction.self; // no, static or simple dynamic call
   
Line 295  void Request::execute(const Array& ops) Line 324  void Request::execute(const Array& ops)
                                         Temp_alias temp_alias(*aliased, *frame->junction.vclass);                                          Temp_alias temp_alias(*aliased, *frame->junction.vclass);
   
                                         Method& method=*frame->junction.method;                                          Method& method=*frame->junction.method;
                                         if(method.native_code) // native code?                                          if(method.native_code) { // native code?
                                                   method.check_actual_numbered_params(frame->numbered_params());
                                                 (*method.native_code)(*this, frame->numbered_params()); // execute it                                                  (*method.native_code)(*this, frame->numbered_params()); // execute it
                                         else // parser code                                          } else // parser code
                                                 execute(*method.parser_code); // execute it                                                  execute(*method.parser_code); // execute it
                                 }                                  }
                                 Value *value=wcontext->result();                                  Value *value=wcontext->result();
Line 323  void Request::execute(const Array& ops) Line 353  void Request::execute(const Array& ops)
                 case OP_INV:                  case OP_INV:
                         {                          {
                                 Value *operand=POP();                                  Value *operand=POP();
                                 Value *value=NEW VDouble(pool(),                                   Value *value=NEW VDouble(pool(), ~static_cast<int>(operand->get_double()));
                                         ~static_cast<int>(operand->get_double()));  
                                 PUSH(value);                                  PUSH(value);
                                 break;                                  break;
                         }                          }
Line 538  void Request::execute(const Array& ops) Line 567  void Request::execute(const Array& ops)
 }  }
   
 Value *Request::get_element() {  Value *Request::get_element() {
         String& name=POP_NAME();          const String& name=POP_NAME();
         Value *ncontext=POP();          Value *ncontext=POP();
         Value *value=ncontext->get_element(name);          Value *value=ncontext->get_element(name);
   
         // autocalc possible code-junction          if(value)
         value=value?&autocalc(*value):NEW VUnknown(pool());                  value=&autocalc(*value, &name); // autocalc possible code-junction
           else {
                   value=NEW VUnknown(pool());
                   value->set_name(name);
           }
   
         value->set_name(name);  
         return value;          return value;
 }  }
   
 Value& Request::autocalc(Value& value, bool make_string) {  Value& Request::autocalc(Value& value, const String *name, bool intercept_string) {
           // intercept_string:
           //      true:
           //              they want result=string value, 
           //              possible object result goes to wcontext
           //      false:
           //              they want any result[string|object]
           //              nothing goes to wcontext.
           //              used in (expression) params evaluation
   
         Junction *junction=value.get_junction();          Junction *junction=value.get_junction();
         if(junction && junction->code) { // is it a code-junction?          if(junction && junction->code) { // is it a code-junction?
                 // autocalc it                  // autocalc it
Line 560  Value& Request::autocalc(Value& value, b Line 601  Value& Request::autocalc(Value& value, b
                 PUSH(wcontext);                  PUSH(wcontext);
                                   
                 WContext *frame;                  WContext *frame;
                 if(make_string) {                  if(intercept_string) {
                         // almost plain wwrapper about junction wcontext,                           // almost plain wwrapper about junction wcontext, 
                         // BUT intercepts string writes                          // BUT intercepts string writes
                         frame=NEW VCodeFrame(pool(), *junction->wcontext);                            frame=NEW VCodeFrame(pool(), *junction->wcontext);  
Line 575  Value& Request::autocalc(Value& value, b Line 616  Value& Request::autocalc(Value& value, b
                 rcontext=junction->rcontext;                  rcontext=junction->rcontext;
                 execute(*junction->code);                  execute(*junction->code);
                 Value *result;                  Value *result;
                 if(make_string) {                  if(intercept_string) {
                         // CodeFrame soul:                          // CodeFrame soul:
                         //   string writes were intercepted                          //   string writes were intercepted
                         //   returning them as the result of getting code-junction                          //   returning them as the result of getting code-junction
                         result=NEW VString(*frame->get_string());                          result=NEW VString(*frame->get_string());
                 } else                   } else 
                         result=frame->result();                          result=frame->result();
                   if(name)
                           result->set_name(*name);
                                   
                 wcontext=static_cast<WContext *>(POP());                    wcontext=static_cast<WContext *>(POP());  
                 rcontext=POP();                    rcontext=POP();  
Line 594  Value& Request::autocalc(Value& value, b Line 637  Value& Request::autocalc(Value& value, b
                 return value;                  return value;
 }  }
   
 void Request::write(Value& avalue) {  
         wcontext->write(avalue);  
 }  
   
   char *Request::execute_static_method(VClass& vclass, String& method_name, bool return_cstr) {
           Value *value=vclass.get_element(method_name);
           if(value) { // found some 'METHOD_NAME' element
                   Junction *junction=value->get_junction();
                   if(junction) {// it even has junction!
                           const Method *method=junction->method;
                           if(method) { // and junction is method-junction! call it
                                   PUSH(self);  
                                   PUSH(root);  
                                   PUSH(rcontext);  
                                   PUSH(wcontext);
                                   
                                   // initialize contexts
                                   root=rcontext=self=&vclass;
                                   wcontext=NEW WWrapper(pool(), &vclass, false /* not constructing */);
                                   
                                   // execute!     
                                   execute(*method->parser_code);
                                   
                                   // result
                                   char *result;
                                   if(return_cstr)
                                           result=wcontext->get_string()->cstr(); // chars
                                   else
                                           result=0; // ignore result
   
                                   wcontext=static_cast<WContext *>(POP());  
                                   rcontext=POP();  
                                   root=POP();  
                                   self=static_cast<VAliased *>(POP());
   
                                   // return
                                   return result;
                           }
                   }
           }
           return 0;
   }

Removed from v.1.75  
changed lines
  Added in v.1.90


E-mail: