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.If2Action;
24 import org.millscript.millscript.vm.CompilerState;
25
26 /**
27 * This class implements an if2 expression. An if2 expresion has a predicate and
28 * ifso expression, where the ifso expression is only executed if the predicate
29 * returns <code>true</code>.
30 *
31 * @see org.millscript.millscript.syntax.ConditionalSyntax
32 * @see If3Expr
33 * @see If2Action
34 */
35 public final class If2Expr extends Expr< If2Action > {
36
37 /**
38 * The ifso expression, to be executed if the predicate returns
39 * <code>true</code>.
40 */
41 private final Expr< ? > ifso;
42
43 /**
44 * The predicate expression.
45 */
46 private final Expr< ? > pred;
47
48 /**
49 * Creates a new if2 expression with the specified predicate and ifso
50 * expressions.
51 *
52 * @param a the predicate expression which must return a single result
53 * @param b the ifso expression
54 */
55 public If2Expr( final Expr< ? > a, final Expr< ? > b ) {
56 pred = CheckExpr.make( a );
57 ifso = b;
58 }
59
60 /**
61 * @see org.millscript.millscript.expr.Expr#arity()
62 */
63 @Override
64 public int arity() {
65
66
67
68 return ifso.arity() == 0 ? 0 : -1;
69 }
70
71 /**
72 * @see org.millscript.millscript.expr.Expr#compileIt()
73 */
74 @Override
75 public If2Action compileIt() {
76 return new If2Action( pred.compile(), ifso.compile() );
77 }
78
79 /**
80 * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
81 */
82 @Override
83 public void resolve( final CompilerState state ) {
84 this.pred.resolve( state );
85 this.ifso.resolve( state );
86 }
87
88 /**
89 * @see org.millscript.millscript.expr.Expr#showComponents(int)
90 */
91 @Override
92 void showComponents( final int n ) {
93 this.pred.show( n );
94 this.ifso.show( n );
95 }
96
97 }