Chapter 8: Statements and State
This commit is contained in:
parent
565b7f9524
commit
48fb99a350
@ -1,5 +1,7 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
import com.craftinginterpreters.lox.Expr.Variable;
|
||||
|
||||
class AstPrinter implements Expr.Visitor<String> {
|
||||
String print(Expr expr) {
|
||||
return expr.accept(this);
|
||||
@ -52,4 +54,10 @@ class AstPrinter implements Expr.Visitor<String> {
|
||||
System.out.println(new AstPrinter().print(expression));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitVariableExpr(Variable expr) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class Environment {
|
||||
final Environment enclosing;
|
||||
private final Map<String, Object> values = new HashMap<>();
|
||||
|
||||
Environment() {
|
||||
enclosing = null;
|
||||
}
|
||||
|
||||
Environment(Environment enclosing) {
|
||||
this.enclosing = enclosing;
|
||||
}
|
||||
|
||||
Object get(Token name) {
|
||||
if (values.containsKey(name.lexeme)) {
|
||||
return values.get(name.lexeme);
|
||||
}
|
||||
|
||||
if (enclosing != null) {
|
||||
return enclosing.get(name);
|
||||
}
|
||||
|
||||
throw new RuntimeError(name, "Undefined variable '" + name.lexeme + "'.");
|
||||
}
|
||||
|
||||
void assign(Token name, Object value) {
|
||||
if (values.containsKey(name.lexeme)) {
|
||||
values.put(name.lexeme, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (enclosing != null) {
|
||||
enclosing.assign(name, value);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new RuntimeError(name, "Undefined variable '" + name.lexeme + "'.");
|
||||
}
|
||||
|
||||
void define(String name, Object value) {
|
||||
values.put(name, value);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -1,21 +1,95 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.craftinginterpreters.lox.Expr.Assign;
|
||||
import com.craftinginterpreters.lox.Expr.Binary;
|
||||
import com.craftinginterpreters.lox.Expr.Grouping;
|
||||
import com.craftinginterpreters.lox.Expr.Literal;
|
||||
import com.craftinginterpreters.lox.Expr.Unary;
|
||||
import com.craftinginterpreters.lox.Expr.Variable;
|
||||
import com.craftinginterpreters.lox.Stmt.Block;
|
||||
import com.craftinginterpreters.lox.Stmt.Expression;
|
||||
import com.craftinginterpreters.lox.Stmt.Print;
|
||||
import com.craftinginterpreters.lox.Stmt.Var;
|
||||
|
||||
class Interpreter implements Expr.Visitor<Object> {
|
||||
class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
|
||||
private Environment environment = new Environment();
|
||||
|
||||
void interpret(Expr expression) {
|
||||
void interpret(List<Stmt> statements) {
|
||||
try {
|
||||
Object value = evaluate(expression);
|
||||
System.out.println(stringify(value));
|
||||
for (Stmt statement : statements) {
|
||||
execute(statement);
|
||||
}
|
||||
} catch (RuntimeError error) {
|
||||
Lox.runtimeError(error);
|
||||
}
|
||||
}
|
||||
|
||||
private Object evaluate(Expr expr) {
|
||||
return expr.accept(this);
|
||||
}
|
||||
|
||||
private void execute(Stmt stmt) {
|
||||
stmt.accept(this);
|
||||
}
|
||||
|
||||
void executeBlock(List<Stmt> statements, Environment environment) {
|
||||
Environment previous = this.environment;
|
||||
|
||||
try {
|
||||
this.environment = environment;
|
||||
|
||||
for (Stmt statement : statements) {
|
||||
execute(statement);
|
||||
}
|
||||
} finally {
|
||||
this.environment = previous;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitBlockStmt(Block stmt) {
|
||||
executeBlock(stmt.statements, new Environment(environment));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitExpressionStmt(Expression stmt) {
|
||||
evaluate(stmt.expression);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitPrintStmt(Print stmt) {
|
||||
Object value = evaluate(stmt.expression);
|
||||
System.out.println(stringify(value));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visitVariableExpr(Variable expr) {
|
||||
return environment.get(expr.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitVarStmt(Var stmt) {
|
||||
Object value = null;
|
||||
if (stmt.initializer != null) {
|
||||
value = evaluate(stmt.initializer);
|
||||
}
|
||||
|
||||
environment.define(stmt.name.lexeme, value);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visitAssignExpr(Assign expr) {
|
||||
Object value = evaluate(expr.value);
|
||||
environment.assign(expr.name, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visitBinaryExpr(Binary expr) {
|
||||
Object left = evaluate(expr.left);
|
||||
@ -137,9 +211,4 @@ class Interpreter implements Expr.Visitor<Object> {
|
||||
|
||||
return object.toString();
|
||||
}
|
||||
|
||||
private Object evaluate(Expr expr) {
|
||||
return expr.accept(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -55,11 +55,11 @@ public class Lox {
|
||||
List<Token> tokens = scanner.scanTokens();
|
||||
|
||||
Parser parser = new Parser(tokens);
|
||||
Expr expression = parser.parse();
|
||||
List<Stmt> statements = parser.parse();
|
||||
|
||||
if (hadError) return;
|
||||
|
||||
interpreter.interpret(expression);
|
||||
interpreter.interpret(statements);
|
||||
}
|
||||
|
||||
static void error(int line, String message) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Parser {
|
||||
@ -13,16 +14,96 @@ class Parser {
|
||||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
Expr parse() {
|
||||
List<Stmt> parse() {
|
||||
List<Stmt> statements = new ArrayList<>();
|
||||
|
||||
while (!isAtEnd()) {
|
||||
statements.add(declaration());
|
||||
}
|
||||
|
||||
return statements;
|
||||
}
|
||||
|
||||
private Expr expression() {
|
||||
return assignment();
|
||||
}
|
||||
|
||||
private Stmt declaration() {
|
||||
try {
|
||||
return expression();
|
||||
if (match(TokenType.VAR)) {
|
||||
return varDeclaration();
|
||||
}
|
||||
|
||||
return statement();
|
||||
} catch (ParseError error) {
|
||||
synchronize();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Expr expression() {
|
||||
return equality();
|
||||
private Stmt statement() {
|
||||
if (match(TokenType.PRINT)) {
|
||||
return printStatement();
|
||||
}
|
||||
|
||||
if (match(TokenType.LEFT_BRACE)) {
|
||||
return new Stmt.Block(block());
|
||||
}
|
||||
|
||||
return expressionStatement();
|
||||
}
|
||||
|
||||
private Stmt printStatement() {
|
||||
Expr value = expression();
|
||||
consume(TokenType.SEMICOLON, "Expect ';' after value.");
|
||||
return new Stmt.Print(value);
|
||||
}
|
||||
|
||||
private Stmt varDeclaration() {
|
||||
Token name = consume(TokenType.IDENTIFIER, "Expect variable name.");
|
||||
|
||||
Expr initializer = null;
|
||||
if (match(TokenType.EQUAL)) {
|
||||
initializer = expression();
|
||||
}
|
||||
|
||||
consume(TokenType.SEMICOLON, "Expect ';' after variable declaration.");
|
||||
return new Stmt.Var(name, initializer);
|
||||
}
|
||||
|
||||
private Stmt expressionStatement() {
|
||||
Expr expr = expression();
|
||||
consume(TokenType.SEMICOLON, "Expect ';' after expression.");
|
||||
return new Stmt.Expression(expr);
|
||||
}
|
||||
|
||||
private List<Stmt> block() {
|
||||
List<Stmt> statements = new ArrayList<>();
|
||||
|
||||
while (!check(TokenType.RIGHT_BRACE) && !isAtEnd()) {
|
||||
statements.add(declaration());
|
||||
}
|
||||
|
||||
consume(TokenType.RIGHT_BRACE, "Expect '}' after block.");
|
||||
return statements;
|
||||
}
|
||||
|
||||
private Expr assignment() {
|
||||
Expr expr = equality();
|
||||
|
||||
if (match(TokenType.EQUAL)) {
|
||||
Token equals = previous();
|
||||
Expr value = assignment();
|
||||
|
||||
if (expr instanceof Expr.Variable) {
|
||||
Token name = ((Expr.Variable)expr).name;
|
||||
return new Expr.Assign(name, value);
|
||||
}
|
||||
|
||||
error(equals, "Invalid assigment target.");
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
private Expr equality() {
|
||||
@ -95,6 +176,10 @@ class Parser {
|
||||
return new Expr.Literal(previous().literal);
|
||||
}
|
||||
|
||||
if (match(TokenType.IDENTIFIER)) {
|
||||
return new Expr.Variable(previous());
|
||||
}
|
||||
|
||||
if (match(TokenType.LEFT_PAREN)) {
|
||||
Expr expr = expression();
|
||||
consume(TokenType.RIGHT_PAREN, "Expect ')' after expression.");
|
||||
|
64
jlox/src/main/java/com/craftinginterpreters/lox/Stmt.java
Normal file
64
jlox/src/main/java/com/craftinginterpreters/lox/Stmt.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.craftinginterpreters.lox;
|
||||
|
||||
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) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitExpressionStmt(this);
|
||||
}
|
||||
|
||||
final Expr expression;
|
||||
}
|
||||
static class Print extends Stmt {
|
||||
Print(Expr expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
<R> R accept(Visitor<R> visitor) {
|
||||
return visitor.visitPrintStmt(this);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
@ -13,10 +13,18 @@ public class GenerateAst {
|
||||
}
|
||||
String outputDir = args[0];
|
||||
defineAst(outputDir, "Expr", Arrays.asList(
|
||||
"Assign : Token name, Expr value",
|
||||
"Binary : Expr left, Token operator, Expr right",
|
||||
"Grouping : Expr expression",
|
||||
"Literal : Object value",
|
||||
"Unary : Token operator, Expr right"));
|
||||
"Unary : Token operator, Expr right",
|
||||
"Variable : Token name"));
|
||||
|
||||
defineAst(outputDir, "Stmt", Arrays.asList(
|
||||
"Block : List<Stmt> statements",
|
||||
"Expression : Expr expression",
|
||||
"Print : Expr expression",
|
||||
"Var : Token name, Expr initializer"));
|
||||
}
|
||||
|
||||
private static void defineAst(String outputDir, String baseName, List<String> types) throws IOException {
|
||||
|
19
tests/test1.lox
Normal file
19
tests/test1.lox
Normal file
@ -0,0 +1,19 @@
|
||||
var a = "global a";
|
||||
var b = "global b";
|
||||
var c = "global c";
|
||||
{
|
||||
var a = "outer a";
|
||||
var b = "outer b";
|
||||
{
|
||||
var a = "inner a";
|
||||
print a;
|
||||
print b;
|
||||
print c;
|
||||
}
|
||||
print a;
|
||||
print b;
|
||||
print c;
|
||||
}
|
||||
print a;
|
||||
print b;
|
||||
print c;
|
Loading…
x
Reference in New Issue
Block a user