From a944813d0d3807bbab073e11b14723090542c5d3 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 16 Aug 2022 16:08:32 -0700 Subject: [PATCH] 10.4 --- ruby/lib/lox/function.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ruby/lib/lox/function.rb diff --git a/ruby/lib/lox/function.rb b/ruby/lib/lox/function.rb new file mode 100644 index 0000000..298a1a0 --- /dev/null +++ b/ruby/lib/lox/function.rb @@ -0,0 +1,23 @@ +require_relative "environment" + +module Lox + class Function + def initialize(decl) + @decl = decl + end + + def arity = @decl.params.size + + def call(interpreter, args) + env = Environment.new(interpreter.globals) + @decl.params.map(&:lexeme).zip(args).each do |name, value| + env.define(name, value) + end + + interpreter.execute_block(@decl.body, env) + nil + end + + def to_s = "" + end +end