-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCCOperationAst.java
More file actions
140 lines (122 loc) · 4.26 KB
/
CCOperationAst.java
File metadata and controls
140 lines (122 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package org.piccode.ast;
import com.github.tomaslanger.chalk.Chalk;
import java.util.concurrent.ExecutionException;
import org.piccode.piccodescript.TargetEnvironment;
import org.piccode.rt.Context;
import org.piccode.rt.PiccodeArray;
import org.piccode.rt.PiccodeException;
import org.piccode.rt.PiccodeModule;
import org.piccode.rt.PiccodeNumber;
import org.piccode.rt.PiccodeObject;
import org.piccode.rt.PiccodeSimpleNote;
import org.piccode.rt.PiccodeString;
import org.piccode.rt.PiccodeTuple;
import org.piccode.rt.PiccodeValue;
/**
*
* @author hexaredecimal
*/
public class CCOperationAst extends Ast {
public Ast lhs;
public Ast rhs;
public CCOperationAst(Ast lhs, Ast rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
@Override
public String toString() {
return lhs + "." + rhs;
}
@Override
public PiccodeValue execute(Integer frame) {
if (lhs instanceof CCOperationAst op) {
var mod = (PiccodeModule) op.execute(frame);
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + mod.name);
}
var id = new IdentifierAst(mod.name);
id.file = file;
id.line = line;
id.column = column;
return process(id, mod, frame);
}
if (lhs instanceof IdentifierAst id && Context.top.getValue(id.text) != null) {
var mod = Context.top.getValue(id.text);
if (!(rhs instanceof CallAst) && !(rhs instanceof IdentifierAst)) {
throw new PiccodeException(file, line, column, "No node " + rhs + " found in module " + id.text);
}
return process(id, (PiccodeModule)mod, frame);
}
var err = new PiccodeException(file, line, column, "Invalid use of `::`. Expected a module on the lhs, but found " + Chalk.on(lhs.toString()).red());
err.frame = frame;
if (lhs instanceof IdentifierAst id) {
var nm = Context.top.getSimilarName(id.text);
if (nm != null && !nm.isEmpty()) {
var note = new PiccodeSimpleNote("Did you mean `" + Chalk.on(nm).green() + "` instead of `" + Chalk.on(id.text).red() + "` ?");
err.addNote(note);
}
}
throw err;
}
private PiccodeValue process(IdentifierAst id, PiccodeModule mod, Integer frame) {
var ctx = frame == null
? Context.top
: Context.getContextAt(frame);
if (rhs instanceof IdentifierAst _id) {
for (var node : mod.nodes) {
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
return node.execute(frame);
}
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
node.execute(frame);
var result = ctx.getValue(_id.text);
if (result == null) {
var err = new PiccodeException(func.file, func.line, func.column, "Function `" + _id.text + "` is not defined");
err.frame = frame;
var nm = ctx.getSimilarName(_id.text);
if (nm != null && !nm.isEmpty()) {
var note = new PiccodeException(func.file, func.line, func.column, "Maybe you meant `" + nm + "`");
err.addNote(note);
}
throw err;
}
return result;
}
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
return node.execute(frame);
}
}
var err = new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
err.frame = frame;
throw err;
}
var call = (CallAst) rhs;
if (!(call.expr instanceof IdentifierAst)) {
var err = new PiccodeException(file, line, column, "Invalid function reference in module access module " + id.text + ": " + call.expr);
err.frame = frame;
throw err;
}
var _id = (IdentifierAst) call.expr;
for (var node : mod.nodes) {
if (node instanceof VarDecl vd && vd.name.equals(_id.text)) {
return node.execute(frame);
}
if (node instanceof FunctionAst func && func.name.equals(_id.text)) {
node.execute(frame);
//return Context.top.getValue(_id.text);
return call.execute(frame);
}
if (node instanceof ModuleAst _mod && _mod.name.equals(_id.text)) {
node.execute(frame);
return ctx.getValue(_id.text);
}
}
var err = new PiccodeException(file, line, column, "No function or identifier " + _id.text + " found in module " + id.text);
err.frame = frame;
throw err;
}
@Override
public String codeGen(TargetEnvironment target) {
return String.format("%s.%s", lhs, rhs);
}
}