Compare commits
2 Commits
fb1a414f90
...
d5470819d5
Author | SHA1 | Date |
---|---|---|
Erik Winter | d5470819d5 | |
Erik Winter | 1771f11749 |
12
Makefile
12
Makefile
|
@ -1,8 +1,14 @@
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build-lox, build-tool
|
||||||
|
|
||||||
build:
|
build-lox:
|
||||||
javac -d build/classes src/com/craftinginterpreters/lox/*
|
javac -d build/classes src/com/craftinginterpreters/lox/*
|
||||||
|
|
||||||
run:
|
build-tool:
|
||||||
|
javac -d build/classes src/com/craftinginterpreters/tool/*
|
||||||
|
|
||||||
|
lox:
|
||||||
java -cp build/classes com.craftinginterpreters.lox.Lox
|
java -cp build/classes com.craftinginterpreters.lox.Lox
|
||||||
|
|
||||||
|
gen-ast:
|
||||||
|
java -cp build/classes com.craftinginterpreters.tool.GenerateAst ./src/com/craftinginterpreters/lox
|
||||||
|
|
11
jlox.iml
11
jlox.iml
|
@ -1,11 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module type="JAVA_MODULE" version="4">
|
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
|
||||||
<exclude-output />
|
|
||||||
<content url="file://$MODULE_DIR$">
|
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
|
||||||
</content>
|
|
||||||
<orderEntry type="inheritedJdk" />
|
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
|
||||||
</component>
|
|
||||||
</module>
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.craftinginterpreters.lox;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,14 @@
|
||||||
package com.craftinginterpreters.lox;
|
package com.craftinginterpreters.lox;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
abstract class Expr {
|
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 {
|
static class Binary extends Expr {
|
||||||
Binary(Expr left, Token operator, Expr right) {
|
Binary(Expr left, Token operator, Expr right) {
|
||||||
this.left = left;
|
this.left = left;
|
||||||
|
@ -8,8 +16,53 @@ abstract class Expr {
|
||||||
this.right = right;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
<R> R accept(Visitor<R> visitor) {
|
||||||
|
return visitor.visitBinaryExpr(this);
|
||||||
|
}
|
||||||
|
|
||||||
final Expr left;
|
final Expr left;
|
||||||
final Token operator;
|
final Token operator;
|
||||||
final Expr right;
|
final 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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
static class Unary extends Expr {
|
||||||
|
Unary(Token operator, Expr right) {
|
||||||
|
this.operator = operator;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
<R> R accept(Visitor<R> visitor) {
|
||||||
|
return visitor.visitUnaryExpr(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Token operator;
|
||||||
|
final Expr right;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract <R> R accept(Visitor<R> visitor);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,12 @@ public class GenerateAst {
|
||||||
|
|
||||||
writer.println("package com.craftinginterpreters.lox;");
|
writer.println("package com.craftinginterpreters.lox;");
|
||||||
writer.println();
|
writer.println();
|
||||||
writer.println("import java.util.list;");
|
writer.println("import java.util.List;");
|
||||||
writer.println();
|
writer.println();
|
||||||
writer.println("abstract class " + baseName + " {");
|
writer.println("abstract class " + baseName + " {");
|
||||||
|
|
||||||
|
defineVisitor(writer, baseName, types);
|
||||||
|
|
||||||
// The AST classes
|
// The AST classes
|
||||||
for (String type : types) {
|
for (String type : types) {
|
||||||
String className = type.split(":")[0].trim();
|
String className = type.split(":")[0].trim();
|
||||||
|
@ -37,9 +39,23 @@ public class GenerateAst {
|
||||||
defineType(writer, baseName, className, fields);
|
defineType(writer, baseName, className, fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the base accept() method
|
||||||
|
writer.println();
|
||||||
|
writer.println(" abstract <R> R accept(Visitor<R> visitor);");
|
||||||
|
|
||||||
writer.println("}");
|
writer.println("}");
|
||||||
writer.close();
|
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) {
|
private static void defineType(PrintWriter writer, String baseName, String className, String fieldList) {
|
||||||
|
@ -49,7 +65,7 @@ public class GenerateAst {
|
||||||
writer.println(" " + className + "(" + fieldList + ") {");
|
writer.println(" " + className + "(" + fieldList + ") {");
|
||||||
|
|
||||||
// Store parameters in fields
|
// Store parameters in fields
|
||||||
String []fields = fieldList.split(",");
|
String []fields = fieldList.split(", ");
|
||||||
for (String field : fields) {
|
for (String field : fields) {
|
||||||
String name = field.split(" ")[1];
|
String name = field.split(" ")[1];
|
||||||
writer.println(" this." + name + " = " + name + ";" );
|
writer.println(" this." + name + " = " + name + ";" );
|
||||||
|
@ -57,10 +73,17 @@ public class GenerateAst {
|
||||||
|
|
||||||
writer.println(" }");
|
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
|
// Fields
|
||||||
writer.println();
|
writer.println();
|
||||||
for (String field : fields) {
|
for (String field : fields) {
|
||||||
writer.println(" final " + field + " = " + field + ";");
|
writer.println(" final " + field + ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.println(" }");
|
writer.println(" }");
|
||||||
|
|
Loading…
Reference in New Issue