Skip to content

Commit 0921fcb

Browse files
committed
CCOptration: Fix symbol access.
1 parent 3dfc13b commit 0921fcb

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

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

0 commit comments

Comments
 (0)