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.CommaAction;
24 import org.millscript.millscript.vm.CompilerState;
25
26 /**
27 * This class implements a comma expression. A comma expression simply has a
28 * left and right hand side expression, which are executed in that order. The
29 * comma sits between the left and right hand side expressions.
30 *
31 * @see CommaAction
32 */
33 public final class CommaExpr extends Expr< CommaAction > {
34
35 /**
36 * Returns an expression for the specified left and right hand side
37 * expressions. This method provides some compile-time optimisation, as it
38 * avoids creating additional unnecessary objects in the expression tree. If
39 * the left hand side expression is a {@link SkipExpr}, the right hand side
40 * expression is returned, and vice versa. Otherwise, if neither the left or
41 * right hand side expressions are {@link SkipExpr}s, a new comma expression
42 * is returned.
43 *
44 * @param l the left hand side expression
45 * @param r the right hand side expression
46 * @return a new {@link CommaExpr} if the left or right hand side are not
47 * {@link SkipExpr}, otherwise the right or left hand side
48 * expression
49 */
50 public static Expr< ? > make( final Expr l, final Expr r ) {
51
52
53 if ( l instanceof SkipExpr ) {
54 return r;
55 } else if ( r instanceof SkipExpr ) {
56 return l;
57 } else {
58 return new CommaExpr( l, r );
59 }
60 }
61
62 /**
63 * The left hand side expression, that which preceeds the comma.
64 */
65 private final Expr< ? > lhs;
66
67 /**
68 * The right hand side expression, which follows the comma.
69 */
70 private final Expr< ? > rhs;
71
72 /**
73 * Creates a new comma expression with the specifed left and right hand side
74 * expressions.
75 *
76 * @param l the left hand side expression
77 * @param r the right hand side expression
78 */
79 public CommaExpr( final Expr< ? > l, final Expr< ? > r ) {
80 lhs = l;
81 rhs = r;
82 }
83
84 /**
85 * {@inheritDoc}
86 *
87 * <p>
88 * For a comma expression, the arity is the sum of each expressions arity.
89 * e.g. if either arity is unknown, the comma expressions arity is unknown,
90 * otherwise the comma expression arity is the sum of the left and right
91 * hand side arity.
92 * </p>
93 *
94 * @see Expr#addArity
95 */
96 @Override
97 public int arity() {
98 return addArity( lhs.arity(), rhs.arity() );
99 }
100
101 /**
102 * @see org.millscript.millscript.expr.Expr#compileIt()
103 */
104 @Override
105 public CommaAction compileIt() {
106 return new CommaAction( lhs.compile(), rhs.compile() );
107 }
108
109 /**
110 * Returns the left hand side expression, that which preceeds the comma.
111 *
112 * @return the left hand side expression
113 */
114 public Expr getLeft() { return lhs; }
115
116 /**
117 * Returns the right hand side expression, that which follows the comma.
118 *
119 * @return the right hand side expression
120 */
121 public Expr getRight() { return rhs; }
122
123 /**
124 * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
125 */
126 @Override
127 public void resolve( final CompilerState state ) {
128 this.lhs.resolve( state );
129 this.rhs.resolve( state );
130 }
131
132 /**
133 * @see org.millscript.millscript.expr.Expr#showComponents(int)
134 */
135 @Override
136 void showComponents( final int n ) {
137 this.lhs.show( n );
138 this.rhs.show( n );
139 }
140
141 }