[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[163] | 4 | |
---|
| 5 | %{ |
---|
| 6 | #include "framscript-defs.h" |
---|
[375] | 7 | #include "common/log.h" |
---|
[163] | 8 | #include <math.h> |
---|
| 9 | #include <ctype.h> |
---|
| 10 | #include <stdio.h> |
---|
| 11 | |
---|
| 12 | #define YYERROR_VERBOSE |
---|
| 13 | #define YYPRINT(file,type,value) yyprint (file,type,value) |
---|
| 14 | |
---|
| 15 | static void yyprint (FILE *file,int type,YYSTYPE value); |
---|
| 16 | void handleTwoArg(YYSTYPE& result,const YYSTYPE& arg1,const YYSTYPE& arg2, |
---|
[332] | 17 | int optoken,const char* opname, bool negarg2, bool uniq); |
---|
| 18 | bool handleCompare(YYSTYPE& result,const YYSTYPE& arg1,const YYSTYPE& arg2, |
---|
| 19 | ExtValue::CmpOperator,const char* opname); |
---|
[163] | 20 | bool handleAssignOp(YYSTYPE& result,const YYSTYPE& var,const YYSTYPE& arg,const char* opname); |
---|
| 21 | bool handleAssignOp2(YYSTYPE& result,const char *var,const YYSTYPE& arg,const char* opname,int stackpos,bool push); |
---|
| 22 | bool variableOk(TokenValue &tok, const TokenValue& var,int &loc); |
---|
| 23 | bool globalOk(const TokenValue& var); |
---|
| 24 | void badVariable(TokenValue &tok, const TokenValue &var); |
---|
| 25 | bool evalVariable(TokenValue &tok, const TokenValue &var); |
---|
| 26 | bool doBreak(int level); |
---|
| 27 | bool doContinue(int level); |
---|
| 28 | void warnTruthValue(const TokenValue& t); |
---|
| 29 | void outFunName(const TokenValue& t); |
---|
[333] | 30 | static bool resultIsRelaxedEqual(ExtValue::CompareResult res); |
---|
[163] | 31 | |
---|
| 32 | static const char* assign_op_names[]={"add","sub","mul","div","mod"}; |
---|
| 33 | |
---|
| 34 | %} |
---|
| 35 | |
---|
| 36 | %token_table |
---|
| 37 | |
---|
| 38 | %token CONSTANT |
---|
[468] | 39 | %token INVALID_NUMBER |
---|
[163] | 40 | |
---|
| 41 | %nonassoc ASSIGN_ADD ASSIGN_SUB ASSIGN_MUL ASSIGN_DIV ASSIGN_MOD |
---|
| 42 | %nonassoc PLUSPLUS MINUSMINUS |
---|
| 43 | %left LOGIC_AND LOGIC_OR '!' |
---|
| 44 | %left EQUAL NOT_EQUAL GEQUAL LEQUAL '>' '<' |
---|
| 45 | %left '|' '&' '^' |
---|
| 46 | %left '-' '+' |
---|
| 47 | %left '*' '/' '%' |
---|
| 48 | %left NEG /* negation--unary minus */ |
---|
| 49 | %left TYPEOF |
---|
[333] | 50 | %left INT_TYPE |
---|
| 51 | %left FLOAT_TYPE |
---|
| 52 | %left STRING_TYPE |
---|
[163] | 53 | |
---|
| 54 | %token IDENT |
---|
| 55 | %token OBJNAME |
---|
| 56 | |
---|
| 57 | %token IF "if" |
---|
| 58 | %token ELSE "else" |
---|
| 59 | %token FOR "for" |
---|
| 60 | %token INNN "in" |
---|
| 61 | %token WHILE "while" |
---|
| 62 | %token DO "do" |
---|
| 63 | %token GOTO "goto" |
---|
| 64 | %token RETURN "return" |
---|
| 65 | %token BREAK "break" |
---|
| 66 | %token CONTINUE "continue" |
---|
| 67 | %token SWITCH "switch" |
---|
| 68 | %token CASE "case" |
---|
| 69 | %token DEFAULT "default" |
---|
| 70 | |
---|
[333] | 71 | %token TYPEOF "typeof" |
---|
| 72 | %token INT_TYPE "int" |
---|
| 73 | %token FLOAT_TYPE "float" |
---|
| 74 | %token STRING_TYPE "string" |
---|
[163] | 75 | |
---|
| 76 | %token ASM |
---|
| 77 | %token ASMLINE |
---|
| 78 | |
---|
| 79 | %token VAR "var" |
---|
| 80 | %token GLOBAL "global" |
---|
| 81 | %token FUNCTION "function" |
---|
| 82 | |
---|
| 83 | %token CALL "call" |
---|
| 84 | |
---|
| 85 | %token ASSIGN |
---|
| 86 | %token ASSIGN_ADD |
---|
| 87 | %token ASSIGN_SUB |
---|
| 88 | %token ASSIGN_MUL |
---|
| 89 | %token ASSIGN_DIV |
---|
| 90 | |
---|
| 91 | %token EQUAL |
---|
| 92 | %token NOT_EQUAL |
---|
| 93 | %token GEQUAL |
---|
| 94 | %token LEQUAL |
---|
| 95 | |
---|
| 96 | %token LOGIC_AND |
---|
| 97 | %token LOGIC_OR |
---|
| 98 | |
---|
| 99 | %token PLUSPLUS |
---|
| 100 | %token MINUSMINUS |
---|
| 101 | |
---|
| 102 | %token LSHIFT |
---|
| 103 | %token RSHIFT |
---|
| 104 | |
---|
| 105 | %% |
---|
| 106 | code: {$$.setInt(trstack.currentPos());} recurcode |
---|
| 107 | { |
---|
| 108 | int pos=$1.getInt(); |
---|
| 109 | if (pos!=trstack.currentPos()) trctx.out->printf("add %d,m0\n",pos-trstack.currentPos()); |
---|
| 110 | } |
---|
| 111 | ; |
---|
| 112 | |
---|
| 113 | recurcode: /* empty string */ |
---|
| 114 | | recurcode statement |
---|
| 115 | ; |
---|
| 116 | |
---|
| 117 | statement: ';' |
---|
| 118 | | VAR vardeflist ';' |
---|
| 119 | | GLOBAL globaldeflist ';' |
---|
| 120 | | IDENT ':' {trctx.out->printf(":%s\n",str($1));} |
---|
[332] | 121 | | expr ';' {if (!$1.constant) { trctx.out->printf("inc m0\n"); trstack.adjust(+1); } trctx.emitLine(); } |
---|
[163] | 122 | | functiondef |
---|
| 123 | | blok |
---|
| 124 | | if_statement |
---|
| 125 | | goto_statement |
---|
| 126 | | return_statement |
---|
| 127 | | for_statement |
---|
| 128 | | while_statement |
---|
| 129 | | dowhile_statement |
---|
| 130 | | break_statement |
---|
| 131 | | continue_statement |
---|
| 132 | | switch_statement |
---|
| 133 | // | error ';' |
---|
| 134 | | asmblock |
---|
| 135 | ; |
---|
| 136 | |
---|
| 137 | asmblock: ASM asmlines '}' |
---|
| 138 | ; |
---|
| 139 | |
---|
| 140 | asmlines: /* empty */ |
---|
| 141 | | ASMLINE {fputs(str($1),trctx.out);fputc('\n',trctx.out);} |
---|
| 142 | | asmlines ASMLINE {fputs(str($2),trctx.out);fputc('\n',trctx.out);} |
---|
| 143 | ; |
---|
| 144 | |
---|
| 145 | goto_statement: GOTO IDENT ';' |
---|
| 146 | { |
---|
| 147 | #ifdef FRAMSCRIPT_GOTO |
---|
[375] | 148 | trctx.out->printf("jump :%s\n",str($2)); logPrintf("FramScriptCompiler","translate",LOG_WARN,"goto is not recommended"); trctx.emitLine(); |
---|
[163] | 149 | #else |
---|
[245] | 150 | trctx.err->printf("goto is not supported\n");return 1; |
---|
[163] | 151 | #endif |
---|
| 152 | } |
---|
| 153 | ; |
---|
| 154 | |
---|
| 155 | return_statement: RETURN expr ';' |
---|
| 156 | { |
---|
| 157 | int offset; |
---|
[466] | 158 | if (trctx.functionstackpos==TranslatorContext::NOT_IN_FUNCTION) |
---|
[163] | 159 | offset=-trstack.currentPos(); |
---|
| 160 | else |
---|
| 161 | offset=trctx.functionstackpos-trstack.currentPos(); |
---|
| 162 | if (!offset) |
---|
| 163 | { |
---|
| 164 | if ($2.constant) |
---|
| 165 | trctx.out->printf("move %s,s0\nreturn\n",litstr($2)); |
---|
| 166 | else |
---|
| 167 | { |
---|
| 168 | trctx.out->printf("move m[m0++],s0\nreturn\n"); |
---|
| 169 | trstack.adjust(+1); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | if ($2.constant) |
---|
| 175 | { |
---|
| 176 | trctx.out->printf("add %d,m0\nmove %s,s0\nreturn\n",offset,litstr($2)); |
---|
| 177 | trstack.adjust(offset); |
---|
| 178 | } |
---|
| 179 | else |
---|
| 180 | { |
---|
| 181 | trctx.out->printf("move s0,s%d\nadd %d,m0\nreturn\n",offset,offset); |
---|
| 182 | trstack.adjust(offset); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | | RETURN ';' |
---|
| 187 | { |
---|
| 188 | int offset; |
---|
[466] | 189 | if (trctx.functionstackpos==TranslatorContext::NOT_IN_FUNCTION) |
---|
[163] | 190 | offset=-trstack.currentPos(); |
---|
| 191 | else |
---|
| 192 | offset=trctx.functionstackpos-trstack.currentPos(); |
---|
[332] | 193 | trctx.emitLine(); |
---|
[163] | 194 | if (!offset) |
---|
| 195 | trctx.out->printf("move invalid,s0\nreturn\n"); |
---|
| 196 | else |
---|
| 197 | trctx.out->printf("add %d,m0\nmove invalid,s0\nreturn\n",offset); |
---|
| 198 | } |
---|
| 199 | ; |
---|
| 200 | |
---|
| 201 | vardeflist: vardef |
---|
| 202 | | vardeflist ',' vardef |
---|
| 203 | ; |
---|
| 204 | |
---|
[332] | 205 | vardef: IDENT { trctx.emitLine(); trstack.addVariable($1.getString()); trctx.out->printf("push invalid\n");} |
---|
| 206 | | IDENT '=' stackexpr { trctx.emitLine(); trstack.adjust(1); trstack.addVariable($1.getString());} |
---|
[163] | 207 | ; |
---|
| 208 | |
---|
| 209 | globaldeflist: globaldef |
---|
| 210 | | globaldeflist ',' globaldef |
---|
| 211 | ; |
---|
| 212 | |
---|
| 213 | globaldef: IDENT {trstack.globals.add($1.getString(),0); |
---|
| 214 | trctx.out->printf("global %s\n",str($1));} |
---|
| 215 | ; |
---|
| 216 | |
---|
| 217 | funparam: IDENT { trstack.addVariable($1.getString()); }; |
---|
| 218 | |
---|
| 219 | paramlist: /* empty */ {$$.setInt(0); } |
---|
| 220 | | funparam {$$.setInt(1); } |
---|
| 221 | | paramlist ',' funparam {$$.setInt($1.getInt()+1);} |
---|
| 222 | ; |
---|
| 223 | |
---|
| 224 | funnamelist: |
---|
| 225 | IDENT {outFunName($1);} |
---|
| 226 | | funnamelist ',' IDENT {outFunName($3);} |
---|
| 227 | ; |
---|
| 228 | |
---|
| 229 | functiondef: FUNCTION funnamelist |
---|
| 230 | { |
---|
[332] | 231 | trctx.emitLine(); |
---|
[163] | 232 | int pos=trstack.currentPos(); |
---|
| 233 | $$.setInt(pos); |
---|
[466] | 234 | if (trctx.functionstackpos!=TranslatorContext::NOT_IN_FUNCTION) |
---|
[245] | 235 | {trctx.err->printf("functions cannot be nested\n");return 1;} |
---|
[466] | 236 | trctx.beforefunctionstackpos=trstack.currentPos(); |
---|
[163] | 237 | } |
---|
| 238 | '(' paramlist ')' |
---|
| 239 | { |
---|
| 240 | trctx.functionstackpos=trstack.currentPos(); |
---|
| 241 | } |
---|
| 242 | blok |
---|
| 243 | {trctx.out->printf("move invalid,s0\nreturn\n"); |
---|
| 244 | int pos=$3.getInt(); |
---|
| 245 | trstack.dropToPos(pos); |
---|
[466] | 246 | trctx.functionstackpos=TranslatorContext::NOT_IN_FUNCTION; |
---|
| 247 | trctx.beforefunctionstackpos=TranslatorContext::NOT_IN_FUNCTION; |
---|
[163] | 248 | trctx.out->printf(":_skipfun_%d\n",trctx.functiontmplabel); |
---|
| 249 | trctx.functiontmplabel=-1; |
---|
[332] | 250 | trctx.emitLine(); |
---|
[163] | 251 | }; |
---|
| 252 | |
---|
| 253 | break_statement: BREAK ';' {if (!doBreak(1)) return 1;} |
---|
| 254 | | BREAK expr ';' |
---|
| 255 | { |
---|
[332] | 256 | trctx.emitLine(); |
---|
[163] | 257 | if (!$2.constant) |
---|
[245] | 258 | {trctx.err->printf("break level must be a constant expression\n");return 1;} |
---|
[163] | 259 | int level=$2.getInt(); |
---|
| 260 | if (level<1) |
---|
[245] | 261 | {trctx.err->printf("break level must be a positive integer\n");return 1;} |
---|
[163] | 262 | if (!doBreak(level)) return 1; |
---|
[332] | 263 | trctx.emitLine(); |
---|
[163] | 264 | }; |
---|
| 265 | |
---|
| 266 | continue_statement: CONTINUE ';' {if (!doContinue(1)) return 1;} |
---|
| 267 | | CONTINUE expr ';' |
---|
| 268 | { |
---|
| 269 | if (!$2.constant) |
---|
[245] | 270 | {trctx.err->printf("continue level must be a constant expression\n");return 1;} |
---|
[163] | 271 | int level=$2.getInt(); |
---|
| 272 | if (level<1) |
---|
[245] | 273 | {trctx.err->printf("continue level must be a positive integer\n");return 1;} |
---|
[163] | 274 | if (!doContinue(level)) return 1; |
---|
[332] | 275 | trctx.emitLine(); |
---|
[163] | 276 | }; |
---|
| 277 | |
---|
| 278 | while_statement: WHILE '(' |
---|
[332] | 279 | { |
---|
| 280 | int c=trctx.labelcounter++; $$.setInt(c); |
---|
[163] | 281 | $$.stack=trstack.currentPos(); |
---|
| 282 | trstack.loops.addLoop(c,$$.stack); |
---|
| 283 | trctx.out->printf(":_loop_%d\n",c);} |
---|
| 284 | expr ')' |
---|
| 285 | { |
---|
| 286 | int c=$3.getInt(); |
---|
| 287 | warnTruthValue($4); |
---|
| 288 | if ($4.constant) |
---|
| 289 | {if (!$4.getInt()) trctx.out->printf("jump :_loop_end_%d\n",c);} |
---|
| 290 | else |
---|
| 291 | { |
---|
[332] | 292 | trctx.out->printf("if ~=,m[m0++],:_loop_end_%d\n",c,c); |
---|
[163] | 293 | trstack.adjust(+1); |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | pseudoblok_statement |
---|
[332] | 297 | { |
---|
| 298 | trctx.out->printf("jump :_loop_%d\n:_loop_end_%d\n",$3.getInt(),$3.getInt()); |
---|
[163] | 299 | trstack.adjust($3.stack-trstack.currentPos()); |
---|
| 300 | trstack.loops.drop(); |
---|
| 301 | } |
---|
| 302 | ; |
---|
| 303 | |
---|
| 304 | dowhile_statement: DO |
---|
[332] | 305 | { |
---|
| 306 | trctx.emitLine(); |
---|
| 307 | int c=trctx.labelcounter++; $$.setInt(c); |
---|
[163] | 308 | $$.stack=trstack.currentPos(); |
---|
| 309 | trstack.loops.addLoop(c,$$.stack); |
---|
| 310 | trctx.out->printf(":_loop_%d\n",c);} //2 |
---|
| 311 | |
---|
| 312 | pseudoblok_statement WHILE '(' expr ')' |
---|
| 313 | |
---|
| 314 | {//8 |
---|
| 315 | int c=$2.getInt(); |
---|
| 316 | warnTruthValue($6); |
---|
| 317 | if ($6.constant) |
---|
| 318 | {if ($6.getInt()) trctx.out->printf("jump :_loop_%d\n",c);} |
---|
| 319 | else |
---|
| 320 | { |
---|
[332] | 321 | trctx.out->printf("if !~,m[m0++],:_loop_%d\n",c); |
---|
[163] | 322 | trstack.adjust(+1); |
---|
| 323 | } |
---|
| 324 | trctx.out->printf(":_loop_end_%d\n",c); |
---|
| 325 | trstack.adjust($2.stack-trstack.currentPos()); |
---|
| 326 | trstack.loops.drop(); |
---|
[332] | 327 | trctx.emitLine(); |
---|
[163] | 328 | } |
---|
| 329 | ; |
---|
| 330 | |
---|
| 331 | switch_statement: SWITCH '(' |
---|
[332] | 332 | { |
---|
| 333 | int c=trctx.labelcounter++; $1.setInt(c); |
---|
[163] | 334 | trstack.loops.addLoop(c,trstack.currentPos());} |
---|
| 335 | stackexpr ')' |
---|
[332] | 336 | {trctx.emitLine(); trctx.out->printf("dec m0\n"); trstack.adjust(-1);} |
---|
[163] | 337 | '{' inside_switch '}' |
---|
[332] | 338 | { |
---|
| 339 | trctx.emitLine(); |
---|
| 340 | LoopInfo *li=trstack.loops.getLoop(0); |
---|
| 341 | trctx.out->printf(":_case_after_%d_%d\n" |
---|
| 342 | "add 2,m0\n" |
---|
| 343 | ":_loop_end_%d\n", |
---|
| 344 | li->id,li->casecounter, |
---|
| 345 | li->id); |
---|
| 346 | trstack.adjust(+2); |
---|
| 347 | trstack.loops.drop(); |
---|
| 348 | } |
---|
[163] | 349 | ; |
---|
| 350 | |
---|
| 351 | inside_switch: /* empty */ |
---|
| 352 | | case_label |
---|
| 353 | | inside_switch case_label |
---|
| 354 | ; |
---|
| 355 | |
---|
| 356 | case_label: CASE expr ':' |
---|
[332] | 357 | { |
---|
| 358 | LoopInfo *li=trstack.loops.getLoop(0); |
---|
| 359 | if ($2.constant) |
---|
| 360 | trctx.out->printf("if s1,!=,%s,:_case_before_%d_%d\n", |
---|
| 361 | litstr($2), |
---|
| 362 | li->id,li->casecounter+1); |
---|
| 363 | else |
---|
| 364 | { |
---|
| 365 | trctx.out->printf("if s2,!=,m[m0++],:_case_before_%d_%d\n", |
---|
| 366 | li->id,li->casecounter+1); |
---|
| 367 | trstack.adjust(+1); |
---|
| 368 | } |
---|
| 369 | trctx.out->printf(":_case_after_%d_%d\n", |
---|
| 370 | li->id,li->casecounter); |
---|
[163] | 371 | int pos=trstack.currentPos(); $$.setInt(pos); |
---|
| 372 | } |
---|
| 373 | recurcode |
---|
| 374 | { |
---|
[332] | 375 | trctx.emitLine(); |
---|
| 376 | LoopInfo *li=trstack.loops.getLoop(0); |
---|
[163] | 377 | int pos=$4.getInt(); |
---|
| 378 | if (pos!=trstack.currentPos()) trctx.out->printf("add %d,m0\n",pos-trstack.currentPos()); |
---|
| 379 | trstack.dropToPos(pos); |
---|
[332] | 380 | li->casecounter++; |
---|
| 381 | trctx.out->printf("jump :_case_after_%d_%d\n" |
---|
| 382 | ":_case_before_%d_%d\n", |
---|
| 383 | li->id,li->casecounter, |
---|
| 384 | li->id,li->casecounter); |
---|
[163] | 385 | } |
---|
[332] | 386 | | DEFAULT ':' |
---|
| 387 | { |
---|
| 388 | LoopInfo *li=trstack.loops.getLoop(0); |
---|
| 389 | trctx.out->printf(":_case_after_%d_%d\n",li->id,li->casecounter); |
---|
| 390 | } |
---|
| 391 | recurcode |
---|
| 392 | { |
---|
| 393 | LoopInfo *li=trstack.loops.getLoop(0); |
---|
| 394 | li->casecounter++; |
---|
| 395 | } |
---|
[163] | 396 | ; |
---|
| 397 | |
---|
| 398 | newvar_or_expr: |
---|
| 399 | VAR IDENT { $$.setInt(trstack.addVariable($2.getString())); trctx.out->printf("push invalid\n"); $$.ident=true; $$.var=true; } |
---|
| 400 | |
---|
| 401 | | |
---|
| 402 | VAR IDENT '=' stackexpr |
---|
| 403 | { |
---|
| 404 | //trctx.out->printf("# VAR IDENT '=' stackexpr pos=%d\n",trstack.currentPos()); |
---|
| 405 | trstack.adjust(+1); |
---|
| 406 | $$.setInt(trstack.addVariable($2.getString())); |
---|
| 407 | $$.ident=true; $$.var=true; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | | |
---|
| 411 | expr_special_ident |
---|
| 412 | { |
---|
| 413 | $$=$1; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | | //nic |
---|
| 417 | { |
---|
| 418 | $$.setInt(1); $$.assign=false; $$.ident=false; $$.var=false; $$.constant=true; |
---|
| 419 | } |
---|
| 420 | ; |
---|
| 421 | |
---|
| 422 | expr_or_objname: |
---|
| 423 | expr { $$=$1; $$.objname=false; } |
---|
| 424 | | |
---|
| 425 | OBJNAME { $$.setString($1.getString()); $$.objname=true; } |
---|
| 426 | ; |
---|
| 427 | |
---|
| 428 | for_statement_begin: FOR '(' |
---|
| 429 | { |
---|
| 430 | int c=trctx.labelcounter++; $$.counter=c; $$.stack=trstack.currentPos(); |
---|
| 431 | } |
---|
| 432 | newvar_or_expr |
---|
| 433 | { |
---|
| 434 | $$=$4; $$.counter=$3.counter; $$.stack=$3.stack; |
---|
| 435 | }; |
---|
| 436 | |
---|
| 437 | for_statement: |
---|
| 438 | |
---|
| 439 | /////////// for(in) ... //////////// |
---|
| 440 | for_statement_begin INNN |
---|
| 441 | {//3 |
---|
| 442 | if (!$1.ident) |
---|
| 443 | { |
---|
| 444 | trctx.err->printf("for(... in ...) requires an variable\n"); |
---|
| 445 | return 1; |
---|
| 446 | } |
---|
| 447 | int loc; |
---|
| 448 | if ($1.var) // for(var x[=expr] in |
---|
| 449 | $$.setInt($1.getInt()); |
---|
| 450 | else |
---|
| 451 | { // for(x in |
---|
| 452 | if (variableOk($$,$1,loc)) |
---|
| 453 | $$.setInt(loc); |
---|
| 454 | else if (globalOk($1)) |
---|
| 455 | { |
---|
| 456 | trctx.err->printf("global '%s' can't be iterating variable in for\n",str($1)); |
---|
| 457 | return 1; |
---|
| 458 | } |
---|
| 459 | else |
---|
| 460 | { |
---|
| 461 | badVariable($$,$1); |
---|
| 462 | return 1; |
---|
| 463 | } |
---|
| 464 | } |
---|
| 465 | } |
---|
| 466 | expr_or_objname ')' |
---|
| 467 | {//6 |
---|
[332] | 468 | trctx.emitLine(); |
---|
[163] | 469 | if ($4.constant) |
---|
| 470 | {trctx.err->printf("%s can't be iterated\n",str($4)); return 1;} |
---|
| 471 | if ($4.objname) |
---|
[348] | 472 | trctx.out->printf("move %s.iterator,m[--m0]\n",$4.getString().c_str()); |
---|
[163] | 473 | else |
---|
| 474 | trctx.out->printf("move s%d,m1\nmove [m1].\"iterator\",m[--m0]\n",0); |
---|
| 475 | trstack.adjust(-1); |
---|
| 476 | // s0=iterator s1=obj (=obj.iterator) |
---|
| 477 | trstack.loops.addLoop($1.counter,trstack.currentPos()); |
---|
| 478 | trctx.out->printf(":_loop1_%d\n",$1.counter); |
---|
| 479 | trctx.out->printf(":_loop_%d\n",$1.counter); |
---|
| 480 | trctx.out->printf("move s0,m1\nmove [m1].\"next\",m2\n"); |
---|
| 481 | trctx.out->printf("if m2,==,0,:_loop_end_%d\n",$1.counter); |
---|
| 482 | trctx.out->printf("move [m1].\"value\",s%d\n",$3.getInt()-trstack.currentPos()); |
---|
| 483 | } |
---|
| 484 | pseudoblok_statement |
---|
| 485 | { |
---|
| 486 | trctx.out->printf("jump :_loop1_%d\n",$1.counter); |
---|
| 487 | trctx.out->printf(":_loop_end_%d\n",$1.counter); |
---|
| 488 | trstack.loops.drop(); |
---|
| 489 | if ($1.stack != trstack.currentPos()) |
---|
| 490 | trctx.out->printf("add %d,m0\n",$1.stack-trstack.currentPos()); |
---|
| 491 | trstack.adjust($1.stack-trstack.currentPos()); |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | | |
---|
| 495 | |
---|
| 496 | /////////// for(;;) ... //////////// |
---|
| 497 | for_statement_begin ';' |
---|
| 498 | { //3 |
---|
[332] | 499 | trctx.emitLine(); |
---|
[163] | 500 | //trctx.out->printf("# for_statement_begin pos=%d ident=%d var=%d\n",trstack.currentPos(),$1.ident,$1.var); |
---|
| 501 | if ((!$1.var) && ($1.ident)) |
---|
| 502 | { // for(x; |
---|
| 503 | int loc; |
---|
| 504 | if ((!variableOk($$,$1,loc)) || (globalOk($1))) |
---|
| 505 | { |
---|
| 506 | badVariable($$,$1); |
---|
| 507 | return 1; |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | if (!$1.constant && !$1.ident) |
---|
| 511 | { |
---|
| 512 | trctx.out->printf("inc m0\n"); |
---|
| 513 | trstack.adjust(+1); |
---|
| 514 | } |
---|
| 515 | trstack.loops.addLoop($1.counter,trstack.currentPos()); |
---|
| 516 | trctx.out->printf(":_loop1_%d\n",$1.counter); |
---|
| 517 | //trctx.out->printf("# expr#2\n"); |
---|
| 518 | } |
---|
| 519 | expr_or_empty ';' |
---|
| 520 | { //6 |
---|
[332] | 521 | trctx.emitLine(); |
---|
[163] | 522 | int c=$1.counter; |
---|
| 523 | warnTruthValue($4); |
---|
| 524 | if ($4.constant) |
---|
| 525 | {if (!$4.getInt()) trctx.out->printf("jump :_loop_end_%d\n",c);} |
---|
| 526 | else |
---|
| 527 | { |
---|
| 528 | trctx.out->printf("if m[m0++],==,0,:_loop_end_%d\n",c,c); |
---|
| 529 | trstack.adjust(+1); |
---|
| 530 | } |
---|
[382] | 531 | trctx.tmp=""; |
---|
[163] | 532 | trctx.divertOut(); |
---|
| 533 | //trctx.out->printf("# expr#3\n"); |
---|
| 534 | } |
---|
| 535 | expr_or_empty ')' |
---|
| 536 | { //9 |
---|
[332] | 537 | trctx.emitLine(); |
---|
[163] | 538 | if (!$7.constant) { trctx.out->printf("inc m0\n"); trstack.adjust(+1); } |
---|
| 539 | trctx.restoreOut(); |
---|
[382] | 540 | $$.setString(trctx.tmp.c_str()); |
---|
[163] | 541 | //trctx.out->printf("# pseudoblok_statement pos=%d\n",trstack.currentPos()); |
---|
| 542 | } |
---|
| 543 | pseudoblok_statement |
---|
| 544 | {//11 |
---|
| 545 | trctx.out->printf(":_loop_%d\n",$1.counter); |
---|
| 546 | LoopInfo* li=trstack.loops.getLoop(0); |
---|
| 547 | if (li->location != trstack.currentPos()) |
---|
| 548 | trctx.out->printf("add %d,m0\n",li->location-trstack.currentPos()); |
---|
| 549 | trctx.out->printf(str($9)); |
---|
| 550 | if (li->location != trstack.currentPos()) |
---|
| 551 | trctx.out->printf("sub %d,m0\n",li->location-trstack.currentPos()); |
---|
| 552 | trctx.out->printf("jump :_loop1_%d\n:_loop_end_%d\n",$1.counter,$1.counter); |
---|
| 553 | if ($1.stack != trstack.currentPos()) |
---|
| 554 | trctx.out->printf("add %d,m0\n",$1.stack-trstack.currentPos()); |
---|
| 555 | trstack.adjust($1.stack-trstack.currentPos()); |
---|
| 556 | trstack.loops.drop(); |
---|
| 557 | } |
---|
| 558 | ; |
---|
| 559 | |
---|
| 560 | pseudoblok_statement: |
---|
[332] | 561 | {trctx.emitLine(); int pos=trstack.currentPos(); $$.setInt(pos);} |
---|
[163] | 562 | statement |
---|
| 563 | { |
---|
| 564 | int pos=$1.getInt(); |
---|
| 565 | if (pos!=trstack.currentPos()) trctx.out->printf("add %d,m0\n",pos-trstack.currentPos()); |
---|
| 566 | trstack.dropToPos(pos); |
---|
[332] | 567 | trctx.emitLine(); |
---|
[163] | 568 | }; |
---|
| 569 | |
---|
| 570 | if_statement: |
---|
| 571 | if_condition pseudoblok_statement |
---|
| 572 | { |
---|
| 573 | if ($1.stack!=trstack.currentPos()) |
---|
| 574 | trctx.out->printf("add %d,m0\n",$1.stack-trstack.currentPos()); |
---|
| 575 | trstack.adjust(trstack.currentPos()-$1.stack); |
---|
| 576 | trctx.out->printf("jump :_if_end_%d\n:_if_else_%d\n",$1.getInt(),$1.getInt()); |
---|
| 577 | } |
---|
| 578 | ELSE |
---|
| 579 | {trstack.adjust($1.stack-trstack.currentPos());} |
---|
| 580 | pseudoblok_statement |
---|
| 581 | { |
---|
| 582 | if ($1.stack!=trstack.currentPos()) |
---|
| 583 | trctx.out->printf("add %d,m0\n",$1.stack-trstack.currentPos()); |
---|
| 584 | trstack.adjust(trstack.currentPos()-$1.stack); |
---|
| 585 | trctx.out->printf(":_if_end_%d\n",$1.getInt()); |
---|
| 586 | } |
---|
| 587 | | |
---|
| 588 | if_condition pseudoblok_statement |
---|
| 589 | { |
---|
| 590 | if ($1.stack!=trstack.currentPos()) |
---|
| 591 | trctx.out->printf("add %d,m0\n",$1.stack-trstack.currentPos()); |
---|
| 592 | trstack.dropToPos($1.stack); |
---|
| 593 | trctx.out->printf(":_if_else_%d\n",$1.getInt()); |
---|
| 594 | } |
---|
| 595 | ; |
---|
| 596 | |
---|
| 597 | if_condition: IF |
---|
[332] | 598 | {$$.stack=trstack.currentPos();trctx.emitLine();} |
---|
[163] | 599 | |
---|
| 600 | '(' expr ')' |
---|
| 601 | { |
---|
[332] | 602 | trctx.emitLine(); |
---|
[163] | 603 | int c=trctx.labelcounter++; |
---|
| 604 | $$.setInt(c); |
---|
| 605 | warnTruthValue($4); |
---|
| 606 | if ($4.constant) |
---|
| 607 | { |
---|
| 608 | if (!$4.getInt()) trctx.out->printf("jump :_if_else_%d\n",c); |
---|
| 609 | } |
---|
| 610 | else |
---|
| 611 | { |
---|
[332] | 612 | trctx.out->printf("if ~=,m[m0++],:_if_else_%d\n",c); |
---|
[163] | 613 | trstack.adjust(+1); |
---|
| 614 | } |
---|
| 615 | $$.stack=$2.stack; |
---|
| 616 | }; |
---|
| 617 | |
---|
| 618 | blok: '{' |
---|
| 619 | { int pos=trstack.currentPos(); |
---|
| 620 | $$.setInt(pos); |
---|
| 621 | } |
---|
| 622 | recurcode '}' |
---|
| 623 | { |
---|
| 624 | int pos=$2.getInt(); |
---|
| 625 | if (pos!=trstack.currentPos()) trctx.out->printf("add %d,m0\n",pos-trstack.currentPos()); |
---|
| 626 | trstack.dropToPos(pos); |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | assign_op: ASSIGN_ADD {$$.setInt(0);} |
---|
| 630 | | ASSIGN_SUB {$$.setInt(1);} |
---|
| 631 | | ASSIGN_MUL {$$.setInt(2);} |
---|
| 632 | | ASSIGN_DIV {$$.setInt(3);} |
---|
| 633 | | ASSIGN_MOD {$$.setInt(4);} |
---|
| 634 | |
---|
| 635 | plusminus: PLUSPLUS {$$.setInt(1);} | MINUSMINUS {$$.setInt(0);} |
---|
| 636 | |
---|
| 637 | expr: expr_special_ident |
---|
| 638 | { |
---|
| 639 | //trctx.out->printf("# expr: ident=%d str=%s\n",$1.ident,(const char*)$1.getString()); |
---|
| 640 | if ($1.ident) |
---|
| 641 | { |
---|
| 642 | if (evalVariable($$,$1)) |
---|
| 643 | $$.constant=false; |
---|
| 644 | else |
---|
| 645 | return 1; |
---|
| 646 | } |
---|
| 647 | else |
---|
| 648 | {$$=$1; $$.ident=false;} |
---|
[332] | 649 | trctx.emitLine(); |
---|
[163] | 650 | } |
---|
| 651 | ; |
---|
| 652 | |
---|
[345] | 653 | stackexpr: expr {if ($1.constant) {trctx.out->printf("push %s\n",litstr($1)); trstack.adjust(-1); $$.constant=0;} } |
---|
| 654 | |
---|
[163] | 655 | expr_or_empty: |
---|
| 656 | expr {$$=$1;} |
---|
| 657 | |
---|
| 658 | | //nic |
---|
| 659 | { $$.setInt(1); $$.assign=false; $$.constant=true; $$.ident=false; $$.ident=false; } |
---|
| 660 | ; |
---|
| 661 | |
---|
| 662 | expr_special_ident: CONSTANT { $$=$1; $$.constant=1; $$.ident=0; } |
---|
| 663 | |
---|
| 664 | | IDENT { $$.ident=true; $$.setString($1.getString()); } |
---|
| 665 | |
---|
| 666 | | OBJNAME ':' IDENT {$$.constant=0; $$.ident=0; |
---|
[348] | 667 | trctx.out->printf("push %s:%s\n",$1.getString().c_str(), |
---|
| 668 | $3.getString().c_str()); |
---|
[163] | 669 | trstack.adjust(-1); |
---|
| 670 | } |
---|
| 671 | | plusminus IDENT |
---|
| 672 | { |
---|
[332] | 673 | trctx.emitLine(); |
---|
[163] | 674 | $$.ident=0; |
---|
| 675 | int loc; if (variableOk($$,$2,loc)) |
---|
| 676 | { loc-=trstack.currentPos(); |
---|
| 677 | trctx.out->printf("%s s%d\npush s%d\n",$1.getInt()?"inc":"dec",loc,loc); |
---|
| 678 | trstack.adjust(-1);} |
---|
| 679 | else if (globalOk($2)) |
---|
| 680 | { trctx.out->printf("%s @%s\npush @%s\n",$1.getInt()?"inc":"dec",str($2),str($2)); |
---|
| 681 | trstack.adjust(-1);} |
---|
| 682 | else {badVariable($$,$2); return 1;} |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | | IDENT plusminus |
---|
| 686 | { |
---|
[332] | 687 | trctx.emitLine(); |
---|
[163] | 688 | $$.ident=0; |
---|
| 689 | int loc; if (variableOk($$,$1,loc)) |
---|
| 690 | {loc-=trstack.currentPos(); trctx.out->printf("push s%d\n%s s%d\n",loc,$2.getInt()?"inc":"dec",loc+1); |
---|
| 691 | trstack.adjust(-1);} |
---|
| 692 | else if (globalOk($1)) |
---|
[348] | 693 | { trctx.out->printf("push @%s\n%s @%s\n",$1.getString().c_str(), |
---|
| 694 | $2.getInt()?"inc":"dec",$1.getString().c_str()); |
---|
[163] | 695 | trstack.adjust(-1);} |
---|
| 696 | else {badVariable($$,$1); return 1;} |
---|
| 697 | } |
---|
| 698 | |
---|
[332] | 699 | | IDENT assign_op expr { trctx.emitLine(); $$.ident=0; |
---|
[163] | 700 | if (!handleAssignOp($$,$1,$3,assign_op_names[$2.getInt()])) |
---|
| 701 | if (globalOk($1)) {SString t="@"; t+=$1.getString(); |
---|
[348] | 702 | handleAssignOp2($$,t.c_str(),$3,assign_op_names[$2.getInt()],0,1);} |
---|
[163] | 703 | else { badVariable($$,$1); return 1; } |
---|
| 704 | } |
---|
| 705 | |
---|
[332] | 706 | | TYPEOF '(' expr ')' { trctx.emitLine(); $$.ident=0; |
---|
| 707 | if ($3.constant) |
---|
| 708 | {$$.constant=1; $$=$3.getExtType();} |
---|
[163] | 709 | else |
---|
| 710 | {trctx.out->printf("type s0,s0\n");} |
---|
| 711 | } |
---|
[333] | 712 | | INT_TYPE '(' expr ')' { trctx.emitLine(); $$.ident=0; |
---|
[332] | 713 | if ($3.constant) |
---|
| 714 | {$$.constant=1; $$=ExtValue($3.getInt());} |
---|
| 715 | else |
---|
| 716 | {trctx.out->printf("conv 1,s0\n");} |
---|
| 717 | } |
---|
[333] | 718 | | FLOAT_TYPE '(' expr ')' { trctx.emitLine(); $$.ident=0; |
---|
[332] | 719 | if ($3.constant) |
---|
| 720 | {$$.constant=1; $$=ExtValue($3.getDouble());} |
---|
| 721 | else |
---|
| 722 | {trctx.out->printf("conv 2,s0\n");} |
---|
| 723 | } |
---|
[333] | 724 | | STRING_TYPE '(' expr ')' { trctx.emitLine(); $$.ident=0; |
---|
[332] | 725 | if ($3.constant) |
---|
| 726 | {$$.constant=1; $$=ExtValue($3.getString());} |
---|
| 727 | else |
---|
| 728 | {trctx.out->printf("conv 3,s0\n");} |
---|
| 729 | } |
---|
[163] | 730 | |
---|
[332] | 731 | | expr '+' expr { handleTwoArg($$,$1,$3,'+',"add",0,1); } |
---|
| 732 | | expr '-' expr { handleTwoArg($$,$1,$3,'-',"sub",0,0); } |
---|
| 733 | | expr '*' expr { handleTwoArg($$,$1,$3,'*',"mul",0,1); } |
---|
| 734 | | expr '/' expr { handleTwoArg($$,$1,$3,'/',"div",0,0); } |
---|
| 735 | | expr '&' expr { handleTwoArg($$,$1,$3,'&',"and",0,0); } |
---|
| 736 | | expr '|' expr { handleTwoArg($$,$1,$3,'|',"or",0,0); } |
---|
| 737 | | expr '%' expr { handleTwoArg($$,$1,$3,'%',"mod",0,0); } |
---|
[163] | 738 | |
---|
[332] | 739 | | expr LOGIC_AND |
---|
| 740 | { |
---|
| 741 | // a && b: |
---|
| 742 | // push a |
---|
| 743 | // if (a) |
---|
| 744 | // pop; goto and_b |
---|
| 745 | // else |
---|
| 746 | // pop; push 0; goto and_end |
---|
| 747 | // and_b: |
---|
| 748 | // push b |
---|
| 749 | // and_end: |
---|
| 750 | trctx.emitLine(); |
---|
| 751 | // trctx.out->printf("\n####### logic AND\n"); |
---|
| 752 | int c=trctx.labelcounter++; |
---|
| 753 | $$.setInt(c); |
---|
| 754 | if ($1.constant) |
---|
| 755 | { |
---|
[333] | 756 | ExtValue::CompareResult cond=$1.compare(ExtValue::zero()); |
---|
| 757 | if (resultIsRelaxedEqual(cond)) |
---|
[332] | 758 | { |
---|
| 759 | $1.counter=0; |
---|
| 760 | // no stack adjust - next tokens are processed in a different context |
---|
| 761 | trctx.out->printf("push 0\njump :_and_end_%d\n",c); |
---|
| 762 | } |
---|
| 763 | else |
---|
| 764 | $1.counter=1; |
---|
| 765 | } |
---|
| 766 | else |
---|
| 767 | { |
---|
| 768 | trstack.adjust(+1); // stack as if (a==true), b expr is processed |
---|
| 769 | trctx.out->printf("if !~,m[m0++],:_and_b_%d\n" |
---|
| 770 | "push 0\n" |
---|
| 771 | "jump :_and_end_%d\n" |
---|
| 772 | ":_and_b_%d\n" |
---|
| 773 | ,c,c,c); |
---|
| 774 | } |
---|
| 775 | } |
---|
| 776 | expr |
---|
| 777 | { |
---|
| 778 | $$.ident=false; |
---|
| 779 | $$.constant=0; |
---|
| 780 | if ($4.constant) |
---|
| 781 | { |
---|
| 782 | if (!($1.constant && $1.counter==0)) |
---|
| 783 | { |
---|
[333] | 784 | ExtValue::CompareResult cond=$4.compare(ExtValue::zero()); |
---|
| 785 | bool value=!resultIsRelaxedEqual(cond); |
---|
[332] | 786 | trstack.adjust(-1); |
---|
| 787 | trctx.out->printf("push %d\n",value); |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | trctx.out->printf(":_and_end_%d\n",$3.getInt()); |
---|
| 791 | // trctx.out->printf("#################\n\n"); |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | | expr LOGIC_OR |
---|
| 795 | { |
---|
| 796 | // a || b: |
---|
| 797 | // push a |
---|
| 798 | // if (!a) |
---|
| 799 | // pop; goto and_b |
---|
| 800 | // else |
---|
| 801 | // pop; push 1; goto and_end |
---|
| 802 | // and_b: |
---|
| 803 | // push b |
---|
| 804 | // and_end: |
---|
| 805 | trctx.emitLine(); |
---|
| 806 | // trctx.out->printf("\n####### logic AND\n"); |
---|
| 807 | int c=trctx.labelcounter++; |
---|
| 808 | $$.setInt(c); |
---|
| 809 | if ($1.constant) |
---|
| 810 | { |
---|
[333] | 811 | ExtValue::CompareResult cond=$1.compare(ExtValue::zero()); |
---|
| 812 | if (!resultIsRelaxedEqual(cond)) |
---|
[332] | 813 | { |
---|
| 814 | $1.counter=1; |
---|
| 815 | // no stack adjust - next tokens are processed in a different context |
---|
| 816 | trctx.out->printf("push 1\njump :_or_end_%d\n",c); |
---|
| 817 | } |
---|
| 818 | else |
---|
| 819 | $1.counter=0; |
---|
| 820 | } |
---|
| 821 | else |
---|
| 822 | { |
---|
| 823 | trstack.adjust(+1); // stack for (a==false) |
---|
| 824 | trctx.out->printf("if ~=,m[m0++],:_or_b_%d\n" |
---|
| 825 | "push 1\n" |
---|
| 826 | "jump :_or_end_%d\n" |
---|
| 827 | ":_or_b_%d\n" |
---|
| 828 | ,c,c,c); |
---|
| 829 | } |
---|
| 830 | } |
---|
| 831 | expr |
---|
| 832 | { |
---|
| 833 | $$.ident=false; |
---|
| 834 | $$.constant=0; |
---|
| 835 | if ($4.constant) |
---|
| 836 | { |
---|
| 837 | if (!($1.constant && $1.counter==1)) |
---|
| 838 | { |
---|
[333] | 839 | ExtValue::CompareResult cond=$4.compare(ExtValue::zero()); |
---|
| 840 | bool value=!resultIsRelaxedEqual(cond); |
---|
[332] | 841 | trstack.adjust(-1); |
---|
| 842 | trctx.out->printf("push %d\n",value); |
---|
| 843 | } |
---|
| 844 | } |
---|
| 845 | trctx.out->printf(":_or_end_%d\n",$3.getInt()); |
---|
| 846 | // trctx.out->printf("#################\n\n"); |
---|
| 847 | } |
---|
| 848 | |
---|
| 849 | |
---|
| 850 | | expr LSHIFT expr { handleTwoArg($$,$1,$3,LSHIFT,"shift",0,0); } |
---|
| 851 | | expr RSHIFT expr { handleTwoArg($$,$1,$3,RSHIFT,"shift",1,0); } |
---|
| 852 | | expr EQUAL expr { if (!handleCompare($$,$1,$3,ExtValue::CmpEQ,"==")) return 1; } |
---|
| 853 | | expr NOT_EQUAL expr { if (!handleCompare($$,$1,$3,ExtValue::CmpNE,"!=")) return 1; } |
---|
| 854 | | expr GEQUAL expr { if (!handleCompare($$,$1,$3,ExtValue::CmpGE,">=")) return 1; } |
---|
| 855 | | expr LEQUAL expr { if (!handleCompare($$,$1,$3,ExtValue::CmpLE,"<=")) return 1; } |
---|
| 856 | | expr '>' expr { if (!handleCompare($$,$1,$3,ExtValue::CmpGT,">")) return 1; } |
---|
| 857 | | expr '<' expr { if (!handleCompare($$,$1,$3,ExtValue::CmpLT,"<")) return 1; } |
---|
| 858 | |
---|
[163] | 859 | | '!' expr { |
---|
[332] | 860 | trctx.emitLine(); $$.assign=$2.assign; $$.parens=0; $$.ident=0; |
---|
[163] | 861 | if ($2.constant) |
---|
[333] | 862 | {$$.constant=1; ExtValue::CompareResult res=$2.compare(ExtValue((paInt)0)); $$.setInt(resultIsRelaxedEqual(res));} |
---|
[163] | 863 | else |
---|
[332] | 864 | {trctx.out->printf("setif ~=,s0,s0\n");} |
---|
[163] | 865 | } |
---|
| 866 | |
---|
| 867 | | '-' expr %prec NEG { |
---|
[332] | 868 | trctx.emitLine(); $$.assign=$2.assign; $$.parens=0; $$.ident=0; |
---|
[163] | 869 | if ($2.constant) |
---|
| 870 | { $$.constant=$2.constant; |
---|
| 871 | if ($2.type==TInt) $$.setInt(-$2.getInt()); |
---|
| 872 | else if ($2.type==TDouble) $$.setDouble(-$2.getDouble()); |
---|
| 873 | else $$=$2; |
---|
| 874 | } |
---|
| 875 | else |
---|
| 876 | { |
---|
| 877 | $$.constant=0; SString t="-"; t+=$2.getString(); $$.setString(t); |
---|
| 878 | trctx.out->printf("mul -1,s0\n"); |
---|
| 879 | } |
---|
| 880 | } |
---|
| 881 | |
---|
[332] | 882 | | '(' expr ')' { trctx.emitLine(); $$ = $2; $$.assign=$2.assign?(!$2.parens):0; $$.parens=1; $$.ident=0; } |
---|
[163] | 883 | |
---|
| 884 | | OBJNAME '.' member { |
---|
[332] | 885 | trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 886 | if ($3.constant) |
---|
| 887 | { |
---|
| 888 | trctx.out->printf("push %s.%s\n",str($1),str($3)); trstack.adjust(-1); |
---|
| 889 | } |
---|
| 890 | else |
---|
| 891 | { |
---|
| 892 | trctx.out->printf("move s0,m1\nmove %s.[m1],s0\n",str($1)); |
---|
| 893 | } |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | | OBJNAME '.' member assign_op expr |
---|
[332] | 897 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 898 | if ($3.constant) |
---|
| 899 | { |
---|
[348] | 900 | handleAssignOp2($$,t.c_str(),$5,assign_op_names[$4.getInt()],0,1); |
---|
[163] | 901 | } |
---|
| 902 | else |
---|
| 903 | { |
---|
| 904 | int sp=($5.constant)?0:1; |
---|
| 905 | t=$1.getString();t+=".[m1]"; |
---|
| 906 | trctx.out->printf("move s0,m1\n",str($1)); |
---|
[348] | 907 | handleAssignOp2($$,t.c_str(),$5,assign_op_names[$4.getInt()],sp,0); |
---|
[163] | 908 | if (sp) {trctx.out->printf("inc m0\n"); trstack.adjust(1);} |
---|
| 909 | } |
---|
| 910 | } |
---|
| 911 | |
---|
| 912 | | plusminus OBJNAME '.' member { |
---|
[332] | 913 | trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$2.getString(); t+="."; t+=$4.getString(); $$.setString(t); |
---|
[163] | 914 | if ($4.constant) |
---|
| 915 | { |
---|
| 916 | trctx.out->printf("%s %s.%s\npush %s.%s\n",$1.getInt()?"inc":"dec", |
---|
| 917 | str($2),str($4),str($2),str($4)); |
---|
| 918 | trstack.adjust(-1); |
---|
| 919 | } |
---|
| 920 | else |
---|
| 921 | { |
---|
| 922 | trctx.out->printf("move s0,m1\n%s %s.[m1]\nmove %s.[m1],s0\n", |
---|
| 923 | $1.getInt()?"inc":"dec",str($2),str($2)); |
---|
| 924 | } |
---|
| 925 | } |
---|
| 926 | |
---|
| 927 | | OBJNAME '.' member plusminus { |
---|
[332] | 928 | trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 929 | if ($3.constant) |
---|
| 930 | { |
---|
| 931 | trctx.out->printf("push %s.%s\n%s %s.%s\n", |
---|
| 932 | str($1),str($3),$4.getInt()?"inc":"dec",str($1),str($3)); |
---|
| 933 | trstack.adjust(-1); |
---|
| 934 | } |
---|
| 935 | else |
---|
| 936 | { |
---|
| 937 | trctx.out->printf("move s0,m1\nmove %s.[m1],s0\n%s %s.[m1]\n", |
---|
| 938 | str($1),$4.getInt()?"inc":"dec",str($1)); |
---|
| 939 | } |
---|
| 940 | } |
---|
| 941 | |
---|
| 942 | | OBJNAME '.' '*' { |
---|
[332] | 943 | trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+=".*"; $$.setString(t); |
---|
[163] | 944 | trctx.out->printf("push %s.*\n",str($1)); trstack.adjust(-1); |
---|
| 945 | } |
---|
| 946 | |
---|
| 947 | |
---|
| 948 | | OBJNAME '.' member '=' expr { |
---|
[332] | 949 | trctx.emitLine(); $$=$5; $$.assign=1; $$.parens=0; $$.ident=0; |
---|
[163] | 950 | if ($3.constant) |
---|
| 951 | { |
---|
| 952 | if ($$.constant) |
---|
| 953 | trctx.out->printf("move %s,%s.%s\n",litstr($5),str($1),str($3)); |
---|
| 954 | else |
---|
| 955 | trctx.out->printf("move s0,%s.%s\n",str($1),str($3)); |
---|
| 956 | } |
---|
| 957 | else |
---|
| 958 | { |
---|
| 959 | if ($$.constant) |
---|
| 960 | { |
---|
| 961 | trctx.out->printf("move m[m0++],m1\nmove %s,%s.[m1]\n", |
---|
| 962 | litstr($5),str($1)); |
---|
| 963 | trstack.adjust(1); |
---|
| 964 | } |
---|
| 965 | else |
---|
| 966 | { |
---|
| 967 | trctx.out->printf("move s1,m1\nmove m[m0++],s0\nmove s0,%s.[m1]\n", |
---|
| 968 | str($1)); |
---|
| 969 | trstack.adjust(1); |
---|
| 970 | } |
---|
| 971 | } |
---|
| 972 | } |
---|
| 973 | |
---|
| 974 | | OBJNAME '.' member '(' arguments ')' |
---|
| 975 | { |
---|
[332] | 976 | trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 977 | int adj=0,adj2=0; |
---|
| 978 | if ($5.getInt()==0) |
---|
| 979 | {trctx.out->printf("dec m0\n");trstack.adjust(-1);adj=1;} |
---|
| 980 | if ($3.constant) |
---|
| 981 | trctx.out->printf("call %s.%s\n",str($1),str($3)); |
---|
| 982 | else |
---|
| 983 | { |
---|
| 984 | trctx.out->printf("move s%d,m1\ncall %s.[m1]\n",$5.getInt()+adj,str($1)); |
---|
| 985 | adj2=1; |
---|
| 986 | } |
---|
| 987 | adj2+=$5.getInt()-1+adj; |
---|
| 988 | if (adj2>0) |
---|
| 989 | { |
---|
| 990 | trctx.out->printf("add %d,m0\nxmove s%d,s0\n",adj2,-adj2); |
---|
| 991 | trstack.adjust(adj2); |
---|
| 992 | } |
---|
| 993 | } |
---|
| 994 | |
---|
| 995 | | CALL expr '(' arguments ')' |
---|
[332] | 996 | { trctx.emitLine(); $$.constant=0; $$.ident=0; $$.setString($2.getString()); |
---|
[163] | 997 | short adj=0; |
---|
| 998 | if ($4.getInt()==0) |
---|
| 999 | {trctx.out->printf("dec m0\n");trstack.adjust(-1);adj=1;} |
---|
| 1000 | if ($2.constant) |
---|
| 1001 | trctx.out->printf("call %s\n",litstr($2)); |
---|
| 1002 | else |
---|
| 1003 | trctx.out->printf("call s%d\n",$4.getInt()+adj); |
---|
| 1004 | if (($4.getInt()+adj) > 0) |
---|
| 1005 | { |
---|
| 1006 | trctx.out->printf("add %d,m0\nxmove s%d,s0\n",$4.getInt()+adj,-($4.getInt()+adj)); |
---|
| 1007 | trstack.adjust($4.getInt()+adj); |
---|
| 1008 | } |
---|
| 1009 | } |
---|
| 1010 | |
---|
| 1011 | | FUNCTION IDENT |
---|
[332] | 1012 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=":"; t+=$1.getString(); $$.setString(t); |
---|
[348] | 1013 | trctx.out->printf("push :%s\n",$2.getString().c_str()); |
---|
[163] | 1014 | trstack.adjust(-1); |
---|
| 1015 | } |
---|
| 1016 | |
---|
[341] | 1017 | | stackexpr '.' member |
---|
[332] | 1018 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 1019 | if ($3.constant) |
---|
| 1020 | trctx.out->printf("move s0,m1\nmove [m1].%s,s0\n",str($3)); |
---|
| 1021 | else |
---|
| 1022 | // trctx.out->printf("move s1,m1\nmove m[m0++],m2\nmove [m1].[m2],s0\n"); |
---|
| 1023 | {trctx.out->printf("move s1,m1\nmove m[m0++],m2\nmove [m1].[m2],s0\n");trstack.adjust(1);} |
---|
| 1024 | } |
---|
| 1025 | |
---|
[341] | 1026 | | plusminus stackexpr '.' member |
---|
[332] | 1027 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$2.getString(); t+="."; t+=$4.getString(); $$.setString(t); |
---|
[163] | 1028 | if ($4.constant) |
---|
| 1029 | trctx.out->printf("move s0,m1\n%s [m1].%s\nmove [m1].%s,s0\n", |
---|
| 1030 | $1.getInt()?"inc":"dec",str($4),str($4)); |
---|
| 1031 | else |
---|
| 1032 | // trctx.out->printf("move s1,m1\nmove m[m0++],m2\nmove [m1].[m2],s0\n"); |
---|
| 1033 | {trctx.out->printf("move s1,m1\nmove m[m0++],m2\n%s [m1].[m2]\nmove [m1].[m2],s0\n", |
---|
| 1034 | $1.getInt()?"inc":"dec");trstack.adjust(1);} |
---|
| 1035 | } |
---|
| 1036 | |
---|
[341] | 1037 | | stackexpr '.' member plusminus |
---|
[332] | 1038 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 1039 | if ($3.constant) |
---|
| 1040 | trctx.out->printf("move s0,m1\nmove [m1].%s,s0\n%s [m1].%s\n", |
---|
| 1041 | str($3),$4.getInt()?"inc":"dec",str($3)); |
---|
| 1042 | else |
---|
| 1043 | // trctx.out->printf("move s1,m1\nmove m[m0++],m2\nmove [m1].[m2],s0\n"); |
---|
| 1044 | {trctx.out->printf("move s1,m1\nmove m[m0++],m2\nmove [m1].[m2],s0\n%s [m1].[m2]\n", |
---|
| 1045 | $4.getInt()?"inc":"dec");trstack.adjust(1);} |
---|
| 1046 | } |
---|
| 1047 | |
---|
[341] | 1048 | | stackexpr '.' member assign_op expr |
---|
[332] | 1049 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 1050 | if ($3.constant) |
---|
| 1051 | { |
---|
| 1052 | int sp; |
---|
| 1053 | if ($5.constant) |
---|
| 1054 | {sp=0; trctx.out->printf("move s0,m1\n");} |
---|
| 1055 | else |
---|
| 1056 | {sp=1; trctx.out->printf("move s1,m1\n");} |
---|
| 1057 | t="[m1]."; t+=str($3); |
---|
[348] | 1058 | handleAssignOp2($$,t.c_str(),$5,assign_op_names[$4.getInt()],sp,0); |
---|
[163] | 1059 | if (sp) {trctx.out->printf("inc m0\n");trstack.adjust(1);} |
---|
| 1060 | } |
---|
| 1061 | else |
---|
| 1062 | { |
---|
| 1063 | int sp; |
---|
| 1064 | char *t; |
---|
| 1065 | if ($5.constant) |
---|
| 1066 | {sp=1; t="move s1,m1\nmove s0,m2\n";} |
---|
| 1067 | else |
---|
| 1068 | {sp=2; t="move s2,m1\nmove s1,m2\n";} |
---|
| 1069 | trctx.out->printf(t); |
---|
| 1070 | handleAssignOp2($$,"[m1].[m2]",$5,assign_op_names[$4.getInt()],sp,0); |
---|
| 1071 | trctx.out->printf("add %d,m0\n",sp); |
---|
| 1072 | trstack.adjust(sp); |
---|
| 1073 | } |
---|
| 1074 | } |
---|
| 1075 | |
---|
[341] | 1076 | | stackexpr '.' member '=' stackexpr |
---|
[332] | 1077 | { trctx.emitLine(); $$=$5; $$.assign=1; $$.parens=0; $$.ident=0; |
---|
[163] | 1078 | if ($3.constant) |
---|
| 1079 | { |
---|
| 1080 | trctx.out->printf("move s1,m1\nmove m[m0++],s0\nmove s0,[m1].%s\n",str($3)); |
---|
| 1081 | trstack.adjust(1); |
---|
| 1082 | } |
---|
| 1083 | else |
---|
| 1084 | { |
---|
| 1085 | trctx.out->printf("move s2,m1\nmove s1,m2\nmove s0,[m1].[m2]\nadd 2,m0\nmove s-2,s0\n"); |
---|
| 1086 | trstack.adjust(2); |
---|
| 1087 | } |
---|
| 1088 | } |
---|
| 1089 | |
---|
[341] | 1090 | | stackexpr '.' member '(' arguments ')' |
---|
[332] | 1091 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+="."; t+=$3.getString(); $$.setString(t); |
---|
[163] | 1092 | int adj=0; |
---|
| 1093 | if ($5.getInt()==0) |
---|
| 1094 | {trctx.out->printf("dec m0\n");trstack.adjust(-1);adj=1;} |
---|
| 1095 | if ($3.constant) |
---|
| 1096 | { |
---|
| 1097 | trctx.out->printf("move s%d,m1\ncall [m1].%s\n",$5.getInt()+adj,str($3)); |
---|
| 1098 | adj+=1; |
---|
| 1099 | } |
---|
| 1100 | else |
---|
| 1101 | { |
---|
| 1102 | trctx.out->printf("move s%d,m2\nmove s%d,m1\ncall [m2].[m1]\n", |
---|
| 1103 | $5.getInt()+adj+1,$5.getInt()+adj); |
---|
| 1104 | adj+=2; |
---|
| 1105 | } |
---|
| 1106 | if (($5.getInt()+adj) > 1) |
---|
| 1107 | { |
---|
| 1108 | trctx.out->printf("add %d,m0\nxmove s%d,s0\n",$5.getInt()-1+adj,-($5.getInt()-1+adj)); |
---|
| 1109 | trstack.adjust($5.getInt()-1+adj); |
---|
| 1110 | } |
---|
| 1111 | } |
---|
| 1112 | |
---|
[341] | 1113 | | stackexpr '[' expr ']' '=' expr // shortcut: expr.set(expr,expr) |
---|
[332] | 1114 | { trctx.emitLine(); $$=$6; $$.assign=1; $$.parens=0; $$.ident=0; |
---|
[163] | 1115 | if ($3.constant) |
---|
| 1116 | { |
---|
| 1117 | if ($6.constant) |
---|
| 1118 | {trctx.out->printf("move s0,m1\ncall [m1].\"set\",%s,%s\ninc m0\n",litstr($3),litstr($6));$$=$6;trstack.adjust(+1);} |
---|
| 1119 | else |
---|
| 1120 | {trctx.out->printf("move s1,m1\npush s0\nmove %s,s1\ncall [m1].\"set\"\nadd 2,m0\nmove s-2,s0\n",litstr($3));trstack.adjust(+1);} |
---|
| 1121 | } |
---|
| 1122 | else |
---|
| 1123 | { |
---|
| 1124 | if ($6.constant) |
---|
| 1125 | {trctx.out->printf("move s1,m1\npush %s\ncall [m1].\"set\"\nadd 3,m0\n",litstr($6)); trstack.adjust(+2);} |
---|
| 1126 | else |
---|
| 1127 | {trctx.out->printf("move s2,m1\ncall [m1].\"set\"\nadd 2,m0\nmove s-2,s0\n"); trstack.adjust(+2);} |
---|
| 1128 | } |
---|
| 1129 | } |
---|
| 1130 | |
---|
[341] | 1131 | | stackexpr '[' expr ']' /* shortcut: expr.get(expr) */ |
---|
[332] | 1132 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+=".get"; $$.setString(t); |
---|
[163] | 1133 | if ($3.constant) |
---|
| 1134 | { |
---|
| 1135 | trctx.out->printf("move s0,m1\ncall [m1].\"get\",%s\n",litstr($3)); |
---|
| 1136 | } |
---|
| 1137 | else |
---|
| 1138 | { |
---|
| 1139 | trctx.out->printf("move s1,m1\ncall [m1].\"get\"\ninc m0\nmove s-1,s0\n"); |
---|
| 1140 | trstack.adjust(+1); |
---|
| 1141 | } |
---|
| 1142 | } |
---|
| 1143 | |
---|
[332] | 1144 | | IDENT '=' expr { trctx.emitLine(); $$=$3; $$.assign=1; $$.ident=0; |
---|
[163] | 1145 | int loc=trstack.getVariableLocation($1.getString()); |
---|
| 1146 | if (loc!=TranslatorStack::NOTFOUND) |
---|
| 1147 | { |
---|
| 1148 | if ($3.constant) |
---|
| 1149 | trctx.out->printf("move %s,s%d\n",litstr($3),loc-trstack.currentPos()); |
---|
| 1150 | else |
---|
| 1151 | trctx.out->printf("move s0,s%d\n",loc-trstack.currentPos()); |
---|
| 1152 | } |
---|
| 1153 | else if (globalOk($1)) { $$=$3; $$.ident=0; $$.assign=1; |
---|
| 1154 | if ($3.constant) trctx.out->printf("move %s,@%s\n",litstr($3),str($1)); |
---|
| 1155 | else trctx.out->printf("move s0,@%s\n",str($1));} |
---|
[245] | 1156 | else {trctx.err->printf("undefined variable: '%s'\n",str($1)); return 1;} |
---|
[163] | 1157 | } |
---|
| 1158 | |
---|
| 1159 | | OBJNAME '[' expr ']' /* shortcut: OBJNAME.get(expr) */ |
---|
[332] | 1160 | { trctx.emitLine(); $$.constant=0; $$.ident=0; SString t=$1.getString(); t+=".get"; $$.setString(t); |
---|
[163] | 1161 | if ($3.constant) |
---|
| 1162 | { |
---|
| 1163 | trctx.out->printf("dec m0\ncall %s.get,%s\n",str($1),litstr($3)); |
---|
| 1164 | trstack.adjust(-1); |
---|
| 1165 | } |
---|
| 1166 | else |
---|
| 1167 | { |
---|
| 1168 | trctx.out->printf("call %s.get\n",str($1)); |
---|
| 1169 | } |
---|
| 1170 | } |
---|
| 1171 | |
---|
| 1172 | | IDENT '(' arguments ')' |
---|
| 1173 | { |
---|
[332] | 1174 | trctx.emitLine(); $$.constant=0; $$.ident=0; $$.setString("function call"); |
---|
[163] | 1175 | if ($3.getInt()==0) |
---|
| 1176 | {trctx.out->printf("dec m0\n");trstack.adjust(-1);} |
---|
| 1177 | trctx.out->printf("call :%s\n",str($1)); |
---|
| 1178 | if ($3.getInt()>1) |
---|
| 1179 | { |
---|
| 1180 | trctx.out->printf("add %d,m0\nxmove s%d,s0\n",$3.getInt()-1,-($3.getInt()-1)); |
---|
| 1181 | trstack.adjust($3.getInt()-1); |
---|
| 1182 | } |
---|
| 1183 | } |
---|
| 1184 | |
---|
[332] | 1185 | | '[' {trctx.emitLine(); $$.ident=0; trctx.out->printf("add -2,m0\ncall Vector.new\nmove s0,s1\n");trstack.adjust(-2);} // s1=vector, s0=nieuzywane ale zarezerwowane zeby nie przesuwac stosu przy kazdym elemencie (trafia tu wartosc zwracana przez add/set) |
---|
[163] | 1186 | v_elements ']' |
---|
| 1187 | {$$.constant=0; trctx.out->printf("inc m0\n");trstack.adjust(1);} |
---|
| 1188 | |
---|
[332] | 1189 | | '{' {trctx.emitLine(); $$.ident=0; trctx.out->printf("add -2,m0\ncall Dictionary.new\nmove s0,s1\n");trstack.adjust(-2);} // s1=dict, s0=nieuzywane ale zarezerwowane zeby nie przesuwac stosu przy kazdym elemencie (trafia tu wartosc zwracana przez add/set) |
---|
[163] | 1190 | d_elements '}' |
---|
| 1191 | {$$.constant=0; trctx.out->printf("inc m0\n"); trstack.adjust(1);} |
---|
| 1192 | |
---|
| 1193 | // | '&' stackexpr {trctx.out->printf("call Ref.new\n");} |
---|
| 1194 | |
---|
| 1195 | | '&' IDENT { |
---|
[332] | 1196 | trctx.emitLine(); $$.ident=0; |
---|
[163] | 1197 | int loc=trstack.getVariableLocation($2.getString()); |
---|
| 1198 | if (loc!=TranslatorStack::NOTFOUND) |
---|
| 1199 | { |
---|
[247] | 1200 | trctx.out->printf("push &%d\n",loc-trstack.currentPos());trstack.adjust(-1); |
---|
[163] | 1201 | } |
---|
| 1202 | else if (globalOk($2)) |
---|
| 1203 | { |
---|
| 1204 | trctx.out->printf("gpush &@%s\ncall Ref.newO\ninc m0\nmove s-1,s0\n",str($2)); |
---|
| 1205 | trstack.adjust(-1); |
---|
| 1206 | } |
---|
[245] | 1207 | else {trctx.err->printf("undefined variable: '%s'\n",str($1)); return 1;} |
---|
[163] | 1208 | } |
---|
| 1209 | |
---|
| 1210 | | '&' OBJNAME '.' member { |
---|
[332] | 1211 | trctx.emitLine(); $$.ident=0; |
---|
[163] | 1212 | if ($4.constant) |
---|
| 1213 | { |
---|
| 1214 | trctx.out->printf("dec m0\ncall Ref.newO,%s.*,%s:%s\n",str($2),str($2),str($4)); |
---|
| 1215 | trstack.adjust(-1); |
---|
| 1216 | } |
---|
| 1217 | else |
---|
| 1218 | { |
---|
| 1219 | trctx.out->printf("call Ref.newO,%s.*,s0\n",str($2)); |
---|
| 1220 | } |
---|
| 1221 | } |
---|
| 1222 | |
---|
| 1223 | | '&' '(' stackexpr ')' '.' member { |
---|
[332] | 1224 | trctx.emitLine(); $$.ident=0; |
---|
[163] | 1225 | if ($6.constant) |
---|
| 1226 | { |
---|
| 1227 | trctx.out->printf("call Ref.newO,s0,%s\n",litstr($6)); |
---|
| 1228 | } |
---|
| 1229 | else |
---|
| 1230 | { |
---|
| 1231 | trctx.out->printf("call Ref.newO,s1,s0\ninc m0\nmove s-1,s0\n"); |
---|
| 1232 | trstack.adjust(1); |
---|
| 1233 | } |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | | '(' stackexpr ',' stackexpr ',' stackexpr ')' { |
---|
[332] | 1237 | trctx.emitLine(); $$.ident=0; |
---|
[163] | 1238 | trctx.out->printf("call XYZ.new\nadd 2,m0\nmove s-2,s0\n");trstack.adjust(2);} |
---|
| 1239 | ; |
---|
| 1240 | |
---|
| 1241 | v_elements: /* empty */ |
---|
| 1242 | | v_element |
---|
| 1243 | | v_elements ',' v_element |
---|
| 1244 | ; |
---|
| 1245 | |
---|
| 1246 | d_elements: /* empty */ |
---|
| 1247 | | d_element |
---|
| 1248 | | d_elements ',' d_element |
---|
| 1249 | ; |
---|
| 1250 | |
---|
| 1251 | v_element: expr |
---|
| 1252 | { |
---|
| 1253 | if ($1.constant) |
---|
| 1254 | trctx.out->printf("move s1,m1\ncall [m1].Vector:add,%s\n",litstr($1)); |
---|
| 1255 | else |
---|
| 1256 | {trctx.out->printf("move s2,m1\ncall [m1].Vector:add\ninc m0\n");trstack.adjust(1);} |
---|
| 1257 | } |
---|
| 1258 | ; |
---|
| 1259 | |
---|
| 1260 | d_element: expr ':' expr |
---|
| 1261 | { |
---|
| 1262 | if ($1.constant) |
---|
| 1263 | { |
---|
| 1264 | if ($3.constant) |
---|
| 1265 | trctx.out->printf("move s1,m1\ncall [m1].Dictionary:set,%s,%s\n",litstr($1),litstr($3)); |
---|
| 1266 | else |
---|
| 1267 | {trctx.out->printf("move s2,m1\nmove %s,s1\ncall [m1].Dictionary:set\ninc m0\n",litstr($1));trstack.adjust(1);} |
---|
| 1268 | } |
---|
| 1269 | else |
---|
| 1270 | { |
---|
| 1271 | if ($3.constant) |
---|
| 1272 | {trctx.out->printf("move s2,m1\nmove s0,s1\nmove %s,s0\ncall [m1].Dictionary:set\ninc m0\n",litstr($3));trstack.adjust(1);} |
---|
| 1273 | else |
---|
| 1274 | {trctx.out->printf("move s3,m1\ncall [m1].Dictionary:set\nadd 2,m0\n");trstack.adjust(2);} |
---|
| 1275 | } |
---|
| 1276 | } |
---|
| 1277 | ; |
---|
| 1278 | |
---|
| 1279 | member: IDENT { $$=$1; $$.constant=1;} |
---|
| 1280 | | OBJNAME ':' IDENT { SString t=$1.getString();t+=":";t+=$3.getString(); |
---|
| 1281 | $$.setString(t);$$.constant=1;} |
---|
| 1282 | | '[' stackexpr ']' { SString t="["; t+=$2.getString(); t+="]"; |
---|
| 1283 | $$.setString(t); $$.constant=0;} |
---|
| 1284 | |
---|
| 1285 | arguments: /* empty */ { $$.setInt(0); } |
---|
| 1286 | | stackexpr { $$.setInt(1); } |
---|
| 1287 | | arguments ',' stackexpr {$$.setInt($1.getInt()+1);} |
---|
| 1288 | ; |
---|
| 1289 | |
---|
| 1290 | %% |
---|
| 1291 | |
---|
| 1292 | SString makeLitString(const ExtValue& val) |
---|
| 1293 | { |
---|
| 1294 | if (val.type!=TString) |
---|
| 1295 | return val.getString(); |
---|
| 1296 | SString s=val.getString(); |
---|
| 1297 | int len=s.len(); |
---|
| 1298 | SString ret((len*11)/10+10); |
---|
| 1299 | ret+='\"'; |
---|
[348] | 1300 | const char*t=s.c_str(); |
---|
[163] | 1301 | while(len>0) |
---|
| 1302 | { |
---|
| 1303 | switch(*t) |
---|
| 1304 | { |
---|
| 1305 | case '\n': ret+="\\n"; break; |
---|
| 1306 | case '\r': ret+="\\r"; break; |
---|
| 1307 | case '\t': ret+="\\t"; break; |
---|
| 1308 | default: ret+=*t; |
---|
| 1309 | } |
---|
| 1310 | t++; len--; |
---|
| 1311 | } |
---|
| 1312 | ret+='\"'; |
---|
| 1313 | return ret; |
---|
| 1314 | } |
---|
| 1315 | |
---|
| 1316 | static void yyprint (FILE *file,int type,YYSTYPE value) |
---|
| 1317 | { |
---|
| 1318 | fprintf(file,"(%s)%s",str(value),value.constant?"c":""); |
---|
| 1319 | } |
---|
| 1320 | |
---|
| 1321 | int yyerror (const char *s) /* Called by yyparse on error */ |
---|
| 1322 | { |
---|
| 1323 | trctx.err->printf ("%s\n",s); |
---|
| 1324 | return 0; // w przykladach do bisona tez nie dali returna... |
---|
| 1325 | } |
---|
| 1326 | |
---|
| 1327 | void handleTwoArg(YYSTYPE& result,const YYSTYPE& arg1,const YYSTYPE& arg2, |
---|
[332] | 1328 | int optoken,const char* opname,bool negarg2,bool uniq) |
---|
[163] | 1329 | { |
---|
[332] | 1330 | trctx.emitLine(); |
---|
[163] | 1331 | result.ident=false; |
---|
| 1332 | if (arg1.constant && arg2.constant) |
---|
| 1333 | { |
---|
| 1334 | result=arg1; |
---|
| 1335 | switch(optoken) |
---|
| 1336 | { |
---|
| 1337 | case '+': result+=arg2; break; |
---|
| 1338 | case '-': result-=arg2; break; |
---|
| 1339 | case '*': result*=arg2; break; |
---|
| 1340 | case '/': result/=arg2; break; |
---|
| 1341 | case '%': result%=arg2; break; |
---|
| 1342 | case '&': result.setInt(arg1.getInt() & arg2.getInt()); break; |
---|
| 1343 | case '|': result.setInt(arg1.getInt() | arg2.getInt()); break; |
---|
| 1344 | case LSHIFT: result.setInt(arg1.getInt() << arg2.getInt()); break; |
---|
| 1345 | case RSHIFT: result.setInt(arg1.getInt() >> arg2.getInt()); break; |
---|
| 1346 | } |
---|
| 1347 | } |
---|
| 1348 | else |
---|
| 1349 | { |
---|
| 1350 | //TODO: prawie kazde uzycie uniq jest niepotrzebne bo typem rzadko bedzie vector, ale w wiekszosci miejsc okreslenie typu wartosci |
---|
| 1351 | // byloby bardzo trudne lub niemozliwe. mozna byloby natomiast zapamietywac przy parsowaniu czy dana wartosc na stosie jest |
---|
| 1352 | // skopiowana ze zmiennej/pola czy jest wynikiem wczesniejszej operacji co pozwoliloby na likwidacje bardzo wielu uniq |
---|
| 1353 | result.constant=0; |
---|
| 1354 | result.assign=arg1.assign || arg2.assign; |
---|
| 1355 | result.parens=0; |
---|
| 1356 | result.setString(opname); |
---|
| 1357 | if (arg1.constant) |
---|
[332] | 1358 | trctx.out->printf("move %s,m1\n%s%s s0,m1\nmove m1,s0\n",litstr(arg1), |
---|
[163] | 1359 | negarg2?"neg s0\n":"", |
---|
| 1360 | opname); |
---|
| 1361 | else if (arg2.constant) |
---|
| 1362 | { |
---|
[332] | 1363 | if (negarg2) |
---|
| 1364 | trctx.out->printf("%s %d,s0\n",opname,-arg2.getInt()); |
---|
[163] | 1365 | else |
---|
[332] | 1366 | trctx.out->printf("%s%s %s,s0\n",(uniq?"uniq s0\n":""),opname,litstr(arg2)); |
---|
[163] | 1367 | } |
---|
| 1368 | else |
---|
| 1369 | { |
---|
| 1370 | trctx.out->printf("%s%s%s s0,s1\ninc m0\n", |
---|
[332] | 1371 | uniq?"uniq s1\n":"", |
---|
[163] | 1372 | negarg2?"neg s0\n":"", |
---|
| 1373 | opname); |
---|
| 1374 | trstack.adjust(+1); |
---|
| 1375 | } |
---|
| 1376 | } |
---|
| 1377 | } |
---|
| 1378 | |
---|
| 1379 | bool handleAssignOp(YYSTYPE& result,const YYSTYPE& var,const YYSTYPE& arg,const char* opname) |
---|
| 1380 | { |
---|
| 1381 | int loc; if (variableOk(result,var,loc)) |
---|
| 1382 | { |
---|
| 1383 | loc-=trstack.currentPos(); |
---|
| 1384 | if (arg.constant) |
---|
| 1385 | { |
---|
| 1386 | trctx.out->printf("%s %s,s%d\npush s%d\n",opname,litstr(arg),loc,loc); |
---|
| 1387 | trstack.adjust(-1); |
---|
| 1388 | } |
---|
| 1389 | else |
---|
| 1390 | trctx.out->printf("%s s0,s%d\nmove s%d,s0\n",opname,loc,loc); |
---|
| 1391 | return 1; |
---|
| 1392 | } |
---|
| 1393 | return 0; |
---|
| 1394 | } |
---|
| 1395 | |
---|
| 1396 | bool handleAssignOp2(YYSTYPE& result,const char *var,const YYSTYPE& arg,const char* opname,int stackpos,bool push) |
---|
| 1397 | { |
---|
| 1398 | if (arg.constant) |
---|
| 1399 | { |
---|
| 1400 | trctx.out->printf("%s %s,%s\n",opname,litstr(arg),var); |
---|
| 1401 | if (!push) |
---|
| 1402 | trctx.out->printf("move %s,s%d\n",var,stackpos); |
---|
| 1403 | else |
---|
| 1404 | { |
---|
| 1405 | trctx.out->printf("push %s\n",var); |
---|
| 1406 | trstack.adjust(-1); |
---|
| 1407 | } |
---|
| 1408 | } |
---|
| 1409 | else |
---|
| 1410 | trctx.out->printf("%s s0,%s\nmove %s,s%d\n",opname,var,var,stackpos); |
---|
| 1411 | return 1; |
---|
| 1412 | } |
---|
| 1413 | |
---|
[332] | 1414 | bool handleCompare(YYSTYPE& result,const YYSTYPE& arg1,const YYSTYPE& arg2,ExtValue::CmpOperator op,const char* opname) |
---|
| 1415 | { |
---|
| 1416 | trctx.emitLine(); |
---|
[163] | 1417 | result.ident=0; |
---|
| 1418 | if (arg1.constant && arg2.constant) |
---|
| 1419 | { |
---|
| 1420 | result.constant=1; |
---|
[333] | 1421 | ExtValue::CompareResult cmp=arg1.compare(arg2); |
---|
[332] | 1422 | ExtValue::CmpContext context; |
---|
| 1423 | context.v1=&arg1; |
---|
| 1424 | context.v2=&arg2; |
---|
| 1425 | int ret=ExtValue::interpretCompare(op,cmp,&context); |
---|
| 1426 | if (ret<0) |
---|
| 1427 | result.setEmpty();//return false; |
---|
| 1428 | else |
---|
| 1429 | result.setInt(ret); |
---|
| 1430 | return true; |
---|
[163] | 1431 | } |
---|
| 1432 | else |
---|
| 1433 | { |
---|
| 1434 | result.constant=0; |
---|
| 1435 | result.assign=arg1.assign || arg2.assign; |
---|
| 1436 | result.parens=0; |
---|
| 1437 | result.setString(opname); |
---|
| 1438 | if (arg1.constant) |
---|
| 1439 | trctx.out->printf("setif %s,%s,s0,s0\n",litstr(arg1),opname); |
---|
| 1440 | else if (arg2.constant) |
---|
| 1441 | trctx.out->printf("setif s0,%s,%s,s0\n",opname,litstr(arg2)); |
---|
| 1442 | else |
---|
| 1443 | { |
---|
| 1444 | trctx.out->printf("setif s1,%s,s0,s1\ninc m0\n",opname); |
---|
| 1445 | trstack.adjust(+1); |
---|
| 1446 | } |
---|
[332] | 1447 | return true; |
---|
[163] | 1448 | } |
---|
| 1449 | } |
---|
| 1450 | |
---|
[333] | 1451 | static bool resultIsRelaxedEqual(ExtValue::CompareResult res) |
---|
| 1452 | { |
---|
| 1453 | return (res==ExtValue::ResultEqual)||(res==ExtValue::ResultEqualUnordered)||(res==ExtValue::ResultUnequal_RelaxedEqual); |
---|
| 1454 | } |
---|
| 1455 | |
---|
[163] | 1456 | bool variableOk(TokenValue &tok, const TokenValue& var,int &loc) |
---|
| 1457 | { |
---|
| 1458 | loc=trstack.getVariableLocation(var.getString()); |
---|
| 1459 | if (loc != TranslatorStack::NOTFOUND) |
---|
[466] | 1460 | { |
---|
| 1461 | if ((trctx.functionstackpos!=TranslatorContext::NOT_IN_FUNCTION) |
---|
| 1462 | && (loc>=trctx.beforefunctionstackpos)) |
---|
| 1463 | return 0; |
---|
| 1464 | tok.setInt(loc); tok.constant=0; |
---|
| 1465 | return 1; |
---|
| 1466 | } |
---|
[163] | 1467 | return 0; |
---|
| 1468 | } |
---|
| 1469 | |
---|
| 1470 | bool globalOk(const TokenValue& var) |
---|
| 1471 | { |
---|
| 1472 | SymTabEntry* found=trstack.globals.find(var.getString()); |
---|
| 1473 | if (found) return true; |
---|
[348] | 1474 | return framscriptIsGlobalName(var.getString().c_str()); |
---|
[163] | 1475 | } |
---|
| 1476 | |
---|
| 1477 | void badVariable(TokenValue &tok, const TokenValue& var) |
---|
| 1478 | { |
---|
| 1479 | tok=var; tok.constant=1; |
---|
| 1480 | trctx.err->printf("undefined variable '%s'\n",str(var)); |
---|
| 1481 | } |
---|
| 1482 | |
---|
| 1483 | bool doBreak(int level) |
---|
| 1484 | { |
---|
| 1485 | if (trstack.loops.size()<level) |
---|
[245] | 1486 | {trctx.err->printf("invalid 'break'\n"); return 0;} |
---|
[163] | 1487 | LoopInfo* li=trstack.loops.getLoop(level-1); |
---|
| 1488 | if (li->location != trstack.currentPos()) |
---|
| 1489 | trctx.out->printf("add %d,m0\n",li->location-trstack.currentPos()); |
---|
| 1490 | trctx.out->printf("jump :_loop_end_%d\n",li->id); |
---|
| 1491 | return 1; |
---|
| 1492 | } |
---|
| 1493 | |
---|
| 1494 | bool doContinue(int level) |
---|
| 1495 | { |
---|
| 1496 | if (trstack.loops.size()<level) |
---|
[245] | 1497 | {trctx.err->printf("invalid 'continue'\n"); return 0;} |
---|
[163] | 1498 | LoopInfo* li=trstack.loops.getLoop(level-1); |
---|
| 1499 | if (li->location != trstack.currentPos()) |
---|
| 1500 | trctx.out->printf("add %d,m0\n",li->location-trstack.currentPos()); |
---|
| 1501 | trctx.out->printf("jump :_loop_%d\n",li->id); |
---|
| 1502 | return 1; |
---|
| 1503 | } |
---|
| 1504 | |
---|
| 1505 | int lookupToken(char *s) |
---|
| 1506 | { |
---|
| 1507 | int len=strlen(s); |
---|
| 1508 | int i; |
---|
| 1509 | const char *t; |
---|
| 1510 | for (i = 0; i < YYNTOKENS; i++) |
---|
| 1511 | { |
---|
| 1512 | t=yytname[i]; |
---|
| 1513 | if (t && (t[0]=='"') |
---|
| 1514 | && (!strncmp(t+1,s,len)) |
---|
| 1515 | && (t[len+1]=='"') |
---|
| 1516 | && (t[len+2] == 0)) |
---|
| 1517 | return yytoknum[i]; |
---|
| 1518 | } |
---|
| 1519 | return -1; |
---|
| 1520 | } |
---|
| 1521 | |
---|
| 1522 | void warnTruthValue(const TokenValue& t) |
---|
| 1523 | { |
---|
| 1524 | if (t.assign && (!t.parens)) |
---|
[375] | 1525 | logPrintf("FramScriptCompiler","translate",LOG_WARN,"Assignment used as truth value, use ((double parens)) if you really mean it"); |
---|
[163] | 1526 | } |
---|
| 1527 | |
---|
| 1528 | void outFunName(const TokenValue& t) |
---|
| 1529 | { |
---|
| 1530 | if (trctx.functiontmplabel<0) |
---|
| 1531 | { |
---|
| 1532 | trctx.functiontmplabel=trctx.labelcounter++; |
---|
| 1533 | trctx.out->printf("jump :_skipfun_%d\n",trctx.functiontmplabel); |
---|
| 1534 | } |
---|
| 1535 | trctx.out->printf(":%s\n",str(t)); |
---|
| 1536 | } |
---|
| 1537 | |
---|
| 1538 | bool evalVariable(TokenValue &tok,const TokenValue &var) |
---|
| 1539 | { |
---|
| 1540 | int loc; |
---|
| 1541 | if (variableOk(tok,var,loc)) |
---|
| 1542 | { |
---|
| 1543 | trctx.out->printf("push s%d\n",loc-trstack.currentPos()); |
---|
| 1544 | trstack.adjust(-1); |
---|
| 1545 | return true; |
---|
| 1546 | } |
---|
| 1547 | else if (globalOk(var)) |
---|
| 1548 | { |
---|
[348] | 1549 | trctx.out->printf("push @%s\n",var.getString().c_str()); |
---|
[163] | 1550 | trstack.adjust(-1); |
---|
| 1551 | return true; |
---|
| 1552 | } |
---|
| 1553 | else |
---|
| 1554 | { |
---|
| 1555 | badVariable(tok,var); return false; |
---|
| 1556 | } |
---|
| 1557 | } |
---|