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, f = nil) ⇒ EnvironmentFrame

Returns a new instance of EnvironmentFrame.



17
18
19
20
21
22
# File 'lib/rubylisp/environment_frame.rb', line 17

def initialize(parent, f=nil)
  @bindings = []
  @parent = parent
  @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

#previousObject

Returns the value of attribute previous.



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

def previous
  @previous
end

Class Method Details

.extending(parent, f = nil) ⇒ Object



12
13
14
15
# File 'lib/rubylisp/environment_frame.rb', line 12

def self.extending(parent, f=nil)
  f ||= parent.frame if parent && parent.has_frame?
  self.new(parent, f)
end

.globalObject



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

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

Instance Method Details

#bind(symbol, value) ⇒ Object



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

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



77
78
79
80
81
82
83
84
# File 'lib/rubylisp/environment_frame.rb', line 77

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



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

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

#depthObject



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

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

#dump(frame_number = 0) ⇒ Object



117
118
119
120
121
# File 'lib/rubylisp/environment_frame.rb', line 117

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



109
110
111
112
113
114
# File 'lib/rubylisp/environment_frame.rb', line 109

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

#dump_headersObject



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

def dump_headers
  puts
  internal_dump_headers(0)
end

#dump_single_frame(frame_number) ⇒ Object



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

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:



24
25
26
# File 'lib/rubylisp/environment_frame.rb', line 24

def has_frame?
  !@frame.nil?
end

#internal_dump_headers(frame_number) ⇒ Object



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

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

Bindings following parent env frame pointer

Returns:



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubylisp/environment_frame.rb', line 30

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



73
74
75
# File 'lib/rubylisp/environment_frame.rb', line 73

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

#quick_value_of(symbol_name) ⇒ Object



103
104
105
106
# File 'lib/rubylisp/environment_frame.rb', line 103

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

#set(symbol, value) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubylisp/environment_frame.rb', line 57

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



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubylisp/environment_frame.rb', line 88

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