Class: PLang::VM::Environment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



6
7
8
9
# File 'lib/vm/environment.rb', line 6

def initialize
  @vars = Hash.new
  @objects = Hash.new
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#add_lambda(id, lamb) ⇒ Object

TODO: new operator to add lambdas



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

def add_lambda(id, lamb)
  v = get_var(id)
  if v
    if v.id == :lambda and lamb.id == :lambda
      while v.params[1].id == :lambda
        v = v.params[1]
      end
      v.params[1] = lamb
    else
      raise "TODO: Environment#add_lambda#2"
    end
  else
    raise "TODO: Environment#add_lambda#1"
  end
end

#get_var(id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/vm/environment.rb', line 19

def get_var(id)
  v = @vars[id]
  if v
    return v
  elsif @parent
    return @parent.get_var(id)
  else
    raise "NameError: undefined variable '#{id}'"
  end
end

#set_object_var(form, object) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vm/environment.rb', line 47

def set_object_var(form, object)
  if form.type == :object
    if form.id.value == object.id
      form.params.each_with_index do |param, i|
        case param.type 
          when :object
            set_object_var(param, object.params[i])
          when :id
            unless param.value == :_
              if object.id == :integer
                set_var(param.value, PObject.new(:integer, [object.params[i]]))
              elsif object.id == :decimal
                set_var(param.value, PObject.new(:decimal, [object.params[i]]))
              elsif object.id == :char
                set_var(param.value, PObject.new(:char, [object.params[i]]))
              elsif object.id == :string
                set_var(param.value, PObject.new(:string, [object.params[i]]))
              else
                set_var(param.value, object.params[i])
              end
            end
          else
            unless param.value == object.params[i].params[0]
              raise "TODO: Environment#set_object_var#3"
            end
        end
      end
    else
      raise "TODO: Environment#set_object_var#1"
    end
  else
    raise "TODO: Environment#set_object_var#2"
  end
end

#set_var(id, value) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/vm/environment.rb', line 11

def set_var(id, value)
  unless @vars[id]
    @vars[id] = value
  else
    raise "LetError: TODO"
  end
end