Class: Lisp::Frame

Inherits:
Atom show all
Defined in:
lib/rubylisp/frame.rb

Instance Attribute Summary

Attributes inherited from Atom

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Atom

#all?, #apply_to, #character?, #class?, #copy, #doc, #environment?, #eof_object?, #eq?, #eqv?, #evaluate, #false?, #function?, #list?, #macro?, #negative?, #number?, #object?, #pair?, #port?, #positive?, #primitive?, #print_string, #quoted, #set!, #set_location, #special?, #string?, #symbol?, #true?, #vector?, #zero?

Constructor Details

#initialize(m = {}) ⇒ Frame

Returns a new instance of Frame.



9
10
11
12
# File 'lib/rubylisp/frame.rb', line 9

def initialize(m = {})
  @value = m
  self
end

Class Method Details

.with_map(m) ⇒ Object



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

def self.with_map(m)
  self.new(m)
end

Instance Method Details

#at_put(key, value) ⇒ Object



96
97
98
# File 'lib/rubylisp/frame.rb', line 96

def at_put(key, value)
  return @value[key] = value
end

#carObject



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

def car
  nil
end

#cdrObject



125
126
127
# File 'lib/rubylisp/frame.rb', line 125

def cdr
  nil
end

#cloneObject



15
16
17
# File 'lib/rubylisp/frame.rb', line 15

def clone
  Lisp::Frame.with_map(@value.clone)
end

#empty?Boolean

Returns:



109
110
111
# File 'lib/rubylisp/frame.rb', line 109

def empty?
  @value.empty?
end

#equal?(other) ⇒ Boolean

Returns:



129
130
131
132
133
134
135
136
# File 'lib/rubylisp/frame.rb', line 129

def equal?(other)
  return false unless other.frame?
  return false unless @value.length == other.value.length
  @value.each do |k, v|
    return false unless other.value[k].equal?(v)
  end
  true
end

#frame?Boolean

Returns:



113
114
115
# File 'lib/rubylisp/frame.rb', line 113

def frame?
  true
end

#get(key) ⇒ Object



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

def get(key)
  get_helper(key, Set.new)
end

#get_helper(key, v) ⇒ Object



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

def get_helper(key, v)
  return nil if v.include?(self)
  v << self
  return @value[key] if has_slot_locally?(key)
  parents.each do |p|
    value = p.get_helper(key, v)
    return value unless value.nil?
  end
  nil
end

#has_parent_slots?Boolean

Returns:



38
39
40
# File 'lib/rubylisp/frame.rb', line 38

def has_parent_slots?
  @value.keys.any? {|k| is_parent_key(k)}
end

#has_slot?(n) ⇒ Boolean

Returns:



67
68
69
# File 'lib/rubylisp/frame.rb', line 67

def has_slot?(n)
  has_slot_helper(n, Set.new)
end

#has_slot_helper(n, v) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rubylisp/frame.rb', line 58

def has_slot_helper(n, v)
  return false if v.include?(self)
  v << self
  return true if has_slot_locally?(n)
  return false unless has_parent_slots?
  return parents.any? {|p| p.has_slot_helper(n, v)}
end

#has_slot_locally?(n) ⇒ Boolean

Returns:



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

def has_slot_locally?(n)
  @value.has_key?(n)
end

#inherited_value_slotsObject



30
31
32
33
34
35
# File 'lib/rubylisp/frame.rb', line 30

def inherited_value_slots
  parent_frames = parent_slots.collect {|pk| get(pk)}
  parent_slots = parent_frames.collect {|p| p.inherited_value_slots}
  local_value_slots = Set[local_slots.reject {|s| is_parent_key(k)}]
  parent_slots.inject(local_value_slots) {|all, s| all + s} 
end

#is_parent_key(k) ⇒ Object



20
21
22
# File 'lib/rubylisp/frame.rb', line 20

def is_parent_key(k)
  k.to_s[-2] == "*"
end

#lengthObject



117
118
119
# File 'lib/rubylisp/frame.rb', line 117

def length
  return @value.length
end

#lisp_object?Boolean

Returns:



101
102
103
# File 'lib/rubylisp/frame.rb', line 101

def lisp_object?
  true
end

#local_slotsObject



25
26
27
# File 'lib/rubylisp/frame.rb', line 25

def local_slots
  @value.keys
end

#parent_slotsObject



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

def parent_slots
  @value.keys.select {|k| is_parent_key(k)}
end

#parentsObject



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

def parents
  parent_slots.collect {|pk| @value[pk]}
end

#remove(key) ⇒ Object



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

def remove(key)
  return false unless has_slot_locally?(key)
  @value.delete(key)
  true
end

#to_sObject



138
139
140
141
# File 'lib/rubylisp/frame.rb', line 138

def to_s
  pairs = @value.collect {|k, v| "#{k.to_s} #{v.to_s}"}
  "{#{pairs.join(' ')}}"
end

#typeObject



105
106
107
# File 'lib/rubylisp/frame.rb', line 105

def type
  :frame
end