Class: Lisp::ConsCell
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/rubylisp/cons_cell.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(car = nil, cdr = nil) ⇒ ConsCell
Returns a new instance of ConsCell.
11
12
13
14
|
# File 'lib/rubylisp/cons_cell.rb', line 11
def initialize(car=nil, cdr=nil)
@car = car
@cdr = cdr
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
186
187
188
189
190
191
192
|
# File 'lib/rubylisp/cons_cell.rb', line 186
def method_missing(name, *args, &block)
if name[0] == ?c && name[-1] == ?r && (name[1..-2].chars.all? {|e| "ad".include?(e)})
self.traverse(name[1..-2].reverse)
else
super
end
end
|
Instance Attribute Details
Returns the value of attribute car.
5
6
7
|
# File 'lib/rubylisp/cons_cell.rb', line 5
def car
@car
end
|
Returns the value of attribute cdr.
5
6
7
|
# File 'lib/rubylisp/cons_cell.rb', line 5
def cdr
@cdr
end
|
Class Method Details
.array_to_list(cells, tail = nil) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/rubylisp/cons_cell.rb', line 165
def self.array_to_list(cells, tail=nil)
return tail if cells.empty?
head = ConsCell.new
last_cell = head
cells.each do |d|
new_cell = self.cons(d, nil)
last_cell.set_cdr!(new_cell)
last_cell = new_cell
end
last_cell.set_cdr!(tail)
head.cdr
end
|
.cons(a = nil, b = nil) ⇒ Object
7
8
9
|
# File 'lib/rubylisp/cons_cell.rb', line 7
def self.cons(a=nil, b=nil)
ConsCell.new(a, b)
end
|
Instance Method Details
96
97
98
|
# File 'lib/rubylisp/cons_cell.rb', line 96
def alist?
false
end
|
48
49
50
|
# File 'lib/rubylisp/cons_cell.rb', line 48
def character?
false
end
|
#each(&block) ⇒ Object
155
156
157
158
159
160
161
162
163
|
# File 'lib/rubylisp/cons_cell.rb', line 155
def each &block
c = self
if self.length > 0
until c.nil?
yield c.car
c = c.cdr
end
end
end
|
40
41
42
|
# File 'lib/rubylisp/cons_cell.rb', line 40
def empty?
@car.nil? && @cdr.nil?
end
|
108
109
110
111
|
# File 'lib/rubylisp/cons_cell.rb', line 108
def eq?(sexpr)
return false unless sexpr.pair?
(@car == sexpr.car || @car.eq?(sexpr.car)) && (@cdr == sexpr.cdr || @cdr.eq?(sexpr.cdr))
end
|
#evaluate(env) ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/rubylisp/cons_cell.rb', line 235
def evaluate(env)
return self if empty?
sexpr = if @car.symbol?
key = @car
frame = nth(2)
value = nth(3)
s = key.name
if s.end_with?(":")
ConsCell.array_to_list([Symbol.named("get-slot"), frame, key])
elsif s.end_with?(":!")
ConsCell.array_to_list([Symbol.named("set-slot!"), frame, Symbol.named(s[0..-2]), value])
elsif s.end_with?(":?")
ConsCell.array_to_list([Symbol.named("has-slot?"), frame, Symbol.named(s[0..-2])])
else
self
end
else
self
end
sexpr.inner_eval(env)
end
|
#evaluate_each(env) ⇒ Object
258
259
260
261
262
|
# File 'lib/rubylisp/cons_cell.rb', line 258
def evaluate_each(env)
result = @car.evaluate(env)
return result if @cdr.nil?
@cdr.evaluate_each(env)
end
|
274
275
276
|
# File 'lib/rubylisp/cons_cell.rb', line 274
def false?
false
end
|
290
291
292
293
|
# File 'lib/rubylisp/cons_cell.rb', line 290
def flatten
ary = to_a.collect {|s| s.list? ? s.to_a : s}
ConsCell.array_to_list(ary.flatten)
end
|
100
101
102
|
# File 'lib/rubylisp/cons_cell.rb', line 100
def frame?
false
end
|
80
81
82
|
# File 'lib/rubylisp/cons_cell.rb', line 80
def function?
false
end
|
#inner_eval(env) ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/rubylisp/cons_cell.rb', line 213
def inner_eval(env)
func = @car.evaluate(env)
return Lisp::Debug.process_error("There is no function or macro named #{@car}", env) if func.nil?
env.current_code.unshift(self.print_string()) if !Lisp::Debug.eval_in_debug_repl && Lisp::Debug.interactive
Lisp::Debug.log_eval(self, env)
unless Lisp::Debug.eval_in_debug_repl
if !Lisp::Debug.target_env.nil? && env == Lisp::Debug.target_env.previous
Lisp::Debug.target_env = nil
Lisp::Debug.debug_repl(env)
elsif Lisp::Debug.single_step || (func.function? && Lisp::Debug.on_entry.include?(func.name))
Lisp::Debug.debug_repl(env)
end
end
result = func.apply_to(@cdr, env)
env.current_code.shift() if !Lisp::Debug.eval_in_debug_repl && Lisp::Debug.interactive
Lisp::Debug.log_result(result, env)
result
end
|
282
283
284
285
286
287
288
|
# File 'lib/rubylisp/cons_cell.rb', line 282
def last
c = self
while !c.cdr.nil? && c.cdr.pair? do
c = c.cdr
end
c
end
|
264
265
266
267
268
|
# File 'lib/rubylisp/cons_cell.rb', line 264
def length
return 0 if empty?
return 1 if @cdr.nil?
return 1 + @cdr.length
end
|
#lisp_object? ⇒ Boolean
24
25
26
|
# File 'lib/rubylisp/cons_cell.rb', line 24
def lisp_object?
true
end
|
92
93
94
|
# File 'lib/rubylisp/cons_cell.rb', line 92
def list?
true
end
|
84
85
86
|
# File 'lib/rubylisp/cons_cell.rb', line 84
def macro?
false
end
|
64
65
66
|
# File 'lib/rubylisp/cons_cell.rb', line 64
def negative?
false
end
|
194
195
196
197
198
|
# File 'lib/rubylisp/cons_cell.rb', line 194
def nth(n)
c = self
(n-1).times {|i| c = c.cdr}
c.car
end
|
#nth_tail(n) ⇒ Object
200
201
202
203
204
|
# File 'lib/rubylisp/cons_cell.rb', line 200
def nth_tail(n)
c = self
(n-1).times {|i| c = c.cdr}
c
end
|
52
53
54
|
# File 'lib/rubylisp/cons_cell.rb', line 52
def number?
false
end
|
#objc_object_or_nil(obj) ⇒ Object
206
207
208
209
|
# File 'lib/rubylisp/cons_cell.rb', line 206
def objc_object_or_nil(obj)
return nil unless obj.object?
return obj.value
end
|
88
89
90
|
# File 'lib/rubylisp/cons_cell.rb', line 88
def pair?
true
end
|
56
57
58
|
# File 'lib/rubylisp/cons_cell.rb', line 56
def positive?
false
end
|
72
73
74
|
# File 'lib/rubylisp/cons_cell.rb', line 72
def primitive?
false
end
|
#print_string ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/rubylisp/cons_cell.rb', line 136
def print_string
return "()" if self.empty?
return "'#{@cdr.car.print_string}" if @car.symbol? && @car.name == "quote"
return "{#{@cdr.print_string_helper}}" if @car.symbol? && @car.name == "make-frame"
return "[#{@cdr.print_string_helper}]" if @car.symbol? && @car.name == "make-vector"
return "(#{@car.print_string} . #{@cdr.print_string})" if !@cdr.nil? && !@cdr.pair?
return "(#{self.print_string_helper})"
end
|
#print_string_helper ⇒ Object
132
133
134
|
# File 'lib/rubylisp/cons_cell.rb', line 132
def print_string_helper
@cdr.nil? ? "#{@car.print_string}" : "#{@car.print_string} #{@cdr.print_string_helper}"
end
|
278
279
280
|
# File 'lib/rubylisp/cons_cell.rb', line 278
def quoted
Lisp::ConsCell.array_to_list([Symbol.named("quote"), self])
end
|
#set_car!(d) ⇒ Object
20
21
22
|
# File 'lib/rubylisp/cons_cell.rb', line 20
def set_car!(d)
@car = d
end
|
#set_cdr!(d) ⇒ Object
28
29
30
|
# File 'lib/rubylisp/cons_cell.rb', line 28
def set_cdr!(d)
@cdr = d
end
|
#set_nth!(n, d) ⇒ Object
33
34
35
36
37
|
# File 'lib/rubylisp/cons_cell.rb', line 33
def set_nth!(n, d)
c = self
(n-1).times {|i| c = c.cdr}
c.set_car!(d)
end
|
76
77
78
|
# File 'lib/rubylisp/cons_cell.rb', line 76
def special?
false
end
|
44
45
46
|
# File 'lib/rubylisp/cons_cell.rb', line 44
def string?
false
end
|
68
69
70
|
# File 'lib/rubylisp/cons_cell.rb', line 68
def symbol?
false
end
|
145
146
147
148
149
150
151
152
153
|
# File 'lib/rubylisp/cons_cell.rb', line 145
def to_a
a = []
c = self
until c.nil?
a << c.car()
c = c.cdr
end
a
end
|
123
124
125
126
127
128
129
130
|
# File 'lib/rubylisp/cons_cell.rb', line 123
def to_s
return "()" if self.empty?
return "'#{@cdr.car.to_s}" if @car.symbol? && @car.name == "quote"
return "{#{@cdr.to_s_helper}}" if @car.symbol? && @car.name == "make-frame"
return "[#{@cdr.to_s_helper}]" if @car.symbol? && @car.name == "make-vector"
return "(#{@car.to_s} . #{@cdr.to_s})" if !@cdr.nil? && !@cdr.pair?
return "(#{self.to_s_helper})"
end
|
#to_s_helper ⇒ Object
117
118
119
120
121
|
# File 'lib/rubylisp/cons_cell.rb', line 117
def to_s_helper
return "#{@car.to_s}" if @cdr.nil?
return "#{@car.to_s} . #{@cdr.to_s}" unless @cdr.pair?
"#{@car.to_s} #{@cdr.to_s_helper}"
end
|
#traverse(path) ⇒ Object
178
179
180
181
182
183
184
|
# File 'lib/rubylisp/cons_cell.rb', line 178
def traverse(path)
return self if path.empty?
next_cell = (path[0] == ?a) ? @car : @cdr
return next_cell if path.length == 1
return nil if next_cell.nil? || !next_cell.pair?
next_cell.traverse(path[1..-1])
end
|
270
271
272
|
# File 'lib/rubylisp/cons_cell.rb', line 270
def true?
true
end
|
113
114
115
|
# File 'lib/rubylisp/cons_cell.rb', line 113
def type
:pair
end
|
16
17
18
|
# File 'lib/rubylisp/cons_cell.rb', line 16
def value
self
end
|
104
105
106
|
# File 'lib/rubylisp/cons_cell.rb', line 104
def vector?
false
end
|
60
61
62
|
# File 'lib/rubylisp/cons_cell.rb', line 60
def zero?
false
end
|