Class: Nydp::Pair
- Inherits:
-
Object
show all
- Extended by:
- Helper
- Includes:
- Enumerable, Helper
- Defined in:
- lib/nydp/pair.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Helper
cons, list, literal?, pair?, sym, sym?
Constructor Details
#initialize(car, cdr) ⇒ Pair
Returns a new instance of Pair.
7
8
9
|
# File 'lib/nydp/pair.rb', line 7
def initialize car, cdr
@car, @cdr = car, cdr
end
|
Instance Attribute Details
#car ⇒ Object
Returns the value of attribute car.
5
6
7
|
# File 'lib/nydp/pair.rb', line 5
def car
@car
end
|
#cdr ⇒ Object
Returns the value of attribute cdr.
5
6
7
|
# File 'lib/nydp/pair.rb', line 5
def cdr
@cdr
end
|
Class Method Details
.from_list(list, last = Nydp.NIL, n = 0) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/nydp/pair.rb', line 29
def self.from_list list, last=Nydp.NIL, n=0
if n >= list.size
last
else
mk list[n], from_list(list, last, n+1)
end
end
|
.mk(a, b) ⇒ Object
11
12
13
|
# File 'lib/nydp/pair.rb', line 11
def self.mk a, b
new a, b
end
|
.parse_list(list) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/nydp/pair.rb', line 21
def self.parse_list list
if sym? list.slice(-2), "."
from_list(list[0...-2], list.slice(-1))
else
from_list list
end
end
|
Instance Method Details
#+(other) ⇒ Object
41
42
43
|
# File 'lib/nydp/pair.rb', line 41
def + other
copy.append other
end
|
#==(other) ⇒ Object
45
46
47
|
# File 'lib/nydp/pair.rb', line 45
def == other
(other.respond_to? :car) && (self.car == other.car) && (self.cdr == other.cdr)
end
|
#append(thing) ⇒ Object
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/nydp/pair.rb', line 116
def append thing
if Nydp.NIL.is? self.cdr
self.cdr = thing
elsif pair? self.cdr
self.cdr.append thing
else
raise "can't append #{thing} to list #{self} : cdr is #{self.cdr.inspect}"
end
self
end
|
#caar ⇒ Object
16
|
# File 'lib/nydp/pair.rb', line 16
def caar ; car.car ; end
|
#cadr ⇒ Object
17
|
# File 'lib/nydp/pair.rb', line 17
def cadr ; cdr.car ; end
|
#cdar ⇒ Object
18
|
# File 'lib/nydp/pair.rb', line 18
def cdar ; car.cdr ; end
|
#cddr ⇒ Object
19
|
# File 'lib/nydp/pair.rb', line 19
def cddr ; cdr.cdr ; end
|
#copy ⇒ Object
37
38
39
|
# File 'lib/nydp/pair.rb', line 37
def copy
cons(car, cdr.copy)
end
|
#each {|car| ... } ⇒ Object
53
54
55
56
|
# File 'lib/nydp/pair.rb', line 53
def each &block
yield car
cdr.each(&block) unless Nydp.NIL.is?(cdr)
end
|
#inspect ⇒ Object
58
59
60
|
# File 'lib/nydp/pair.rb', line 58
def inspect
"(#{inspect_rest})"
end
|
#inspect_rest ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/nydp/pair.rb', line 104
def inspect_rest
cdr_s = if cdr.is_a?(self.class)
cdr.inspect_rest
elsif cdr == Nydp.NIL
nil
else
". #{cdr.inspect}"
end
[car.inspect, cdr_s].compact.join " "
end
|
#nydp_type ⇒ Object
15
|
# File 'lib/nydp/pair.rb', line 15
def nydp_type ; :pair ; end
|
#repush(instructions, _) ⇒ Object
127
128
129
|
# File 'lib/nydp/pair.rb', line 127
def repush instructions, _
instructions.push self
end
|
#size ⇒ Object
49
50
51
|
# File 'lib/nydp/pair.rb', line 49
def size
1 + (cdr.is_a?(Nydp::Pair) ? cdr.size : 0)
end
|
#to_s ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/nydp/pair.rb', line 62
def to_s
if car.is_a?(Nydp::Symbol) && car.is?(:quote)
if Nydp.NIL.is? cdr.cdr
"'#{cdr.car.to_s}"
else
"'#{cdr.to_s}"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:quasiquote)
if Nydp.NIL.is? cdr.cdr
"`#{cdr.car.to_s}"
else
"`#{cdr.to_s}"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:unquote)
if Nydp.NIL.is? cdr.cdr
",#{cdr.car.to_s}"
else
",#{cdr.to_s}"
end
elsif car.is_a?(Nydp::Symbol) && car.is?(:"unquote-splicing")
if Nydp.NIL.is? cdr.cdr
",@#{cdr.car.to_s}"
else
",@#{cdr.to_s}"
end
else
"(#{to_s_rest})"
end
end
|
#to_s_rest ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/nydp/pair.rb', line 92
def to_s_rest
cdr_s = if cdr.is_a?(self.class)
cdr.to_s_rest
elsif Nydp.NIL.is? cdr
nil
else
". #{cdr.to_s}"
end
[car.to_s, cdr_s].compact.join " "
end
|