diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-03-10 11:09:45 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-03-10 11:09:45 -0300 |
commit | 26794616374fee54532d0030ae006abb77dfb7ba (patch) | |
tree | ebc4595695eb616e66b4f63779404e8ea6644b48 /lparser.c | |
parent | 0870a2d1d8a1434eecae1923886ba219c4e699c7 (diff) | |
download | lua-26794616374fee54532d0030ae006abb77dfb7ba.tar.gz lua-26794616374fee54532d0030ae006abb77dfb7ba.tar.bz2 lua-26794616374fee54532d0030ae006abb77dfb7ba.zip |
no more assignment expressions (they don't fit in Lua...)
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 51 |
1 files changed, 16 insertions, 35 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.26 1999/03/04 21:17:26 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.27 1999/03/05 21:16:07 roberto Exp roberto $ |
3 | ** LL(1) Parser and code generator for Lua | 3 | ** LL(1) Parser and code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -99,7 +99,7 @@ typedef struct FuncState { | |||
99 | /* | 99 | /* |
100 | ** prototypes for non-terminal functions | 100 | ** prototypes for non-terminal functions |
101 | */ | 101 | */ |
102 | static int assignment (LexState *ls, vardesc *v, int nvars, OpCode *codes); | 102 | static int assignment (LexState *ls, vardesc *v, int nvars); |
103 | static int cond (LexState *ls); | 103 | static int cond (LexState *ls); |
104 | static int funcname (LexState *ls, vardesc *v); | 104 | static int funcname (LexState *ls, vardesc *v); |
105 | static int funcparams (LexState *ls, int slf); | 105 | static int funcparams (LexState *ls, int slf); |
@@ -114,7 +114,6 @@ static void chunk (LexState *ls); | |||
114 | static void constructor (LexState *ls); | 114 | static void constructor (LexState *ls); |
115 | static void decinit (LexState *ls, listdesc *d); | 115 | static void decinit (LexState *ls, listdesc *d); |
116 | static void exp0 (LexState *ls, vardesc *v); | 116 | static void exp0 (LexState *ls, vardesc *v); |
117 | static void Gexp (LexState *ls, vardesc *v); | ||
118 | static void exp1 (LexState *ls); | 117 | static void exp1 (LexState *ls); |
119 | static void exp2 (LexState *ls, vardesc *v); | 118 | static void exp2 (LexState *ls, vardesc *v); |
120 | static void explist (LexState *ls, listdesc *e); | 119 | static void explist (LexState *ls, listdesc *e); |
@@ -467,22 +466,16 @@ static void lua_pushvar (LexState *ls, vardesc *var) { | |||
467 | } | 466 | } |
468 | 467 | ||
469 | 468 | ||
470 | /* to be used by "storevar" and assignment */ | 469 | static void storevar (LexState *ls, vardesc *var) { |
471 | static OpCode set_pop[] = {SETLOCAL, SETGLOBAL, SETTABLEPOP, SETTABLE}; | ||
472 | static OpCode set_dup[] = {SETLOCALDUP, SETGLOBALDUP, SETTABLEPOPDUP, | ||
473 | SETTABLEDUP}; | ||
474 | |||
475 | |||
476 | static void storevar (LexState *ls, vardesc *var, OpCode *codes) { | ||
477 | switch (var->k) { | 470 | switch (var->k) { |
478 | case VLOCAL: | 471 | case VLOCAL: |
479 | code_oparg(ls, codes[0], var->info, -1); | 472 | code_oparg(ls, SETLOCAL, var->info, -1); |
480 | break; | 473 | break; |
481 | case VGLOBAL: | 474 | case VGLOBAL: |
482 | code_oparg(ls, codes[1], var->info, -1); | 475 | code_oparg(ls, SETGLOBAL, var->info, -1); |
483 | break; | 476 | break; |
484 | case VINDEXED: | 477 | case VINDEXED: |
485 | code_opcode(ls, codes[2], -3); | 478 | code_opcode(ls, SETTABLEPOP, -3); |
486 | break; | 479 | break; |
487 | default: | 480 | default: |
488 | LUA_INTERNALERROR("invalid var kind to store"); | 481 | LUA_INTERNALERROR("invalid var kind to store"); |
@@ -739,7 +732,7 @@ static int stat (LexState *ls) { | |||
739 | next(ls); | 732 | next(ls); |
740 | needself = funcname(ls, &v); | 733 | needself = funcname(ls, &v); |
741 | body(ls, needself, line); | 734 | body(ls, needself, line); |
742 | storevar(ls, &v, set_pop); | 735 | storevar(ls, &v); |
743 | return 1; | 736 | return 1; |
744 | } | 737 | } |
745 | 738 | ||
@@ -765,7 +758,7 @@ static int stat (LexState *ls) { | |||
765 | close_exp(ls, v.info, 0); | 758 | close_exp(ls, v.info, 0); |
766 | } | 759 | } |
767 | else { /* stat -> ['%'] NAME assignment */ | 760 | else { /* stat -> ['%'] NAME assignment */ |
768 | int left = assignment(ls, &v, 1, set_pop); | 761 | int left = assignment(ls, &v, 1); |
769 | adjuststack(ls, left); /* remove eventual 'garbage' left on stack */ | 762 | adjuststack(ls, left); /* remove eventual 'garbage' left on stack */ |
770 | } | 763 | } |
771 | return 1; | 764 | return 1; |
@@ -948,19 +941,6 @@ static void exp0 (LexState *ls, vardesc *v) { | |||
948 | } | 941 | } |
949 | 942 | ||
950 | 943 | ||
951 | static void Gexp (LexState *ls, vardesc *v) { | ||
952 | /* Gexp -> exp0 | assignment */ | ||
953 | exp0(ls, v); | ||
954 | if (v->k != VEXP && (ls->token == '=' || ls->token == ',')) { | ||
955 | int left = assignment(ls, v, 1, set_dup); | ||
956 | deltastack(ls, 1); /* DUP operations push an extra value */ | ||
957 | if (left > 0) | ||
958 | code_oparg(ls, POPDUP, left, -left); | ||
959 | v->k = VEXP; v->info = 0; /* this expression is closed now */ | ||
960 | } | ||
961 | } | ||
962 | |||
963 | |||
964 | static void push (LexState *ls, stack_op *s, int op) { | 944 | static void push (LexState *ls, stack_op *s, int op) { |
965 | if (s->top >= MAXOPS) | 945 | if (s->top >= MAXOPS) |
966 | luaX_error(ls, "expression too complex"); | 946 | luaX_error(ls, "expression too complex"); |
@@ -1015,9 +995,9 @@ static void simpleexp (LexState *ls, vardesc *v, stack_op *s) { | |||
1015 | ifpart(ls, 1, ls->linenumber); | 995 | ifpart(ls, 1, ls->linenumber); |
1016 | break; | 996 | break; |
1017 | 997 | ||
1018 | case '(': /* simpleexp -> '(' Gexp ')' */ | 998 | case '(': /* simpleexp -> '(' exp0 ')' */ |
1019 | next(ls); | 999 | next(ls); |
1020 | Gexp(ls, v); | 1000 | exp0(ls, v); |
1021 | check(ls, ')'); | 1001 | check(ls, ')'); |
1022 | return; | 1002 | return; |
1023 | 1003 | ||
@@ -1239,7 +1219,7 @@ static void decinit (LexState *ls, listdesc *d) { | |||
1239 | } | 1219 | } |
1240 | 1220 | ||
1241 | 1221 | ||
1242 | static int assignment (LexState *ls, vardesc *v, int nvars, OpCode *codes) { | 1222 | static int assignment (LexState *ls, vardesc *v, int nvars) { |
1243 | int left = 0; | 1223 | int left = 0; |
1244 | unloaddot(ls, v); | 1224 | unloaddot(ls, v); |
1245 | if (ls->token == ',') { /* assignment -> ',' NAME assignment */ | 1225 | if (ls->token == ',') { /* assignment -> ',' NAME assignment */ |
@@ -1248,7 +1228,7 @@ static int assignment (LexState *ls, vardesc *v, int nvars, OpCode *codes) { | |||
1248 | var_or_func(ls, &nv); | 1228 | var_or_func(ls, &nv); |
1249 | if (nv.k == VEXP) | 1229 | if (nv.k == VEXP) |
1250 | luaX_error(ls, "syntax error"); | 1230 | luaX_error(ls, "syntax error"); |
1251 | left = assignment(ls, &nv, nvars+1, set_pop); | 1231 | left = assignment(ls, &nv, nvars+1); |
1252 | } | 1232 | } |
1253 | else { /* assignment -> '=' explist1 */ | 1233 | else { /* assignment -> '=' explist1 */ |
1254 | listdesc d; | 1234 | listdesc d; |
@@ -1258,15 +1238,16 @@ static int assignment (LexState *ls, vardesc *v, int nvars, OpCode *codes) { | |||
1258 | } | 1238 | } |
1259 | if (v->k != VINDEXED || left+(nvars-1) == 0) { | 1239 | if (v->k != VINDEXED || left+(nvars-1) == 0) { |
1260 | /* global/local var or indexed var without values in between */ | 1240 | /* global/local var or indexed var without values in between */ |
1261 | storevar(ls, v, codes); | 1241 | storevar(ls, v); |
1262 | } | 1242 | } |
1263 | else { /* indexed var with values in between*/ | 1243 | else { /* indexed var with values in between*/ |
1264 | code_oparg(ls, codes[3], left+(nvars-1), -1); | 1244 | code_oparg(ls, SETTABLE, left+(nvars-1), -1); |
1265 | left += 2; /* table/index are not popped, because they aren't on top */ | 1245 | left += 2; /* table&index are not popped, because they aren't on top */ |
1266 | } | 1246 | } |
1267 | return left; | 1247 | return left; |
1268 | } | 1248 | } |
1269 | 1249 | ||
1250 | |||
1270 | static void constructor (LexState *ls) { | 1251 | static void constructor (LexState *ls) { |
1271 | /* constructor -> '{' part [';' part] '}' */ | 1252 | /* constructor -> '{' part [';' part] '}' */ |
1272 | int line = ls->linenumber; | 1253 | int line = ls->linenumber; |