5.4
This commit is contained in:
parent
1771f11749
commit
d5470819d5
6
Makefile
6
Makefile
|
@ -1,8 +1,10 @@
|
||||||
|
|
||||||
.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/*
|
||||||
|
|
||||||
|
build-tool:
|
||||||
javac -d build/classes src/com/craftinginterpreters/tool/*
|
javac -d build/classes src/com/craftinginterpreters/tool/*
|
||||||
|
|
||||||
lox:
|
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,6 @@
|
||||||
package com.craftinginterpreters.lox;
|
package com.craftinginterpreters.lox;
|
||||||
|
|
||||||
import java.util.list;
|
import java.util.List;
|
||||||
|
|
||||||
abstract class Expr {
|
abstract class Expr {
|
||||||
interface Visitor<R> {
|
interface Visitor<R> {
|
||||||
|
@ -12,8 +12,8 @@ abstract class 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;
|
||||||
this.Token = Token;
|
this.operator = operator;
|
||||||
this.Expr = Expr;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -21,9 +21,9 @@ abstract class Expr {
|
||||||
return visitor.visitBinaryExpr(this);
|
return visitor.visitBinaryExpr(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Expr left = Expr left;
|
final Expr left;
|
||||||
final Token operator = Token operator;
|
final Token operator;
|
||||||
final Expr right = Expr right;
|
final Expr right;
|
||||||
}
|
}
|
||||||
static class Grouping extends Expr {
|
static class Grouping extends Expr {
|
||||||
Grouping(Expr expression) {
|
Grouping(Expr expression) {
|
||||||
|
@ -35,7 +35,7 @@ abstract class Expr {
|
||||||
return visitor.visitGroupingExpr(this);
|
return visitor.visitGroupingExpr(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Expr expression = Expr expression;
|
final Expr expression;
|
||||||
}
|
}
|
||||||
static class Literal extends Expr {
|
static class Literal extends Expr {
|
||||||
Literal(Object value) {
|
Literal(Object value) {
|
||||||
|
@ -47,12 +47,12 @@ abstract class Expr {
|
||||||
return visitor.visitLiteralExpr(this);
|
return visitor.visitLiteralExpr(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Object value = Object value;
|
final Object value;
|
||||||
}
|
}
|
||||||
static class Unary extends Expr {
|
static class Unary extends Expr {
|
||||||
Unary(Token operator, Expr right) {
|
Unary(Token operator, Expr right) {
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
this.Expr = Expr;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -60,8 +60,8 @@ abstract class Expr {
|
||||||
return visitor.visitUnaryExpr(this);
|
return visitor.visitUnaryExpr(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Token operator = Token operator;
|
final Token operator;
|
||||||
final Expr right = Expr right;
|
final Expr right;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract <R> R accept(Visitor<R> visitor);
|
abstract <R> R accept(Visitor<R> visitor);
|
||||||
|
|
|
@ -26,7 +26,7 @@ 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 + " {");
|
||||||
|
|
||||||
|
@ -65,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 + ";" );
|
||||||
|
@ -83,7 +83,7 @@ public class GenerateAst {
|
||||||
// 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