|
|
@ -1,5 +1,12 @@
|
|
|
|
|
|
|
|
require_relative "environment"
|
|
|
|
|
|
|
|
|
|
|
|
module Lox
|
|
|
|
module Lox
|
|
|
|
class Interpreter
|
|
|
|
class Interpreter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
|
|
|
@env = Environment.new
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# The book does printing and error catching here, but
|
|
|
|
# The book does printing and error catching here, but
|
|
|
|
# we're going to do it in the runner instead.
|
|
|
|
# we're going to do it in the runner instead.
|
|
|
|
def interpret(stmts)
|
|
|
|
def interpret(stmts)
|
|
|
@ -20,6 +27,12 @@ module Lox
|
|
|
|
nil
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def visit_var(stmt)
|
|
|
|
|
|
|
|
value = stmt.initializer&.yield_self { evaluate(_1) }
|
|
|
|
|
|
|
|
@env.define(stmt.name.lexeme, value)
|
|
|
|
|
|
|
|
nil
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def visit_grouping(expr) = evaluate(expr.expr)
|
|
|
|
def visit_grouping(expr) = evaluate(expr.expr)
|
|
|
|
def visit_literal(expr) = expr.value
|
|
|
|
def visit_literal(expr) = expr.value
|
|
|
|
|
|
|
|
|
|
|
@ -35,6 +48,10 @@ module Lox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def visit_variable(expr)
|
|
|
|
|
|
|
|
@env.get(expr.name)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def visit_binary(expr)
|
|
|
|
def visit_binary(expr)
|
|
|
|
left = evaluate(expr.left)
|
|
|
|
left = evaluate(expr.left)
|
|
|
|
right = evaluate(expr.right)
|
|
|
|
right = evaluate(expr.right)
|
|
|
|