|
1 | 1 | package org.piccode.ast; |
2 | 2 |
|
| 3 | +import com.github.tomaslanger.chalk.Chalk; |
3 | 4 | import java.util.concurrent.ExecutionException; |
4 | 5 | import org.piccode.piccodescript.TargetEnvironment; |
5 | 6 | import org.piccode.rt.Context; |
@@ -34,18 +35,37 @@ public String toString() { |
34 | 35 |
|
35 | 36 | @Override |
36 | 37 | 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); |
40 | 40 | 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); |
42 | 42 | } |
43 | | - |
| 43 | + |
| 44 | + var id = new IdentifierAst(mod.name); |
| 45 | + id.file = file; |
| 46 | + id.line = line; |
| 47 | + id.column = column; |
44 | 48 | return process(id, mod, frame); |
45 | 49 | } |
| 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 | + } |
46 | 58 |
|
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()); |
48 | 60 | 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 | + } |
49 | 69 | throw err; |
50 | 70 | } |
51 | 71 |
|
@@ -75,8 +95,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame) |
75 | 95 | return result; |
76 | 96 | } |
77 | 97 | 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); |
80 | 99 | } |
81 | 100 | } |
82 | 101 |
|
@@ -105,7 +124,7 @@ private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame) |
105 | 124 | } |
106 | 125 | if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) { |
107 | 126 | node.execute(frame); |
108 | | - return Context.modules.get(_id.text); |
| 127 | + return ctx.getValue(_id.text); |
109 | 128 | } |
110 | 129 | } |
111 | 130 |
|
|
0 commit comments