chapter 9 in java
This commit is contained in:
parent
a629ddca05
commit
1a485ebcb1
@ -12,6 +12,8 @@ abstract class Expr {
|
|||||||
|
|
||||||
R visitLiteralExpr(Literal expr);
|
R visitLiteralExpr(Literal expr);
|
||||||
|
|
||||||
|
R visitLogicalExpr(Logical expr);
|
||||||
|
|
||||||
R visitUnaryExpr(Unary expr);
|
R visitUnaryExpr(Unary expr);
|
||||||
|
|
||||||
R visitVariableExpr(Variable expr);
|
R visitVariableExpr(Variable expr);
|
||||||
@ -75,6 +77,23 @@ abstract class Expr {
|
|||||||
final Object value;
|
final Object value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class Logical extends Expr {
|
||||||
|
Logical(Expr left, Token operator, Expr right) {
|
||||||
|
this.left = left;
|
||||||
|
this.operator = operator;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
<R> R accept(Visitor<R> visitor) {
|
||||||
|
return visitor.visitLogicalExpr(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Expr left;
|
||||||
|
final Token operator;
|
||||||
|
final Expr right;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -24,6 +24,21 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
|
|||||||
return expr.value;
|
return expr.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object visitLogicalExpr(Expr.Logical expr) {
|
||||||
|
Object left = evaluate(expr.left);
|
||||||
|
|
||||||
|
if (expr.operator.type() == TokenType.OR) {
|
||||||
|
if (isTruthy(left))
|
||||||
|
return left;
|
||||||
|
} else {
|
||||||
|
if (!isTruthy(left))
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
return evaluate(expr.right);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitGroupingExpr(Expr.Grouping expr) {
|
public Object visitGroupingExpr(Expr.Grouping expr) {
|
||||||
return evaluate(expr.expression);
|
return evaluate(expr.expression);
|
||||||
@ -67,6 +82,16 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitIfStmt(Stmt.If stmt) {
|
||||||
|
if (isTruthy(evaluate(stmt.condition))) {
|
||||||
|
execute(stmt.thenBranch);
|
||||||
|
} else if (stmt.elseBranch != null) {
|
||||||
|
execute(stmt.elseBranch);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visitPrintStmt(Stmt.Print stmt) {
|
public Void visitPrintStmt(Stmt.Print stmt) {
|
||||||
Object value = evaluate(stmt.expression);
|
Object value = evaluate(stmt.expression);
|
||||||
@ -85,6 +110,14 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitWhileStmt(Stmt.While stmt) {
|
||||||
|
while (isTruthy(evaluate(stmt.condition))) {
|
||||||
|
execute(stmt.body);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visitAssignExpr(Expr.Assign expr) {
|
public Object visitAssignExpr(Expr.Assign expr) {
|
||||||
Object value = evaluate(expr.value);
|
Object value = evaluate(expr.value);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ch.vanwa.lox_interpreter;
|
package ch.vanwa.lox_interpreter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static ch.vanwa.lox_interpreter.TokenType.*;
|
import static ch.vanwa.lox_interpreter.TokenType.*;
|
||||||
@ -69,9 +70,18 @@ class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Stmt statement() {
|
private Stmt statement() {
|
||||||
|
if (match(FOR)) {
|
||||||
|
return forStatement();
|
||||||
|
}
|
||||||
|
if (match(IF)) {
|
||||||
|
return ifStatement();
|
||||||
|
}
|
||||||
if (match(PRINT)) {
|
if (match(PRINT)) {
|
||||||
return printStatement();
|
return printStatement();
|
||||||
}
|
}
|
||||||
|
if (match(WHILE)) {
|
||||||
|
return whileStatement();
|
||||||
|
}
|
||||||
if (match(LEFT_BRACE)) {
|
if (match(LEFT_BRACE)) {
|
||||||
return new Stmt.Block(block());
|
return new Stmt.Block(block());
|
||||||
}
|
}
|
||||||
@ -79,6 +89,65 @@ class Parser {
|
|||||||
return expressionStatement();
|
return expressionStatement();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Stmt forStatement() {
|
||||||
|
consume(LEFT_PAREN, "Expect '(' after 'for'.");
|
||||||
|
|
||||||
|
Stmt initializer;
|
||||||
|
if (match(SEMICOLON)) {
|
||||||
|
initializer = null;
|
||||||
|
} else if (match(VAR)) {
|
||||||
|
initializer = varDeclaration();
|
||||||
|
} else {
|
||||||
|
initializer = expressionStatement();
|
||||||
|
}
|
||||||
|
|
||||||
|
Expr condition = null;
|
||||||
|
if (!check(SEMICOLON)) {
|
||||||
|
condition = expression();
|
||||||
|
}
|
||||||
|
consume(SEMICOLON, "Expect ';' after loop condition.");
|
||||||
|
|
||||||
|
Expr increment = null;
|
||||||
|
if (!check(RIGHT_PAREN)) {
|
||||||
|
increment = expression();
|
||||||
|
}
|
||||||
|
consume(RIGHT_PAREN, "Expect ')' after for clauses.");
|
||||||
|
|
||||||
|
Stmt body = statement();
|
||||||
|
|
||||||
|
if (increment != null) {
|
||||||
|
body = new Stmt.Block(
|
||||||
|
Arrays.asList(
|
||||||
|
body,
|
||||||
|
new Stmt.Expression(increment)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (condition == null) {
|
||||||
|
condition = new Expr.Literal(true);
|
||||||
|
}
|
||||||
|
body = new Stmt.While(condition, body);
|
||||||
|
|
||||||
|
if (initializer != null) {
|
||||||
|
body = new Stmt.Block(Arrays.asList(initializer, body));
|
||||||
|
}
|
||||||
|
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stmt ifStatement() {
|
||||||
|
consume(LEFT_PAREN, "Expect '(' after 'if'.");
|
||||||
|
Expr condition = expression();
|
||||||
|
consume(RIGHT_PAREN, "Expect ')' after if condition.");
|
||||||
|
|
||||||
|
Stmt thenBranch = statement();
|
||||||
|
Stmt elseBranch = null;
|
||||||
|
if (match(ELSE)) {
|
||||||
|
elseBranch = statement();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Stmt.If(condition, thenBranch, elseBranch);
|
||||||
|
}
|
||||||
|
|
||||||
private Stmt printStatement() {
|
private Stmt printStatement() {
|
||||||
Expr value = expression();
|
Expr value = expression();
|
||||||
consume(SEMICOLON, "Expect ';' after value.");
|
consume(SEMICOLON, "Expect ';' after value.");
|
||||||
@ -97,6 +166,15 @@ class Parser {
|
|||||||
return new Stmt.Var(name, initializer);
|
return new Stmt.Var(name, initializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Stmt whileStatement() {
|
||||||
|
consume(LEFT_PAREN, "Expect '(' after 'while'.");
|
||||||
|
Expr condition = expression();
|
||||||
|
consume(RIGHT_PAREN, "Expect ')' after condition.");
|
||||||
|
Stmt body = statement();
|
||||||
|
|
||||||
|
return new Stmt.While(condition, body);
|
||||||
|
}
|
||||||
|
|
||||||
private Stmt expressionStatement() {
|
private Stmt expressionStatement() {
|
||||||
Expr expr = expression();
|
Expr expr = expression();
|
||||||
consume(SEMICOLON, "Expect ';' after expression.");
|
consume(SEMICOLON, "Expect ';' after expression.");
|
||||||
@ -115,7 +193,7 @@ class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Expr assignment() {
|
private Expr assignment() {
|
||||||
Expr expr = equality();
|
Expr expr = or();
|
||||||
|
|
||||||
if (match(EQUAL)) {
|
if (match(EQUAL)) {
|
||||||
Token equals = previous();
|
Token equals = previous();
|
||||||
@ -132,6 +210,30 @@ class Parser {
|
|||||||
return expr;
|
return expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Expr or() {
|
||||||
|
Expr expr = and();
|
||||||
|
|
||||||
|
while (match(OR)) {
|
||||||
|
Token operator = previous();
|
||||||
|
Expr right = and();
|
||||||
|
expr = new Expr.Logical(expr, operator, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Expr and() {
|
||||||
|
Expr expr = equality();
|
||||||
|
|
||||||
|
while (match(AND)) {
|
||||||
|
Token operator = previous();
|
||||||
|
Expr right = equality();
|
||||||
|
expr = new Expr.Logical(expr, operator, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
private Expr equality() {
|
private Expr equality() {
|
||||||
Expr expr = comparison();
|
Expr expr = comparison();
|
||||||
|
|
||||||
|
@ -8,9 +8,13 @@ abstract class Stmt {
|
|||||||
|
|
||||||
R visitExpressionStmt(Expression stmt);
|
R visitExpressionStmt(Expression stmt);
|
||||||
|
|
||||||
|
R visitIfStmt(If stmt);
|
||||||
|
|
||||||
R visitPrintStmt(Print stmt);
|
R visitPrintStmt(Print stmt);
|
||||||
|
|
||||||
R visitVarStmt(Var stmt);
|
R visitVarStmt(Var stmt);
|
||||||
|
|
||||||
|
R visitWhileStmt(While stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Block extends Stmt {
|
static class Block extends Stmt {
|
||||||
@ -39,6 +43,23 @@ abstract class Stmt {
|
|||||||
final Expr expression;
|
final Expr expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class If extends Stmt {
|
||||||
|
If(Expr condition, Stmt thenBranch, Stmt elseBranch) {
|
||||||
|
this.condition = condition;
|
||||||
|
this.thenBranch = thenBranch;
|
||||||
|
this.elseBranch = elseBranch;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
<R> R accept(Visitor<R> visitor) {
|
||||||
|
return visitor.visitIfStmt(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Expr condition;
|
||||||
|
final Stmt thenBranch;
|
||||||
|
final Stmt elseBranch;
|
||||||
|
}
|
||||||
|
|
||||||
static class Print extends Stmt {
|
static class Print extends Stmt {
|
||||||
Print(Expr expression) {
|
Print(Expr expression) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
@ -67,5 +88,20 @@ abstract class Stmt {
|
|||||||
final Expr initializer;
|
final Expr initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class While extends Stmt {
|
||||||
|
While(Expr condition, Stmt body) {
|
||||||
|
this.condition = condition;
|
||||||
|
this.body = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
<R> R accept(Visitor<R> visitor) {
|
||||||
|
return visitor.visitWhileStmt(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
final Expr condition;
|
||||||
|
final Stmt body;
|
||||||
|
}
|
||||||
|
|
||||||
abstract <R> R accept(Visitor<R> visitor);
|
abstract <R> R accept(Visitor<R> visitor);
|
||||||
}
|
}
|
||||||
|
@ -18,14 +18,18 @@ public class GenerateAst {
|
|||||||
"Binary : Expr left, Token operator, Expr right",
|
"Binary : Expr left, Token operator, Expr right",
|
||||||
"Grouping : Expr expression",
|
"Grouping : Expr expression",
|
||||||
"Literal : Object value",
|
"Literal : Object value",
|
||||||
|
"Logical : Expr left, Token operator, Expr right",
|
||||||
"Unary : Token operator, Expr right",
|
"Unary : Token operator, Expr right",
|
||||||
"Variable : Token name"));
|
"Variable : Token name"));
|
||||||
|
|
||||||
defineAst(outputDir, "Stmt", Arrays.asList(
|
defineAst(outputDir, "Stmt", Arrays.asList(
|
||||||
"Block : List<Stmt> statements",
|
"Block : List<Stmt> statements",
|
||||||
"Expression : Expr expression",
|
"Expression : Expr expression",
|
||||||
|
"If : Expr condition, Stmt thenBranch," +
|
||||||
|
" Stmt elseBranch",
|
||||||
"Print : Expr expression",
|
"Print : Expr expression",
|
||||||
"Var : Token name, Expr initializer"));
|
"Var : Token name, Expr initializer",
|
||||||
|
"While : Expr condition, Stmt body"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void defineAst(
|
private static void defineAst(
|
||||||
|
Loading…
Reference in New Issue
Block a user