Skip to content

Commit 880c5ab

Browse files
committed
Fixed FlowForSpec and FlowWhileSpec
the body must be executed inside a frame
1 parent ad983f3 commit 880c5ab

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/main/java/org/jamplate/glucose/spec/command/hashfor/FlowForSpec.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.jamplate.glucose.spec.command.hashfor;
1717

18-
import org.jamplate.unit.Spec;
1918
import org.jamplate.function.Analyzer;
2019
import org.jamplate.function.Compiler;
2120
import org.jamplate.glucose.instruction.flow.IRepeat;
2221
import org.jamplate.glucose.instruction.memory.frame.IDumpFrame;
2322
import org.jamplate.glucose.instruction.memory.frame.IGlueFrame;
23+
import org.jamplate.glucose.instruction.memory.frame.IPopFrame;
2424
import org.jamplate.glucose.instruction.memory.frame.IPushFrame;
2525
import org.jamplate.glucose.instruction.memory.heap.ISet;
2626
import org.jamplate.glucose.instruction.memory.resource.IPushConst;
@@ -38,13 +38,14 @@
3838
import org.jamplate.model.Instruction;
3939
import org.jamplate.model.Sketch;
4040
import org.jamplate.model.Tree;
41+
import org.jamplate.unit.Spec;
4142
import org.jetbrains.annotations.NotNull;
4243

44+
import static org.jamplate.glucose.internal.analyzer.BinaryFlowAnalyzer.flow;
4345
import static org.jamplate.glucose.internal.util.Values.text;
4446
import static org.jamplate.impl.analyzer.FilterAnalyzer.filter;
4547
import static org.jamplate.impl.analyzer.HierarchyAnalyzer.hierarchy;
4648
import static org.jamplate.impl.compiler.FilterCompiler.filter;
47-
import static org.jamplate.glucose.internal.analyzer.BinaryFlowAnalyzer.flow;
4849
import static org.jamplate.util.Functions.analyzer;
4950
import static org.jamplate.util.Functions.compiler;
5051
import static org.jamplate.util.Query.*;
@@ -202,6 +203,8 @@ public Compiler getCompiler() {
202203
//compile as a foreach REPEAT
203204
return new Block(
204205
tree,
206+
//push the loop frame
207+
new IPushFrame(tree),
205208
//push an anchoring null
206209
new IPushConst(tree, Value.NULL),
207210
//value sandbox
@@ -214,29 +217,33 @@ public Compiler getCompiler() {
214217
//glue the answer
215218
new IGlueFrame(tree),
216219
//cast the answer into array
217-
new ICastArray(tree),
220+
new ICastArray(tree), /*[ [first,second,third] ]*/
218221
//reverse the array
219-
new IReverse(tree),
222+
new IReverse(tree), /*[ [third,second,first] ]*/
220223
//dump the frame
221224
new IDumpFrame(tree)
222225
),
223226
//spread the evaluated value
224-
new ISplit(tree),
227+
new ISplit(tree), /*[ third, second, first ]*/
225228
//duplicate the value
226-
new IDup(tree),
229+
new IDup(tree), /*[ third, second, first, first ]*/
227230
//check if anchor
228-
new IDefined(tree),
231+
new IDefined(tree), /*[ third, second, first, boolean ]*/
229232
//iterate until the anchoring null
230233
new IRepeat(tree, new Block(
231234
tree,
232235
//push the allocation address
233-
keyI,
236+
keyI, /*[third, second, first, key]*/
234237
//swap the address with the value
235-
new ISwap(tree),
238+
new ISwap(tree), /*[third, second, key, first]*/
236239
//allocate the loop variable, at top frame
237-
new ISet(tree),
240+
new ISet(tree), /*[third, second]*/
241+
//push body frame
242+
new IPushFrame(tree),
238243
//execute the body
239244
bodyI,
245+
//pop body frame
246+
new IPopFrame(tree),
240247
//duplicate the value
241248
new IDup(tree),
242249
//check if null
@@ -247,7 +254,9 @@ public Compiler getCompiler() {
247254
//swap the anchoring null with the allocation address
248255
new ISwap(tree),
249256
//allocate the anchoring null to the allocation address, at top frame
250-
new ISet(tree)
257+
new ISet(tree),
258+
//pop the loop frame
259+
new IPopFrame(tree)
251260
);
252261
}
253262
);

src/main/java/org/jamplate/glucose/spec/command/hashwhile/FlowWhileSpec.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.jamplate.glucose.spec.command.hashwhile;
1717

18+
import org.jamplate.glucose.instruction.memory.frame.IPopFrame;
1819
import org.jamplate.unit.Spec;
1920
import org.jamplate.function.Analyzer;
2021
import org.jamplate.function.Compiler;
@@ -206,8 +207,12 @@ public Compiler getCompiler() {
206207
valueWrapI,
207208
//repeat while the answer evaluate to true
208209
new IRepeat(tree, new Block(
210+
//push body frame
211+
new IPushFrame(tree),
209212
//execute the body
210213
bodyI,
214+
//pop body frame
215+
new IPopFrame(tree),
211216
//evaluate the value for the next round
212217
valueWrapI
213218
)),

0 commit comments

Comments
 (0)