Add GenerateAst and AstPrinter
This commit is contained in:
parent
5fcfc5a015
commit
2e15e8bab8
3 changed files with 146 additions and 2 deletions
|
@ -9,7 +9,10 @@ add_jar(Lox
|
||||||
com/craftinginterpreters/lox/Lox.java
|
com/craftinginterpreters/lox/Lox.java
|
||||||
com/craftinginterpreters/lox/TokenType.java
|
com/craftinginterpreters/lox/TokenType.java
|
||||||
com/craftinginterpreters/lox/Token.java
|
com/craftinginterpreters/lox/Token.java
|
||||||
|
com/craftinginterpreters/lox/Expr.java
|
||||||
|
com/craftinginterpreters/lox/AstPrinter.java
|
||||||
ENTRY_POINT com/craftinginterpreters/lox/Lox)
|
ENTRY_POINT com/craftinginterpreters/lox/Lox)
|
||||||
|
|
||||||
get_target_property(_jarFile Lox JAR_FILE)
|
add_jar(GenerateAst
|
||||||
get_target_property(_classDir Lox CLASSDIR)
|
com/craftinginterpreters/tool/GenerateAst.java
|
||||||
|
ENTRY_POINT com/craftinginterpreters/tool/GenerateAst)
|
||||||
|
|
53
src/com/craftinginterpreters/lox/AstPrinter.java
Normal file
53
src/com/craftinginterpreters/lox/AstPrinter.java
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package com.craftinginterpreters.lox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AstPrinter
|
||||||
|
*/
|
||||||
|
class AstPrinter implements Expr.Visitor<String> {
|
||||||
|
String print(Expr expr) {
|
||||||
|
return expr.accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitBinaryExpr(Expr.Binary expr) {
|
||||||
|
return parenthesize(expr.operator.lexeme, expr.left, expr.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitGroupingExpr(Expr.Grouping expr) {
|
||||||
|
return parenthesize("group", expr.expression);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitLiteralExpr(Expr.Literal expr) {
|
||||||
|
if (expr.value == null)
|
||||||
|
return "nil";
|
||||||
|
return expr.value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitUnaryExpr(Expr.Unary expr) {
|
||||||
|
return parenthesize(expr.operator.lexeme, expr.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String parenthesize(String name, Expr... exprs) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
|
builder.append("(").append(name);
|
||||||
|
for (Expr expr : exprs) {
|
||||||
|
builder.append(" ");
|
||||||
|
builder.append(expr.accept(this));
|
||||||
|
}
|
||||||
|
builder.append(")");
|
||||||
|
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Expr expression = new Expr.Binary(
|
||||||
|
new Expr.Unary(new Token(TokenType.MINUS, "-", null, 1), new Expr.Literal(123)),
|
||||||
|
new Token(TokenType.STAR, "*", null, 1), new Expr.Grouping(new Expr.Literal(45.67)));
|
||||||
|
|
||||||
|
System.out.println(new AstPrinter().print(expression));
|
||||||
|
}
|
||||||
|
}
|
88
src/com/craftinginterpreters/tool/GenerateAst.java
Normal file
88
src/com/craftinginterpreters/tool/GenerateAst.java
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
package com.craftinginterpreters.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);
|
||||||
|
|
||||||
|
writer.println("package com.craftinginterpreters.lox;");
|
||||||
|
writer.println();
|
||||||
|
writer.println("import java.util.List;");
|
||||||
|
writer.println();
|
||||||
|
writer.println("abstract class " + baseName + " {");
|
||||||
|
|
||||||
|
defineVisitor(writer, baseName, types);
|
||||||
|
|
||||||
|
for (String type : types) {
|
||||||
|
String className = type.split(":")[0].trim();
|
||||||
|
String fields = type.split(":")[1].trim();
|
||||||
|
defineType(writer, baseName, className, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The base accept() method
|
||||||
|
writer.println();
|
||||||
|
writer.println(" abstract <R> R accept(Visitor<R> visitor);");
|
||||||
|
|
||||||
|
writer.println("}");
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void defineVisitor(PrintWriter writer, String baseName, List<String> types) {
|
||||||
|
writer.println(" interface Visitor<R> {");
|
||||||
|
|
||||||
|
for (String type : types) {
|
||||||
|
String typeName = type.split(":")[0].trim();
|
||||||
|
writer.println(" R visit" + typeName + baseName + "(" + typeName + " " + baseName.toLowerCase() + ");");
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.println(" }");
|
||||||
|
}
|
||||||
|
|
||||||
|
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(" }");
|
||||||
|
|
||||||
|
// Visitor pattern.
|
||||||
|
writer.println();
|
||||||
|
writer.println(" @Override");
|
||||||
|
writer.println(" <R> R accept(Visitor<R> visitor) {");
|
||||||
|
writer.println(" return visitor.visit" + className + baseName + "(this);");
|
||||||
|
writer.println(" }");
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
writer.println();
|
||||||
|
for (String field : fields) {
|
||||||
|
writer.println(" final " + field + ";");
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.println(" }");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue