1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-17 17:50:24 +03:00
mal/impls/scala/printer.scala
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

44 lines
1.2 KiB
Scala

import types.{MalList, MalVector, MalHashMap, MalFunction}
object printer {
def _pr_str(obj: Any, print_readably: Boolean = true): String = {
val _r = print_readably
return obj match {
case v: MalVector => v.toString(_r)
case l: MalList => l.toString(_r)
case hm: MalHashMap => hm.toString(_r)
case s: String => {
if (s.length > 0 && s(0) == '\u029e') {
":" + s.substring(1,s.length)
} else if (_r) {
//println("here1: " + s)
"\"" + s.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n") + "\""
} else {
s
}
}
case Symbol(s) => s
case a: types.Atom => "(atom " + a.value + ")"
case null => "nil"
case _ => {
if (obj.isInstanceOf[MalFunction]) {
val f = obj.asInstanceOf[MalFunction]
"<function (fn* " + _pr_str(f.params) + " " + _pr_str(f.ast) + ")>"
} else {
obj.toString
}
}
}
}
def _pr_list(lst: List[Any], print_readably: Boolean = true,
sep: String = " "): String = {
lst.map{_pr_str(_, print_readably)}.mkString(sep)
}
}
// vim: ts=2:sw=2