Class: Lisp::EnvironmentFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylisp/environment_frame.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, f = nil) ⇒ EnvironmentFrame

Returns a new instance of EnvironmentFrame.



20
21
22
23
24
25
26
# File 'lib/rubylisp/environment_frame.rb', line 20

def initialize(parent, name, f=nil)
  @bindings = []
  @parent = parent
  @name = name
  @frame = f
  @current_code = []
end

Instance Attribute Details

#current_codeObject

Returns the value of attribute current_code.



5
6
7
# File 'lib/rubylisp/environment_frame.rb', line 5

def current_code
  @current_code
end

#frameObject

Returns the value of attribute frame.



5
6
7
# File 'lib/rubylisp/environment_frame.rb', line 5

def frame
  @frame
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/rubylisp/environment_frame.rb', line 5

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/rubylisp/environment_frame.rb', line 6

def parent
  @parent
end

#previousObject

Returns the value of attribute previous.



5
6
7
# File 'lib/rubylisp/environment_frame.rb', line 5

def previous
  @previous
end

#project_environmentObject (readonly)

Returns the value of attribute project_environment.



6
7
8
# File 'lib/rubylisp/environment_frame.rb', line 6

def project_environment
  @project_environment
end

Class Method Details

.extending(parent, name, f = nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/rubylisp/environment_frame.rb', line 13

def self.extending(parent, name, f=nil)
  f ||= parent.frame if parent && parent.has_frame?
  e = self.new(parent, name, f)
  TopLevelEnvironments[name] = e if parent.nil? || parent == self.global
  e
end

.globalObject



8
9
10
11
# File 'lib/rubylisp/environment_frame.rb', line 8

def self.global
  @@global_frame ||= EnvironmentFrame.new(nil, "GLOBAL")
  @@global_frame
end

Instance Method Details

#bind(symbol, value) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/rubylisp/environment_frame.rb', line 96

def bind(symbol, value)
  b = self.binding_for(symbol)
  if b.nil?
    @bindings << Lisp::Binding.new(symbol, value)
  else
    b.value = value
  end
end

#bind_locally(symbol, value) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/rubylisp/environment_frame.rb', line 125

def bind_locally(symbol, value)
  b = self.local_binding_for(symbol)
  if b.nil?
    @bindings << Lisp::Binding.new(symbol, value)
  else
    b.value = value
  end
end

#binding_for(symbol) ⇒ Object



89
90
91
92
93
94
# File 'lib/rubylisp/environment_frame.rb', line 89

def binding_for(symbol)
  binding = @bindings.detect {|b| b.symbol.name == symbol.name}
  return binding unless binding.nil?
  return @parent.binding_for(symbol) unless @parent.nil?
  nil
end

#bound_namesObject

Bindings following parent env frame pointer



65
66
67
# File 'lib/rubylisp/environment_frame.rb', line 65

def bound_names
  @bindings.map {|b| b.symbol}
end

#bound_valuesObject



69
70
71
# File 'lib/rubylisp/environment_frame.rb', line 69

def bound_values
  @bindings.map {|b| b.value}
end

#clearObject



29
30
31
32
# File 'lib/rubylisp/environment_frame.rb', line 29

def clear
  TopLevelEnvironments[@name] = nil if TopLevelEnvironments.has_key?(@name)
  @bindings.each {|b| b.value = nil}
end

#depthObject



196
197
198
199
200
201
202
# File 'lib/rubylisp/environment_frame.rb', line 196

def depth
  if @previous.nil?
    1
  else
    1 + @previous.depth
  end
end

#dump(frame_number = 0) ⇒ Object



165
166
167
168
169
# File 'lib/rubylisp/environment_frame.rb', line 165

def dump(frame_number=0)
  puts "Frame #{frame_number}: #{@current_code[0]}"
  dump_bindings
  @previous.dump(frame_number + 1) unless @previous.nil?
end

#dump_bindingsObject



157
158
159
160
161
162
# File 'lib/rubylisp/environment_frame.rb', line 157

def dump_bindings
  @bindings.each do |b|
    puts b.to_s if b.value.nil? || !b.value.primitive?
  end
  puts
end

#dump_headersObject



190
191
192
193
# File 'lib/rubylisp/environment_frame.rb', line 190

def dump_headers
  puts
  internal_dump_headers(0)
end

#dump_single_frame(frame_number) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/rubylisp/environment_frame.rb', line 172

def dump_single_frame(frame_number)
  if frame_number == 0
    puts "Evaling: #{@current_code[0]}"
    dump_bindings
  elsif !@previous.nil?
    @previous.dump_single_frame(frame_number - 1)
  else
    puts "Invalid frame selected."
  end
end

#has_code?Boolean

Returns:



58
59
60
# File 'lib/rubylisp/environment_frame.rb', line 58

def has_code?
  !@current_code.empty?
end

#has_frame?Boolean

Returns:



35
36
37
# File 'lib/rubylisp/environment_frame.rb', line 35

def has_frame?
  !@frame.nil?
end

#internal_dump_headers(frame_number) ⇒ Object



183
184
185
186
# File 'lib/rubylisp/environment_frame.rb', line 183

def internal_dump_headers(frame_number)
  puts "Frame #{frame_number}: #{@current_code[0]}"
  @previous.internal_dump_headers(frame_number + 1) unless @previous.nil?
end

#is_name_bound?(str) ⇒ Boolean

Returns:



73
74
75
76
77
78
79
80
81
82
# File 'lib/rubylisp/environment_frame.rb', line 73

def is_name_bound?(str)
  if !@frame && @frame.has_slot?(Lisp:Symbol.named("#{str}:", true))
    return true
  end
    
  binding = @bindings.detect {|b| b.symbol.name == str}
  return true unless binding.nil?
  return false if @parent.nil?
  return @parent.is_name_bound?(str)
end

#local_binding_for(symbol) ⇒ Object

Bindings local to this env frame only



121
122
123
# File 'lib/rubylisp/environment_frame.rb', line 121

def local_binding_for(symbol)
  @bindings.detect {|b| b.symbol.name == symbol.name}
end

#name_bound_locally?(str) ⇒ Boolean

Returns:



84
85
86
87
# File 'lib/rubylisp/environment_frame.rb', line 84

def name_bound_locally?(str)
  binding = @bindings.detect {|b| b.symbol.name == str}
  !binding.nil?
end

#pop_codeObject



48
49
50
# File 'lib/rubylisp/environment_frame.rb', line 48

def pop_code
  @current_code.pop
end

#push_code(code) ⇒ Object


Exaling code management



43
44
45
# File 'lib/rubylisp/environment_frame.rb', line 43

def push_code(code)
  @current_code.push(code)
end

#quick_value_of(symbol_name) ⇒ Object



151
152
153
154
# File 'lib/rubylisp/environment_frame.rb', line 151

def quick_value_of(symbol_name)
  b = binding_for(Symbol.new(symbol_name))
  b.nil? ? nil : b.value
end

#set(symbol, value) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rubylisp/environment_frame.rb', line 105

def set(symbol, value)
  naked_symbol = symbol.to_naked
  if @frame && @frame.has_slot?(naked_symbol)
    return @frame.at_put(naked_symbol, value)
  end
    
  b = self.binding_for(symbol)
  if b.nil?
    raise "#{symbol} is undefined."
  else
    b.value = value
  end
end

#top_codeObject



53
54
55
# File 'lib/rubylisp/environment_frame.rb', line 53

def top_code
  @current_code[-1]
end

#value_of(symbol) ⇒ Object

Look up a symbol



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rubylisp/environment_frame.rb', line 136

def value_of(symbol)
  b = local_binding_for(symbol)
  return b.value unless b.nil?
            
  naked_symbol = symbol.to_naked
  if @frame && @frame.has_slot?(naked_symbol)
    return @frame.get(naked_symbol)
  end

  b = binding_for(symbol)
  return b.value unless b.nil?
  nil
end