Class: Function

Inherits:
DataType show all
Defined in:
lib/sdx/vm/datatypes.rb

Instance Attribute Summary collapse

Attributes inherited from DataType

#fields, #internal

Instance Method Summary collapse

Constructor Details

#initialize(args, body) ⇒ Function

Returns a new instance of Function.



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/sdx/vm/datatypes.rb', line 447

def initialize(args, body)
    @args = args
    @internal = body

    @fields = {
        "__call" => (NativeFnInternal.new (lambda do |args, scope|
            call args, scope
        end)),
        "__arity" => (Int.new args.size),
        "__eq" => (NativeFnInternal.new (lambda do |other|
            Bool.new @internal == other[0].internal
        end)),
        "__neq" => (NativeFnInternal.new (lambda do |other|
            Bool.new @internal != other[0].internal
        end))
    }
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



445
446
447
# File 'lib/sdx/vm/datatypes.rb', line 445

def args
  @args
end

Instance Method Details

#call(passed, scope) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/sdx/vm/datatypes.rb', line 465

def call(passed, scope)
    passed.reverse!
    vm = VM.new StringIO.new @internal
    scope.variables.each do |k|
        vm.global.add_var k[0], (scope.get_var k[0])
    end
    args.each_with_index do |arg, i|
        vm.global.add_var arg, passed[i]
    end
    vm.interpret
    vm.stack[-1]
end