Skip to content

Commit 56ff40d

Browse files
committed
PipeAst: Create new symbols. Do not mutate matched symbols. This was causing weird error if functions are called more than once.
1 parent 176982e commit 56ff40d

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,36 @@ public PiccodeValue execute(Integer frame) {
3434
throw err;
3535
}
3636

37+
var newArgs = new ArrayList<Ast>();
38+
newArgs.add(lhs);
3739
if (rhs instanceof IdentifierAst id) {
38-
var call = new CallAst(id, new ArrayList<>());
39-
call.nodes.addFirst(lhs);
40+
var call = new CallAst(id, newArgs);
4041
var node = Ast.finalizeNode(call, id);
4142
return node.execute(frame);
4243
}
4344

4445
if (rhs instanceof ClosureAst closure) {
45-
var call = new CallAst(closure, new ArrayList<>());
46-
call.nodes.addFirst(lhs);
46+
var call = new CallAst(closure, newArgs);
4747
var node = Ast.finalizeNode(call, closure);
4848
return node.execute(frame);
4949
}
5050

5151
if (rhs instanceof CCOperationAst dot) {
5252
if (dot.rhs instanceof CallAst call) {
53-
if (call.nodes == null) {
54-
call.nodes = new ArrayList<>();
53+
if (call.nodes != null) {
54+
newArgs.addAll(call.nodes);
5555
}
56-
call.nodes.addFirst(lhs);
57-
return rhs.execute(frame);
56+
var _call = new CallAst(call.expr, newArgs);
57+
var nd = new CCOperationAst(dot.lhs, _call);
58+
var node = Ast.finalizeNode(nd, rhs);
59+
return node.execute(frame);
5860
}
5961

6062
if (dot.rhs instanceof IdentifierAst id) {
61-
var args = new ArrayList<Ast>();
62-
args.addFirst(lhs);
63-
var call = new CallAst(id, args);
64-
dot.rhs = Ast.finalizeNode(call, id);
65-
return rhs.execute(frame);
63+
var call = new CallAst(id, newArgs);
64+
var nd = new CCOperationAst(dot.lhs, call);
65+
var node = Ast.finalizeNode(nd, id);
66+
return node.execute(frame);
6667
}
6768

6869
var err = new PiccodeException(file, line, column, "Invalid expression at the right side of |> : " + dot.rhs);
@@ -71,8 +72,9 @@ public PiccodeValue execute(Integer frame) {
7172
}
7273

7374
var call = (CallAst) rhs;
74-
call.nodes.addFirst(lhs);
75-
return call.execute(frame);
75+
newArgs.addAll(call.nodes);
76+
var _call = new CallAst(call, newArgs);
77+
return _call.execute(frame);
7678
});
7779
}
7880

0 commit comments

Comments
 (0)