Skip to content

Commit e6d06c3

Browse files
committed
ast: Add support for dereferencing values
1 parent 1e9055a commit e6d06c3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,13 @@ private Ast visitUnaryExpr(UnaryContext ctx) {
555555
return result;
556556
}
557557

558+
if (ctx.MUL() != null) {
559+
var tok = ctx.MUL().getSymbol();
560+
var result = finalizeAstNode(
561+
new UnaryAst("*", visitExpr(ctx.expr())),
562+
tok);
563+
return result;
564+
}
558565
if (ctx.SUB() != null) {
559566
var tok = ctx.SUB().getSymbol();
560567
var result = finalizeAstNode(

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.piccode.rt.PiccodeBoolean;
55
import org.piccode.rt.PiccodeException;
66
import org.piccode.rt.PiccodeNumber;
7+
import org.piccode.rt.PiccodeReference;
78
import org.piccode.rt.PiccodeValue;
89

910
/**
@@ -56,6 +57,28 @@ public PiccodeValue execute(Integer frame) {
5657
var bool = (boolean) result.raw();
5758
return new PiccodeBoolean(String.valueOf(!bool));
5859
}
60+
61+
if (op.equals("&")) {
62+
return new PiccodeReference(result);
63+
}
64+
65+
if (op.equals("*")) {
66+
if (!(result instanceof PiccodeReference)) {
67+
var err = new PiccodeException(file, line, column,"Cannot dereference a value that is not a Referebce. expression: " + expr + " results to value " + result);
68+
err.frame = frame;
69+
throw err;
70+
}
71+
var value = ((PiccodeReference)result).deref();
72+
if (!(value instanceof PiccodeValue)) {
73+
var err = new PiccodeException(file, line, column,"Cannot dereference a native reference. expression: " + expr + " results to value " + value);
74+
err.frame = frame;
75+
throw err;
76+
}
77+
78+
return (PiccodeValue) value;
79+
}
80+
81+
5982

6083
var err = new PiccodeException(file, line, column,"Not supported yet: " + op);
6184
err.frame = frame;

0 commit comments

Comments
 (0)