1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript.expr;
23
24 import org.millscript.millscript.action.Action;
25 import org.millscript.millscript.action.UnaryOpAction;
26 import org.millscript.millscript.action.arithmetic.ConstantLeftAddAction;
27 import org.millscript.millscript.action.arithmetic.ConstantLeftDivAction;
28 import org.millscript.millscript.action.arithmetic.ConstantLeftModAction;
29 import org.millscript.millscript.action.arithmetic.ConstantLeftMulAction;
30 import org.millscript.millscript.action.arithmetic.ConstantLeftSubAction;
31 import org.millscript.millscript.alert.Alerts;
32 import org.millscript.millscript.tools.IntegerTools;
33
34 /**
35 * This class implements an arithmetic expression with a constant integer value
36 * for the left hand side. This expression always returns one result.
37 *
38 * @see IntegerTools
39 */
40 public final class ConstantLeftArithExpr extends UnaryOpExpr< UnaryOpAction > {
41
42 /**
43 * The arithmetic symbol this expression is for.
44 */
45 private String sym;
46
47 /**
48 * The left hand side integer.
49 */
50 private Integer lhs;
51
52 /**
53 * Creates a new arithmetic expression for the specified symbol and left and
54 * right hand side expressions.
55 *
56 * @param s the arithmetic symbol to make an expression for
57 * @param a the left hand side integer
58 * @param b the right hand side expression, which should return a single
59 * result
60 */
61 public ConstantLeftArithExpr( final String s, final Integer a, final Expr< ? > b ) {
62 super( b );
63 this.sym = s;
64 this.lhs = a;
65 }
66
67 /**
68 * @see org.millscript.millscript.expr.UnaryOpExpr#newAction(org.millscript.millscript.action.Action)
69 */
70 @Override
71 public UnaryOpAction newAction( final Action a ) {
72
73
74
75 final int intleft = lhs.intValue();
76 if ( sym == "+" ) {
77 return new ConstantLeftAddAction( intleft, a );
78 } else if ( sym == "-" ) {
79 return new ConstantLeftSubAction( intleft, a );
80 } else if ( sym == "*" ) {
81 return new ConstantLeftMulAction( intleft, a );
82 } else if ( sym == "div" ) {
83 return new ConstantLeftDivAction( intleft, a );
84 } else if ( sym == "mod" ) {
85 return new ConstantLeftModAction( intleft, a );
86 } else {
87 throw(
88 Alerts.fault(
89 "Unrecognized arithmetic symbol: " + sym
90 ).mishap()
91 );
92 }
93 }
94
95 }