Module: RubyLisp::Printer

Defined in:
lib/rubylisp/printer.rb

Class Method Summary collapse

Class Method Details

.pr_str(*xs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubylisp/printer.rb', line 7

def pr_str(*xs)
  if xs.count > 1
    xs.map {|x| pr_str(x)}.join(' ')
  else
    x = xs.first
    case x
    when Array, Hamster::Vector
      "[#{x.map {|item| pr_str(item)}.join(' ')}]"
    when Hash
      "{#{x.map {|k, v| "#{pr_str(k)} #{pr_str(v)}"}.join ", "}}"
    when Hamster::Hash
      pr_str x.to_hash
    when Hamster::List
      "(#{x.map {|item| pr_str(item)}.join(' ')})"
    when Function
      "#<#{x.is_macro ? 'Macro' : 'Function'}: #{x.name}>"
    when Symbol
      x.value
    when Value
      x.value.inspect
    when Object::Symbol
      ":#{x.name}"
    else
      x.inspect
    end
  end
end