1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript.expr;
22
23 import org.millscript.millscript.action.Action;
24 import org.millscript.millscript.vm.CompilerState;
25
26 /**
27 * This class represents binary opereration syntax. A binary operation is one
28 * which returns a single result, from two single inputs. This means the left
29 * and right hand side expressions must produce single results, and that the
30 * expression as a whole will return a single result.
31 *
32 * @see AppendExpr
33 * @see org.millscript.millscript.action.BinaryOpAction
34 */
35 public abstract class BinaryOpExpr< A extends Action > extends Expr< A > implements OneResult {
36
37 /**
38 * The left hand side expression
39 */
40 private final Expr< ? > lhs;
41
42 /**
43 * The right hand side expression
44 */
45 private final Expr< ? > rhs;
46
47 /**
48 * Creates a new binary operation expression with the specified left and
49 * right hand side expressions.
50 *
51 * @param a the left hand side expression, which must return a single
52 * result
53 * @param b the right hand side expression, which must return a single
54 * result
55 */
56 public BinaryOpExpr( final Expr< ? > a, final Expr< ? > b ) {
57 this.lhs = CheckExpr.make( a );
58 this.rhs = CheckExpr.make( b );
59 }
60
61 /**
62 * @see org.millscript.millscript.expr.Expr#compileIt()
63 */
64 @Override
65 public final A compileIt() {
66
67
68
69
70 return newAction( lhs.compile(), rhs.compile() );
71 }
72
73 /**
74 * Compiles this expression into an action. This is method performs the
75 * expression specific compilation, but does not need to handle the left
76 * and right hand side expression compilation.
77 *
78 * @param a the compiled left hand side expression
79 * @param b the compiled right hand side expression
80 * @return the {@link Action} for this expression
81 * @see #compileIt
82 */
83 public abstract A newAction( final Action a, final Action b );
84
85 /**
86 * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
87 */
88 @Override
89 public void resolve( final CompilerState state ) {
90 this.lhs.resolve( state );
91 this.rhs.resolve( state );
92 }
93
94 /**
95 * @see org.millscript.millscript.expr.Expr#showComponents(int)
96 */
97 @Override
98 void showComponents( final int n ) {
99 this.lhs.show( n );
100 this.rhs.show( n );
101 }
102
103 }