2014-12-19 08:52:59 +03:00
|
|
|
import types.{MalList, MalVector, MalHashMap, MalFunction}
|
|
|
|
|
2014-12-19 08:21:39 +03:00
|
|
|
|
|
|
|
object printer {
|
|
|
|
def _pr_str(obj: Any, print_readably: Boolean = true): String = {
|
|
|
|
val _r = print_readably
|
|
|
|
return obj match {
|
2014-12-19 08:52:59 +03:00
|
|
|
case v: MalVector => v.toString(_r)
|
|
|
|
case l: MalList => l.toString(_r)
|
|
|
|
case hm: MalHashMap => hm.toString(_r)
|
2014-12-19 08:21:39 +03:00
|
|
|
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 _ => {
|
2014-12-19 08:52:59 +03:00
|
|
|
if (obj.isInstanceOf[MalFunction]) {
|
|
|
|
val f = obj.asInstanceOf[MalFunction]
|
2014-12-19 08:21:39 +03:00
|
|
|
"<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
|