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



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

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



101
102
103
104
105
106
107
108
# File 'lib/rubylisp/environment_frame.rb', line 101

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



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

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



41
42
43
# File 'lib/rubylisp/environment_frame.rb', line 41

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

#bound_valuesObject



45
46
47
# File 'lib/rubylisp/environment_frame.rb', line 45

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



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

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

#dump(frame_number = 0) ⇒ Object



141
142
143
144
145
# File 'lib/rubylisp/environment_frame.rb', line 141

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



133
134
135
136
137
138
# File 'lib/rubylisp/environment_frame.rb', line 133

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

#dump_headersObject



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

def dump_headers
  puts
  internal_dump_headers(0)
end

#dump_single_frame(frame_number) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/rubylisp/environment_frame.rb', line 148

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_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



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

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:



49
50
51
52
53
54
55
56
57
58
# File 'lib/rubylisp/environment_frame.rb', line 49

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



97
98
99
# File 'lib/rubylisp/environment_frame.rb', line 97

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

#name_bound_locally?(str) ⇒ Boolean

Returns:



60
61
62
63
# File 'lib/rubylisp/environment_frame.rb', line 60

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

#quick_value_of(symbol_name) ⇒ Object



127
128
129
130
# File 'lib/rubylisp/environment_frame.rb', line 127

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

#set(symbol, value) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubylisp/environment_frame.rb', line 81

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

#value_of(symbol) ⇒ Object

Look up a symbol



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubylisp/environment_frame.rb', line 112

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