Changeset 731:07611d53a431 in livinglogic.java.ul4

Show
Ignore:
Timestamp:
11/01/12 11:38:35 (6 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Tags:
exp-56
Message:

Implemented constant folding for binary and unary operators and GetSlice?.

Location:
src/main
Files:
23 modified

Legend:

Unmodified
Added
Removed
  • src/main/antlr3/com/livinglogic/ul4/UL4.g

    r730 r731  
    419419                    ')' 
    420420                ) 
    421             )? { if (!callmeth) $node = new GetAttr(location, $node, $n.text); } 
     421            )? { if (!callmeth) $node = GetAttr.make(location, $node, $n.text); } 
    422422        | 
    423423            /* Item/slice access */ 
     
    427427                ( 
    428428                    e2=expr1 { index2 = $e2.node; } 
    429                 )? { $node = new GetSlice(location, $node, null, index2); } 
     429                )? { $node = GetSlice.make(location, $node, null, index2); } 
    430430            | 
    431431                e2=expr1 { index1 = $e2.node; } 
     
    435435                        e3=expr1 { index2 = $e3.node; } 
    436436                    )? 
    437                 )? { $node = slice ? new GetSlice(location, $node, index1, index2) : new GetItem(location, $node, index1); } 
     437                )? { $node = slice ? GetSlice.make(location, $node, index1, index2) : GetItem.make(location, $node, index1); } 
    438438            ) 
    439439            ']' 
     
    451451            '-' { ++count; } 
    452452        )* 
    453         e=expr9 { $node = $e.node; while (count-- != 0) { $node = new Neg(location, $node); } } 
     453        e=expr9 { $node = $e.node; while (count-- != 0) { $node = Neg.make(location, $node); } } 
    454454    ; 
    455455 
     
    472472                '%' { opcode = 3; } 
    473473            ) 
    474             e2=expr8 { switch (opcode) { case 0: $node = new Mul(location, $node, $e2.node); break; case 1: $node = new TrueDiv(location, $node, $e2.node); break; case 2: $node = new FloorDiv(location, $node, $e2.node); break; case 3: $node = new Mod(location, $node, $e2.node); break; } } 
     474            e2=expr8 { switch (opcode) { case 0: $node = Mul.make(location, $node, $e2.node); break; case 1: $node = TrueDiv.make(location, $node, $e2.node); break; case 2: $node = FloorDiv.make(location, $node, $e2.node); break; case 3: $node = Mod.make(location, $node, $e2.node); break; } } 
    475475        )* 
    476476    ; 
     
    490490                '-' { add = false; } 
    491491            ) 
    492             e2=expr7 { $node = add ? new Add(location, $node, $e2.node) : new Sub(location, $node, $e2.node); } 
     492            e2=expr7 { $node = add ? Add.make(location, $node, $e2.node) : Sub.make(location, $node, $e2.node); } 
    493493        )* 
    494494    ; 
     
    516516                '>=' { opcode = 5; } 
    517517            ) 
    518             e2=expr6 { switch (opcode) { case 0: $node = new EQ(location, $node, $e2.node); break; case 1: $node = new NE(location, $node, $e2.node); break; case 2: $node = new LT(location, $node, $e2.node); break; case 3: $node = new LE(location, $node, $e2.node); break; case 4: $node = new GT(location, $node, $e2.node); break; case 5: $node = new GE(location, $node, $e2.node); break; } } 
     518            e2=expr6 { switch (opcode) { case 0: $node = EQ.make(location, $node, $e2.node); break; case 1: $node = NE.make(location, $node, $e2.node); break; case 2: $node = LT.make(location, $node, $e2.node); break; case 3: $node = LE.make(location, $node, $e2.node); break; case 4: $node = GT.make(location, $node, $e2.node); break; case 5: $node = GE.make(location, $node, $e2.node); break; } } 
    519519        )* 
    520520    ; 
     
    534534            )? 
    535535            'in' 
    536             e2=expr5 { $node = not ? new NotContains(location, $node, $e2.node) : new Contains(location, $node, $e2.node); } 
     536            e2=expr5 { $node = not ? NotContains.make(location, $node, $e2.node) : Contains.make(location, $node, $e2.node); } 
    537537        )? 
    538538    ; 
     
    542542    : 
    543543        'not' 
    544         e=expr4 { $node = new Not(location, $e.node); } 
     544        e=expr4 { $node = Not.make(location, $e.node); } 
    545545    | 
    546546        e=expr4 { $node = $e.node; } 
     
    554554        ( 
    555555            'and' 
    556             e2=expr3 { $node = new And(location, $node, $e2.node); } 
     556            e2=expr3 { $node = And.make(location, $node, $e2.node); } 
    557557        )* 
    558558    ; 
     
    564564        ( 
    565565            'or' 
    566             e2=expr2 { $node = new Or(location, $node, $e2.node); } 
     566            e2=expr2 { $node = Or.make(location, $node, $e2.node); } 
    567567        )* 
    568568    ; 
  • src/main/java/com/livinglogic/ul4/Add.java

    r720 r731  
    2626    } 
    2727 
     28    public static AST make(Location location, AST obj1, AST obj2) 
     29    { 
     30        if (obj1 instanceof Const && obj2 instanceof Const) 
     31            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     32        return new Add(location, obj1, obj2); 
     33    } 
     34 
    2835    public Object evaluate(EvaluationContext context) throws IOException 
    2936    { 
  • src/main/java/com/livinglogic/ul4/And.java

    r683 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new And(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
     
    3037    } 
    3138 
    32     // we can't implement a static call version here, because that would require that we evaluate both sides 
     39    // this static version is only used for constant folding, not in evaluate(), because that would require that we evaluate both sides 
     40    public static Object call(Object arg1, Object arg2) 
     41    { 
     42        return !FunctionBool.call(arg1) ? arg1 : arg2; 
     43    } 
    3344} 
  • src/main/java/com/livinglogic/ul4/Contains.java

    r648 r731  
    2121    { 
    2222        return "contains"; 
     23    } 
     24 
     25    public static AST make(Location location, AST obj1, AST obj2) 
     26    { 
     27        if (obj1 instanceof Const && obj2 instanceof Const) 
     28            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     29        return new Contains(location, obj1, obj2); 
    2330    } 
    2431 
  • src/main/java/com/livinglogic/ul4/EQ.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new EQ(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/FloorDiv.java

    r720 r731  
    2121    { 
    2222        return "floordiv"; 
     23    } 
     24 
     25    public static AST make(Location location, AST obj1, AST obj2) 
     26    { 
     27        if (obj1 instanceof Const && obj2 instanceof Const) 
     28            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     29        return new FloorDiv(location, obj1, obj2); 
    2330    } 
    2431 
  • src/main/java/com/livinglogic/ul4/GE.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new GE(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/GT.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new GT(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/GetAttr.java

    r678 r731  
    3434    { 
    3535        return "getattr"; 
     36    } 
     37 
     38    public static AST make(Location location, AST obj, String attrname) 
     39    { 
     40        if (obj instanceof Const) 
     41            return new Const(location, call(((Const)obj).value, attrname)); 
     42        return new GetAttr(location, obj, attrname); 
    3643    } 
    3744 
  • src/main/java/com/livinglogic/ul4/GetItem.java

    r730 r731  
    2121    { 
    2222        return "getitem"; 
     23    } 
     24 
     25    public static AST make(Location location, AST obj1, AST obj2) 
     26    { 
     27        if (obj1 instanceof Const && obj2 instanceof Const) 
     28            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     29        return new GetItem(location, obj1, obj2); 
    2330    } 
    2431 
  • src/main/java/com/livinglogic/ul4/GetSlice.java

    r678 r731  
    3939    { 
    4040        return "getslice"; 
     41    } 
     42 
     43    public static AST make(Location location, AST obj, AST index1, AST index2) 
     44    { 
     45        if (obj instanceof Const) 
     46        { 
     47            if (index1 == null) 
     48            { 
     49                if (index2 == null) 
     50                    return new Const(location, call(((Const)obj).value, null, null)); 
     51                else if (index2 instanceof Const) 
     52                    return new Const(location, call(((Const)obj).value, null, ((Const)index2).value)); 
     53            } 
     54            else if (index1 instanceof Const) 
     55            { 
     56                if (index2 == null) 
     57                    return new Const(location, call(((Const)obj).value, ((Const)index1).value, null)); 
     58                else if (index2 instanceof Const) 
     59                    return new Const(location, call(((Const)obj).value, ((Const)index1).value, ((Const)index2).value)); 
     60            } 
     61        } 
     62        return new GetSlice(location, obj, index1, index2); 
    4163    } 
    4264 
  • src/main/java/com/livinglogic/ul4/LE.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new LE(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/LT.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new LT(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/Mod.java

    r678 r731  
    2121    { 
    2222        return "mod"; 
     23    } 
     24 
     25    public static AST make(Location location, AST obj1, AST obj2) 
     26    { 
     27        if (obj1 instanceof Const && obj2 instanceof Const) 
     28            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     29        return new Mod(location, obj1, obj2); 
    2330    } 
    2431 
  • src/main/java/com/livinglogic/ul4/Mul.java

    r720 r731  
    2525    { 
    2626        return "mul"; 
     27    } 
     28 
     29    public static AST make(Location location, AST obj1, AST obj2) 
     30    { 
     31        if (obj1 instanceof Const && obj2 instanceof Const) 
     32            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     33        return new Mul(location, obj1, obj2); 
    2734    } 
    2835 
  • src/main/java/com/livinglogic/ul4/NE.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new NE(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/Neg.java

    r720 r731  
    2121    { 
    2222        return "neg"; 
     23    } 
     24 
     25    public static AST make(Location location, AST obj) 
     26    { 
     27        if (obj instanceof Const) 
     28            return new Const(location, call(((Const)obj).value)); 
     29        return new Neg(location, obj); 
    2330    } 
    2431 
  • src/main/java/com/livinglogic/ul4/Not.java

    r647 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj) 
     24    { 
     25        if (obj instanceof Const) 
     26            return new Const(location, call(((Const)obj).value)); 
     27        return new Not(location, obj); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/NotContains.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new NotContains(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
  • src/main/java/com/livinglogic/ul4/Or.java

    r648 r731  
    2121    } 
    2222 
     23    public static AST make(Location location, AST obj1, AST obj2) 
     24    { 
     25        if (obj1 instanceof Const && obj2 instanceof Const) 
     26            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     27        return new Or(location, obj1, obj2); 
     28    } 
     29 
    2330    public Object evaluate(EvaluationContext context) throws IOException 
    2431    { 
     
    3037    } 
    3138 
    32     // we can't implement a static call version here, because that would require that we evaluate both sides 
     39    // this static version is only used for constant folding, not in evaluate(), because that would require that we evaluate both sides 
     40    public static Object call(Object arg1, Object arg2) 
     41    { 
     42        return FunctionBool.call(arg1) ? arg1 : arg2; 
     43    } 
    3344} 
  • src/main/java/com/livinglogic/ul4/Sub.java

    r720 r731  
    2424    { 
    2525        return "sub"; 
     26    } 
     27 
     28    public static AST make(Location location, AST obj1, AST obj2) 
     29    { 
     30        if (obj1 instanceof Const && obj2 instanceof Const) 
     31            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     32        return new Sub(location, obj1, obj2); 
    2633    } 
    2734 
  • src/main/java/com/livinglogic/ul4/TrueDiv.java

    r720 r731  
    2222    { 
    2323        return "truediv"; 
     24    } 
     25 
     26    public static AST make(Location location, AST obj1, AST obj2) 
     27    { 
     28        if (obj1 instanceof Const && obj2 instanceof Const) 
     29            return new Const(location, call(((Const)obj1).value, ((Const)obj2).value)); 
     30        return new TrueDiv(location, obj1, obj2); 
    2431    } 
    2532 
  • src/main/resources/versiondoc.txt

    r730 r731  
    55* UL4ON can now read/write TimeDelta and MonthDelta objects. 
    66* Added the Undefined singleton. 
     7* Implemented constant folding for binary and unary operators and GetSlice. 
    78 
    89