Skip to content

Commit 5793c72

Browse files
committed
ast: Improve the display of the symbol dump
1 parent f3ab944 commit 5793c72

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ public String toString() {
2828
@Override
2929
public PiccodeValue execute(Integer frame) {
3030
var ctx = frame == null
31-
? Context.top
32-
: Context.getContextAt(frame);
31+
? Context.top
32+
: Context.getContextAt(frame);
3333

34-
3534
var value = ctx.getValue(text);
36-
35+
3736
if (value == null) {
3837
var err = new PiccodeException(file, line, column, "Unknown variable `" + Chalk.on(text).red() + "` ");
3938
err.frame = frame;
@@ -42,36 +41,62 @@ public PiccodeValue execute(Integer frame) {
4241
var note = new PiccodeSimpleNote("Did you mean `" + Chalk.on(nm).green() + "` instead of `" + Chalk.on(text).red() + "` ?");
4342
err.addNote(note);
4443
}
45-
44+
4645
var stack = ctx.getTopFrame().toMap();
4746
var sb = new StringBuilder();
47+
var nms = new StringBuilder();
4848
var entrySet = stack.entrySet();
4949
var size = entrySet.size();
50-
var index = 0;
51-
for (var kv: entrySet) {
50+
var index = 0;
51+
var count = 1;
52+
53+
for (var kv : entrySet) {
5254
var key = kv.getKey();
53-
sb.append(key);
55+
nms.append(key);
56+
var val = kv.getValue().hashCode();
57+
var fmt = String.format("0x%08X", val);
58+
sb.append(fmt);
59+
5460
if (index < size - 1) {
55-
sb.append(" ,");
61+
sb.append(" ");
62+
nms.append(" ");
5663
}
57-
var str = sb.toString().length();
58-
if (index % 5 == 0) {
64+
count++;
65+
66+
if (index + 1 == size) {
67+
var split = sb.toString().split("\n");
68+
if (split.length > 0 && count < 3) {
69+
var part = split[0].split("|")[1];
70+
var lastSplt = split[split.length - 1].split("|");
71+
var last = lastSplt.length == 1 ? lastSplt[0] : lastSplt[lastSplt.length - 1];
72+
var padd = " ".repeat(part.length() - last.length());
73+
sb.append(padd)
74+
.append(" | ")
75+
.append(nms.toString().trim());
76+
count=1;
77+
}
78+
} else if ((index + 1) % 3 == 0) {
79+
count =1;
80+
sb
81+
.append(" | ")
82+
.append(nms.toString().trim());
5983
sb.append("\n");
84+
nms = new StringBuilder();
6085
}
6186
index++;
6287
}
63-
64-
var note = new PiccodeSimpleNote("Track size: " + ctx.getFramesCount());
88+
89+
var note = new PiccodeSimpleNote("Stack size: " + ctx.getFramesCount());
6590
err.addNote(note);
66-
91+
6792
note = new PiccodeSimpleNote("Symbol table dump: \n" + sb.toString());
6893
err.addNote(note);
6994
throw err;
7095
}
7196

7297
if (value instanceof NativeFunction nat) {
7398
nat.frame = frame;
74-
}
99+
}
75100
return value;
76101
}
77102

0 commit comments

Comments
 (0)