diff --git a/Makefile b/Makefile index 7daf5ce..0e5346e 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ -.PHONY: build +.PHONY: build-lox, build-tool -build: +build-lox: javac -d build/classes src/com/craftinginterpreters/lox/* + +build-tool: javac -d build/classes src/com/craftinginterpreters/tool/* - + lox: java -cp build/classes com.craftinginterpreters.lox.Lox diff --git a/jlox.iml b/jlox.iml deleted file mode 100644 index c90834f..0000000 --- a/jlox.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/src/com/craftinginterpreters/lox/AstPrinter.java b/src/com/craftinginterpreters/lox/AstPrinter.java new file mode 100644 index 0000000..5118e9d --- /dev/null +++ b/src/com/craftinginterpreters/lox/AstPrinter.java @@ -0,0 +1,51 @@ +package com.craftinginterpreters.lox; + +class AstPrinter implements Expr.Visitor { + 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)); + } +} diff --git a/src/com/craftinginterpreters/lox/Expr.java b/src/com/craftinginterpreters/lox/Expr.java index 24d97fe..8be1d39 100644 --- a/src/com/craftinginterpreters/lox/Expr.java +++ b/src/com/craftinginterpreters/lox/Expr.java @@ -1,6 +1,6 @@ package com.craftinginterpreters.lox; -import java.util.list; +import java.util.List; abstract class Expr { interface Visitor { @@ -12,8 +12,8 @@ abstract class Expr { static class Binary extends Expr { Binary(Expr left, Token operator, Expr right) { this.left = left; - this.Token = Token; - this.Expr = Expr; + this.operator = operator; + this.right = right; } @Override @@ -21,9 +21,9 @@ abstract class Expr { return visitor.visitBinaryExpr(this); } - final Expr left = Expr left; - final Token operator = Token operator; - final Expr right = Expr right; + final Expr left; + final Token operator; + final Expr right; } static class Grouping extends Expr { Grouping(Expr expression) { @@ -35,7 +35,7 @@ abstract class Expr { return visitor.visitGroupingExpr(this); } - final Expr expression = Expr expression; + final Expr expression; } static class Literal extends Expr { Literal(Object value) { @@ -47,12 +47,12 @@ abstract class Expr { return visitor.visitLiteralExpr(this); } - final Object value = Object value; + final Object value; } static class Unary extends Expr { Unary(Token operator, Expr right) { this.operator = operator; - this.Expr = Expr; + this.right = right; } @Override @@ -60,8 +60,8 @@ abstract class Expr { return visitor.visitUnaryExpr(this); } - final Token operator = Token operator; - final Expr right = Expr right; + final Token operator; + final Expr right; } abstract R accept(Visitor visitor); diff --git a/src/com/craftinginterpreters/tool/GenerateAst.java b/src/com/craftinginterpreters/tool/GenerateAst.java index 55b1b1a..820af01 100644 --- a/src/com/craftinginterpreters/tool/GenerateAst.java +++ b/src/com/craftinginterpreters/tool/GenerateAst.java @@ -26,7 +26,7 @@ public class GenerateAst { writer.println("package com.craftinginterpreters.lox;"); writer.println(); - writer.println("import java.util.list;"); + writer.println("import java.util.List;"); writer.println(); writer.println("abstract class " + baseName + " {"); @@ -65,7 +65,7 @@ public class GenerateAst { writer.println(" " + className + "(" + fieldList + ") {"); // Store parameters in fields - String []fields = fieldList.split(","); + String []fields = fieldList.split(", "); for (String field : fields) { String name = field.split(" ")[1]; writer.println(" this." + name + " = " + name + ";" ); @@ -83,7 +83,7 @@ public class GenerateAst { // Fields writer.println(); for (String field : fields) { - writer.println(" final " + field + " = " + field + ";"); + writer.println(" final " + field + ";"); } writer.println(" }");