Class: Hivemind::Runtime::HivemindClass

Inherits:
Object
  • Object
show all
Defined in:
lib/hivemind/runtime.rb,
lib/hivemind/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, parent = nil, methods = {}) ⇒ HivemindClass

Returns a new instance of HivemindClass.



16
17
18
# File 'lib/hivemind/runtime.rb', line 16

def initialize(label, parent = nil, methods = {})
	@label, @parent, @methods = label, parent, methods
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



14
15
16
# File 'lib/hivemind/runtime.rb', line 14

def label
  @label
end

#methodsObject (readonly)

Returns the value of attribute methods.



14
15
16
# File 'lib/hivemind/runtime.rb', line 14

def methods
  @methods
end

#parentObject (readonly)

Returns the value of attribute parent.



14
15
16
# File 'lib/hivemind/runtime.rb', line 14

def parent
  @parent
end

Instance Method Details

#call(function, args, env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hivemind/vm.rb', line 31

def call(function, args, env)
  h = Runtime::HivemindObject.new({}, self)
  function = dispatch_method(:init)
  if function.is_a?(UniversalAST::MethodStatement)
    args_values = {:self => h}
    function.args[1..-1].zip(args) do |label, arg|
      args_values[label.value.to_sym] = arg
    end
    body_env = Environment.new(env, **args_values)
    function.body.map { |expr| expr.run(body_env) }[-1] || env.top[:@nil]
  else
    function.call h, *args, env
  end
  h
end

#define_hivemind_method(label, &handler) ⇒ Object



20
21
22
# File 'lib/hivemind/runtime.rb', line 20

def define_hivemind_method(label, &handler)
  @methods[label] = handler
end

#dispatch_method(label) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/hivemind/runtime.rb', line 24

def dispatch_method(label)
  current = self
  until current.nil? || current.methods.key?(label)
    current = current.parent
  end
  !current ? nil : current.methods[label]
end