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.ConstantRightAddAction;
27 import org.millscript.millscript.action.arithmetic.ConstantRightDivAction;
28 import org.millscript.millscript.action.arithmetic.ConstantRightModAction;
29 import org.millscript.millscript.action.arithmetic.ConstantRightMulAction;
30 import org.millscript.millscript.action.arithmetic.ConstantRightSubAction;
31 import org.millscript.millscript.alert.Alerts;
32 import org.millscript.millscript.tools.IntegerTools;
33
34 /**
35 * This class implements an arithmetic expression. This expression always
36 * returns one result.
37 *
38 * @see IntegerTools
39 */
40 public final class ConstantRightArithExpr extends UnaryOpExpr< UnaryOpAction > {
41
42 /**
43 * The arithmetic symbol this expression is for.
44 */
45 private String sym;
46
47 /**
48 * The right hand side integer.
49 */
50 private Integer rhs;
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 expression, which should return a single
58 * result
59 * @param b the right hand side integer
60 */
61 public ConstantRightArithExpr( final String s, final Expr< ? > a, final Integer b ) {
62 super( a );
63 this.sym = s;
64 this.rhs = b;
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 intright = rhs.intValue();
76 if ( sym == "+" ) {
77 return new ConstantRightAddAction( a, intright );
78 } else if ( sym == "-" ) {
79 return new ConstantRightSubAction( a, intright );
80 } else if ( sym == "*" ) {
81 return new ConstantRightMulAction( a, intright );
82 } else if ( sym == "div" ) {
83 return new ConstantRightDivAction( a, intright );
84 } else if ( sym == "mod" ) {
85 return new ConstantRightModAction( a, intright );
86 } else {
87 throw(
88 Alerts.fault(
89 "Unrecognized arithmetic symbol: " + sym
90 ).mishap()
91 );
92 }
93 }
94
95 }