Skip to content

Commit 19bd594

Browse files
Merge pull request #1 from Glimmr-Lang/main
LGTM
2 parents 8470f59 + 9e960a8 commit 19bd594

14 files changed

+337
-128
lines changed

src/main/antlr4/PiccodeScript.g4

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ symbol_lift
2828
symbol_entry
2929
: ID (symbol_lift)? ;
3030

31-
declaration: ID CC (module | func);
31+
declaration: ID CC (module | func | import_module);
3232

3333
module:
3434
MODULE LBRACE module_stmts RBRACE;
@@ -102,11 +102,12 @@ closure_decl: BOR arg_list? BOR ARROW expr;
102102
unary:
103103
EXCLAIM expr
104104
| SUB expr
105+
| RETURN expr
105106
| TILDE expr
106107
| BAND expr;
107108

108109
if_expr:
109-
IF expr LBRACE expr* RBRACE (ELSE LBRACE expr* RBRACE)?;
110+
IF expr LBRACE expr RBRACE (ELSE LBRACE expr RBRACE)?;
110111

111112
when_expr:
112113
WHEN expr LBRACE when_cases else_case? RBRACE;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010
public class Arg extends Ast {
1111
public String name;
1212
public Ast def_val;
13+
public boolean export;
1314

1415
public Arg(String name, Ast def_val) {
1516
this.name = name;
1617
this.def_val = def_val;
18+
this.export = false;
1719
}
1820

1921
public Arg(String name) {
2022
this.name = name;
2123
this.def_val = null;
24+
this.export = false;
2225
}
2326

2427
@Override

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.piccode.ast;
22

3+
import com.github.tomaslanger.chalk.Chalk;
34
import java.util.concurrent.ExecutionException;
45
import org.piccode.piccodescript.TargetEnvironment;
56
import org.piccode.rt.Context;
@@ -34,18 +35,37 @@ public String toString() {
3435

3536
@Override
3637
public PiccodeValue execute(Integer frame) {
37-
if (lhs instanceof IdentifierAst id && Context.modules.containsKey(id.text)) {
38-
var mod = Context.modules.get(id.text);
39-
38+
if (lhs instanceof CCOperationAst op) {
39+
var mod = (PiccodeModule) op.execute(frame);
4040
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
41-
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + id.text);
41+
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + mod.name);
4242
}
43-
43+
44+
var id = new IdentifierAst(mod.name);
45+
id.file = file;
46+
id.line = line;
47+
id.column = column;
4448
return process(id, mod, frame);
4549
}
50+
51+
if (lhs instanceof IdentifierAst id && Context.top.getValue(id.text) != null) {
52+
var mod = Context.top.getValue(id.text);
53+
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
54+
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + id.text);
55+
}
56+
return process(id, (PiccodeModule)mod, frame);
57+
}
4658

47-
var err = new PiccodeException(file, line, column, "Invalid use of `::`. Expected a module on the lhs");
59+
var err = new PiccodeException(file, line, column, "Invalid use of `::`. Expected a module on the lhs, but found " + Chalk.on(lhs.toString()).red());
4860
err.frame = frame;
61+
62+
if (lhs instanceof IdentifierAst id) {
63+
var nm = Context.top.getSimilarName(id.text);
64+
if (nm != null && !nm.isEmpty()) {
65+
var note = new PiccodeSimpleNote("Did you mean `" + Chalk.on(nm).green() + "` instead of `" + Chalk.on(id.text).red() + "` ?");
66+
err.addNote(note);
67+
}
68+
}
4969
throw err;
5070
}
5171

@@ -75,8 +95,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
7595
return result;
7696
}
7797
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
78-
node.execute(frame);
79-
return Context.modules.get(_id.text);
98+
return node.execute(frame);
8099
}
81100
}
82101

@@ -105,7 +124,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame)
105124
}
106125
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
107126
node.execute(frame);
108-
return Context.modules.get(_id.text);
127+
return ctx.getValue(_id.text);
109128
}
110129
}
111130

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

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String toString() {
3636
@Override
3737
public PiccodeValue execute(Integer frame) {
3838

39-
if (lhs instanceof IdentifierAst id && Context.modules.containsKey(id.text)) {
39+
if (lhs instanceof IdentifierAst id && Context.top.getValue(id.text) != null && Context.top.getValue(id.text) instanceof PiccodeModule) {
4040
var err = new PiccodeException(file, line, column, "Cannot access the module `" + id.text + "` using dot. Please use `::` instead");
4141
err.frame = frame;
4242
throw err;
@@ -56,10 +56,6 @@ public PiccodeValue execute(Integer frame) {
5656
return processArrayIndexing(tupl.array(), rhs.execute(frame), frame);
5757
}
5858

59-
if (left instanceof PiccodeModule mod) {
60-
return process(new IdentifierAst(mod.name), mod, frame);
61-
}
62-
6359
if (!(left instanceof PiccodeObject)) {
6460
var err = new PiccodeException(file, line, column, "Invalid expression on the side of `.` : " + lhs + " has value " + left + " which is not an object");
6561
err.frame = frame;
@@ -112,70 +108,6 @@ public PiccodeValue execute(Integer frame) {
112108
return value;
113109
}
114110

115-
private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame) {
116-
var ctx = frame == null
117-
? Context.top
118-
: Context.getContextAt(frame);
119-
120-
if (rhs instanceof IdentifierAst _id) {
121-
for (var node : mod.nodes) {
122-
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
123-
return node.execute(frame);
124-
}
125-
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
126-
node.execute(frame);
127-
var result = ctx.getValue(_id.text);
128-
if (result == null) {
129-
var err = new PiccodeException(func.file, func.line, func.column, "Function `" + _id.text + "` is not defined");
130-
err.frame = frame;
131-
var nm = ctx.getSimilarName(_id.text);
132-
if (nm != null && !nm.isEmpty()) {
133-
var note = new PiccodeException(func.file, func.line, func.column, "Maybe you meant `" + nm + "`");
134-
err.addNote(note);
135-
}
136-
throw err;
137-
}
138-
return result;
139-
}
140-
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
141-
node.execute(frame);
142-
return Context.modules.get(_id.text);
143-
}
144-
}
145-
146-
var err = new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
147-
err.frame = frame;
148-
throw err;
149-
}
150-
151-
var call = (CallAst) rhs;
152-
153-
if (!(call.expr instanceof IdentifierAst)) {
154-
var err = new PiccodeException(file, line, column, "Invalid function reference in module access module " + id.text + ": " + call.expr);
155-
err.frame = frame;
156-
throw err;
157-
}
158-
159-
var _id = (IdentifierAst) call.expr;
160-
for (var node : mod.nodes) {
161-
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
162-
return node.execute(frame);
163-
}
164-
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
165-
node.execute(frame);
166-
//return Context.top.getValue(_id.text);
167-
return call.execute(frame);
168-
}
169-
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
170-
node.execute(frame);
171-
return Context.modules.get(_id.text);
172-
}
173-
}
174-
175-
var err = new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
176-
err.frame = frame;
177-
throw err;
178-
}
179111

180112
private PiccodeValue processArrayIndexing(PiccodeValue[] arr, PiccodeValue execute, Integer frame) {
181113
if (!(execute instanceof PiccodeNumber)) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ private String formatArgs() {
5353

5454
@Override
5555
public PiccodeValue execute(Integer frame) {
56+
var ctx = frame == null
57+
? Context.top
58+
: Context.getContextAt(frame);
59+
5660
Map<String, PiccodeValue> newArgs = new HashMap<>();
5761
var cl = new PiccodeClosure(arg, newArgs, 0, body);
5862
cl.creator = this;
@@ -62,7 +66,7 @@ public PiccodeValue execute(Integer frame) {
6266
cl.file = file;
6367
cl.column = column;
6468
cl.line = line;
65-
Context.addGlobal(name, cl);
69+
ctx.putLocal(name, cl);
6670
return cl;
6771
}
6872

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public PiccodeValue execute(Integer frame) {
6464
var note = new PiccodeSimpleNote("Track size: " + ctx.getFramesCount());
6565
err.addNote(note);
6666

67-
note = new PiccodeSimpleNote("Symbol table dump: " + sb.toString());
67+
note = new PiccodeSimpleNote("Symbol table dump: \n" + sb.toString());
6868
err.addNote(note);
6969
throw err;
7070
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void executeLifted(List<Ast> moduleNodes, List<Ast> nestedLifted, Intege
122122
}
123123
}
124124

125-
private List<Ast> loadModuleFromStdLib(String module, Integer frame) {
125+
public List<Ast> loadModuleFromStdLib(String module, Integer frame) {
126126
var storage = getAppStorage();
127127
var paths = List.of(storage, "./");
128128
var nodes = new ArrayList<Ast>();

0 commit comments

Comments
 (0)