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.action;
22
23 import org.millscript.commons.alert.alerts.Fault;
24 import org.millscript.millscript.expr.Ident;
25 import org.millscript.millscript.tools.CastLibrary;
26 import org.millscript.millscript.vm.Machine;
27 import org.millscript.millscript.vm.Ref;
28
29 /**
30 * This class represents a for loop from counter binding action. A from counter
31 * action binds a counter to a name, where the counter increases on each
32 * iteration. This condition should never cause the for loop to terminate, as
33 * the counter should continue to infinity.
34 */
35 public final class BindingFromAction extends BindingAction {
36
37 /**
38 * This class implements the underlying iterator for the <code>from</code>
39 * binding in a <code>for</code> loop.
40 */
41 private final class BindingFromIterator extends ForIterator {
42
43 /**
44 * The current value of this iterator.
45 */
46 private int count;
47
48 /**
49 * Constructs a new from iterator to start counting from the specified
50 * value.
51 *
52 * @param fromInt the value to start counting from
53 */
54 private BindingFromIterator( final int fromInt ) {
55 this.count = fromInt;
56 }
57
58 /**
59 * @see org.millscript.millscript.action.ForConditionAction.ForIterator#bindAction(org.millscript.millscript.vm.Machine)
60 */
61 @Override
62 public void bindAction( final Machine mc ) {
63
64 mc.saveRef( ref );
65 if ( count > Integer.MAX_VALUE ) {
66 throw new Fault( "Exceeded range of an int" ).mishap();
67 } else {
68
69 ref.value = new Integer( count++ );
70 }
71 }
72
73 /**
74 * @see org.millscript.millscript.action.ForConditionAction.ForIterator#hasNext()
75 */
76 @Override
77 public boolean hasNext() {
78
79 return true;
80 }
81
82 }
83
84 /**
85 * The ident reference this from action is bound to.
86 */
87 Ref ref;
88
89 /**
90 * The ident this from action is bound to.
91 */
92 private Ident var;
93
94 /**
95 * The action that generates the starting point for the counter.
96 */
97 private Action fromAct;
98
99 /**
100 * Constructs a new from counter binding action, with the supplied ident and
101 * starting point action.
102 *
103 * @param a Ident of the variable this action is bound to
104 * @param b Action which generates starting point for the counter
105 */
106 public BindingFromAction( final Ident a, final Action b ) {
107 var = a;
108 ref = var.getRef();
109 fromAct = b;
110 }
111
112 /**
113 * @see org.millscript.millscript.action.ForConditionAction#getForIterator(org.millscript.millscript.vm.Machine)
114 */
115 @Override
116 ForIterator getForIterator( final Machine mc ) {
117 return new BindingFromIterator(
118 CastLibrary.toInt( fromAct.act1( mc ) )
119 );
120 }
121
122 }