summaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Interpreter.java
diff options
context:
space:
mode:
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);