5.3
This commit is contained in:
parent
fb1a414f90
commit
1771f11749
6
Makefile
6
Makefile
|
@ -3,6 +3,10 @@
|
|||
|
||||
build:
|
||||
javac -d build/classes src/com/craftinginterpreters/lox/*
|
||||
javac -d build/classes src/com/craftinginterpreters/tool/*
|
||||
|
||||
run:
|
||||
lox:
|
||||
java -cp build/classes com.craftinginterpreters.lox.Lox
|
||||
|
||||
gen-ast:
|
||||
java -cp build/classes com.craftinginterpreters.tool.GenerateAst ./src/com/craftinginterpreters/lox
|
||||
|
|
|
@ -1,15 +1,68 @@
|
|||
package com.craftinginterpreters.lox;
|
||||
|
||||
import java.util.list;
|
||||
|
||||
abstract class Expr {
|
||||
interface Visitor<R> {
|
||||
R visitBinaryExpr(Binary expr);
|
||||
R visitGroupingExpr(Grouping expr);
|
||||
R visitLiteralExpr(Literal expr);
|
||||
R visitUnaryExpr(Unary expr);
|
||||
}
|
||||
static class Binary extends Expr {
|
||||
Binary(Expr left, Token operator, Expr right) {
|
||||
this.left = left;
|
||||
this.operator = operator;
|
||||
this.right = right;
|
||||
this.Token = Token;
|
||||
this.Expr = Expr;
|
||||
}
|
||||
|
||||
final Expr left;
|
||||
final Token operator;
|
||||
final Expr right;
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitBinaryExpr(this);
|
||||
}
|
||||
|
||||
final Expr left = Expr left;
|
||||
final Token operator = Token operator;
|
||||
final Expr right = Expr right;
|
||||
}
|
||||
static class Grouping extends Expr {
|
||||
Grouping(Expr expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitGroupingExpr(this);
|
||||
}
|
||||
|
||||
final Expr expression = Expr expression;
|
||||
}
|
||||
static class Literal extends Expr {
|
||||
Literal(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitLiteralExpr(this);
|
||||
}
|
||||
|
||||
final Object value = Object value;
|
||||
}
|
||||
static class Unary extends Expr {
|
||||
Unary(Token operator, Expr right) {
|
||||
this.operator = operator;
|
||||
this.Expr = Expr;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitUnaryExpr(this);
|
||||
}
|
||||
|
||||
final Token operator = Token operator;
|
||||
final Expr right = Expr right;
|
||||
}
|
||||
|
||||
abstract <R> R accept(Visitor<R> visitor);
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@ public class GenerateAst {
|
|||
writer.println();
|
||||
writer.println("abstract class " + baseName + " {");
|
||||
|
||||
defineVisitor(writer, baseName, types);
|
||||
|
||||
// The AST classes
|
||||
for (String type : types) {
|
||||
String className = type.split(":")[0].trim();
|
||||
|
@ -37,9 +39,23 @@ public class GenerateAst {
|
|||
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) {
|
||||
|
@ -57,6 +73,13 @@ public class GenerateAst {
|
|||
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue