Class: ToyLangMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/toylang/runtime/method.rb

Instance Method Summary collapse

Constructor Details

#initialize(params, body) ⇒ ToyLangMethod

Returns a new instance of ToyLangMethod.



4
5
6
7
# File 'lib/toylang/runtime/method.rb', line 4

def initialize(params, body)
  @params = params
  @body = body
end

Instance Method Details

#call(receiver, arguments) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/toylang/runtime/method.rb', line 9

def call(receiver, arguments)
  context = Context.new(receiver)

  @params.each_with_index do |param, index|
    context.locals[param] = arguments[index]
  end

  @body.eval(context)
end