Froj/src/com/jsaasta/froj/Froj.java
Jesper Saastamoinen 4212bec61f Added FileReader to stdlib
Added FileReader to stdlib to read in files and return it as a string
2024-09-17 14:48:10 +02:00

101 lines
2.8 KiB
Java

package com.jsaasta.froj;
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 Froj {
private static final Interpreter interpreter = new Interpreter();
static boolean hadError = false;
static boolean hadRuntimeError = false;
public static void main(String[] args) throws IOException {
Froj froj = new Froj();
if (args.length != 1) {
System.out.println("Usage: froj [script]");
System.exit(64);
}
froj.runFile(args[0]);
}
public void runFile(String path) throws IOException {
try {
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);
} catch (IOException exception) {
System.err.println("File not found: " + path);
}
}
/*
* Old REPL implementation. Not tested functionality in a while.
*
private 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;
hadRuntimeError = false;
}
}
*/
private void run(String source) {
Scanner scanner = new Scanner(source);
List<Token> tokens = scanner.scanTokens();
Parser parser = new Parser(tokens);
List<Stmt> statements = parser.parse();
if (hadError) return;
Resolver resolver = new Resolver(interpreter);
resolver.resolve(statements);
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;
}
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);
}
}
static void runtimeError(RuntimeError error) {
System.err.println(error.getMessage() +
"\n[line " + error.token.line + "]");
hadRuntimeError = true;
}
}