diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-09 11:49:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-09 11:49:13 -0300 |
commit | 5f22f8961c80ce36297f086ae31d87bbb6897c97 (patch) | |
tree | 61837abde7ea51dfe61140734af716437253f605 | |
parent | a7c1390ffaf0edfe839d075df484f2b81b3151bf (diff) | |
download | lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.tar.gz lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.tar.bz2 lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.zip |
better code for unary/binary operators
-rw-r--r-- | lcode.c | 84 | ||||
-rw-r--r-- | lcode.h | 22 | ||||
-rw-r--r-- | lparser.c | 79 |
3 files changed, 114 insertions, 71 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 1.43 2000/08/08 18:26:05 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.44 2000/08/08 20:42:07 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -348,9 +348,9 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { | |||
348 | } | 348 | } |
349 | 349 | ||
350 | 350 | ||
351 | void luaK_prefix (LexState *ls, int op, expdesc *v) { | 351 | void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) { |
352 | FuncState *fs = ls->fs; | 352 | FuncState *fs = ls->fs; |
353 | if (op == '-') { | 353 | if (op == OPR_MINUS) { |
354 | luaK_tostack(ls, v, 1); | 354 | luaK_tostack(ls, v, 1); |
355 | luaK_code0(fs, OP_MINUS); | 355 | luaK_code0(fs, OP_MINUS); |
356 | } | 356 | } |
@@ -368,46 +368,54 @@ void luaK_prefix (LexState *ls, int op, expdesc *v) { | |||
368 | } | 368 | } |
369 | 369 | ||
370 | 370 | ||
371 | void luaK_infix (LexState *ls, int op, expdesc *v) { | 371 | void luaK_infix (LexState *ls, BinOpr op, expdesc *v) { |
372 | FuncState *fs = ls->fs; | 372 | FuncState *fs = ls->fs; |
373 | if (op == TK_AND) | 373 | switch (op) { |
374 | luaK_goiftrue(fs, v, 1); | 374 | case OPR_AND: |
375 | else if (op == TK_OR) | 375 | luaK_goiftrue(fs, v, 1); |
376 | luaK_goiffalse(fs, v, 1); | 376 | break; |
377 | else | 377 | case OPR_OR: |
378 | luaK_tostack(ls, v, 1); /* all other binary operators need a value */ | 378 | luaK_goiffalse(fs, v, 1); |
379 | break; | ||
380 | default: | ||
381 | luaK_tostack(ls, v, 1); /* all other binary operators need a value */ | ||
382 | } | ||
379 | } | 383 | } |
380 | 384 | ||
381 | 385 | ||
382 | void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) { | 386 | |
387 | static const struct { | ||
388 | OpCode opcode; /* opcode for each binary operator */ | ||
389 | int arg; /* default argument for the opcode */ | ||
390 | } codes[] = { /* ORDER OPR */ | ||
391 | {OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0}, | ||
392 | {OP_POW, 0}, {OP_CONCAT, 2}, | ||
393 | {OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP}, | ||
394 | {OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP}, | ||
395 | {OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP} | ||
396 | }; | ||
397 | |||
398 | |||
399 | void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) { | ||
383 | FuncState *fs = ls->fs; | 400 | FuncState *fs = ls->fs; |
384 | if (op == TK_AND) { | 401 | switch (op) { |
385 | LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed"); | 402 | case OPR_AND: { |
386 | discharge1(fs, v2); | 403 | LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed"); |
387 | v1->u.l.t = v2->u.l.t; | 404 | discharge1(fs, v2); |
388 | luaK_concat(fs, &v1->u.l.f, v2->u.l.f); | 405 | v1->u.l.t = v2->u.l.t; |
389 | } | 406 | luaK_concat(fs, &v1->u.l.f, v2->u.l.f); |
390 | else if (op == TK_OR) { | 407 | break; |
391 | LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed"); | 408 | } |
392 | discharge1(fs, v2); | 409 | case OPR_OR: { |
393 | v1->u.l.f = v2->u.l.f; | 410 | LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed"); |
394 | luaK_concat(fs, &v1->u.l.t, v2->u.l.t); | 411 | discharge1(fs, v2); |
395 | } | 412 | v1->u.l.f = v2->u.l.f; |
396 | else { | 413 | luaK_concat(fs, &v1->u.l.t, v2->u.l.t); |
397 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ | 414 | break; |
398 | switch (op) { | 415 | } |
399 | case '+': luaK_code0(fs, OP_ADD); break; | 416 | default: { |
400 | case '-': luaK_code0(fs, OP_SUB); break; | 417 | luaK_tostack(ls, v2, 1); /* `v2' must be a value */ |
401 | case '*': luaK_code0(fs, OP_MULT); break; | 418 | luaK_code1(fs, codes[op].opcode, codes[op].arg); |
402 | case '/': luaK_code0(fs, OP_DIV); break; | ||
403 | case '^': luaK_code0(fs, OP_POW); break; | ||
404 | case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break; | ||
405 | case TK_EQ: luaK_code1(fs, OP_JMPEQ, NO_JUMP); break; | ||
406 | case TK_NE: luaK_code1(fs, OP_JMPNE, NO_JUMP); break; | ||
407 | case '>': luaK_code1(fs, OP_JMPGT, NO_JUMP); break; | ||
408 | case '<': luaK_code1(fs, OP_JMPLT, NO_JUMP); break; | ||
409 | case TK_GE: luaK_code1(fs, OP_JMPGE, NO_JUMP); break; | ||
410 | case TK_LE: luaK_code1(fs, OP_JMPLE, NO_JUMP); break; | ||
411 | } | 419 | } |
412 | } | 420 | } |
413 | } | 421 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.h,v 1.14 2000/06/16 17:51:40 roberto Exp roberto $ | 2 | ** $Id: lcode.h,v 1.15 2000/06/28 20:20:36 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -20,6 +20,20 @@ | |||
20 | #define NO_JUMP (-1) | 20 | #define NO_JUMP (-1) |
21 | 21 | ||
22 | 22 | ||
23 | /* | ||
24 | ** grep "ORDER OPR" if you change these enums | ||
25 | */ | ||
26 | typedef enum BinOpr { | ||
27 | OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW, | ||
28 | OPR_CONCAT, | ||
29 | OPR_NE, OPR_EQ, OPR_LT, OPR_LE, OPR_GT, OPR_GE, | ||
30 | OPR_AND, OPR_OR, | ||
31 | OPR_NOBINOPR | ||
32 | } BinOpr; | ||
33 | |||
34 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr; | ||
35 | |||
36 | |||
23 | enum Mode {iO, iU, iS, iAB}; /* instruction format */ | 37 | enum Mode {iO, iU, iS, iAB}; /* instruction format */ |
24 | 38 | ||
25 | #define VD 100 /* flag for variable delta */ | 39 | #define VD 100 /* flag for variable delta */ |
@@ -48,9 +62,9 @@ int luaK_lastisopen (FuncState *fs); | |||
48 | void luaK_setcallreturns (FuncState *fs, int nresults); | 62 | void luaK_setcallreturns (FuncState *fs, int nresults); |
49 | void luaK_tostack (LexState *ls, expdesc *v, int onlyone); | 63 | void luaK_tostack (LexState *ls, expdesc *v, int onlyone); |
50 | void luaK_storevar (LexState *ls, const expdesc *var); | 64 | void luaK_storevar (LexState *ls, const expdesc *var); |
51 | void luaK_prefix (LexState *ls, int op, expdesc *v); | 65 | void luaK_prefix (LexState *ls, UnOpr op, expdesc *v); |
52 | void luaK_infix (LexState *ls, int op, expdesc *v); | 66 | void luaK_infix (LexState *ls, BinOpr op, expdesc *v); |
53 | void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2); | 67 | void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2); |
54 | 68 | ||
55 | 69 | ||
56 | #endif | 70 | #endif |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.104 2000/08/08 20:42:07 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.105 2000/08/08 20:48:55 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 | */ |
@@ -670,55 +670,76 @@ static void exp1 (LexState *ls) { | |||
670 | } | 670 | } |
671 | 671 | ||
672 | 672 | ||
673 | /* | 673 | static UnOpr getunopr (int op) { |
674 | ** gets priorities of an operator. Returns the priority to the left, and | ||
675 | ** sets `rp' to the priority to the right. | ||
676 | */ | ||
677 | static int get_priority (int op, int *rp) { | ||
678 | switch (op) { | 674 | switch (op) { |
675 | case TK_NOT: return OPR_NOT; | ||
676 | case '-': return OPR_MINUS; | ||
677 | default: return OPR_NOUNOPR; | ||
678 | } | ||
679 | } | ||
679 | 680 | ||
680 | case '^': *rp = 8; return 9; /* right associative */ | ||
681 | |||
682 | #define UNARY_PRIORITY 7 | ||
683 | |||
684 | case '*': case '/': *rp = 6; return 6; | ||
685 | |||
686 | case '+': case '-': *rp = 5; return 5; | ||
687 | 681 | ||
688 | case TK_CONCAT: *rp = 3; return 4; /* right associative (?) */ | 682 | static BinOpr getbinopr (int op) { |
683 | switch (op) { | ||
684 | case '+': return OPR_ADD; | ||
685 | case '-': return OPR_SUB; | ||
686 | case '*': return OPR_MULT; | ||
687 | case '/': return OPR_DIV; | ||
688 | case '^': return OPR_POW; | ||
689 | case TK_CONCAT: return OPR_CONCAT; | ||
690 | case TK_NE: return OPR_NE; | ||
691 | case TK_EQ: return OPR_EQ; | ||
692 | case '<': return OPR_LT; | ||
693 | case TK_LE: return OPR_LE; | ||
694 | case '>': return OPR_GT; | ||
695 | case TK_GE: return OPR_GE; | ||
696 | case TK_AND: return OPR_AND; | ||
697 | case TK_OR: return OPR_OR; | ||
698 | default: return OPR_NOBINOPR; | ||
699 | } | ||
700 | } | ||
689 | 701 | ||
690 | case TK_EQ: case TK_NE: case '>': case '<': case TK_LE: case TK_GE: | ||
691 | *rp = 2; return 2; | ||
692 | 702 | ||
693 | case TK_AND: case TK_OR: *rp = 1; return 1; | 703 | static const struct { |
704 | char left; /* left priority for each binary operator */ | ||
705 | char right; /* right priority */ | ||
706 | } priority[] = { /* ORDER OPR */ | ||
707 | {5, 5}, {5, 5}, {6, 6}, {6, 6}, /* arithmetic */ | ||
708 | {9, 8}, {4, 3}, /* power and concat (right associative) */ | ||
709 | {2, 2}, {2, 2}, /* equality */ | ||
710 | {2, 2}, {2, 2}, {2, 2}, {2, 2}, /* order */ | ||
711 | {1, 1}, {1, 1} /* logical */ | ||
712 | }; | ||
694 | 713 | ||
695 | default: *rp = -1; return -1; | 714 | #define UNARY_PRIORITY 7 /* priority for unary operators */ |
696 | } | ||
697 | } | ||
698 | 715 | ||
699 | 716 | ||
700 | /* | 717 | /* |
701 | ** subexpr -> (simplexep | (NOT | '-') subexpr) { binop subexpr } | 718 | ** subexpr -> (simplexep | unop subexpr) { binop subexpr } |
702 | ** where `binop' is any binary operator with a priority higher than `limit' | 719 | ** where `binop' is any binary operator with a priority higher than `limit' |
703 | */ | 720 | */ |
704 | static void subexpr (LexState *ls, expdesc *v, int limit) { | 721 | static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { |
705 | int rp; | 722 | BinOpr op; |
706 | if (ls->t.token == '-' || ls->t.token == TK_NOT) { | 723 | UnOpr uop = getunopr(ls->t.token); |
707 | int op = ls->t.token; /* operator */ | 724 | if (uop != OPR_NOUNOPR) { |
708 | next(ls); | 725 | next(ls); |
709 | subexpr(ls, v, UNARY_PRIORITY); | 726 | subexpr(ls, v, UNARY_PRIORITY); |
710 | luaK_prefix(ls, op, v); | 727 | luaK_prefix(ls, uop, v); |
711 | } | 728 | } |
712 | else simpleexp(ls, v); | 729 | else simpleexp(ls, v); |
713 | /* expand while operators have priorities higher than `limit' */ | 730 | /* expand while operators have priorities higher than `limit' */ |
714 | while (get_priority(ls->t.token, &rp) > limit) { | 731 | op = getbinopr(ls->t.token); |
732 | while (op != OPR_NOBINOPR && priority[op].left > limit) { | ||
715 | expdesc v2; | 733 | expdesc v2; |
716 | int op = ls->t.token; /* current operator (with priority == `rp') */ | 734 | BinOpr nextop; |
717 | next(ls); | 735 | next(ls); |
718 | luaK_infix(ls, op, v); | 736 | luaK_infix(ls, op, v); |
719 | subexpr(ls, &v2, rp); /* read sub-expression with priority > `rp' */ | 737 | /* read sub-expression with higher priority */ |
738 | nextop = subexpr(ls, &v2, priority[op].right); | ||
720 | luaK_posfix(ls, op, v, &v2); | 739 | luaK_posfix(ls, op, v, &v2); |
740 | op = nextop; | ||
721 | } | 741 | } |
742 | return op; /* return first untreated operator */ | ||
722 | } | 743 | } |
723 | 744 | ||
724 | 745 | ||