Skip to content

Commit 658370b

Browse files
committed
ast: Make functio remember their runtime state when used with '::'
1 parent 6acbe1e commit 658370b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/main/java/org/piccode/ast/CCOperationAst.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.piccode.piccodescript.TargetEnvironment;
77
import org.piccode.rt.Context;
88
import org.piccode.rt.PiccodeArray;
9+
import org.piccode.rt.PiccodeClosure;
910
import org.piccode.rt.PiccodeException;
1011
import org.piccode.rt.PiccodeModule;
1112
import org.piccode.rt.PiccodeNumber;
@@ -129,10 +130,18 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
129130
return result;
130131
}
131132
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
132-
var result = Ast.safeExecute(frame, func, (expr) -> {
133-
node.execute(frame);
134-
return call.execute(frame);
135-
});
133+
PiccodeValue result = null;
134+
if (func.rtObject == null) {
135+
result = Ast.safeExecute(frame, func, (expr) -> {
136+
func.setRtObject((PiccodeClosure) node.execute(frame));
137+
return call.execute(frame);
138+
});
139+
} else {
140+
result = Ast.safeExecute(frame, func, (expr) -> {
141+
ctx.putLocal(func.name, func.rtObject);
142+
return call.execute(frame);
143+
});
144+
}
136145
ctx.deleteLocal(func.name);
137146
return result;
138147
}

src/main/java/org/piccode/ast/FunctionAst.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ public class FunctionAst extends Ast {
2222
public Ast body;
2323
public List<ClauseAst> clauses = new ArrayList<>();
2424
public List<String> annotations = new ArrayList<>();
25+
public PiccodeClosure rtObject = null;
2526

2627
public FunctionAst(String name, List<Ast> arg, Ast body) {
2728
this.name = name;
2829
this.arg = arg;
2930
this.body = body;
3031
}
3132

33+
public void setRtObject(PiccodeClosure value) {
34+
rtObject = value;
35+
}
36+
3237
@Override
3338
public String toString() {
3439
StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)