Compare commits

..

No commits in common. "d5470819d56a66000fa42dc60c5e550ab9eee291" and "fb1a414f900e946121c09a4853b1596832fcc850" have entirely different histories.

5 changed files with 29 additions and 151 deletions

View File

@ -1,14 +1,8 @@
.PHONY: build-lox, build-tool .PHONY: build
build-lox: build:
javac -d build/classes src/com/craftinginterpreters/lox/* javac -d build/classes src/com/craftinginterpreters/lox/*
build-tool: run:
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 Normal file
View File

@ -0,0 +1,11 @@
<?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>

View File

@ -1,51 +0,0 @@
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));
}
}

View File

@ -1,14 +1,6 @@
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;
@ -16,53 +8,8 @@ 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);
} }

View File

@ -26,12 +26,10 @@ 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();
@ -39,23 +37,9 @@ 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) {
@ -65,7 +49,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 + ";" );
@ -73,17 +57,10 @@ 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 + ";"); writer.println(" final " + field + " = " + field + ";");
} }
writer.println(" }"); writer.println(" }");