diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java index 24cf1c0..b8ba090 100644 --- a/src/com/craftinginterpreters/lox/Interpreter.java +++ b/src/com/craftinginterpreters/lox/Interpreter.java @@ -1,6 +1,8 @@ package com.craftinginterpreters.lox; -class Interpreter implements Expr.Visitor { +import java.util.List; + +class Interpreter implements Expr.Visitor, Stmt.Visitor { @Override public Object visitLiteralExpr(Expr.Literal expr) { return expr.value; @@ -76,6 +78,23 @@ class Interpreter implements Expr.Visitor { return expr.accept(this); } + private void execute(Stmt stmt) { + stmt.accept(this); + } + + @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 Object visitBinaryExpr(Expr.Binary expr) { Object left = evaluate(expr.left); @@ -123,10 +142,11 @@ class Interpreter implements Expr.Visitor { return null; } - public void interpret(Expr expression) { + public void interpret(List statements) { try { - Object value = evaluate(expression); - System.out.println(stringify(value)); + for (Stmt statement : statements) { + execute(statement); + } } catch (RuntimeError error) { Lox.runtimeError(error); } diff --git a/src/com/craftinginterpreters/lox/Lox.java b/src/com/craftinginterpreters/lox/Lox.java index a3334e3..bca117e 100644 --- a/src/com/craftinginterpreters/lox/Lox.java +++ b/src/com/craftinginterpreters/lox/Lox.java @@ -53,13 +53,13 @@ public class Lox { Scanner scanner = new Scanner(source); List tokens = scanner.scanTokens(); Parser parser = new Parser(tokens); - Expr expression = parser.parse(); + List statements = parser.parse(); // Stop if there was a syntax error if (hadError) return; - interpreter.interpret(expression); + interpreter.interpret(statements); } public static void error(int line, String message) { diff --git a/src/com/craftinginterpreters/lox/Parser.java b/src/com/craftinginterpreters/lox/Parser.java index c5e50e0..c68316f 100644 --- a/src/com/craftinginterpreters/lox/Parser.java +++ b/src/com/craftinginterpreters/lox/Parser.java @@ -2,6 +2,7 @@ package com.craftinginterpreters.lox; import static com.craftinginterpreters.lox.TokenType.*; +import java.util.ArrayList; import java.util.List; class Parser { @@ -15,18 +16,39 @@ class Parser { this.tokens = tokens; } - public Expr parse() { - try { - return expression(); - } catch (ParseError error) { - return null; + public List parse() { + List statements = new ArrayList<>(); + + while (!isAtEnd()) { + statements.add(statement()); } + + return statements; } private Expr expression() { return equality(); } + private Stmt statement() { + if (match(PRINT)) + return printStatement(); + + return expressionStatement(); + } + + private Stmt printStatement() { + Expr value = expression(); + consume(SEMICOLON, "Expect ';' after value."); + return new Stmt.Print(value); + } + + private Stmt expressionStatement() { + Expr expr = expression(); + consume(SEMICOLON, "Expect ';' after expression."); + return new Stmt.Expression(expr); + } + private Expr equality() { Expr expr = comparison();