A fork of Crisp for HARP
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.1 KiB

  1. require "./types"
  2. def pr_str(value, print_readably = true)
  3. case value
  4. when Nil then "nil"
  5. when Bool then value.to_s
  6. when Int32 then value.to_s
  7. when Mal::List then "(#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")})"
  8. when Mal::Vector then "[#{value.map{|v| pr_str(v, print_readably) as String}.join(" ")}]"
  9. when Mal::Symbol then value.str.to_s
  10. when Mal::Func then "<function>"
  11. when Mal::Closure then "<closure>"
  12. when Mal::HashMap
  13. # step1_read_print.cr requires specifying type
  14. "{#{value.map{|k, v| "#{pr_str(k, print_readably)} #{pr_str(v, print_readably)}" as String}.join(" ")}}"
  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. when Mal::Atom
  25. "(atom #{pr_str(value.val, print_readably)})"
  26. else
  27. raise "invalid MalType: #{value.to_s}"
  28. end
  29. end
  30. def pr_str(t : Mal::Type, print_readably = true)
  31. pr_str(t.unwrap, print_readably) + (t.macro? ? " (macro)" : "")
  32. end