1
0

Moving from gradle to maven

Gradle was causing to much of a headache and made the project just
frustrating to work in with all these nested folders and unnecessary
files
This commit is contained in:
2025-12-30 23:59:51 +01:00
parent f87ea7a09c
commit 8f9048c8dd
16 changed files with 411 additions and 721 deletions

View File

@@ -0,0 +1,16 @@
package app;
abstract class Expr {
static class Binary extends Expr {
Binary(Expr left, Token operator, Expr right) {
this.left = left;
this.operator = operator;
this.right = right;
}
final Expr left;
final Token operator;
final Expr right;
}
// Other expressions
}

View File

@@ -0,0 +1,71 @@
package app;
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 {
static boolean hadError = 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 {
try {
runPrompt();
} catch (IOException exception) {
System.out.println(exception.getMessage());
}
}
}
private static void runFile(String path) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(path));
run(new String(bytes, Charset.defaultCharset()));
if (hadError) {
System.out.println("had Error!");
System.exit(65);
}
}
private static void runPrompt() throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
for (;;) {
System.out.println("> ");
String line = reader.readLine();
if (line == null)
break;
run(line);
hadError = false;
}
}
private static void run(String source) {
System.out.println("Starting scan of source");
Scanner scanner = new Scanner(source);
List<Token> tokens = scanner.scanTokens();
// For now, just print the tokens
for (Token token : tokens) {
System.out.println(token);
}
}
static void error(int line, String message) {
report(line, "", message);
}
private static void report(int line, String where, String message) {
hadError = true;
}
}

View File

@@ -0,0 +1,228 @@
package app;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Scanner {
private final String source;
private final List<Token> tokens = new ArrayList<>();
private int start = 0;
private int current = 0;
private int line = 1;
private static final Map<String, TokenType> keywords;
static {
keywords = new HashMap<>();
keywords.put("and", TokenType.AND);
keywords.put("class", TokenType.CLASS);
keywords.put("else", TokenType.ELSE);
keywords.put("false", TokenType.FALSE);
keywords.put("for", TokenType.FOR);
keywords.put("fun", TokenType.FUN);
keywords.put("if", TokenType.IF);
keywords.put("nil", TokenType.NIL);
keywords.put("or", TokenType.OR);
keywords.put("print", TokenType.PRINT);
keywords.put("return", TokenType.RETURN);
keywords.put("super", TokenType.SUPER);
keywords.put("this", TokenType.THIS);
keywords.put("true", TokenType.TRUE);
keywords.put("var", TokenType.TRUE);
keywords.put("while", TokenType.WHILE);
}
Scanner(String source) {
this.source = source;
}
List<Token> scanTokens() {
while (!isAtEnd()) {
// We are at the beginning of the next lexeme
start = current;
scanToken();
}
tokens.add(new Token(TokenType.EOF, "", null, line));
return tokens;
}
private void scanToken() {
char c = advance();
switch (c) {
case '(':
addToken(TokenType.LEFT_PAREN);
break;
case ')':
addToken(TokenType.RIGHT_PAREN);
break;
case '{':
addToken(TokenType.LEFT_BRACE);
break;
case '}':
addToken(TokenType.RIGHT_BRACE);
break;
case ',':
addToken(TokenType.COMMA);
break;
case '.':
addToken(TokenType.DOT);
break;
case '-':
addToken(TokenType.MINUS);
break;
case '+':
addToken(TokenType.PLUS);
break;
case ';':
addToken(TokenType.SEMICOLON);
break;
case '*':
addToken(TokenType.STAR);
break;
case '!':
addToken(match('=') ? TokenType.BANG_EQUAL : TokenType.BANG);
break;
case '=':
addToken(match('=') ? TokenType.EQUAL_EQUAL : TokenType.EQUAL);
break;
case '<':
addToken(match('=') ? TokenType.LESS_EQUAL : TokenType.LESS);
break;
case '>':
addToken(match('=') ? TokenType.GREATER_EQUAL : TokenType.EQUAL);
break;
case '/':
if (match('/')) {
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd())
advance();
} else {
addToken(TokenType.SLASH);
}
break;
case ' ':
case '\r':
case '\t':
// Ignore whitespace.
break;
case '\n':
line++;
break;
case '"':
string();
break;
default:
if (isDigit(c)) {
number();
} else if (isAlpha(c)) {
identifier();
} else {
Lox.error(line, "Unexpected character.");
}
break;
}
}
private void identifier() {
while (isAlphaNumeric(peek()))
advance();
String text = source.substring(start, current);
TokenType type = keywords.get(text);
if (type == null)
type = TokenType.IDENTIFIER;
addToken(type);
}
private boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
private boolean isAlphaNumeric(char c) {
return isAlpha(c) || isDigit(c);
}
private boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
private void number() {
while (isDigit(peek()))
advance();
// Look for a fractional part.
if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();
while (isDigit(peek()))
advance();
}
addToken(TokenType.NUMBER, Double.parseDouble(source.substring(start, current)));
}
private char peekNext() {
if (current + 1 >= source.length())
return '\0';
return source.charAt(current + 1);
}
private void string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n')
line++;
advance();
}
if (isAtEnd()) {
Lox.error(line, "Unterminated string.");
return;
}
// The closing ".
advance();
// Trim the surrounding quotes.
String value = source.substring(start + 1, current - 1);
addToken(TokenType.STRING, value);
}
private char peek() {
if (isAtEnd())
return '\0';
return source.charAt(current);
}
private boolean match(char expected) {
if (isAtEnd())
return false;
if (source.charAt(current) != expected)
return false;
current++;
return true;
}
private char advance() {
return source.charAt(current++);
}
private void addToken(TokenType type) {
addToken(type, null);
}
private void addToken(TokenType type, Object literal) {
String text = source.substring(start, current);
tokens.add(new Token(type, text, literal, line));
}
private boolean isAtEnd() {
return current >= source.length();
}
}

View File

@@ -0,0 +1,19 @@
package app;
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;
}
}

View File

@@ -0,0 +1,22 @@
package app;
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
}