--- parser3/src/main/compile.y 2001/03/06 14:28:35 1.54 +++ parser3/src/main/compile.y 2001/03/06 15:30:49 1.56 @@ -1,5 +1,5 @@ /* - $Id: compile.y,v 1.54 2001/03/06 14:28:35 paf Exp $ + $Id: compile.y,v 1.56 2001/03/06 15:30:49 paf Exp $ */ %{ @@ -41,7 +41,16 @@ int yylex(YYSTYPE *lvalp, void *pc); %token EON %token STRING %token BOGUS -%token LOGICAL_AND LOGICAL_OR + +/* numerical */ +%left '-' '+' +%left '*' '/' +%left NEG /* negation: unary - */ +/* bitwise */ +%left INV /* invertion: unary ~ */ + +/* logical */ +%left NOT /* not: unary ! */ %% @@ -217,21 +226,21 @@ name_expr_wdive_root: ':' name_expr_dive name_expr_wdive_class: class_prefix name_expr_dive_code { $$=$1; P($$, $2) }; constructor_value: - '[' constructor_code_value ']' { $$=$2 } -| '(' expression_value ')' { $$=$2 } + '[' any_constructor_code_value ']' { $$=$2 } +| '(' any_expr ')' { $$=$2 } ; -constructor_code_value: - empty_value /* optimized $var[] case */ +any_constructor_code_value: + empty_string_value /* optimized $var[] case */ | STRING /* optimized $var[STRING] case */ -| complex_constructor_code_value /* $var[something complex] */ +| constructor_code_value /* $var[something complex] */ ; -complex_constructor_code_value: complex_constructor_code { +constructor_code_value: constructor_code { $$=N(POOL); O($$, OP_CREATE_EWPOOL); /* stack: empty write context */ P($$, $1); /* some codes to that context */ O($$, OP_REDUCE_EWPOOL); /* context=pop; stack: context.value() */ }; -complex_constructor_code: codes__excluding_sole_str_literal; +constructor_code: codes__excluding_sole_str_literal; codes__excluding_sole_str_literal: action | code codes { $$=$1; P($$, $2) }; /* call */ @@ -263,7 +272,7 @@ store_param_part: $$=$1; O($$, OP_STORE_PARAM); } -| complex_constructor_code_value { /* (something complex) */ +| constructor_code_value { /* (something complex) */ $$=$1; O($$, OP_STORE_PARAM); } @@ -345,22 +354,52 @@ with: '$' name_without_curly_rdive '{' c O($$, OP_WRITE); }; -/* expression */ +/* expr */ -expression_value: - empty_value /* optimized $var() case */ -| number /* optimized $var(STRING) case */ -| complex_expression /* $var(something complex) */ +any_expr: + empty_double_value /* optimized $var() case */ +| expr /* $var(something) */ ; -complex_expression: expression_operand '*' expression_operand { +expr: + number +| expr '+' expr { + $$=$1; // stack: first operand + P($$, $3); // stack: first,second operands + O($$, OP_ADD); // value=first+second; stack: value +} +| expr '-' expr { + $$=$1; // stack: first operand + P($$, $3); // stack: first,second operands + O($$, OP_SUB); // value=first-second; stack: value +} +| expr '*' expr { $$=$1; // stack: first operand P($$, $3); // stack: first,second operands O($$, OP_MUL); // value=first*second; stack: value -}; -expression_operand: number; +} +| expr '/' expr { + $$=$1; // stack: first operand + P($$, $3); // stack: first,second operands + O($$, OP_DIV); // value=first/second; stack: value +} +| '-' expr %prec NEG { + $$=$2; // stack: operand + O($$, OP_NEG); // value=-operand; stack: value +} +| '~' expr %prec INV { + $$=$2; // stack: operand + O($$, OP_INV); // value=~operand; stack: value +} +| '!' expr %prec NOT { + $$=$2; // stack: operand + O($$, OP_NOT); // value=!operand; stack: value +} +| '(' expr ')' { $$ = $2; } +; + /* -complex_expression_value: complex_expression { +complex_expr_value: complex_expr { $$=N(POOL); O($$, OP_CREATE_SWPOOL); /* stack: empty write context * / P($$, $1); /* some codes to that context * / @@ -383,7 +422,8 @@ number: STRING { change_string_literal_to_double_literal($$=$1); }; -empty_value: /* empty */ { $$=SL(NEW VString(POOL)) }; +empty_double_value: /* empty */ { $$=VL(NEW VDouble(POOL)) }; +empty_string_value: /* empty */ { $$=VL(NEW VString(POOL)) }; empty: /* empty */ { $$=N(POOL) }; %% @@ -557,8 +597,9 @@ int yylex(YYSTYPE *lvalp, void *pc) { lexical_brackets_nestage++; RC; case '+': case '-': case '*': case '/': case '%': + case '!': case '~': case '&': case '|': - case '<': case '>': case '=': case '!': + case '<': case '>': case '=': case ';': RC; case '"': @@ -592,7 +633,7 @@ int yylex(YYSTYPE *lvalp, void *pc) { case LS_VAR_NAME_SIMPLE: case LS_VAR_NAME_IN_EXPRESSION: if(PC->ls==LS_VAR_NAME_IN_EXPRESSION) { - // name in expression ends also before binary operators + // name in expr ends also before binary operators switch(c) { case '+': case '-': case '*': case '/': case '%': case '&': case '|': @@ -785,7 +826,7 @@ break2: PC->string->APPEND(begin, end-begin, PC->file, begin_line/*, start_col*/); } // create STRING value: array of OP_VALUE+vstring - *lvalp=SL(NEW VString(*PC->string)); + *lvalp=VL(NEW VString(*PC->string)); // new pieces storage PC->string=NEW String(POOL); // go!