class Lisp::EnvironmentFrame

Attributes

current_code[RW]
frame[RW]
name[RW]
parent[R]
previous[RW]
project_environment[R]

Public Class Methods

extending(parent, name, f=nil) click to toggle source
# 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
global() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 8
def self.global
  @@global_frame ||= EnvironmentFrame.new(nil, "GLOBAL")
  @@global_frame
end
new(parent, name, f=nil) click to toggle source
# File lib/rubylisp/environment_frame.rb, line 20
def initialize(parent, name, f=nil)
  @bindings = []
  @parent = parent
  @name = name
  @frame = f
  @current_code = []
end

Public Instance Methods

bind(symbol, value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_names() click to toggle source

Bindings following parent env frame pointer

# File lib/rubylisp/environment_frame.rb, line 65
def bound_names
  @bindings.map {|b| b.symbol}
end
bound_values() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 69
def bound_values
  @bindings.map {|b| b.value}
end
clear() click to toggle source
# 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
depth() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 196
def depth
  if @previous.nil?
    1
  else
    1 + @previous.depth
  end
end
dump(frame_number=0) click to toggle source
# 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_bindings() click to toggle source
# 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_headers() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 190
def dump_headers
  puts
  internal_dump_headers(0)
end
dump_single_frame(frame_number) click to toggle source
# 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?() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 58
def has_code?
  !@current_code.empty?
end
has_frame?() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 35
def has_frame?
  !@frame.nil?
end
internal_dump_headers(frame_number) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Bindings local to this env frame only

# 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) click to toggle source
# 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_code() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 48
def pop_code
  @current_code.pop
end
push_code(code) click to toggle source

Exaling code management

# File lib/rubylisp/environment_frame.rb, line 43
def push_code(code)
  @current_code.push(code)
end
quick_value_of(symbol_name) click to toggle source
# 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) click to toggle source
# 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_code() click to toggle source
# File lib/rubylisp/environment_frame.rb, line 53
def top_code
  @current_code[-1]
end
value_of(symbol) click to toggle source

Look up a symbol

# 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