Code Generation for Expression trees

- Ditched Gradle because I couldn't get annotation processing to work,
  switched to maven instead
This commit is contained in:
ctsk
2022-09-06 17:34:55 +02:00
parent 3857580ff0
commit e68f0edc1c
19 changed files with 242 additions and 366 deletions

30
jlox/lox/pom.xml Normal file
View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xyz.ctsk.lox</groupId>
<artifactId>lox</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>interpreter</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>xyz.ctsk.lox</groupId>
<artifactId>tools</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,62 @@
package xyz.ctsk.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;
public class Lox {
private static boolean hadError = false;
private static void run(String source) {
var scanner = new Scanner(source);
scanner.scanTokens().forEach(System.out::println);
}
static void error(int line, String message) {
report(line, "", message);
}
private static void report(int line, String where, String message) {
System.err.printf("[line %d] Error %s: %s%n", line, where, message);
hadError = true;
}
private static void runPrompt() throws IOException {
var input = new InputStreamReader(System.in);
var reader = new BufferedReader(input);
while (true) {
System.out.println("> ");
String line = reader.readLine();
if (line == null) break;
run(line);
hadError = false;
}
};
private static void runFile(String path) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(path));
run(new String(bytes, Charset.defaultCharset()));
if (hadError) {
System.exit(65);
}
}
private static void printUsage() {
System.out.println("Usage: jlox [script]");
}
public static void main(String[] args) throws IOException {
if (args.length == 0) {
runPrompt();
} else if (args.length == 1) {
runFile(args[0]);
} else {
printUsage();
}
}
}

View File

@@ -0,0 +1,197 @@
package xyz.ctsk.lox;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static java.util.Map.entry;
import static xyz.ctsk.lox.TokenType.*;
public class Scanner {
private static final Map<String, TokenType> keywords =
Map.ofEntries(
entry("and", AND),
entry("class", CLASS),
entry("else", ELSE),
entry("false", FALSE),
entry("for", FOR),
entry("fun", FUN),
entry("if", IF),
entry("nil", NIL),
entry("or", OR),
entry("print", PRINT),
entry("return", RETURN),
entry("super", SUPER),
entry("this", THIS),
entry("true", TRUE),
entry("var", VAR),
entry("while", WHILE)
);
private final String source;
private final List<Token> tokens = new ArrayList<>();
private int start = 0;
private int current = 0;
private int line = 1;
public Scanner(String source) {
this.source = source;
}
private String currentMatch() {
return source.substring(start, current);
}
private void addToken(TokenType type, Object literal) {
tokens.add(new Token(type, currentMatch(), literal, line));
}
private void addToken(TokenType type) {
addToken(type, null);
}
private boolean isAtEnd() {
return current >= source.length();
}
private char advance() {
return source.charAt(current++);
}
private char peek() {
return isAtEnd() ? '\0' : source.charAt(current);
}
private boolean match(char expected) {
if (isAtEnd()) {
return false;
}
if (source.charAt(current) != expected) {
return false;
}
current++;
return true;
}
private void string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++;
advance();
}
if (isAtEnd()) {
Lox.error(line, "Unterminated string.");
return;
}
advance();
String value = source.substring(start + 1, current - 1);
addToken(STRING, value);
}
private char peekNext() {
if (current + 1 >= source.length()) return '\0';
return source.charAt(current + 1);
}
private static boolean isDigit(char c) {
return '0' <= c && c <= '9';
}
private static boolean isAlpha(char c) {
return ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
c == '_';
}
private static boolean isAlphaNumeric(char c) {
return isDigit(c) || isAlpha(c);
}
private void number() {
while (isDigit(peek())) advance();
// Look for a fractional part.
if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();
while (isDigit(peek())) advance();
}
// Enhancement: Do not allow Letters in numbers
if (isAlpha(peek())) {
Lox.error(line, "Unexpected character in number.");
return;
}
addToken(NUMBER,
Double.parseDouble(currentMatch()));
}
private void identifier() {
while (isAlphaNumeric(peek())) advance();
var text = currentMatch();
addToken(
keywords.getOrDefault(text, IDENTIFIER)
);
}
private void scanToken() {
var c = advance();
switch (c) {
case '(' -> addToken(LEFT_PAREN);
case ')' -> addToken(RIGHT_PAREN);
case '{' -> addToken(LEFT_BRACE);
case '}' -> addToken(RIGHT_BRACE);
case ',' -> addToken(COMMA);
case '.' -> addToken(DOT);
case '-' -> addToken(MINUS);
case '+' -> addToken(PLUS);
case ';' -> addToken(SEMICOLON);
case '*' -> addToken(STAR);
case '!' -> addToken(match('=') ? BANG_EQUAL : BANG);
case '=' -> addToken(match('=') ? EQUAL_EQUAL : EQUAL);
case '<' -> addToken(match('=') ? LESS_EQUAL : LESS);
case '>' -> addToken(match('=') ? GREATER_EQUAL : GREATER);
case '/' -> {
if (match('/')) {
while (peek() != '\n' && !isAtEnd()) advance();
} else {
addToken(SLASH);
}
}
case ' ', '\r', '\t' -> {}
case '\n' -> line++;
case '"' -> string();
default -> {
if (isDigit(c)) {
number();
} else if (isAlpha(c)) {
identifier();
} else {
Lox.error(line, "Unexpected character.");
}
}
}
}
List<Token> scanTokens() {
while (!isAtEnd()) {
start = current;
scanToken();
}
tokens.add(new Token(EOF, "", line));
return tokens;
}
}

View File

@@ -0,0 +1,16 @@
package xyz.ctsk.lox;
public record Token(TokenType type, String lexeme, Object literal, int line) {
public Token(TokenType type, String lexeme, int line) {
this(type, lexeme, null, line);
}
@Override
public String toString() {
if (literal == null) {
return "%s %s".formatted(type, lexeme);
} else {
return "%s %s %s".formatted(type, lexeme, literal);
}
}
};

View File

@@ -0,0 +1,22 @@
package xyz.ctsk.lox;
public 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
}

View File

@@ -0,0 +1,15 @@
@Grammar({
@Root(name = "Expr",
rules = {
@Rule(head = "Binary", body = {"Expr left", "Token operator", "Expr right"}),
@Rule(head = "Grouping", body = {"Expr expression"}),
@Rule(head = "Literal", body = {"Object value"}),
@Rule(head = "Unary", body = {"Token operator", "Expr right"})
})
})
package xyz.ctsk.lox;
import xyz.ctsk.lox.codegen.Grammar;
import static xyz.ctsk.lox.codegen.Grammar.Rule;
import static xyz.ctsk.lox.codegen.Grammar.Root;