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 /lparser.c | |
parent | a7c1390ffaf0edfe839d075df484f2b81b3151bf (diff) | |
download | lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.tar.gz lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.tar.bz2 lua-5f22f8961c80ce36297f086ae31d87bbb6897c97.zip |
better code for unary/binary operators
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 79 |
1 files changed, 50 insertions, 29 deletions
@@ -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 | ||