--- parser3/src/main/compile.y 2010/10/12 21:55:14 1.261 +++ parser3/src/main/compile.y 2015/04/08 18:08:52 1.270 @@ -2,12 +2,14 @@ /** @file Parser: compiler(lexical parser and grammar). - Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: compile.y,v 1.261 2010/10/12 21:55:14 moko Exp $ + */ +volatile const char * IDENT_COMPILE_Y = "$Id: compile.y,v 1.270 2015/04/08 18:08:52 moko Exp $"; + /** @todo parser4: - cache compiled code from request to request. to do that... @@ -26,6 +28,8 @@ #define YYERROR_VERBOSE 1 #define yyerror(msg) real_yyerror((Parse_control *)pc, msg) #define YYPRINT(file, type, value) yyprint(file, type, value) +#define YYMALLOC pa_malloc +#define YYFREE pa_free // includes @@ -47,7 +51,7 @@ // forwards -static int real_yyerror(Parse_control* pc, char* s); +static int real_yyerror(Parse_control* pc, const char* s); static void yyprint(FILE* file, int type, YYSTYPE value); static int yylex(YYSTYPE* lvalp, void* pc); @@ -61,6 +65,13 @@ static const VString vempty; #undef POOL #define POOL (*PC.pool) #ifndef DOXYGEN + +#define CLASS_ADD if(PC.class_add()){ \ + strncpy(PC.error, PC.cclass->name().cstr(), MAX_STRING/2); \ + strcat(PC.error, " - class is already defined"); \ + YYERROR; \ +} + %} %pure_parser @@ -154,17 +165,20 @@ control_method: '@' STRING '\n' } if(command==CLASS_NAME) { if(strings_code->count()==1*OPERATIONS_PER_OPVALUE) { + CLASS_ADD; // new class' name const String& name=LA2S(*strings_code)->trim(String::TRIM_END); // creating the class VStateless_class* cclass=new VClass; PC.cclass_new=cclass; PC.cclass_new->set_name(name); + PC.append=false; } else { - strcpy(PC.error, "@"CLASS_NAME" must contain only one line with class name (contains more then one)"); + strcpy(PC.error, "@" CLASS_NAME " must contain only one line with class name (contains more then one)"); YYERROR; } } else if(command==USE_CONTROL_METHOD_NAME) { + CLASS_ADD; for(size_t i=0; icount(); i+=OPERATIONS_PER_OPVALUE) PC.request.use_file(PC.request.main_class, LA2S(*strings_code, i)->trim(String::TRIM_END), PC.request.get_used_filename(PC.file_no)); } else if(command==BASE_NAME) { @@ -174,7 +188,7 @@ control_method: '@' STRING '\n' strcat(PC.error, "'"); YYERROR; } - PC.class_add(); + CLASS_ADD; if(PC.cclass->base_class()) { // already changed from default? strcpy(PC.error, "class already have a base '"); strncat(PC.error, PC.cclass->base_class()->name().cstr(), MAX_STRING/2); @@ -187,22 +201,22 @@ control_method: '@' STRING '\n' // @CLASS == @BASE sanity check if(VStateless_class *base_class=base_class_value->get_class()) { if(PC.cclass==base_class) { - strcpy(PC.error, "@"CLASS_NAME" equals @"BASE_NAME); + strcpy(PC.error, "@" CLASS_NAME " equals @" BASE_NAME); YYERROR; } PC.cclass->get_class()->set_base(base_class); } else { // they asked to derive from a class without methods ['env' & co] strcpy(PC.error, base_name.cstr()); - strcat(PC.error, ": you can not derive from this class in @"BASE_NAME); + strcat(PC.error, ": you can not derive from this class in @" BASE_NAME); YYERROR; } } else { strcpy(PC.error, base_name.cstr()); - strcat(PC.error, ": undefined class in @"BASE_NAME); + strcat(PC.error, ": undefined class in @" BASE_NAME); YYERROR; } } else { - strcpy(PC.error, "@"BASE_NAME" must contain sole name"); + strcpy(PC.error, "@" BASE_NAME " must contain sole name"); YYERROR; } } else if(command==OPTIONS_CONTROL_METHOD_NAME) { @@ -224,7 +238,7 @@ control_method: '@' STRING '\n' PC.cclass_new->set_partial(); } } else { - strcpy(PC.error, "'"OPTION_PARTIAL_CLASS"' option should be used straight after @"CLASS_NAME); + strcpy(PC.error, "'" OPTION_PARTIAL_CLASS "' option should be used straight after @" CLASS_NAME); YYERROR; } } else if(option==method_call_type_static){ @@ -235,8 +249,8 @@ control_method: '@' STRING '\n' strcpy(PC.error, "'"); strncat(PC.error, option.cstr(), MAX_STRING/2); strcat(PC.error, "' invalid option. valid options are " - "'"OPTION_PARTIAL_CLASS"', '"OPTION_ALL_VARS_LOCAL_NAME"'" - ", '"METHOD_CALL_TYPE_STATIC"' and '"METHOD_CALL_TYPE_DYNAMIC"'" + "'" OPTION_PARTIAL_CLASS "', '" OPTION_ALL_VARS_LOCAL_NAME "'" + ", '" METHOD_CALL_TYPE_STATIC "' and '" METHOD_CALL_TYPE_DYNAMIC "'" ); YYERROR; } @@ -245,7 +259,7 @@ control_method: '@' STRING '\n' strcpy(PC.error, "'"); strncat(PC.error, command.cstr(), MAX_STRING/2); strcat(PC.error, "' invalid special name. valid names are " - "'"CLASS_NAME"', '"USE_CONTROL_METHOD_NAME"', '"BASE_NAME"' and '"OPTIONS_CONTROL_METHOD_NAME"'."); + "'" CLASS_NAME "', '" USE_CONTROL_METHOD_NAME "', '" BASE_NAME "' and '" OPTIONS_CONTROL_METHOD_NAME "'."); YYERROR; } }; @@ -255,7 +269,7 @@ control_string: maybe_string '\n'; maybe_string: empty | STRING; code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' { - PC.class_add(); + CLASS_ADD; PC.explicit_result=false; YYSTYPE params_names_code=$3; @@ -327,33 +341,40 @@ get: get_value { #ifdef OPTIMIZE_BYTECODE_GET_ELEMENT if( count!=3 - || !maybe_change_first_opcode(*code, OP::OP_VALUE__GET_ELEMENT, /*=>*/OP::OP_VALUE__GET_ELEMENT__WRITE) + || !change_first(*code, OP::OP_VALUE__GET_ELEMENT, /*=>*/OP::OP_VALUE__GET_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_SELF_ELEMENT if( count!=3 - || !maybe_change_first_opcode(*code, OP::OP_WITH_SELF__VALUE__GET_ELEMENT, /*=>*/OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE) + || !change_first(*code, OP::OP_WITH_SELF__VALUE__GET_ELEMENT, /*=>*/OP::OP_WITH_SELF__VALUE__GET_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_ELEMENT if( count!=5 - || !maybe_change_first_opcode(*code, OP::OP_GET_OBJECT_ELEMENT, /*=>*/OP::OP_GET_OBJECT_ELEMENT__WRITE) + || !change_first(*code, OP::OP_GET_OBJECT_ELEMENT, /*=>*/OP::OP_GET_OBJECT_ELEMENT__WRITE) ) #endif #ifdef OPTIMIZE_BYTECODE_GET_OBJECT_VAR_ELEMENT if( count!=5 - || !maybe_change_first_opcode(*code, OP::OP_GET_OBJECT_VAR_ELEMENT, /*=>*/OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE) + || !change_first(*code, OP::OP_GET_OBJECT_VAR_ELEMENT, /*=>*/OP::OP_GET_OBJECT_VAR_ELEMENT__WRITE) ) #endif + +#ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL + if( + !change(*code, count-1/* last */, OP::OP_GET_ELEMENT__SPECIAL, /*=>*/OP::OP_GET_ELEMENT__SPECIAL__WRITE) + ) +#endif + { - changetail_or_append(*code, - OP::OP_GET_ELEMENT, false, /*=>*/OP::OP_GET_ELEMENT__WRITE, + change_or_append(*code, count-1 /* last */, + OP::OP_GET_ELEMENT, /*=>*/OP::OP_GET_ELEMENT__WRITE, /*or */OP::OP_WRITE_VALUE ); /* value=pop; wcontext.write(value) */ } @@ -520,14 +541,15 @@ codes__excluding_sole_str_literal: actio /* call */ call: call_value { + size_t count=$1->count(); #ifdef OPTIMIZE_BYTECODE_CUT_REM_OPERATOR - if((*$1).count()) + if(count) #endif { $$=$1; /* stack: value */ - if(!maybe_change_first_opcode(*$$, OP::OP_CONSTRUCT_OBJECT, /*=>*/OP::OP_CONSTRUCT_OBJECT__WRITE)) - changetail_or_append(*$$, - OP::OP_CALL, true, /*=>*/ OP::OP_CALL__WRITE, + if(!change_first(*$$, OP::OP_CONSTRUCT_OBJECT, /*=>*/OP::OP_CONSTRUCT_OBJECT__WRITE)) + change_or_append(*$$, count-2 /* second last */, + OP::OP_CALL, /*=>*/ OP::OP_CALL__WRITE, /*or */OP::OP_WRITE_VALUE); /* value=pop; wcontext.write(value) */ } }; @@ -566,7 +588,7 @@ call_value: '^' { && (*var_code)[4].code==OP::OP_VALUE && (*var_code)[7].code==OP::OP_GET_ELEMENT ){ - yyval=N(); + $$=N(); O(*$$, OP::OP_CONSTRUCT_OBJECT); P(*$$, *var_code, 1/*offset*/, 2/*limit*/); // class name P(*$$, *var_code, 5/*offset*/, 2/*limit*/); // constructor name @@ -646,12 +668,20 @@ name_advance1: name_expr_value { /* stack: context */ $$=$1; /* stack: context,name */ +#ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL + O(*$$, is_special_element(*$$) ? OP::OP_GET_ELEMENT__SPECIAL : OP::OP_GET_ELEMENT); +#else O(*$$, OP::OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */ +#endif }; name_advance2: name_expr_value { /* stack: context */ $$=$1; /* stack: context,name */ +#ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL + O(*$$, is_special_element(*$$) ? OP::OP_GET_ELEMENT__SPECIAL : OP::OP_GET_ELEMENT); +#else O(*$$, OP::OP_GET_ELEMENT); /* name=pop; context=pop; stack: context.get_element(name) */ +#endif } | STRING BOGUS ; @@ -681,9 +711,14 @@ name_square_code_value: '[' { PC.explicit_result=*reinterpret_cast(&$2); } ']' { $$=N(); +#ifdef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL + if(!maybe_append_simple_diving_code(*$$, *$3)) +#endif + { OA(*$$, OP::OP_OBJECT_POOL, $3); /* stack: empty write context */ /* some code that writes to that context */ /* context=pop; stack: context.value() */ + } }; subvar_ref_name_rdive: STRING { $$=N(); @@ -711,7 +746,7 @@ class_static_prefix: STRING ':' { } } // optimized OP_VALUE+origin+string+OP_GET_CLASS => OP_VALUE__GET_CLASS+origin+string - maybe_change_first_opcode(*$$, OP::OP_VALUE, OP::OP_VALUE__GET_CLASS); + change_first(*$$, OP::OP_VALUE, OP::OP_VALUE__GET_CLASS); }; class_constructor_prefix: class_static_prefix ':' { $$=$1; @@ -784,7 +819,7 @@ string_inside_quotes_value: maybe_codes // it brakes ^if(" 09 "){...} YYSTYPE code=$1; $$=N(); - if(code->count()==3 && maybe_change_first_opcode(*code, OP::OP_STRING__WRITE, OP::OP_VALUE)){ + if(code->count()==3 && change_first(*code, OP::OP_STRING__WRITE, OP::OP_VALUE)){ // optimized OP_STRING__WRITE+origin+value => OP_VALUE+origin+value without starting OP_STRING_POOL P(*$$, *code); } else { @@ -1038,8 +1073,8 @@ default: case '"': case '\'': if( - pc.ls == LS_EXPRESSION_STRING_QUOTED && c=='"' || - pc.ls == LS_EXPRESSION_STRING_APOSTROFED && c=='\'') { + (pc.ls == LS_EXPRESSION_STRING_QUOTED && c=='"') || + (pc.ls == LS_EXPRESSION_STRING_APOSTROFED && c=='\'') ) { pop_LS(pc); //"abc". | 'abc'. RC; } @@ -1112,11 +1147,12 @@ default: case LS_METHOD_ROUND: switch(c) { case ')': - if(--lexical_brackets_nestage==0) + if(--lexical_brackets_nestage==0) { if(pc.ls==LS_METHOD_ROUND) // method round param ended pc.ls=LS_METHOD_AFTER; // look for method end else // pc.ls==LS_VAR_ROUND // variable constructor ended pop_LS(pc); // return to normal life + } RC; case '#': // comment start skipping if(end!=begin) { @@ -1607,7 +1643,7 @@ break2: return result; } -static int real_yyerror(Parse_control *pc, char *s) { // Called by yyparse on error +static int real_yyerror(Parse_control *pc, const char *s) { // Called by yyparse on error strncpy(PC.error, s, MAX_STRING); return 1; }