Class: GLOBAL_SCOPE

Inherits:
Object
  • Object
show all
Defined in:
lib/sdx/vm/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}) ⇒ GLOBAL_SCOPE

Returns a new instance of GLOBAL_SCOPE.



8
9
10
# File 'lib/sdx/vm/scope.rb', line 8

def initialize(variables={})
    @variables = variables
end

Instance Attribute Details

#variablesObject (readonly)

Returns the value of attribute variables.



2
3
4
# File 'lib/sdx/vm/scope.rb', line 2

def variables
  @variables
end

Instance Method Details

#add_fn(fn_name, fn_class) ⇒ Object



16
17
18
# File 'lib/sdx/vm/scope.rb', line 16

def add_fn(fn_name, fn_class)
    @variables[fn_name] = fn_class
end

#add_obj(obj_name, obj_class) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/sdx/vm/scope.rb', line 49

def add_obj(obj_name, obj_class)
    if obj_class.value.args.size == 0
        scope = obj_class.value.fields["__new"].call [], self
        scope.variables.each do |k, v|
            add_var "#{obj_name}:#{k}", scope.variables[k]
        end
    else
        @variables[obj_name] = obj_class
    end
end

#add_var(var_name, var_class) ⇒ Object



12
13
14
# File 'lib/sdx/vm/scope.rb', line 12

def add_var(var_name, var_class)
    @variables[var_name] = var_class
end

#error(msg) ⇒ Object



4
5
6
# File 'lib/sdx/vm/scope.rb', line 4

def error(msg)
    puts "\x1b[0;31mError in VM: #{msg}\x1b[0;0m"
end

#get_fn(fn_name) ⇒ Object



45
46
47
# File 'lib/sdx/vm/scope.rb', line 45

def get_fn(fn_name)
    @variables[fn_name]
end

#get_var(var_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sdx/vm/scope.rb', line 20

def get_var(var_name)
    scope = self
    val = nil
    name = var_name.split "."
    name.each do |part|
        val = scope.variables[part]
        unless val
            return nil
        end
        case val.value
        when InstantiatedObj
            scope = val.value.internal
        else
            fields = {}
            if val.value.respond_to? :fields
                val.value.fields.each do |k, v|
                    fields[k] = Variable.new v, (get_type v), self
                end
                scope = GLOBAL_SCOPE.new fields
            end
        end
    end
    return val
end