[Chapter8&Statements] - Adding support for statments from chapter 7
This commit is contained in:
parent
920afcc25d
commit
ec614721c6
2
run.sh
2
run.sh
@ -5,7 +5,7 @@ javac -d out src/com/lox/*.java
|
|||||||
# Compile Lox tool
|
# Compile Lox tool
|
||||||
javac -d out src/com/tool/*.java
|
javac -d out src/com/tool/*.java
|
||||||
# Run Lox interpreter
|
# Run Lox interpreter
|
||||||
java -cp out com.lox.Lox
|
java -cp out com.lox.Lox $1
|
||||||
# Run Lox ast print printer
|
# Run Lox ast print printer
|
||||||
# java -cp out com.lox.AstPrinter
|
# java -cp out com.lox.AstPrinter
|
||||||
# Run Lox tool
|
# Run Lox tool
|
||||||
|
|||||||
43
src/com/lox/Environment.java
Normal file
43
src/com/lox/Environment.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package com.lox;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class Environment {
|
||||||
|
final Environment enclosing;
|
||||||
|
Environment() {
|
||||||
|
enclosing = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Environment(Environment enclosing) {
|
||||||
|
this.enclosing = enclosing;
|
||||||
|
}
|
||||||
|
private final Map<String, Object> values = new HashMap<>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,9 @@
|
|||||||
package com.lox;
|
package com.lox;
|
||||||
|
|
||||||
class Interpreter implements Expr.Visitor<Object> {
|
import java.util.List;
|
||||||
|
|
||||||
|
class Interpreter implements Expr.Visitor<Object>,Stmt.Visitor<Void> {
|
||||||
|
private Environment environment = new Environment();
|
||||||
@Override
|
@Override
|
||||||
public Object visitLiteralExpr(Expr.Literal expr) {
|
public Object visitLiteralExpr(Expr.Literal expr) {
|
||||||
return expr.value;
|
return expr.value;
|
||||||
@ -20,6 +23,11 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||||||
// Unreachable.
|
// Unreachable.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public Object visitVariableExpr(Expr.Variable expr) {
|
||||||
|
return environment.get(expr.name);
|
||||||
|
}
|
||||||
|
|
||||||
private void checkNumberOperand(Token operator, Object operand) {
|
private void checkNumberOperand(Token operator, Object operand) {
|
||||||
if (operand instanceof Double) return;
|
if (operand instanceof Double) return;
|
||||||
throw new RuntimeError(operator, "Operand must be a number.");
|
throw new RuntimeError(operator, "Operand must be a number.");
|
||||||
@ -79,6 +87,33 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||||||
// Unreachable.
|
// Unreachable.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitExpressionStmt(Stmt.Expression stmt) {
|
||||||
|
evaluate(stmt.expression);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitPrintStmt(Stmt.Print stmt) {
|
||||||
|
Object value = evaluate(stmt.expression);
|
||||||
|
System.out.println(stringify(value));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Void visitVarStmt(Stmt.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(Expr.Assign expr) {
|
||||||
|
Object value = evaluate(expr.value);
|
||||||
|
environment.assign(expr.name, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
private Object evaluate(Expr expr) {
|
private Object evaluate(Expr expr) {
|
||||||
return expr.accept(this);
|
return expr.accept(this);
|
||||||
}
|
}
|
||||||
@ -94,14 +129,38 @@ class Interpreter implements Expr.Visitor<Object> {
|
|||||||
|
|
||||||
return a.equals(b);
|
return a.equals(b);
|
||||||
}
|
}
|
||||||
void interpret(Expr expression) {
|
void interpret(List<Stmt> statements) {
|
||||||
try {
|
try {
|
||||||
Object value = evaluate(expression);
|
for (Stmt statement : statements) {
|
||||||
System.out.println(stringify(value));
|
execute(statement);
|
||||||
|
}
|
||||||
} catch (RuntimeError error) {
|
} catch (RuntimeError error) {
|
||||||
Lox.runtimeError(error);
|
Lox.runtimeError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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(Stmt.Block stmt) {
|
||||||
|
executeBlock(stmt.statements, new Environment(environment));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private String stringify(Object object) {
|
private String stringify(Object object) {
|
||||||
if (object == null) return "nil";
|
if (object == null) return "nil";
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ public class Lox {
|
|||||||
private static final Interpreter interpreter = new Interpreter();
|
private static final Interpreter interpreter = new Interpreter();
|
||||||
static boolean hadError = false;
|
static boolean hadError = false;
|
||||||
static boolean hadRuntimeError = false;
|
static boolean hadRuntimeError = false;
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
if (args.length > 1) {
|
if (args.length > 1) {
|
||||||
System.out.println("Usage: jlox [script]");
|
System.out.println("Usage: jlox [script]");
|
||||||
System.exit(64);
|
System.exit(64);
|
||||||
@ -27,11 +27,12 @@ public class Lox {
|
|||||||
Scanner scanner = new Scanner(source);
|
Scanner scanner = new Scanner(source);
|
||||||
List<Token> tokens = scanner.scanTokens();
|
List<Token> tokens = scanner.scanTokens();
|
||||||
Parser parser = new Parser(tokens);
|
Parser parser = new Parser(tokens);
|
||||||
Expr expression = parser.parse();
|
List<Stmt> statements = parser.parse();
|
||||||
|
|
||||||
// Stop if there was a syntax error.
|
// Stop if there was a syntax error.
|
||||||
if (hadError) return;
|
if (hadError) return;
|
||||||
interpreter.interpret(expression);
|
|
||||||
|
interpreter.interpret(statements);
|
||||||
}
|
}
|
||||||
static void error(int line, String message) {
|
static void error(int line, String message) {
|
||||||
report(line, "", message);
|
report(line, "", message);
|
||||||
@ -59,6 +60,7 @@ public class Lox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void runFile(String path) throws IOException {
|
private static void runFile(String path) throws IOException {
|
||||||
|
|
||||||
byte[] bytes = Files.readAllBytes(Paths.get(path));
|
byte[] bytes = Files.readAllBytes(Paths.get(path));
|
||||||
run(new String(bytes, Charset.defaultCharset()));
|
run(new String(bytes, Charset.defaultCharset()));
|
||||||
if(hadError) System.exit(65);
|
if(hadError) System.exit(65);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package com.lox;
|
package com.lox;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
import static com.lox.TokenType.*;
|
import static com.lox.TokenType.*;
|
||||||
|
|
||||||
class Parser {
|
class Parser {
|
||||||
@ -12,15 +12,81 @@ class Parser {
|
|||||||
Parser(List<Token> tokens) {
|
Parser(List<Token> tokens) {
|
||||||
this.tokens = tokens;
|
this.tokens = tokens;
|
||||||
}
|
}
|
||||||
Expr parse() {
|
List<Stmt> parse() {
|
||||||
|
List<Stmt> statements = new ArrayList<>();
|
||||||
|
while (!isAtEnd()) {
|
||||||
|
statements.add(declaration());
|
||||||
|
}
|
||||||
|
|
||||||
|
return statements;
|
||||||
|
}
|
||||||
|
private Stmt declaration() {
|
||||||
try {
|
try {
|
||||||
return expression();
|
if (match(VAR)) return varDeclaration();
|
||||||
} catch (ParseError error) {
|
|
||||||
|
return statement();
|
||||||
|
} catch (ParseError error) {
|
||||||
|
synchronize();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private Expr expression(){
|
private Expr expression(){
|
||||||
return equality();
|
return assignment();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stmt statement() {
|
||||||
|
if (match(PRINT)) return printStatement();
|
||||||
|
if (match(LEFT_BRACE)) return new Stmt.Block(block());
|
||||||
|
return expressionStatement();
|
||||||
|
}
|
||||||
|
private Stmt printStatement() {
|
||||||
|
Expr value = expression();
|
||||||
|
consume(SEMICOLON, "Expect ';' after value.");
|
||||||
|
return new Stmt.Print(value);
|
||||||
|
}
|
||||||
|
private Stmt varDeclaration() {
|
||||||
|
Token name = consume(IDENTIFIER, "Expect variable name.");
|
||||||
|
|
||||||
|
Expr initializer = null;
|
||||||
|
if (match(EQUAL)) {
|
||||||
|
initializer = expression();
|
||||||
|
}
|
||||||
|
|
||||||
|
consume(SEMICOLON, "Expect ';' after variable declaration.");
|
||||||
|
return new Stmt.Var(name, initializer);
|
||||||
|
}
|
||||||
|
private Stmt expressionStatement() {
|
||||||
|
Expr expr = expression();
|
||||||
|
consume(SEMICOLON, "Expect ';' after expression.");
|
||||||
|
return new Stmt.Expression(expr);
|
||||||
|
}
|
||||||
|
private List<Stmt> block() {
|
||||||
|
List<Stmt> statements = new ArrayList<>();
|
||||||
|
|
||||||
|
while (!check(RIGHT_BRACE) && !isAtEnd()) {
|
||||||
|
statements.add(declaration());
|
||||||
|
}
|
||||||
|
|
||||||
|
consume(RIGHT_BRACE, "Expect '}' after block.");
|
||||||
|
return statements;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Expr assignment() {
|
||||||
|
Expr expr = equality();
|
||||||
|
|
||||||
|
if (match(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 assignment target.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return expr;
|
||||||
}
|
}
|
||||||
private Expr equality() {
|
private Expr equality() {
|
||||||
Expr expr = comparison();
|
Expr expr = comparison();
|
||||||
@ -91,6 +157,11 @@ class Parser {
|
|||||||
consume(RIGHT_PAREN, "Expect ')' after expression.");
|
consume(RIGHT_PAREN, "Expect ')' after expression.");
|
||||||
return new Expr.Grouping(expr);
|
return new Expr.Grouping(expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (match(IDENTIFIER)) {
|
||||||
|
return new Expr.Variable(previous());
|
||||||
|
}
|
||||||
|
|
||||||
throw error(peek(), "Expect expression.");
|
throw error(peek(), "Expect expression.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
src/com/lox/Stmt.java
Normal file
36
src/com/lox/Stmt.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.lox;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
abstract class Stmt {
|
||||||
|
interface Visitor<R> {
|
||||||
|
R visitExpressionStmt(Expression stmt);
|
||||||
|
R visitPrintStmt(Print stmt);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract <R> R accept(Visitor<R> visitor);
|
||||||
|
}
|
||||||
@ -13,11 +13,19 @@ public class GenerateAst {
|
|||||||
}
|
}
|
||||||
String outputDir = args[0];
|
String outputDir = args[0];
|
||||||
defineAst(outputDir, "Expr", Arrays.asList(
|
defineAst(outputDir, "Expr", Arrays.asList(
|
||||||
"Binary : Expr left, Token operator, Expr right",
|
"Assign : Token name, Expr value",
|
||||||
"Grouping : Expr expression",
|
"Binary : Expr left, Token operator, Expr right",
|
||||||
"Literal : Object value",
|
"Grouping : Expr expression",
|
||||||
"Unary : Token operator, Expr right"
|
"Literal : Object value",
|
||||||
));
|
"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(
|
private static void defineAst(
|
||||||
String outputDir, String baseName, List<String> types)
|
String outputDir, String baseName, List<String> types)
|
||||||
|
|||||||
3
tests/print_tests.lox
Normal file
3
tests/print_tests.lox
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
print "Hi!";
|
||||||
|
print true;
|
||||||
|
print 1 + 2;
|
||||||
Loading…
Reference in New Issue
Block a user