Skip to content

Commit 9a01b53

Browse files
committed
piccode: Use the gleam style error messages as the default
1 parent 9b67842 commit 9a01b53

File tree

2 files changed

+85
-38
lines changed

2 files changed

+85
-38
lines changed

src/main/java/org/piccode/backend/Compiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.piccode.ast.PiccodeVisitor;
2020
import org.piccode.ast.ReturnAst;
2121
import org.piccode.ast.StatementList;
22+
import org.piccode.piccodescript.ErrorAsciiKind;
2223
import org.piccode.rt.Context;
2324
import org.piccode.rt.PiccodeArray;
2425
import org.piccode.rt.PiccodeBoolean;
@@ -47,6 +48,7 @@
4748
*/
4849
public class Compiler {
4950
public static PrintStream out = System.out;
51+
public static ErrorAsciiKind errorKind = ErrorAsciiKind.GLEAM_STYLE;
5052
public static boolean exitOnError = true;
5153
private static List<Runnable> nativeFunctions = new ArrayList<>();
5254

@@ -97,7 +99,6 @@ public static PiccodeValue compile(String file, String code, List<PiccodeValue>
9799
Context.top.dropStackFrame();
98100
}
99101
//Context.top.dropStackFrame();
100-
e.out = out;
101102
e.reportError(exitOnError);
102103
//e.printStackTrace();
103104
return new PiccodeUnit();
@@ -136,7 +137,6 @@ public static List<Ast> compileDeclarationsAndGetExpressions(String file, String
136137
if (Context.top.getFramesCount() > 0) {
137138
Context.top.dropStackFrame();
138139
}
139-
e.out = out;
140140
e.reportError();
141141
return nodes;
142142
} catch (Exception rte) {

src/main/java/org/piccode/rt/PiccodeException.java

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.Scanner;
10+
import org.piccode.backend.Compiler;
11+
import org.piccode.piccodescript.ErrorAsciiKind;
1012

1113
/**
1214
*
@@ -17,7 +19,7 @@ public class PiccodeException extends RuntimeException implements PiccodeInfo {
1719
public String file;
1820
public int line, col;
1921
public String message;
20-
public PrintStream out = System.out;
22+
private PrintStream out = Compiler.out;
2123

2224
public Integer frame = null;
2325

@@ -38,7 +40,7 @@ public void addNote(PiccodeInfo note) {
3840
public void reportError() {
3941
report(!ReplState.ACTIVE, "ERROR");
4042
}
41-
43+
4244
public void reportError(boolean die) {
4345
report(die, "ERROR");
4446
}
@@ -57,6 +59,54 @@ public void report(boolean die, String kind) {
5759
}
5860
}
5961

62+
var errorKind = Compiler.errorKind;
63+
if (errorKind == ErrorAsciiKind.GLEAM_STYLE) {
64+
printGleamStyleError(kind, lines, fp);
65+
} else {
66+
printEmacsCompStyleError(kind, lines, fp);
67+
}
68+
69+
if (!notes.isEmpty()) {
70+
out.println((Chalk.on(".").yellow() + "\n").repeat(2));
71+
for (var note : notes) {
72+
note.report(false, "INFO");
73+
}
74+
}
75+
76+
var ctx = frame == null
77+
? Context.top
78+
: Context.getContextAt(frame);
79+
80+
if (frame != null) {
81+
ctx = Context.getContextAt(frame);
82+
}
83+
var stack = ctx.getCallStack();
84+
85+
var list = List.of(stack.toArray(StackFrame[]::new)).reversed();
86+
87+
if (!list.isEmpty()) {
88+
var thread = frame == null ? "" : String.format(".THREAD[%s]", frame);
89+
out.println("\n[STACK TRACE]" + thread);
90+
for (int i = 0; i < list.size(); i++) {
91+
var _frame = list.get(i);
92+
var callSite = _frame.caller;
93+
var _str = String.format(
94+
"[%s:%d:%d]: %s", callSite.file,
95+
callSite.line, callSite.column + 1,
96+
i == 0
97+
? Chalk.on(callSite.toString()).red()
98+
: callSite.toString());
99+
100+
out.println(_str);
101+
}
102+
}
103+
104+
if (die) {
105+
System.exit(1);
106+
}
107+
}
108+
109+
private void printGleamStyleError(String kind, String[] lines, File fp) {
60110
var line_fmt = String.format(" %d │", line);
61111
var gap = " ".repeat(line_fmt.length());
62112
var gap2 = " ".repeat(line_fmt.length() - 1);
@@ -92,47 +142,44 @@ public void report(boolean die, String kind) {
92142
line_fmt = String.format(" %d │", line + 1);
93143
System.out.println(line_fmt);
94144
}
145+
}
95146

96-
if (!notes.isEmpty()) {
97-
out.println((Chalk.on(".").yellow() + "\n").repeat(2));
98-
for (var note : notes) {
99-
note.report(false, "INFO");
100-
}
101-
}
102-
103-
var ctx = frame == null
104-
? Context.top
105-
: Context.getContextAt(frame);
147+
private void printEmacsCompStyleError(String kind, String[] lines, File fp) {
148+
var line_fmt = String.format(" %d │", line);
149+
var gap2 = " ".repeat(line_fmt.length() - 1);
150+
var fmt = String.format(
151+
"%s:%d:%d: %s: %s",
152+
file == null || file.equals("repl")
153+
? "repl"
154+
: fp.getName(),
155+
line, col + 1,
156+
kind == null || kind.equals("ERROR")
157+
? Chalk.on("ERROR").red() // I hard code ERROR just in case it is null;
158+
: Chalk.on(kind.toUpperCase()).yellow(),
159+
message
160+
);
106161

107-
if (frame != null) {
108-
ctx = Context.getContextAt(frame);
162+
if (file == null) {
163+
out.println("" + fmt);
164+
return;
109165
}
110-
var stack = ctx.getCallStack();
111166

112-
var list = List.of(stack.toArray(StackFrame[]::new)).reversed();
113-
114-
if (!list.isEmpty()) {
115-
var thread = frame == null ? "" : String.format(".THREAD[%s]", frame);
116-
out.println("\n[STACK TRACE]" + thread);
117-
for (int i = 0; i < list.size(); i++) {
118-
var _frame = list.get(i);
119-
var callSite = _frame.caller;
120-
var _str = String.format(
121-
"[%s:%d:%d]: %s", callSite.file,
122-
callSite.line, callSite.column + 1,
123-
i == 0
124-
? Chalk.on(callSite.toString()).red()
125-
: callSite.toString());
126-
127-
out.println(_str);
128-
}
167+
var index = line - 1 < lines.length ? line - 1 : lines.length - 1;
168+
if (index < 0) {
169+
index = 0;
129170
}
130-
131-
if (die) {
132-
System.exit(1);
171+
var code_line = lines[index].replaceAll("\t", " ".repeat(1));
172+
out.println(fmt);
173+
out.println(line_fmt + " " + code_line);
174+
var tick2 = " ".repeat(col + 1) + "^";
175+
out.println(gap2 + " " + tick2);
176+
if (line + 1 < lines.length) {
177+
line_fmt = String.format(" %d │", line + 1);
178+
System.out.println(line_fmt);
133179
}
134180
}
135-
181+
182+
136183
private static String[] toLines(File fp) {
137184
List<String> lines = new ArrayList<>();
138185

0 commit comments

Comments
 (0)