This commit is contained in:
Erik Winter 2024-07-22 14:30:33 +02:00
parent 65e7abe83d
commit 1e78c8c7f3
4 changed files with 52 additions and 2 deletions

View File

@ -4,7 +4,6 @@
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/.idea/copilot/chatSessions" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

View File

@ -4,9 +4,12 @@ import java.io.InputStreamReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner; import java.util.Scanner;
public class Lox { public class Lox {
static boolean hadError = false;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
if (args.length > 1) { if (args.length > 1) {
System.out.println("Usage: jlox [script]"); System.out.println("Usage: jlox [script]");
@ -21,6 +24,9 @@ public class Lox {
private static void runFile(String path) throws IOException { private static void runFile(String path) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(path)); byte[] bytes = Files.readAllBytes(Paths.get(path));
run(new String(bytes, Charset.defaultCharset())); run(new String(bytes, Charset.defaultCharset()));
if (hadError) {
System.exit(65);
}
} }
private static void runPrompt() throws IOException { private static void runPrompt() throws IOException {
@ -32,6 +38,7 @@ public class Lox {
String line = reader.readLine(); String line = reader.readLine();
if (line == null) break; if (line == null) break;
run(line); run(line);
hadError = false;
} }
} }
@ -44,4 +51,13 @@ public class Lox {
} }
} }
static void error(int line, String message) {
report(line, "", message);
}
private static void report(int line, String where, String message) {
System.err.println("[" + line + "] " + where + ": " + message);
hadError = true;
}
} }

17
src/Token.java Normal file
View File

@ -0,0 +1,17 @@
class Token {
final TokenType type;
final String lexeme;
final Object literal;
final int line;
Token(TokenType type, String lexeme, Object literal, int line) {
this.type = type;
this.lexeme = lexeme;
this.literal = literal;
this.line = line;
}
public String toString() {
return type + " " + lexeme + " " + literal;
}
}

18
src/TokenType.java Normal file
View File

@ -0,0 +1,18 @@
enum TokenType {
// Single-character tokens
LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE,
COMMA, DOT, MINUS, PLUS, SEMICOLON, SLASH, STAR,
// One or two character tokens
BANG, BANG_EQUAL, EQUAL, EQUAL_EQUAL,
GREATER, GREATER_EQUAL, LESS, LESS_EQUAL,
// Literals
IDENTIFIER, STRING, NUMBER,
// Keywords
AND, CLASS, ELSE, FALSE, FUN, FOR, IF, NIL, OR,
PRINT, RETURN, SUPER, THIS, TRUE, VAR, WHILE,
EOF
}