--- parser3/src/main/compile.y 2002/09/13 11:35:13 1.192 +++ parser3/src/main/compile.y 2003/04/07 07:07:30 1.205 @@ -2,10 +2,10 @@ /** @file Parser: compiler(lexical parser and grammar). - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: compile.y,v 1.192 2002/09/13 11:35:13 paf Exp $ + $Id: compile.y,v 1.205 2003/04/07 07:07:30 paf Exp $ */ /** @@ -34,8 +34,8 @@ #include "pa_vdouble.h" #include "pa_globals.h" #include "pa_vvoid.h" +#include "pa_vmethod_frame.h" -#define SELF_ELEMENT_NAME "self" #define USE_CONTROL_METHOD_NAME "USE" static int real_yyerror(parse_control *pc, char *s); @@ -44,6 +44,7 @@ static int yylex(YYSTYPE *lvalp, void *p // local convinient inplace typecast & var +#undef PC #define PC (*(parse_control *)pc) #define POOL (*PC.pool) #undef NEW @@ -60,15 +61,20 @@ static int yylex(YYSTYPE *lvalp, void *p %token BAD_STRING_COMPARISON_OPERATOR %token BAD_HEX_LITERAL %token BAD_METHOD_DECL_START +%token BAD_METHOD_PARAMETER_NAME_CHARACTER +%token BAD_MATH_OPERATOR_CHARACTER %token LAND "&&" %token LOR "||" -%token LXOR "##" +%token LXOR "!||" +%token NXOR "!|" %token NLE "<=" %token NGE ">=" %token NEQ "==" %token NNE "!=" +%token NSL "<<" +%token NSR ">>" %token SLT "lt" %token SGT "gt" @@ -84,27 +90,30 @@ static int yylex(YYSTYPE *lvalp, void *p %token IS "is" /* logical */ -%left "##" +%left "!||" %left "||" %left "&&" %left '<' '>' "<=" ">=" "lt" "gt" "le" "ge" %left "==" "!=" "eq" "ne" %left "is" "def" "in" "-f" "-d" -%left '!' /* bitwise */ -%left '#' +%left "!|" %left '|' %left '&' -%left '~' +%left "<<" ">>" /* numerical */ -%left '-' '+' -%left '*' '/' '%' '\\' -%left NEG /* negation: unary - */ +%left '+' '-' +%left '*' '/' '\\' '%' +%left NUNARY /* unary - + */ + +/* out-of-group */ +%left '~' /* bitwise */ +%left '!' /* logical */ %% -all: +all: one_big_piece { Method& method=*NEW Method(POOL, PC.request->main_method_name, @@ -152,7 +161,7 @@ control_method: '@' STRING '\n' } } else if(command==USE_CONTROL_METHOD_NAME) { for(int i=0; isize(); i+=2) - PC.request->use_file(*LA2S(strings_code, i)); + PC.request->use_file(PC.request->main_class, *LA2S(strings_code, i)); } else if(command==BASE_NAME) { if(PC.cclass->base_class()) { // already changed from default? strcpy(PC.error, "class already have a base '"); @@ -522,8 +531,8 @@ expr: | '\'' string_inside_quotes_value '\'' { $$ = $2; } | '(' expr ')' { $$ = $2; } /* stack: operand // stack: @operand */ -| '-' expr %prec NEG { $$=$2; O($$, OP_NEG) } -| '+' expr %prec NEG { $$=$2 } +| '-' expr %prec NUNARY { $$=$2; O($$, OP_NEG) } +| '+' expr %prec NUNARY { $$=$2 } | '~' expr { $$=$2; O($$, OP_INV) } | '!' expr { $$=$2; O($$, OP_NOT) } | "def" expr { $$=$2; O($$, OP_DEF) } @@ -537,12 +546,14 @@ expr: | expr '/' expr { $$=$1; P($$, $3); O($$, OP_DIV) } | expr '%' expr { $$=$1; P($$, $3); O($$, OP_MOD) } | expr '\\' expr { $$=$1; P($$, $3); O($$, OP_INTDIV) } +| expr "<<" expr { $$=$1; P($$, $3); O($$, OP_BIN_SL) } +| expr ">>" expr { $$=$1; P($$, $3); O($$, OP_BIN_SR) } | expr '&' expr { $$=$1; P($$, $3); O($$, OP_BIN_AND) } | expr '|' expr { $$=$1; P($$, $3); O($$, OP_BIN_OR) } -| expr '#' expr { $$=$1; P($$, $3); O($$, OP_BIN_XOR) } +| expr "!|" expr { $$=$1; P($$, $3); O($$, OP_BIN_XOR) } | expr "&&" expr { $$=$1; OA($$, OP_NESTED_CODE, $3); O($$, OP_LOG_AND) } | expr "||" expr { $$=$1; OA($$, OP_NESTED_CODE, $3); O($$, OP_LOG_OR) } -| expr "##" expr { $$=$1; P($$, $3); O($$, OP_LOG_XOR) } +| expr "!||" expr { $$=$1; P($$, $3); O($$, OP_LOG_XOR) } | expr '<' expr { $$=$1; P($$, $3); O($$, OP_NUM_LT) } | expr '>' expr { $$=$1; P($$, $3); O($$, OP_NUM_GT) } | expr "<=" expr { $$=$1; P($$, $3); O($$, OP_NUM_LE) } @@ -634,7 +645,14 @@ static int yylex(YYSTYPE *lvalp, void *p } else // @ in first column inside some code [when could that be?] result=BAD_METHOD_DECL_START; goto break2; - } else if(c=='^') + } else if(c=='^') { + if(PC.ls==LS_METHOD_AFTER) { + // handle after-method situation + pop_LS(PC); + result=EON; + skip_analized=-1; // return to punctuation afterwards to assure it's literality + goto break2; + } switch(PC.ls) { case LS_EXPRESSION_VAR_NAME_WITH_COLON: case LS_EXPRESSION_VAR_NAME_WITHOUT_COLON: @@ -642,7 +660,7 @@ case LS_VAR_NAME_SIMPLE_WITH_COLON: case LS_VAR_NAME_SIMPLE_WITHOUT_COLON: case LS_VAR_NAME_CURLY: case LS_METHOD_NAME: -case LS_COMMENT: +case LS_USER_COMMENT: case LS_DEF_COMMENT: // no literals in names, please break; @@ -659,19 +677,12 @@ default: PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line); } // reset piece 'begin' position & line - end=begin=PC.source; // ^ + begin=PC.source; // ->punctuation begin_line=PC.line; - if(PC.ls==LS_METHOD_AFTER) { - pop_LS(PC); - result=EON; - skip_analized=-1; // return to ^ afterwards to assure it's literality - goto break2; - } else { - // skip over _ after ^ - PC.source++; PC.col++; - // skip analysis = forced literal - continue; - } + // skip over _ after ^ + PC.source++; PC.col++; + // skip analysis = forced literal + continue; // converting ^#HH into char(hex(HH)) case '#': @@ -695,13 +706,15 @@ default: PC.source+=3; PC.col+=3; // reset piece 'begin' position & line - begin=PC.source; // ^ + begin=PC.source; // ->after ^#HH begin_line=PC.line; + // skip analysis = forced literal continue; } break; } break; + } } // #comment start skipping if(c=='#' && PC.col==1) { @@ -710,7 +723,7 @@ default: PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line); } // fall into COMMENT lexical state [wait for \n] - push_LS(PC, LS_COMMENT); + push_LS(PC, LS_USER_COMMENT); continue; } switch(PC.ls) { @@ -749,7 +762,7 @@ default: break; // #COMMENT - case LS_COMMENT: + case LS_USER_COMMENT: if(c=='\n') { // skip comment begin=PC.source; @@ -797,7 +810,8 @@ default: case LS_DEF_PARAMS: switch(c) { case '$': // common error - RC; + result=BAD_METHOD_PARAMETER_NAME_CHARACTER; + goto break2; case ';': RC; case ']': @@ -846,6 +860,15 @@ default: else // PC.ls==LS_VAR_ROUND // variable constructor ended pop_LS(PC); // return to normal life RC; + case '#': // comment start skipping + if(end!=begin) { + // append piece till # + PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line); + } + // fall into COMMENT lexical state [wait for \n] + push_LS(PC, LS_EXPRESSION_COMMENT); + lexical_brackets_nestage=1; + continue; case '$': push_LS(PC, LS_EXPRESSION_VAR_NAME_WITH_COLON); RC; @@ -874,25 +897,59 @@ default: case '~': case ';': RC; - case '&': case '|': case '#': + case '&': case '|': if(*PC.source==c) { // && || - result=c=='#'?LXOR:c=='&'?LAND:LOR; + result=c=='&'?LAND:LOR; skip_analized=1; } else result=c; goto break2; - case '<': case '>': case '=': case '!': - if(*PC.source=='=') { // <= >= == != + case '!': + switch(PC.source[0]) { + case '|': // !| !|| skip_analized=1; - switch(c) { - case '<': result=NLE; break; - case '>': result=NGE; break; - case '=': result=NEQ; break; - case '!': result=NNE; break; - } - } else - result=c; + if(PC.source[1]=='|') { + skip_analized++; + result=LXOR; + } else + result=NXOR; + goto break2; + case '=': // != + skip_analized=1; + result=NNE; + goto break2; + } + RC; + + case '<': // <<, <=, < + switch(*PC.source) { + case '<': // <[<] + skip_analized=1; result=NSL; break; + case '=': // <[=] + skip_analized=1; result=NLE; break; + default: // <[] + result=c; break; + } goto break2; + case '>': // >>, >=, > + switch(*PC.source) { + case '>': // >[>] + skip_analized=1; result=NSR; break; + case '=': // >[=] + skip_analized=1; result=NGE; break; + default: // >[] + result=c; break; + } + goto break2; + case '=': // == + switch(*PC.source) { + case '=': // =[=] + skip_analized=1; result=NEQ; break; + default: // =[] + result=c; break; // not used now + } + goto break2; + case '"': push_LS(PC, LS_EXPRESSION_STRING_QUOTED); RC; @@ -955,6 +1012,24 @@ default: continue; } break; + case LS_EXPRESSION_COMMENT: + if(c=='(') + lexical_brackets_nestage++; + + switch(*PC.source) { + case '\n': case ')': + if(*PC.source==')') + if(--lexical_brackets_nestage!=0) + continue; + + // skip comment + begin=PC.source; + begin_line=PC.line; + + pop_LS(PC); + continue; + } + break; // VARIABLE GET/PUT/WITH case LS_VAR_NAME_SIMPLE_WITH_COLON: @@ -1218,7 +1293,7 @@ break2: if(end!=begin && end[-1]=='\n') // allow one empty line before LS_DEF_NAME end--; } - if(end!=begin && PC.ls!=LS_COMMENT) { // last piece still alive and not comment? + if(end!=begin && PC.ls!=LS_USER_COMMENT) { // last piece still alive and not comment? // append it PC.string->APPEND_CLEAN(begin, end-begin, PC.file, begin_line/*, start_col*/); }