diff options
author | Tom Willemse | 2021-01-14 23:53:47 -0800 |
---|---|---|
committer | Tom Willemse | 2021-01-14 23:53:47 -0800 |
commit | c62b73a2477b9fa625285e9c2ca1e51c2bcb8024 (patch) | |
tree | d8ea9b22fcdc28d34a1aec9f40a87ddd2dfd9660 /src/com/craftinginterpreters/lox/Interpreter.java | |
parent | ca9fd3ae3a1ef5ca1fbe33a83abb12e656a2556f (diff) | |
download | crafting-interpreters-c62b73a2477b9fa625285e9c2ca1e51c2bcb8024.tar.gz crafting-interpreters-c62b73a2477b9fa625285e9c2ca1e51c2bcb8024.zip |
Add branching primitives
Diffstat (limited to 'src/com/craftinginterpreters/lox/Interpreter.java')
-rw-r--r-- | src/com/craftinginterpreters/lox/Interpreter.java | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java index e03ca37..6c32233 100644 --- a/src/com/craftinginterpreters/lox/Interpreter.java +++ b/src/com/craftinginterpreters/lox/Interpreter.java @@ -11,6 +11,19 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> { } @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 public Object visitUnaryExpr(Expr.Unary expr) { Object right = evaluate(expr.right); @@ -116,6 +129,17 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> { } @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 public Void visitPrintStmt(Stmt.Print stmt) { Object value = evaluate(stmt.expression); System.out.println(stringify(value)); |