class Lisp::ConsCell
Attributes
car[R]
cdr[R]
Public Class Methods
array_to_list(cells, tail=nil)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 180 def self.array_to_list(cells, tail=nil) return cons() if cells.empty? && tail.nil? head = ConsCell.new last_cell = head (0...cells.length).each do |i| new_cell = self.cons(cells[i], 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)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 11 def self.cons(a=nil, b=nil) b = nil if b.pair? && b.empty? ConsCell.new(a, b) end
new(car=nil, cdr=nil)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 16 def initialize(car=nil, cdr=nil) @car = car @cdr = cdr end
Public Instance Methods
character?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 54 def character? false end
each() { |car| ... }
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 169 def each &block return if empty? c = self if self.length > 0 until c.nil? yield c.car c = c.cdr end end end
empty?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 46 def empty? @car.nil? && @cdr.nil? end
eq?(other)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 110 def eq?(other) return true if empty? && (other.nil? || (other.pair? && other.empty?)) other.pair? && self == other end
equal?(other)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 120 def equal?(other) return true if empty? && (other.nil? || (other.pair? && other.empty?)) return false unless other.pair? @car.equal?(other.car) && @cdr.equal?(other.cdr) end
eqv?(other)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 115 def eqv?(other) return true if empty? && (other.nil? || (other.pair? && other.empty?)) other.pair? && self == other end
evaluate(env)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 256 def evaluate(env) return self if empty? sexpr = if @car.symbol? throw :named_let_application, @cdr.to_a.map {|a| a.evaluate(env)} if @car.eq?(Lisp.named_let_stack[-1]) key = @car frame = nth(1) value = nth(2) 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])]) elsif s.end_with?(":>") ConsCell.array_to_list([Symbol.named("send"), frame, Symbol.named(s[0..-2])] << self.cdddr) elsif s.end_with?(":^") ConsCell.array_to_list([Symbol.named("send-super"), frame, Symbol.named(s[0..-2])] << self.cdddr) else self end else self end sexpr.inner_eval(env) end
evaluate_each(env)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 284 def evaluate_each(env) return nil if empty? result = @car.evaluate(env) return result if @cdr.nil? @cdr.evaluate_each(env) end
false?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 301 def false? false end
flatten()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 321 def flatten ary = [] to_a.each do |s| if s.nil? ary << nil elsif s.list? s.to_a.each {|e| ary << e} else ary << s end end ConsCell.array_to_list(ary) end
frame?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 102 def frame? false end
function?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 86 def function? false end
inner_eval(env)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 233 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? push_current_code 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.pop_code if !Lisp::Debug.eval_in_debug_repl && Lisp::Debug.interactive Lisp::Debug.log_result(result, env) result end
last()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 309 def last c = self while !c.cdr.nil? && c.cdr.pair? do c = c.cdr end c end
length()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 291 def length return 0 if empty? return 1 if @cdr.nil? return 1 + @cdr.length end
lisp_object?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 29 def lisp_object? true end
list?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 98 def list? true end
macro?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 90 def macro? false end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/rubylisp/cons_cell.rb, line 202 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
negative?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 70 def negative? false end
nth(n)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 210 def nth(n) c = self n.times {|i| c = c.cdr} c.car end
nth_tail(n)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 216 def nth_tail(n) c = self n.times {|i| c = c.cdr} c end
number?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 58 def number? false end
objc_object_or_nil(obj)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 222 def objc_object_or_nil(obj) return nil unless obj.object? return obj.value end
pair?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 94 def pair? true end
positive?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 62 def positive? false end
primitive?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 78 def primitive? false end
print_ary(a)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 317 def print_ary(a) (0...a.length).map {|i| puts (a[i].nil? ? "nil" : a[i])} end
print_string()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 149 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()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 145 def print_string_helper @cdr.nil? ? "#{@car.print_string}" : "#{@car.print_string} #{@cdr.print_string_helper}" end
push_current_code()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 228 def push_current_code env.push_code(self.print_string) end
quoted()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 305 def quoted Lisp::ConsCell.array_to_list([Symbol.named("quote"), self]) end
set_car!(d)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 25 def set_car!(d) @car = d end
set_cdr!(d)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 33 def set_cdr!(d) @cdr = d end
set_location(type, package, file, start, length)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 7 def set_location(type, package, file, start, length) end
set_nth!(n, d)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 38 def set_nth!(n, d) return nil if empty? c = self n.times {|i| c = c.cdr} c.set_car!(d) end
special?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 82 def special? false end
string?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 50 def string? false end
symbol?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 74 def symbol? false end
to_a()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 158 def to_a a = [] return a if empty? c = self until c.nil? a << c.car c = c.cdr end a end
to_s()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 136 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()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 130 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)
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 193 def traverse(path) next_cell = self path.chars.each do |p| return nil if next_cell.nil? || !next_cell.pair? next_cell = ((p == ?a) ? next_cell.car : next_cell.cdr) end next_cell end
true?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 297 def true? true end
type()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 126 def type :pair end
value()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 21 def value self end
vector?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 106 def vector? false end
zero?()
click to toggle source
# File lib/rubylisp/cons_cell.rb, line 66 def zero? false end