1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript.action;
23
24 import org.millscript.millscript.alert.Alerts;
25 import org.millscript.millscript.vm.Machine;
26
27 /**
28 * The class represents the base of all for loop condition actions.
29 */
30 public abstract class ForConditionAction extends Action {
31
32 /**
33 * This class represents the conditions, hence bindings in for loops.
34 * Instances of this class can perform bindings, control loop termination.
35 * They also serve as a location to store an action which can be executed
36 * when a for loop terminates. This class is a relativly simple extension
37 * of an Iterator.
38 */
39 abstract class ForIterator {
40
41 /**
42 * The binding action for this iterator.
43 *
44 * @param mc the machine to perform binding operations on
45 */
46 public abstract void bindAction( final Machine mc );
47
48 /**
49 * @see org.millscript.commons.util.MapIterator#hasNext()
50 */
51 public abstract boolean hasNext();
52
53 /**
54 * The termination action for this iterator. This is performed by the
55 * for loop action, if this iterator causes the for loop to terminate.
56 *
57 * @param mc the machine to perform the termination action on
58 * @see #terminatesLoop()
59 * @see ForAction#action(Machine)
60 */
61 public void terminateAction( final Machine mc ) {
62 finallyAction.act( mc );
63 }
64
65 /**
66 * Determines if this iterator causes the termination of the for loop.
67 * The for loop action calls this method when this iterator is
68 * exhausted. By default a ForIterator will terminate the for loop when
69 * there are no more values.
70 *
71 * @return a boolean indicating if this iterator terminates the loop
72 * @see #hasNext()
73 * @see ForAction#action(Machine)
74 */
75 public boolean terminatesLoop() {
76 return true;
77 }
78
79 }
80
81 /**
82 * This action is executed when the loop terminates. There is no default
83 * action.
84 *
85 * @see ForAction#action(Machine)
86 */
87 Action finallyAction = new SkipAction();
88
89 /**
90 * Returns a for loop iterator for this loop condition.
91 *
92 * @param mc the machine to operate the for interator with
93 * @return a ForIterator for this condition
94 */
95 abstract ForIterator getForIterator( final Machine mc );
96
97 /**
98 * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
99 */
100 @Override
101 public void action( final Machine mc ) {
102 throw(
103 Alerts.fault( "Calling a ForConditionAction?" ).mishap()
104 );
105 }
106
107 /**
108 * Sets the finally(termination) action for this for loop condition. This
109 * action will be performed if this condition causes the for loop to
110 * terminate.
111 *
112 * @param a the finally action
113 */
114 public void setFinallyAction( final Action a ) {
115
116
117
118 if ( finallyAction instanceof SkipAction ) {
119 finallyAction = a;
120 }
121 }
122
123 }