1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 10:07:45 +03:00
mal/crystal/printer.cr

27 lines
841 B
Crystal
Raw Normal View History

2015-05-04 06:12:24 +03:00
require "./types"
def pr_str(value, print_readably = true)
2015-05-04 06:12:24 +03:00
case value
when Nil then "nil"
when Bool then value.to_s
when Int32 then value.to_s
when Mal::List then "(#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")})"
when Mal::Vector then "[#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")}]"
2015-05-04 06:12:24 +03:00
when Mal::Symbol then value.val.to_s
when Proc then "<function>"
when Mal::HashMap
"{#{value.map{|k, v| "#{pr_str(k, print_readably) as String} #{pr_str(v, print_readably) as String}"}.join(" ")}}"
when String
case
when value.empty?()
print_readably ? value.inspect : value
when value[0] == '\u029e'
":#{value[1..-1]}"
else
print_readably ? value.inspect : value
end
else
raise "invalid MalType: #{value.to_s}"
2015-05-04 06:12:24 +03:00
end
end