Class: Skeem::SkmPair

Inherits:
SkmElement show all
Defined in:
lib/skeem/skm_pair.rb

Instance Attribute Summary collapse

Attributes inherited from SkmElement

#position

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SkmElement

#boolean?, #bound!, #callable?, #char?, #complex?, #done!, #inspect, #integer?, #null?, #number?, #procedure?, #rational?, #real?, #skm_eq?, #string?, #symbol?, #vector?

Constructor Details

#initialize(head, tail) ⇒ SkmPair

Returns a new instance of SkmPair.



16
17
18
19
20
# File 'lib/skeem/skm_pair.rb', line 16

def initialize(head, tail)
  super(0)
  @car = head
  @cdr = tail
end

Instance Attribute Details

#carSkmElement Also known as: first

Returns:



8
9
10
# File 'lib/skeem/skm_pair.rb', line 8

def car
  @car
end

#cdrSkmElement

Returns:



11
12
13
# File 'lib/skeem/skm_pair.rb', line 11

def cdr
  @cdr
end

Class Method Details

.create_from_a(anArray) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/skeem/skm_pair.rb', line 46

def self.create_from_a(anArray)
  current = nil
  return SkmEmptyList.instance if anArray.empty?

  anArray.reverse_each do |elem|
    if current.nil?
      current = new(elem, SkmEmptyList.instance)
    else
      current = new(elem, current)
    end
  end

  current
end

Instance Method Details

#accept(aVisitor) ⇒ Object

Part of the 'visitee' role in Visitor design pattern.

Parameters:



189
190
191
# File 'lib/skeem/skm_pair.rb', line 189

def accept(aVisitor)
  aVisitor.visit_pair(self)
end

#append(anElement) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/skeem/skm_pair.rb', line 147

def append(anElement)
  last_one = last_pair
  if last_one.cdr.nil? || last_one.cdr.kind_of?(SkmEmptyList)
    last_one.cdr = SkmPair.new(anElement, SkmEmptyList.instance)
  else
    raise StandardError, "Cannot append #{anElement.inspect}"
  end
end

#append_list(aList) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/skeem/skm_pair.rb', line 156

def append_list(aList)
  last_one = last_pair
  if last_one.cdr.nil? || last_one.cdr.kind_of?(SkmEmptyList)
    last_one.cdr = aList
  else
    raise StandardError, "Cannot append list #{aList.inspect}"
  end
end

#clone_evaluate(aRuntime) ⇒ Object

Construct new instance with car and cdr respectively equal to car.evaluate and cdr.evaluate



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/skeem/skm_pair.rb', line 33

def clone_evaluate(aRuntime)
  new_car = car.evaluate(aRuntime)
  if cdr.nil?
    new_cdr = nil
  elsif cdr.kind_of?(SkmPair)
    new_cdr = cdr.clone_evaluate(aRuntime)
  else
    new_cdr = cdr.evaluate(aRuntime)
  end

  self.class.new(new_car, new_cdr)
end

#each(&aBlock) ⇒ Object



142
143
144
145
# File 'lib/skeem/skm_pair.rb', line 142

def each(&aBlock)
  aBlock.call(car)
  cdr.each(&aBlock) if cdr && !cdr.null?
end

#empty?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/skeem/skm_pair.rb', line 61

def empty?
  return false if car

  if [SkmPair, SkmEmptyList].include? cdr.class
    cdr.empty?
  else
    false
  end
end

#eqv?(_other) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/skeem/skm_pair.rb', line 118

def eqv?(_other)
  false
end

#evaluate(aRuntime) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/skeem/skm_pair.rb', line 165

def evaluate(aRuntime)
  return SkmEmptyList.instance if empty?

  if car.kind_of?(SkmIdentifier) && car.is_var_name
    result = aRuntime.evaluate_form(self)
  else
    begin
      result = clone_evaluate(aRuntime)
    rescue NoMethodError => e
      $stderr.puts "SkmPair#evaluate:   #{inspect}"
      $stderr.puts "SkmPair as Array:   #{to_a.inspect}"
      raise e
    end
  end
  result
end

#kloneObject



22
23
24
25
26
27
28
29
# File 'lib/skeem/skm_pair.rb', line 22

def klone
  if cdr.kind_of?(SkmPair)
    new_cdr = cdr.klone
  else
    new_cdr = cdr
  end
  self.class.new(car, new_cdr)
end

#lastObject



114
115
116
# File 'lib/skeem/skm_pair.rb', line 114

def last
  to_a.last
end

#last_pairObject



101
102
103
104
105
106
107
# File 'lib/skeem/skm_pair.rb', line 101

def last_pair
  if cdr.nil? || (cdr == SkmEmptyList.instance) || !cdr.kind_of?(SkmPair)
    self
  else
    cdr.last_pair
  end
end

#lengthObject



83
84
85
86
87
88
89
# File 'lib/skeem/skm_pair.rb', line 83

def length
  if [SkmPair, SkmEmptyList].include?(cdr.class)
    cdr.length + 1
  else
    raise StandardError, 'Improper list'
  end
end

#list?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/skeem/skm_pair.rb', line 71

def list?
  if cdr.nil?
    false
  else
    cdr.list?
  end
end

#membersObject



14
# File 'lib/skeem/skm_pair.rb', line 14

alias members to_a

#pair?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/skeem/skm_pair.rb', line 79

def pair?
  true
end

#proper?Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/skeem/skm_pair.rb', line 109

def proper?
  last_node = last_pair
  last_node.cdr.nil? || (last_node.cdr == SkmEmptyList.instance)
end

#quasiquote(aRuntime) ⇒ Object



182
183
184
185
# File 'lib/skeem/skm_pair.rb', line 182

def quasiquote(aRuntime)
  members_eval = to_a.map { |elem| elem.quasiquote(aRuntime) }
  self.class.create_from_a(members_eval)
end

#quoted!Object



193
194
195
196
# File 'lib/skeem/skm_pair.rb', line 193

def quoted!
  car.quoted!
  cdr&.quoted!
end

#skm_equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/skeem/skm_pair.rb', line 122

def skm_equal?(other)
  return true if equal?(other)

  equal = true
  if car.nil?
    equal = other.car.nil?
  else
    equal &&= car.skm_equal?(other.car)
  end
  return false unless equal

  if cdr.nil?
    equal &&= other.cdr.nil?
  else
    equal &&= cdr.skm_equal?(other.cdr)
  end

  equal
end

#to_aObject

Works correctly for proper lists.



92
93
94
95
96
97
98
99
# File 'lib/skeem/skm_pair.rb', line 92

def to_a
  result = [car]
  if cdr && !cdr.null?
    result.concat(cdr.to_a)
  end

  result
end

#unquoted!Object



198
199
200
201
# File 'lib/skeem/skm_pair.rb', line 198

def unquoted!
  car.unquoted!
  cdr&.unquoted!
end

#verbatim?Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
210
211
# File 'lib/skeem/skm_pair.rb', line 203

def verbatim?
  return false unless car.verbatim?

  if cdr.nil?
    true
  else
    cdr.verbatim?
  end
end