| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Jython
Revision: 5216
Author: fwierzbicki
Date: 20 Aug 2008 08:10:26
Changes:Much better BoolOp.
Files:| ... | ...@@ -776,52 +776,6 @@ | |
| 776 | 776 | |
| 777 | 777 | //test: or_test ['if' or_test 'else' test] | lambdef |
| 778 | 778 | test[expr_contextType ctype] |
| 779 | @after { | |
| 780 | if ($test.tree instanceof BoolOp) { | |
| 781 | BoolOp b = (BoolOp)$test.tree; | |
| 782 | List values = new ArrayList(); | |
| 783 | ||
| 784 | exprType left = (exprType)b.getChild(0); | |
| 785 | exprType right = (exprType)b.getChild(1); | |
| 786 | ||
| 787 | exprType[] e; | |
| 788 | if (left.getType() == b.getType() && right.getType() == b.getType()) { | |
| 789 | BoolOp leftB = (BoolOp)left; | |
| 790 | BoolOp rightB = (BoolOp)right; | |
| 791 | int lenL = leftB.values.length; | |
| 792 | int lenR = rightB.values.length; | |
| 793 | e = new exprType[lenL + lenR]; | |
| 794 | System.arraycopy(leftB.values, 0, e, 0, lenL - 1); | |
| 795 | System.arraycopy(rightB.values, 0, e, lenL - 1, lenL + lenR); | |
| 796 | } else if (left.getType() == b.getType()) { | |
| 797 | BoolOp leftB = (BoolOp)left; | |
| 798 | e = new exprType[leftB.values.length + 1]; | |
| 799 | System.arraycopy(leftB.values, 0, e, 0, leftB.values.length); | |
| 800 | e[e.length - 1] = right; | |
| 801 | } else if (right.getType() == b.getType()) { | |
| 802 | BoolOp rightB = (BoolOp)right; | |
| 803 | e = new exprType[rightB.values.length + 1]; | |
| 804 | System.arraycopy(rightB.values, 0, e, 0, rightB.values.length); | |
| 805 | e[e.length - 1] = left; | |
| 806 | } else { | |
| 807 | e = new exprType[2]; | |
| 808 | e[0] = left; | |
| 809 | e[1] = right; | |
| 810 | } | |
| 811 | b.values = e; | |
| 812 | switch (b.getType()) { | |
| 813 | case AND: | |
| 814 | b.op = boolopType.And; | |
| 815 | break; | |
| 816 | case OR: | |
| 817 | b.op = boolopType.Or; | |
| 818 | break; | |
| 819 | default: | |
| 820 | b.op = boolopType.UNDEFINED; | |
| 821 | } | |
| 822 | } | |
| 823 | } | |
| 824 | ||
| 825 | 779 | :o1=or_test[ctype] |
| 826 | 780 | ( (IF or_test[expr_contextType.Load] ORELSE) => IF o2=or_test[ctype] ORELSE test[expr_contextType.Load] |
| 827 | 781 | -> ^(IfExp ^(Test $o2) ^(Body $o1) ^(ORELSE test)) |
| ... | ...@@ -831,12 +785,32 @@ | |
| 831 | 785 | ; |
| 832 | 786 | |
| 833 | 787 | //or_test: and_test ('or' and_test)* |
| 834 | or_test[expr_contextType ctype] : and_test[ctype] (OR<BoolOp>^ and_test[ctype])* | |
| 835 | ; | |
| 788 | or_test[expr_contextType ctype] | |
| 789 | @after { | |
| 790 | if ($or != null) { | |
| 791 | $or_test.tree = actions.makeBoolOp($left.tree, boolopType.Or, $right); | |
| 792 | } | |
| 793 | } | |
| 794 | : left=and_test[ctype] | |
| 795 | ( (or=OR right+=and_test[ctype] | |
| 796 | )+ | |
| 797 | | -> $left | |
| 798 | ) | |
| 799 | ; | |
| 836 | 800 | |
| 837 | 801 | //and_test: not_test ('and' not_test)* |
| 838 | and_test[expr_contextType ctype] : not_test[ctype] (AND<BoolOp>^ not_test[ctype])* | |
| 839 | ; | |
| 802 | and_test[expr_contextType ctype] | |
| 803 | @after { | |
| 804 | if ($and != null) { | |
| 805 | $and_test.tree = actions.makeBoolOp($left.tree, boolopType.And, $right); | |
| 806 | } | |
| 807 | } | |
| 808 | : left=not_test[ctype] | |
| 809 | ( (and=AND right+=not_test[ctype] | |
| 810 | )+ | |
| 811 | | | |
| 812 | ) -> $left | |
| 813 | ; | |
| 840 | 814 | |
| 841 | 815 | //not_test: 'not' not_test | comparison |
| 842 | 816 | not_test[expr_contextType ctype] returns [exprType etype] |
| ... | ...@@ -850,7 +824,6 @@ | |
| 850 | 824 | ; |
| 851 | 825 | |
| 852 | 826 | //comparison: expr (comp_op expr)* |
| 853 | //comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' | |
| 854 | 827 | comparison[expr_contextType ctype] |
| 855 | 828 | @init { |
| 856 | 829 | List cmps = new ArrayList(); |
| ... | ...@@ -12,10 +12,6 @@ | |
| 12 | 12 | |
| 13 | 13 | public static final String[] _fields = new String[] {"op","values"}; |
| 14 | 14 | |
| 15 | public BoolOp(Token token) { | |
| 16 | super(token); | |
| 17 | } | |
| 18 | ||
| 19 | 15 | public BoolOp(Token token, boolopType op, exprType[] values) { |
| 20 | 16 | super(token); |
| 21 | 17 | this.op = op; |
| ... | ...@@ -724,4 +724,11 @@ | |
| 724 | 724 | } |
| 725 | 725 | return new cmpopType[0]; |
| 726 | 726 | } |
| 727 | ||
| 728 | BoolOp makeBoolOp(PythonTree left, boolopType op, List right) { | |
| 729 | List values = new ArrayList(); | |
| 730 | values.add(left); | |
| 731 | values.addAll(right); | |
| 732 | return new BoolOp(left, op, makeExprs(values)); | |
| 733 | } | |
| 727 | 734 | } |