Skip to content

Commit 0e526fc

Browse files
committed
ast: Implement let in expression and add a node for error at runtime
1 parent 23a3e23 commit 0e526fc

File tree

3 files changed

+68
-53
lines changed

3 files changed

+68
-53
lines changed
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
package org.piccode.ast;
22

33
import org.piccode.piccodescript.TargetEnvironment;
4+
import org.piccode.rt.PiccodeException;
45
import org.piccode.rt.PiccodeValue;
56

67
/**
78
*
89
* @author hexaredecimal
910
*/
10-
public class Arg extends Ast {
11-
public String name;
12-
public Ast def_val;
13-
public boolean export;
11+
public class ErrorNodeExpr extends Ast {
12+
public String message;
1413

15-
public Arg(String name, Ast def_val) {
16-
this.name = name;
17-
this.def_val = def_val;
18-
this.export = false;
14+
public ErrorNodeExpr(String message) {
15+
this.message = message;
1916
}
20-
21-
public Arg(String name) {
22-
this.name = name;
23-
this.def_val = null;
24-
this.export = false;
25-
}
26-
17+
2718
@Override
28-
public String toString() {
29-
if (def_val == null) {
30-
return name;
31-
}
32-
return name + "=" + def_val;
19+
public PiccodeValue execute(Integer frame) {
20+
throw new PiccodeException(file, line, column, message);
3321
}
3422

3523
@Override
36-
public PiccodeValue execute(Integer frame) {
24+
public String codeGen(TargetEnvironment target) {
3725
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
3826
}
3927

4028
@Override
41-
public String codeGen(TargetEnvironment target) {
42-
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
29+
public String toString() {
30+
return "ErrorExpr";
4331
}
44-
4532
}
Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,76 @@
11
package org.piccode.ast;
22

33
import java.util.List;
4+
import java.util.function.Function;
5+
import java.util.function.Supplier;
46
import org.piccode.piccodescript.TargetEnvironment;
57
import org.piccode.rt.Context;
68
import org.piccode.rt.PiccodeBoolean;
9+
import org.piccode.rt.PiccodeException;
10+
import org.piccode.rt.PiccodeReturnException;
11+
import org.piccode.rt.PiccodeUnit;
712
import org.piccode.rt.PiccodeValue;
813

914
/**
1015
*
1116
* @author hexaredecimal
1217
*/
13-
public class DoExprAst extends Ast {
14-
public List<Ast> nodes;
18+
public class LetInExprAst extends Ast {
1519

16-
public DoExprAst(List<Ast> nodes) {
17-
this.nodes = nodes;
18-
}
20+
public List<Ast> vars;
21+
public Ast expr;
1922

23+
public LetInExprAst(List<Ast> vars, Ast expr) {
24+
this.vars = vars;
25+
this.expr = expr;
26+
}
2027

2128
@Override
2229
public String toString() {
23-
StringBuilder sb = new StringBuilder();
24-
for (int i = 0; i < nodes.size(); i++) {
25-
sb.append(nodes.get(i));
26-
sb.append("\n");
30+
var sb = new StringBuilder();
31+
sb.append("let\n");
32+
for (var decl : vars) {
33+
sb.append(decl.toString().indent(4));
2734
}
35+
sb.append("in\n");
36+
sb.append(expr.toString().indent(4));
2837
return sb.toString();
2938
}
3039

3140
@Override
3241
public PiccodeValue execute(Integer frame) {
33-
PiccodeValue result = null;
42+
return evaluate(frame, () -> {
43+
for (var decl : vars) {
44+
decl.execute(frame);
45+
}
46+
return evaluate(frame, () -> expr.execute(frame));
47+
});
48+
}
49+
50+
private PiccodeValue evaluate(Integer frame, Supplier<PiccodeValue> fx) {
3451
var ctx = frame == null
35-
? Context.top
36-
: Context.getContextAt(frame);
37-
52+
? Context.top
53+
: Context.getContextAt(frame);
3854
ctx.pushScope();
39-
for (var stmt: nodes) {
40-
result = stmt.execute(frame);
55+
try {
56+
return fx.get();
57+
} catch (PiccodeReturnException | PiccodeException e) {
58+
ctx.dropScope();
59+
throw e;
4160
}
42-
ctx.dropScope();
43-
return result;
4461
}
45-
62+
4663
@Override
4764
public String codeGen(TargetEnvironment target) {
4865
return switch (target) {
49-
case JS -> codeGenJSDoExpr(target);
50-
default -> "todo";
66+
case JS ->
67+
codeGenJSDoExpr(target);
68+
default ->
69+
"todo";
5170
};
5271
}
5372

5473
private String codeGenJSDoExpr(TargetEnvironment env) {
55-
var sb = new StringBuilder()
56-
.append("() => {\n");
57-
58-
nodes.forEach(node -> {
59-
sb.append(node.codeGen(env)).append(";");
60-
});
61-
62-
sb.append("};\n");
63-
return sb.toString();
74+
return null;
6475
}
6576
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ public PiccodeVisitor(String file) {
2323
fileName = file;
2424
}
2525

26+
@Override
27+
public Ast visitLet_decl(Let_declContext ctx) {
28+
var tok = ctx.getStart();
29+
List<Ast> vars = new ArrayList<>();
30+
for (var decl: ctx.var_decl()) {
31+
vars.add(visitVar_decl(decl));
32+
}
33+
var expr = visitExpr(ctx.expr());
34+
var result = new LetInExprAst(vars, expr);
35+
result.file = fileName;
36+
return finalizeAstNode(result, tok);
37+
}
38+
2639
@Override
2740
public Ast visitVar_decl(Var_declContext var_decl) {
2841
var tok = var_decl.ID().getSymbol();
@@ -269,6 +282,10 @@ public Ast visitExpr(ExprContext expr) {
269282
return visitVar_decl(expr.var_decl());
270283
}
271284

285+
if (expr.let_decl() != null) {
286+
return visitLet_decl(expr.let_decl());
287+
}
288+
272289
if (expr.closure_decl() != null) {
273290
return visitClosure_decl(expr.closure_decl());
274291
}

0 commit comments

Comments
 (0)