diff --git a/ruby/lib/lox/interpreter.rb b/ruby/lib/lox/interpreter.rb
index 56c4891..8e9b305 100644
--- a/ruby/lib/lox/interpreter.rb
+++ b/ruby/lib/lox/interpreter.rb
@@ -1,8 +1,11 @@
require_relative "environment"
+require_relative "function"
module Lox
class Interpreter
+ attr_reader :globals
+
def initialize(env=Environment.new)
@globals = env
@env = @globals
@@ -46,6 +49,12 @@ module Lox
nil
end
+ def visit_function(stmt)
+ function = Function.new(stmt)
+ @env.define(stmt.name.lexeme, function)
+ nil
+ end
+
def visit_if(stmt)
if truthy?(evaluate(stmt.cond))
evaluate(stmt.then)
diff --git a/ruby/lib/lox/stmt.rb b/ruby/lib/lox/stmt.rb
index 0f377d7..2826c67 100644
--- a/ruby/lib/lox/stmt.rb
+++ b/ruby/lib/lox/stmt.rb
@@ -13,7 +13,7 @@ module Lox
end
stmt :Block, :stmts
- stmt :Fun, :name, :params, :body
+ stmt :Function, :name, :params, :body
stmt :Expr, :expr
stmt :If, :cond, :then, :else
stmt :Print, :expr
diff --git a/ruby/test/lox/test_interpreter.rb b/ruby/test/lox/test_interpreter.rb
index 21a1258..7960957 100644
--- a/ruby/test/lox/test_interpreter.rb
+++ b/ruby/test/lox/test_interpreter.rb
@@ -224,6 +224,17 @@ class TestInterpreter < Lox::Test
SRC
end
+ def test_function
+ assert_interpreted <<~EXPECTED.chomp, <<~SRC
+ Hi, Dear Reader!
+ EXPECTED
+ fun sayHi(first, last) {
+ print "Hi, " + first + " " + last + "!";
+ }
+
+ sayHi("Dear", "Reader");
+ SRC
+ end
private
def assert_interpreted(expected, src)