aboutsummaryrefslogtreecommitdiffstats
path: root/jlox/src/com/craftinginterpreters/lox/Lox.java
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-07-08 00:14:31 -0700
committerGravatar Tom Willemse2021-07-08 02:25:13 -0700
commit68a2ebd34fc94488e89ffb82b359ec6e7e152ae9 (patch)
tree0dbbe26aff8fc38805b4b3780ae7842c433b9522 /jlox/src/com/craftinginterpreters/lox/Lox.java
parent62bd0f83dc909547a69abb8b0aed40cf098b4c95 (diff)
downloadcrafting-interpreters-68a2ebd34fc94488e89ffb82b359ec6e7e152ae9.tar.gz
crafting-interpreters-68a2ebd34fc94488e89ffb82b359ec6e7e152ae9.zip
Restructure project to make room for clox
Diffstat (limited to 'jlox/src/com/craftinginterpreters/lox/Lox.java')
-rw-r--r--jlox/src/com/craftinginterpreters/lox/Lox.java93
1 files changed, 93 insertions, 0 deletions
diff --git a/jlox/src/com/craftinginterpreters/lox/Lox.java b/jlox/src/com/craftinginterpreters/lox/Lox.java
new file mode 100644
index 0000000..6115f8f
--- /dev/null
+++ b/jlox/src/com/craftinginterpreters/lox/Lox.java
@@ -0,0 +1,93 @@
+package com.craftinginterpreters.lox;
+
+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 java.util.List;
+
+public class Lox {
+ private static final Interpreter interpreter = new Interpreter();
+ public static boolean hadError = false;
+ public static boolean hadRuntimeError = false;
+
+ public static void main(String[] args) throws IOException {
+ if (args.length > 1) {
+ System.out.println("Usage: jlox [script]");
+ System.exit(64);
+ } else if (args.length == 1) {
+ runFile(args[0]);
+ } else {
+ runPrompt();
+ }
+ }
+
+ private static void runFile(String path) throws IOException {
+ byte[] bytes = Files.readAllBytes(Paths.get(path));
+ run(new String(bytes, Charset.defaultCharset()));
+
+ // Indicate an error in the exit code.
+ if (hadError)
+ System.exit(65);
+ if (hadRuntimeError)
+ System.exit(70);
+ }
+
+ private static void runPrompt() throws IOException {
+ InputStreamReader input = new InputStreamReader(System.in);
+ BufferedReader reader = new BufferedReader(input);
+
+ for (;;) {
+ System.out.print("> ");
+ String line = reader.readLine();
+ if (line == null)
+ break;
+ run(line);
+ hadError = false;
+ }
+ }
+
+ private static void run(String source) {
+ Scanner scanner = new Scanner(source);
+ List<Token> tokens = scanner.scanTokens();
+ Parser parser = new Parser(tokens);
+ List<Stmt> statements = parser.parse();
+
+ // Stop if there was a syntax error
+ if (hadError)
+ return;
+
+ Resolver resolver = new Resolver(interpreter);
+ resolver.resolve(statements);
+
+ // Stop if there was a resolution error
+ if (hadError)
+ return;
+
+ interpreter.interpret(statements);
+ }
+
+ public static void error(int line, String message) {
+ report(line, "", message);
+ }
+
+ private static void report(int line, String where, String message) {
+ System.err.println("[line " + line + "] Error" + where + ": " + message);
+ hadError = true;
+ }
+
+ public static void error(Token token, String message) {
+ if (token.type == TokenType.EOF) {
+ report(token.line, " at end", message);
+ } else {
+ report(token.line, " at '" + token.lexeme + "'", message);
+ }
+ }
+
+ public static void runtimeError(RuntimeError error) {
+ System.err.println(error.getMessage() + "\n[line " + error.token.line + "]");
+ hadRuntimeError = true;
+ }
+}