Class: QQuote

Inherits:
Object
  • Object
show all
Defined in:
lib/qtypes.rb

Overview

class for quark quotes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p, b) ⇒ QQuote

Returns a new instance of QQuote.



64
65
66
67
# File 'lib/qtypes.rb', line 64

def initialize(p, b)
  @pattern = p
  @body = b
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



62
63
64
# File 'lib/qtypes.rb', line 62

def body
  @body
end

#patternObject

Returns the value of attribute pattern.



62
63
64
# File 'lib/qtypes.rb', line 62

def pattern
  @pattern
end

Instance Method Details

#==(x) ⇒ Object



84
85
86
87
88
# File 'lib/qtypes.rb', line 84

def ==(x)
  if x.is_a? QQuote
    (@pattern == x.pattern) && (@body == x.body)
  else false end
end

#bind(bindings) ⇒ Object

takes a hash of var strings to quark items and recursively subs its body



103
104
105
106
107
108
109
110
# File 'lib/qtypes.rb', line 103

def bind bindings
  @body.map! do |x|
    if x.is_a? QQuote then x.bind bindings
    elsif x.is_a? QAtom then bindings[x.val] || x
    else x end
  end
  return self
end

#dupObject



80
81
82
# File 'lib/qtypes.rb', line 80

def dup
  Marshal.load(Marshal.dump(self))
end

#popObject

pops from quark body



98
99
100
# File 'lib/qtypes.rb', line 98

def pop
  @body.pop
end

#push(x) ⇒ Object

pushes to quote body



93
94
95
# File 'lib/qtypes.rb', line 93

def push x
  @body.push x
end

#qtypeObject



90
# File 'lib/qtypes.rb', line 90

def qtype; :Quote end

#to_s(n = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/qtypes.rb', line 69

def to_s(n=nil)
  return "[]" if @body.empty? and @pattern.empty?
  serialize = lambda do |arr|
    xs = n ? arr.first(n) : arr
    xs.map { |x| x.is_a?(QQuote) ? x.to_s(n) : x.to_s }.join(' ') + (n && arr.length > n ? ' ...' : '')
  end
  pattern_str = " #{serialize.call(@pattern)} |" if !@pattern.empty?
  body_str = " #{serialize.call(@body)} " if !@body.empty?
  "[#{pattern_str}#{body_str}]"
end