--- parser3/src/main/execute.C 2009/05/23 05:23:03 1.338 +++ parser3/src/main/execute.C 2009/05/24 07:34:10 1.342 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_EXECUTE_C="$Date: 2009/05/23 05:23:03 $"; +static const char * const IDENT_EXECUTE_C="$Date: 2009/05/24 07:34:10 $"; #include "pa_opcode.h" #include "pa_array.h" @@ -68,22 +68,37 @@ char *opcode_name[]={ #ifdef OPTIMIZE_BYTECODE_CONSTRUCT "ROOT_CONSTRUCT_EXPR", "ROOT_ELEMENT_CONSTRUCT_EXPR", - "ROOT_CALL_CONSTRUCT_EXPR", - + "ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR", + "ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR", "ROOT_CONSTRUCT_VALUE", "ROOT_ELEMENT_CONSTRUCT_VALUE", - "ROOT_CALL_CONSTRUCT_VALUE", - + "ROOT_OBJECT_ELEMENT_CONSTRUCT_VALUE", + "ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE", "WRITE_CONSTRUCT_EXPR", "WRITE_ELEMENT_CONSTRUCT_EXPR", - "WRITE_CALL_CONSTRUCT_EXPR", - + "WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR", + "WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR", "WRITE_CONSTRUCT_VALUE", "WRITE_ELEMENT_CONSTRUCT_VALUE", + "WRITE_OBJECT_ELEMENT_CONSTRUCT_VALUE", + "WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE", +#endif + +#ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT + "ROOT_CALL_CONSTRUCT_EXPR", + //"ROOT_CALL_OBJECT_ELEMENT_CONSTRUCT_EXPR", + + "ROOT_CALL_CONSTRUCT_VALUE", + //"ROOT_CALL_OBJECT_ELEMENT_CONSTRUCT_VALUE", + + "WRITE_CALL_CONSTRUCT_EXPR", + //"WRITE_CALL_OBJECT_ELEMENT_CONSTRUCT_EXPR", + "WRITE_CALL_CONSTRUCT_VALUE", + //"WRITE_CALL_OBJECT_ELEMENT_CONSTRUCT_VALUE", #endif // expression ops: unary @@ -98,6 +113,18 @@ char *opcode_name[]={ "IS" }; +const char* debug_value_to_cstr(Value& value){ + const String* string=value.get_string(); + + if(string) + return string->cstr(); + else + if(value.is_bool()) + return value.as_bool()?"":""; + else + return ""; +} + void va_debug_printf(SAPI_Info& sapi_info, const char* fmt,va_list args) { char buf[MAX_STRING]; vsnprintf(buf, MAX_STRING, fmt, args); @@ -130,18 +157,21 @@ void debug_dump(SAPI_Info& sapi_info, in #ifdef OPTIMIZE_BYTECODE_CONSTRUCT || opcode==OP::OP_ROOT_CONSTRUCT_EXPR || opcode==OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR - || opcode==OP::OP_ROOT_CALL_CONSTRUCT_EXPR || opcode==OP::OP_ROOT_CONSTRUCT_VALUE || opcode==OP::OP_ROOT_ELEMENT_CONSTRUCT_VALUE - || opcode==OP::OP_ROOT_CALL_CONSTRUCT_VALUE || opcode==OP::OP_WRITE_CONSTRUCT_EXPR || opcode==OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR - || opcode==OP::OP_WRITE_CALL_CONSTRUCT_EXPR || opcode==OP::OP_WRITE_CONSTRUCT_VALUE || opcode==OP::OP_WRITE_ELEMENT_CONSTRUCT_VALUE +#endif +#ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT + || opcode==OP::OP_ROOT_CALL_CONSTRUCT_EXPR + || opcode==OP::OP_ROOT_CALL_CONSTRUCT_VALUE + + || opcode==OP::OP_WRITE_CALL_CONSTRUCT_EXPR || opcode==OP::OP_WRITE_CALL_CONSTRUCT_VALUE #endif ){ @@ -153,7 +183,7 @@ void debug_dump(SAPI_Info& sapi_info, in "%*s%s" " \"%s\" \"%s\"", level*4, "", opcode_name[opcode], - value1.get_string()->cstr(), value2.get_string()->cstr()); + debug_value_to_cstr(value1), debug_value_to_cstr(value2)); continue; } #endif @@ -168,14 +198,28 @@ void debug_dump(SAPI_Info& sapi_info, in || opcode==OP::OP_VALUE__GET_ELEMENT__WRITE || opcode==OP::OP_VALUE__GET_ELEMENT_OR_OPERATOR #endif +#ifdef OPTIMIZE_BYTECODE_CONSTRUCT + || opcode==OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR + || opcode==OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR + + || opcode==OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_VALUE + || opcode==OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE + + || opcode==OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR + || opcode==OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR + + || opcode==OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_VALUE + || opcode==OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE +#endif ) { Operation::Origin origin=i.next().origin; Value& value=*i.next().value; + debug_printf(sapi_info, "%*s%s" " \"%s\" %s", level*4, "", opcode_name[opcode], - value.get_string()->cstr(), value.type()); + debug_value_to_cstr(value), value.type()); continue; } debug_printf(sapi_info, "%*s%s", level*4, "", opcode_name[opcode]); @@ -196,6 +240,7 @@ void debug_dump(SAPI_Info& sapi_info, in } #define DEBUG_PRINT_STR(str) debug_printf(sapi_info, str); #define DEBUG_PRINT_STRING(value) debug_printf(sapi_info, " \"%s\" ", value.cstr()); +#define DEBUG_PRINT_VALUE_AND_TYPE(value) debug_printf(sapi_info, " \"%s\" %s", debug_value_to_cstr(value), value.type()); #define DEBUG_PRINT_OPS(local_ops) \ debug_printf(sapi_info, \ " (%d)\n", local_ops?local_ops->count():0); \ @@ -204,6 +249,7 @@ void debug_dump(SAPI_Info& sapi_info, in #else #define DEBUG_PRINT_STR(str) #define DEBUG_PRINT_STRING(value) +#define DEBUG_PRINT_VALUE_AND_TYPE(value) #define DEBUG_PRINT_OPS(local_ops) #endif @@ -235,9 +281,9 @@ void Request::execute(ArrayOperation& op { debug_origin=i.next().origin; Value& value=*i.next().value; -#ifdef DEBUG_EXECUTE - debug_printf(sapi_info, " \"%s\" %s", value.get_string()->cstr(), value.type()); -#endif + + DEBUG_PRINT_VALUE_AND_TYPE(value) + stack.push(value); break; } @@ -329,13 +375,11 @@ void Request::execute(ArrayOperation& op with_root=false; // don't stop here. continue in the next block } - case OP::OP_ROOT_CONSTRUCT_EXPR: case OP::OP_ROOT_CONSTRUCT_VALUE: { debug_origin=i.next().origin; const String& name=*i.next().value->get_string(); debug_name=&name; - DEBUG_PRINT_STRING(name) debug_origin=i.next().origin; @@ -345,10 +389,9 @@ void Request::execute(ArrayOperation& op opcode == OP::OP_WRITE_CONSTRUCT_EXPR || opcode == OP::OP_ROOT_CONSTRUCT_EXPR ){ - wcontext->set_in_expression(true); - put_element( (with_root)?*method_frame:*wcontext, name, &value.as_expr_result()); + put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result()); } else { - put_element( (with_root)?*method_frame:*wcontext, name, &value); + put_element( with_root?*method_frame:*wcontext, name, &value); } break; } @@ -363,18 +406,15 @@ void Request::execute(ArrayOperation& op with_root=false; // don't stop here. continue in the next block } - case OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR: case OP::OP_ROOT_ELEMENT_CONSTRUCT_VALUE: { debug_origin=i.next().origin; const String& name=*i.next().value->get_string(); debug_name=&name; - DEBUG_PRINT_STRING(name) debug_origin=i.next().origin; const String& source_var_name=*i.next().value->get_string(); debug_name=&source_var_name; - DEBUG_PRINT_STRING(source_var_name) Value& value=get_element(*rcontext, source_var_name); @@ -383,14 +423,98 @@ void Request::execute(ArrayOperation& op opcode == OP::OP_WRITE_ELEMENT_CONSTRUCT_EXPR || opcode == OP::OP_ROOT_ELEMENT_CONSTRUCT_EXPR ){ - wcontext->set_in_expression(true); - put_element( (with_root)?*method_frame:*wcontext, name, &value.as_expr_result()); + put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result()); + } else { + put_element( with_root?*method_frame:*wcontext, name, value.is_void()?new VString():&value); + } + break; + } + + case OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR: + case OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_VALUE: + { + if(wcontext==method_frame) + throw Exception(PARSER_RUNTIME, + 0, + "$.name outside of $name[...]"); + with_root=false; + // don't stop here. continue in the next block + } + case OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR: + case OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_VALUE: + { + debug_origin=i.next().origin; + const String& name=*i.next().value->get_string(); debug_name=&name; + DEBUG_PRINT_STRING(name) + + i.next(); // skip OP_GET_OBJECT_ELEMENT + debug_origin=i.next().origin; + const String& context_name=*i.next().value->get_string(); debug_name=&context_name; + DEBUG_PRINT_STRING(context_name) + + Value& object=get_element(*rcontext, context_name); + + debug_origin=i.next().origin; + const String& field_name=*i.next().value->get_string(); debug_name=&field_name; + DEBUG_PRINT_STRING(field_name) + + Value& value=get_element(object, field_name); + + if( + opcode == OP::OP_WRITE_OBJECT_ELEMENT_CONSTRUCT_EXPR + || opcode == OP::OP_ROOT_OBJECT_ELEMENT_CONSTRUCT_EXPR + ){ + put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result()); + } else { + put_element( with_root?*method_frame:*wcontext, name, value.is_void()?new VString():&value); + } + break; + } + + case OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR: + case OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE: + { + if(wcontext==method_frame) + throw Exception(PARSER_RUNTIME, + 0, + "$.name outside of $name[...]"); + with_root=false; + // don't stop here. continue in the next block + } + case OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR: + case OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_VALUE: + { + debug_origin=i.next().origin; + const String& name=*i.next().value->get_string(); debug_name=&name; + DEBUG_PRINT_STRING(name) + + i.next(); // skip OP_GET_OBJECT_VAR_ELEMENT + debug_origin=i.next().origin; + const String& context_name=*i.next().value->get_string(); debug_name=&context_name; + DEBUG_PRINT_STRING(context_name) + + Value& object=get_element(*rcontext, context_name); + + debug_origin=i.next().origin; + const String& var_name=*i.next().value->get_string(); debug_name=&var_name; + DEBUG_PRINT_STRING(var_name) + + const String* field_name=get_element(*rcontext, var_name).get_string(); + Value& value=get_element(object, *field_name); + + if( + opcode == OP::OP_WRITE_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR + || opcode == OP::OP_ROOT_OBJECT_VAR_ELEMENT_CONSTRUCT_EXPR + ){ + put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result()); } else { - put_element( (with_root)?*method_frame:*wcontext, name, &value); + put_element( with_root?*method_frame:*wcontext, name, value.is_void()?new VString():&value); } break; } +#endif // OPTIMIZE_BYTECODE_CONSTRUCT +#ifdef OPTIMIZE_BYTECODE_CALL_CONSTRUCT case OP::OP_WRITE_CALL_CONSTRUCT_EXPR: case OP::OP_WRITE_CALL_CONSTRUCT_VALUE: { @@ -415,6 +539,10 @@ void Request::execute(ArrayOperation& op DEBUG_PRINT_STRING(method_name) + bool is_expr=(opcode==OP::OP_WRITE_CALL_CONSTRUCT_EXPR || opcode==OP::OP_ROOT_CALL_CONSTRUCT_EXPR); + if(is_expr) + wcontext->set_in_expression(true); + Value* vjunction; if(Method* method=main_class.get_method(method_name)){ // looking operator of that name FIRST if(!method->junction_template) @@ -425,7 +553,7 @@ void Request::execute(ArrayOperation& op } // CALL - i.next(); // ignore OP_CALL|OP_CALL__WRITE, they are redundant... + Operation call_op=i.next(); ArrayOperation* local_ops=i.next().ops; DEBUG_PRINT_OPS(local_ops) @@ -456,19 +584,20 @@ void Request::execute(ArrayOperation& op frame.empty_params(); op_call(frame); } - - Value& value=frame.result().as_value(); - + DEBUG_PRINT_STR("<-returned") - if( - opcode == OP::OP_WRITE_CALL_CONSTRUCT_EXPR - || opcode == OP::OP_ROOT_CALL_CONSTRUCT_EXPR - ){ - wcontext->set_in_expression(true); - put_element( (with_root)?*method_frame:*wcontext, name, &value.as_expr_result()); + if(call_op.code==OP::OP_CALL__WRITE){ + write_assign_lang(frame.result()); } else { - put_element( (with_root)?*method_frame:*wcontext, name, &value); + Value& value=frame.result().as_value(); + + if(is_expr){ + wcontext->set_in_expression(false); + put_element( with_root?*method_frame:*wcontext, name, &value.as_expr_result()); + } else { + put_element( with_root?*method_frame:*wcontext, name, &value); + } } if(get_skip()) @@ -482,7 +611,7 @@ void Request::execute(ArrayOperation& op break; } -#endif // OPTIMIZE_BYTECODE_CONSTRUCT +#endif // OPTIMIZE_BYTECODE_CALL_CONSTRUCT case OP::OP_CONSTRUCT_EXPR: {