crystal: refactor by adding Array to Mal type conversion
[jackhill/mal.git] / crystal / printer.cr
CommitLineData
fdf28b76 1require "./types"
2
448769e3 3def pr_str(value, print_readably = true)
fdf28b76 4 case value
436f169c 5 when Nil then "nil"
6 when Bool then value.to_s
7 when Int32 then value.to_s
8 when Mal::List then "(#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")})"
9 when Mal::Vector then "[#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")}]"
10 when Mal::Symbol then value.str.to_s
11 when Mal::Func then "<function>"
12 when Mal::Closure then "<closure>"
afc3a8d5 13 when Mal::HashMap
436f169c 14 "{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}"}.join(" ")}}"
afc3a8d5 15 when String
16 case
17 when value.empty?()
18 print_readably ? value.inspect : value
19 when value[0] == '\u029e'
20 ":#{value[1..-1]}"
21 else
22 print_readably ? value.inspect : value
23 end
24 else
25 raise "invalid MalType: #{value.to_s}"
fdf28b76 26 end
27end
7fe6282e 28
29def pr_str(t : Mal::Type, print_readably = true)
9bbb8ccc 30 pr_str(t.unwrap, print_readably) + (t.macro? ? " (macro)" : "")
7fe6282e 31end