-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIdentifierAst.java
More file actions
83 lines (68 loc) · 1.85 KB
/
IdentifierAst.java
File metadata and controls
83 lines (68 loc) · 1.85 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
package org.piccode.ast;
import com.github.tomaslanger.chalk.Chalk;
import org.piccode.piccodescript.TargetEnvironment;
import org.piccode.rt.Context;
import org.piccode.rt.NativeFunction;
import org.piccode.rt.PiccodeException;
import org.piccode.rt.PiccodeSimpleNote;
import org.piccode.rt.PiccodeValue;
/**
*
* @author hexaredecimal
*/
public class IdentifierAst extends Ast {
public String text;
public IdentifierAst(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
@Override
public PiccodeValue execute(Integer frame) {
var ctx = frame == null
? Context.top
: Context.getContextAt(frame);
var value = ctx.getValue(text);
if (value == null) {
var err = new PiccodeException(file, line, column, "Unknown variable `" + Chalk.on(text).red() + "` ");
err.frame = frame;
var nm = Context.top.getSimilarName(text);
if (nm != null && !nm.isEmpty()) {
var note = new PiccodeSimpleNote("Did you mean `" + Chalk.on(nm).green() + "` instead of `" + Chalk.on(text).red() + "` ?");
err.addNote(note);
}
var stack = ctx.getTopFrame().toMap();
var sb = new StringBuilder();
var entrySet = stack.entrySet();
var size = entrySet.size();
var index = 0;
for (var kv: entrySet) {
var key = kv.getKey();
sb.append(key);
if (index < size - 1) {
sb.append(" ,");
}
var str = sb.toString().length();
if (index % 5 == 0) {
sb.append("\n");
}
index++;
}
var note = new PiccodeSimpleNote("Track size: " + ctx.getFramesCount());
err.addNote(note);
note = new PiccodeSimpleNote("Symbol table dump: \n" + sb.toString());
err.addNote(note);
throw err;
}
if (value instanceof NativeFunction nat) {
nat.frame = frame;
}
return value;
}
@Override
public String codeGen(TargetEnvironment target) {
return text;
}
}