Skip to content

Commit 36b4707

Browse files
committed
rt: Fix some minor bugs on the Error and Warning classes
1 parent 9d38093 commit 36b4707

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private void printGleamStyleError(String kind, String[] lines, File fp) {
141141
out.println(gap2 + "╰" + tick);
142142
if (line + 1 < lines.length) {
143143
line_fmt = String.format(" %d │", line + 1);
144-
System.out.println(line_fmt);
144+
out.println(line_fmt);
145145
}
146146
}
147147

@@ -176,7 +176,7 @@ private void printEmacsCompStyleError(String kind, String[] lines, File fp) {
176176
out.println(gap2 + " " + tick2);
177177
if (line + 1 < lines.length) {
178178
line_fmt = String.format(" %d │", line + 1);
179-
System.out.println(line_fmt);
179+
out.println(line_fmt);
180180
}
181181
}
182182

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

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import com.github.tomaslanger.chalk.Chalk;
44
import java.io.File;
55
import java.io.FileNotFoundException;
6+
import java.io.PrintStream;
67
import java.util.ArrayList;
78
import java.util.List;
89
import java.util.Scanner;
10+
import org.piccode.backend.Compiler;
11+
import org.piccode.piccodescript.ErrorAsciiKind;
912

1013
/**
1114
*
@@ -17,6 +20,7 @@ public class PiccodeWarning implements PiccodeInfo {
1720
public int line, col;
1821
public String message;
1922

23+
private PrintStream out = Compiler.out;
2024
public Integer frame = null;
2125

2226
private List<PiccodeInfo> notes = new ArrayList<>();
@@ -50,6 +54,36 @@ public void report(boolean die, String kind) {
5054
}
5155
}
5256

57+
58+
var errorKind = Compiler.errorKind;
59+
if (errorKind == ErrorAsciiKind.GLEAM_STYLE) {
60+
printGleamStyleError(kind, lines, fp);
61+
} else {
62+
printEmacsCompStyleError(kind, lines, fp);
63+
}
64+
65+
if (!notes.isEmpty()) {
66+
System.out.println((Chalk.on(".").yellow() + "\n").repeat(2));
67+
for (var note : notes) {
68+
note.report(false, "INFO");
69+
}
70+
}
71+
}
72+
73+
private static String[] toLines(File fp) {
74+
List<String> lines = new ArrayList<>();
75+
76+
try (Scanner sc = new Scanner(fp)) {
77+
while (sc.hasNextLine()) {
78+
lines.add(sc.nextLine());
79+
}
80+
} catch (FileNotFoundException ex) {
81+
}
82+
return lines.toArray(String[]::new);
83+
}
84+
85+
86+
private void printGleamStyleError(String kind, String[] lines, File fp) {
5387
var line_fmt = String.format(" %d │", line);
5488
var gap = " ".repeat(line_fmt.length());
5589
var gap2 = " ".repeat(line_fmt.length() - 1);
@@ -59,14 +93,14 @@ public void report(boolean die, String kind) {
5993
? "repl"
6094
: fp.getName(),
6195
line, col + 1,
62-
kind == null || kind.equals("WARNING")
63-
? Chalk.on("WARNING").yellow()// I hard code ERROR just in case it is null;
96+
kind == null || kind.equals("ERROR")
97+
? Chalk.on("ERROR").red() // I hard code ERROR just in case it is null;
6498
: Chalk.on(kind.toUpperCase()).yellow(),
6599
message
66100
);
67101

68102
if (file == null) {
69-
System.out.println("" + fmt);
103+
out.println("" + fmt);
70104
return;
71105
}
72106

@@ -75,34 +109,52 @@ public void report(boolean die, String kind) {
75109
index = 0;
76110
}
77111
var code_line = lines[index].replaceAll("\t", " ".repeat(1));
78-
System.out.println(gap2 + fmt);
79-
System.out.println(line_fmt + " " + code_line);
112+
out.println(gap2 + fmt);
113+
out.println(line_fmt + " " + code_line);
80114
var tick = "─".repeat(col + 1) + "╯";
81115
var tick2 = " ".repeat(col + 1) + "^";
82-
System.out.println(gap2 + "│" + tick2);
83-
System.out.println(gap2 + "╰" + tick);
116+
out.println(gap2 + "│" + tick2);
117+
out.println(gap2 + "╰" + tick);
84118
if (line + 1 < lines.length) {
85119
line_fmt = String.format(" %d │", line + 1);
86-
System.out.println(line_fmt);
87-
}
88-
89-
if (!notes.isEmpty()) {
90-
System.out.println((Chalk.on(".").yellow() + "\n").repeat(2));
91-
for (var note : notes) {
92-
note.report(false, "INFO");
93-
}
120+
out.println(line_fmt);
94121
}
95122
}
96123

97-
private static String[] toLines(File fp) {
98-
List<String> lines = new ArrayList<>();
124+
private void printEmacsCompStyleError(String kind, String[] lines, File fp) {
125+
var line_fmt = String.format(" %d │", line);
126+
var gap2 = " ".repeat(line_fmt.length() - 1);
127+
var fmt = String.format(
128+
"%s:%d:%d: %s: %s",
129+
file == null || file.equals("repl")
130+
? "repl"
131+
: fp.getName(),
132+
line, col + 1,
133+
kind == null || kind.equals("ERROR")
134+
? Chalk.on("ERROR").red() // I hard code ERROR just in case it is null;
135+
: Chalk.on(kind.toUpperCase()).yellow(),
136+
message
137+
);
99138

100-
try (Scanner sc = new Scanner(fp)) {
101-
while (sc.hasNextLine()) {
102-
lines.add(sc.nextLine());
103-
}
104-
} catch (FileNotFoundException ex) {
139+
if (file == null) {
140+
out.println("" + fmt);
141+
return;
142+
}
143+
144+
var index = line - 1 < lines.length ? line - 1 : lines.length - 1;
145+
if (index < 0) {
146+
index = 0;
147+
}
148+
var code_line = lines[index].replaceAll("\t", " ".repeat(1));
149+
out.println(fmt);
150+
out.println(line_fmt + " " + code_line);
151+
var tick2 = " ".repeat(col + 1) + "^";
152+
out.println(gap2 + " " + tick2);
153+
if (line + 1 < lines.length) {
154+
line_fmt = String.format(" %d │", line + 1);
155+
out.println(line_fmt);
105156
}
106-
return lines.toArray(String[]::new);
107157
}
158+
159+
108160
}

0 commit comments

Comments
 (0)