Repository navigation
Expand file tree
/
Copy pathLox.java
More file actions
62 lines (47 loc) · 1.54 KB
/
Copy pathLox.java
File metadata and controls
62 lines (47 loc) · 1.54 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.xml.transform.Source;
public class Lox {
static boolean hadError = false;
private static void report(int line, String where, String message) {
hadError = true;
System.out.println();
}
static void error(int line, String message) {
report(line, "", message);
}
private static void run(String source) {
Scanner scanner = new Scanner(source);
List<Token> tokens = scanner.scanTokens();
for (Token token: tokens) {
System.out.println(token);
}
}
private static void runFile(String path) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(path));
run(new String(bytes, Charset.defaultCharset()));
}
private static void runPrompt() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("> ");
String line = reader.readLine();
if (line == null) break;
run(line);
}
}
public static void main(String[] args) throws IOException {
if (args.length > 1) {
System.out.println("Usage: Lox [option]");
System.exit(64);
} else if (args.length == 1) {
runFile(args[0]);
} else {
runPrompt();
}
}
}