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.BindingFromToAction;
24 import org.millscript.millscript.vm.CompilerState;
25
26 import java.util.Stack;
27
28 /**
29 * This class represents a for loop from-to counter binding expression. A
30 * from-to counter expression binds a counter to a name, where the counter
31 * increases on each iteration, up to a specified value. This condition will
32 * cause the for loop to terminate when the counter reaches the end value.
33 */
34 public final class BindingFromToExpr extends BindingExpr< BindingFromToAction > {
35
36 /**
37 * The expression that generates the starting point for the counter.
38 */
39 private final Expr< ? > fromExpr;
40
41 /**
42 * The name expression to bind the counter to.
43 */
44 private final NameExpr name;
45
46 /**
47 * The expression that generates the end point for the counter.
48 */
49 private final Expr< ? > toExpr;
50
51 /**
52 * Constructs a new from-to counter binding expression, with the suplied
53 * name, starting point and end point expressions.
54 *
55 * @param n the name expression to bind to
56 * @param f the counter starting point expression
57 * @param t the counter end point expression
58 */
59 public BindingFromToExpr( final NameExpr n, final Expr< ? > f, final Expr< ? > t ) {
60 name = n;
61 fromExpr = CheckExpr.make( f );
62 toExpr = CheckExpr.make( t );
63 }
64
65 /**
66 * @see org.millscript.millscript.expr.Expr#compileIt()
67 */
68 @Override
69 public BindingFromToAction compileIt() {
70 return new BindingFromToAction(
71 name.getIdent(),
72 fromExpr.compile(),
73 toExpr.compile()
74 );
75 }
76
77 /**
78 * @see org.millscript.millscript.expr.BindingExpr#pushNames(java.util.Stack)
79 */
80 @Override
81 void pushNames( final Stack< Expr > s ) {
82 s.push( name );
83 }
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.fromExpr.resolve( state );
91 this.toExpr.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.fromExpr.show( n );
100 this.toExpr.show( n );
101 }
102
103 }