aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Interpreter.java
diff options
context:
space:
mode:
authorGravatar Tom Willemse2020-11-18 23:30:46 -0800
committerGravatar Tom Willemse2020-11-18 23:30:46 -0800
commit899ecea236288b5ebc7795c204fdad59b3276002 (patch)
tree00d8cdfa98f265455060279e9c98c10279baf9a6 /src/com/craftinginterpreters/lox/Interpreter.java
parent8019f6aa414f5126ebbcd3afc44bcebc16855d5f (diff)
downloadcrafting-interpreters-899ecea236288b5ebc7795c204fdad59b3276002.tar.gz
crafting-interpreters-899ecea236288b5ebc7795c204fdad59b3276002.zip
Chapter 8: Add variable declarations
Diffstat (limited to 'src/com/craftinginterpreters/lox/Interpreter.java')
-rw-r--r--src/com/craftinginterpreters/lox/Interpreter.java18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java
index b8ba090..f4e7f99 100644
--- a/src/com/craftinginterpreters/lox/Interpreter.java
+++ b/src/com/craftinginterpreters/lox/Interpreter.java
@@ -3,6 +3,8 @@ package com.craftinginterpreters.lox;
import java.util.List;
class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
+ private Environment environment = new Environment();
+
@Override
public Object visitLiteralExpr(Expr.Literal expr) {
return expr.value;
@@ -24,6 +26,11 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
return null;
}
+ @Override
+ public Object visitVariableExpr(Expr.Variable expr) {
+ return environment.get(expr.name);
+ }
+
private void checkNumberOperand(Token operator, Object operand) {
if (operand instanceof Double)
return;
@@ -96,6 +103,17 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
}
@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 visitBinaryExpr(Expr.Binary expr) {
Object left = evaluate(expr.left);
Object right = evaluate(expr.right);