diff options
Diffstat (limited to 'src/com/craftinginterpreters/lox/Interpreter.java')
-rw-r--r-- | src/com/craftinginterpreters/lox/Interpreter.java | 28 |
1 files changed, 24 insertions, 4 deletions
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<Object> { +import java.util.List; + +class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> { @Override public Object visitLiteralExpr(Expr.Literal expr) { return expr.value; @@ -76,6 +78,23 @@ class Interpreter implements Expr.Visitor<Object> { 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<Object> { return null; } - public void interpret(Expr expression) { + public 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); } |