--- parser3/src/main/compile.y 2003/10/07 14:29:03 1.207 +++ parser3/src/main/compile.y 2005/11/16 14:45:22 1.215 @@ -2,10 +2,8 @@ /** @file Parser: compiler(lexical parser and grammar). - Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: compile.y,v 1.207 2003/10/07 14:29:03 paf Exp $ */ /** @@ -48,7 +46,6 @@ static int real_yyerror(Parse_control* p static void yyprint(FILE* file, int type, YYSTYPE value); static int yylex(YYSTYPE* lvalp, void* pc); - // local convinient inplace typecast & var #undef PC #define PC (*(Parse_control *)pc) @@ -67,7 +64,7 @@ static int yylex(YYSTYPE* lvalp, void* p %token BAD_HEX_LITERAL %token BAD_METHOD_DECL_START %token BAD_METHOD_PARAMETER_NAME_CHARACTER -%token BAD_MATH_OPERATOR_CHARACTER +%token BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE %token LAND "&&" %token LOR "||" @@ -210,8 +207,8 @@ control_strings: control_string | contro control_string: maybe_string '\n'; maybe_string: empty | STRING; -code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' - maybe_codes { +code_method: '@' STRING bracketed_maybe_strings maybe_bracketed_strings maybe_comment '\n' { + PC.explicit_result=false; const String& name=*LA2S(*$2); YYSTYPE params_names_code=$3; @@ -226,17 +223,28 @@ code_method: '@' STRING bracketed_maybe_ ArrayString* locals_names=0; if(int size=locals_names_code->count()) { locals_names=new ArrayString; - for(int i=0; iadd_method(PC.alias_method(name), method); + 0/*to be filled later in next {} */, 0); + PC.cclass->add_method(PC.alias_method(name), *method); + *reinterpret_cast(&$$)=method; + + // todo: check [][;result;] +} maybe_codes { + // fill in the code + reinterpret_cast($7)->parser_code=$8; }; maybe_bracketed_strings: empty | bracketed_maybe_strings; @@ -333,9 +341,14 @@ construct: | construct_round | construct_curly ; -construct_square: '[' any_constructor_code_value ']' { +construct_square: '[' { + // allow $result_or_other_variable[ letters here any time ] + *reinterpret_cast(&$$)=PC.explicit_result; PC.explicit_result=false; +} any_constructor_code_value { + PC.explicit_result=*reinterpret_cast(&$2); +} ']' { // stack: context, name - $$=$2; // stack: context, name, value + $$=$3; // stack: context, name, value O(*$$, OP_CONSTRUCT_VALUE); /* value=pop; name=pop; context=pop; construct(context,name,value) */ } ; @@ -402,7 +415,12 @@ store_param: | store_round_param | store_curly_param ; -store_square_param: '[' store_code_param_parts ']' {$$=$2}; +store_square_param: '[' { + // allow ^call[ letters here any time ] + *reinterpret_cast(&$$)=PC.explicit_result; PC.explicit_result=false; +} store_code_param_parts { + PC.explicit_result=*reinterpret_cast(&$2); +} ']' {$$=$3}; store_round_param: '(' store_expr_param_parts ')' {$$=$2}; store_curly_param: '{' store_curly_param_parts '}' {$$=$2}; store_code_param_parts: @@ -421,9 +439,20 @@ store_code_param_part: code_param_value $$=$1; O(*$$, OP_STORE_PARAM); }; -store_expr_param_part: write_expr_value { - $$=N(); - OA(*$$, OP_EXPR_CODE__STORE_PARAM, $1); +store_expr_param_part: expr_value { + YYSTYPE expr_code=$1; + if(expr_code->count()==3 + && (*expr_code)[0].code==OP_VALUE) { // optimizing (double) case. [OP_VALUE+origin+Double] + $$=expr_code; + O(*$$, OP_STORE_PARAM); // no evaluating + } else { + ArrayOperation* code=N(); + O(*code, OP_PREPARE_TO_EXPRESSION); + P(*code, *expr_code); + O(*code, OP_WRITE_EXPR_RESULT); + $$=N(); + OA(*$$, OP_EXPR_CODE__STORE_PARAM, code); + } }; store_curly_param_part: maybe_codes { $$=N(); @@ -434,12 +463,6 @@ code_param_value: | STRING /* optimized [STRING] case */ | constructor_code_value /* [something complex] */ ; -write_expr_value: expr_value { - $$=N(); - O(*$$, OP_PREPARE_TO_EXPRESSION); - P(*$$, *$1); - O(*$$, OP_WRITE_EXPR_RESULT); -}; /* name */ @@ -481,9 +504,14 @@ name_expr_with_subvar_value: STRING subv $$=N(); OA(*$$, OP_STRING_POOL, code); }; -name_square_code_value: '[' codes ']' { +name_square_code_value: '[' { + // allow $result_or_other_variable[ letters here any time ] + *reinterpret_cast(&$$)=PC.explicit_result; PC.explicit_result=false; +} codes { + PC.explicit_result=*reinterpret_cast(&$2); +} ']' { $$=N(); - OA(*$$, OP_OBJECT_POOL, $2); /* stack: empty write context */ + OA(*$$, OP_OBJECT_POOL, $3); /* stack: empty write context */ /* some code that writes to that context */ /* context=pop; stack: context.value() */ }; @@ -526,13 +554,9 @@ class_constructor_prefix: class_static_p /* expr */ -expr_value: expr { - // see OP_PREPARE_TO_EXPRESSION!! - if(($$=$1)->count()==2) // only one string literal in there? - change_string_literal_to_double_literal(*$$); // make that string literal Double -}; +expr_value: expr; expr: - STRING + double_or_STRING | get_value | call_value | '"' string_inside_quotes_value '"' { $$ = $2; } @@ -577,6 +601,11 @@ expr: | expr "is" expr { $$=$1; P(*$$, *$3); O(*$$, OP_IS) } ; +double_or_STRING: STRING { + // optimized from OP_STRING->OP_VALUE for doubles + maybe_change_string_literal_to_double_literal(*($$=$1)); +}; + string_inside_quotes_value: maybe_codes { $$=N(); OA(*$$, OP_STRING_POOL, $1); /* stack: empty write context */ @@ -644,12 +673,13 @@ static int yylex(YYSTYPE *lvalp, void *a c=*(end=(pc.source++)); // fprintf(stderr, "\nchar: %c %02X; nestage: %d, sp=%d", c, c, lexical_brackets_nestage, pc.sp); - if(c=='\n') { + if(c=='\n') pc.pos_next_line(); - } else + else pc.pos_next_c(c); +// fprintf(stderr, "\nchar: %c file(%d:%d)", c, pc.pos.line, pc.pos.col); - if(c=='@' && pc.pos.col==0+1) { + if(pc.pos.col==0+1 && c=='@') { if(pc.ls==LS_DEF_SPECIAL_BODY) { // @SPECIAL // ... @@ -662,7 +692,8 @@ static int yylex(YYSTYPE *lvalp, void *a } else // @ in first column inside some code [when could that be?] result=BAD_METHOD_DECL_START; goto break2; - } else if(c=='^') { + } + if(c=='^') { if(pc.ls==LS_METHOD_AFTER) { // handle after-method situation pop_LS(pc); @@ -781,6 +812,16 @@ default: lexical_brackets_nestage++; break; } + if(pc.explicit_result && c) + switch(c) { + case '\n': case ' ': case '\t': + begin=pc.source; + begin_pos=pc.pos; + continue; // skip it + default: + result=BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE; + goto break2; + } break; // #COMMENT @@ -1281,6 +1322,16 @@ default: lexical_brackets_nestage++; break; } + if(pc.explicit_result && c) + switch(c) { + case '\n': case ' ': case '\t': + begin=pc.source; + begin_pos=pc.pos; + continue; // skip it + default: + result=BAD_NONWHITESPACE_CHARACTER_IN_EXPLICIT_RESULT_MODE; + goto break2; + } break; case LS_METHOD_AFTER: