[Chapter8&Fix] - Adding missing code and implemeting a little of the AstPrinter
This commit is contained in:
parent
ec614721c6
commit
b4546ceb1c
@ -5,6 +5,23 @@ class AstPrinter implements Expr.Visitor<String> {
|
||||
return expr.accept(this);
|
||||
}
|
||||
@Override
|
||||
public String visitVariableExpr(Expr.Variable expr){
|
||||
if(expr.name.literal == null){
|
||||
return expr.name.lexeme;
|
||||
}
|
||||
return parenthesize(
|
||||
"variable"
|
||||
,new Expr.Literal(expr.name.lexeme)
|
||||
,new Expr.Literal(expr.name.literal));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitAssignExpr(Expr.Assign assign){
|
||||
return parenthesize("set!"
|
||||
,new Expr.Literal(assign.name.lexeme)
|
||||
,assign.value);
|
||||
}
|
||||
@Override
|
||||
public String visitBinaryExpr(Expr.Binary expr) {
|
||||
return parenthesize(expr.operator.lexeme,
|
||||
expr.left, expr.right);
|
||||
@ -37,16 +54,23 @@ class AstPrinter implements Expr.Visitor<String> {
|
||||
|
||||
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));
|
||||
// }
|
||||
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)));
|
||||
Token varToken = new Token(TokenType.IDENTIFIER,"drink","tea",0);
|
||||
Expr varExpr = new Expr.Variable(varToken);
|
||||
Expr assignExpr = new Expr.Assign(
|
||||
varToken
|
||||
,new Expr.Literal("\"cola\"")
|
||||
);
|
||||
System.out.println(new AstPrinter().print(expression));
|
||||
System.out.println(new AstPrinter().print(varExpr));
|
||||
System.out.println(new AstPrinter().print(assignExpr));
|
||||
}
|
||||
|
||||
}
|
@ -4,10 +4,26 @@ import java.util.List;
|
||||
|
||||
abstract class Expr {
|
||||
interface Visitor<R> {
|
||||
R visitAssignExpr(Assign expr);
|
||||
R visitBinaryExpr(Binary expr);
|
||||
R visitGroupingExpr(Grouping expr);
|
||||
R visitLiteralExpr(Literal expr);
|
||||
R visitUnaryExpr(Unary expr);
|
||||
R visitVariableExpr(Variable expr);
|
||||
}
|
||||
static class Assign extends Expr {
|
||||
Assign(Token name, Expr value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitAssignExpr(this);
|
||||
}
|
||||
|
||||
final Token name;
|
||||
final Expr value;
|
||||
}
|
||||
static class Binary extends Expr {
|
||||
Binary(Expr left, Token operator, Expr right) {
|
||||
@ -63,6 +79,18 @@ abstract class Expr {
|
||||
final Token operator;
|
||||
final Expr right;
|
||||
}
|
||||
static class Variable extends Expr {
|
||||
Variable(Token name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitVariableExpr(this);
|
||||
}
|
||||
|
||||
final Token name;
|
||||
}
|
||||
|
||||
abstract <R> R accept(Visitor<R> visitor);
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,22 @@ import java.util.List;
|
||||
|
||||
abstract class Stmt {
|
||||
interface Visitor<R> {
|
||||
R visitBlockStmt(Block stmt);
|
||||
R visitExpressionStmt(Expression stmt);
|
||||
R visitPrintStmt(Print stmt);
|
||||
R visitVarStmt(Var stmt);
|
||||
}
|
||||
static class Block extends Stmt {
|
||||
Block(List<Stmt> statements) {
|
||||
this.statements = statements;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitBlockStmt(this);
|
||||
}
|
||||
|
||||
final List<Stmt> statements;
|
||||
}
|
||||
static class Expression extends Stmt {
|
||||
Expression(Expr expression) {
|
||||
@ -31,6 +45,20 @@ abstract class Stmt {
|
||||
|
||||
final Expr expression;
|
||||
}
|
||||
static class Var extends Stmt {
|
||||
Var(Token name, Expr initializer) {
|
||||
this.name = name;
|
||||
this.initializer = initializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitVarStmt(this);
|
||||
}
|
||||
|
||||
final Token name;
|
||||
final Expr initializer;
|
||||
}
|
||||
|
||||
abstract <R> R accept(Visitor<R> visitor);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user