add ast generator in java

This commit is contained in:
Sebastian Hugentobler 2025-02-08 10:21:26 +01:00
parent a257beb170
commit cac74b1c10
4 changed files with 131 additions and 4 deletions

View File

@ -13,7 +13,9 @@ Though my recommendation is to use the provided
Implementation of the tree-walk interpreter in java after the book.
Run `mvn package` to create an executable jar file in the `target` directory and
`mvn exec:java` to get a repl.
`mvn exec:java@jlox` to get a repl.
Run `mvn exec:java@generate-ast` to generate the expression class.
## [Rust implementation](./rust/rox/)

View File

@ -112,14 +112,27 @@
<version>3.5.0</version>
<executions>
<execution>
<id>jlox</id>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>ch.vanwa.lox_interpreter.App</mainClass>
</configuration>
</execution>
<execution>
<id>generate-ast</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>ch.vanwa.tool.GenerateAst</mainClass>
<arguments>
<argument>src/main/java/ch/vanwa/lox_interpreter</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -0,0 +1,40 @@
package ch.vanwa.lox_interpreter;
import java.util.List;
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;
}
static class Grouping extends Expr {
Grouping(Expr expression) {
this.expression = expression;
}
final Expr expression;
}
static class Literal extends Expr {
Literal(Object value) {
this.value = value;
}
final Object value;
}
static class Unary extends Expr {
Unary(Token operator, Expr right) {
this.operator = operator;
this.right = right;
}
final Token operator;
final Expr right;
}
}

View File

@ -0,0 +1,72 @@
package ch.vanwa.tool;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
public class GenerateAst {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: generate_ast <output directory>");
System.exit(64);
}
String outputDir = args[0];
defineAst(outputDir, "Expr", Arrays.asList(
"Binary : Expr left, Token operator, Expr right",
"Grouping : Expr expression",
"Literal : Object value",
"Unary : Token operator, Expr right"));
}
private static void defineAst(
String outputDir, String baseName, List<String> types)
throws IOException {
String path = outputDir + "/" + baseName + ".java";
PrintWriter writer = new PrintWriter(path, "UTF-8");
writer.println("package ch.vanwa.lox_interpreter;");
writer.println();
writer.println("import java.util.List;");
writer.println();
writer.println("abstract class " + baseName + " {");
// The AST classes.
for (String type : types) {
String className = type.split(":")[0].trim();
String fields = type.split(":")[1].trim();
defineType(writer, baseName, className, fields);
}
writer.println("}");
writer.close();
}
private static void defineType(
PrintWriter writer, String baseName,
String className, String fieldList) {
writer.println(" static class " + className + " extends " +
baseName + " {");
// Constructor.
writer.println(" " + className + "(" + fieldList + ") {");
// Store parameters in fields.
String[] fields = fieldList.split(", ");
for (String field : fields) {
String name = field.split(" ")[1];
writer.println(" this." + name + " = " + name + ";");
}
writer.println(" }");
// Fields.
writer.println();
for (String field : fields) {
writer.println(" final " + field + ";");
}
writer.println(" }");
}
}