1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 17:47:53 +03:00
mal/impls/wren/printer.wren
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

31 lines
1.2 KiB
Plaintext

import "./types" for MalVal, MalList, MalVector, MalMap, MalNativeFn, MalFn, MalAtom
class Printer {
static joinElements(elements, print_readably) {
return elements.map { |e| pr_str(e, print_readably) }.join(" ")
}
static joinMapElements(data, print_readably) {
return data.map { |e| pr_str(e.key, print_readably) + " " + pr_str(e.value, print_readably) }.join(" ")
}
static escape(s) {
return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\""
}
static pr_str(obj) { pr_str(obj, true) }
static pr_str(obj, print_readably) {
if (obj == null) return "nil"
if (obj is MalList) return "(%(joinElements(obj.elements, print_readably)))"
if (obj is MalVector) return "[%(joinElements(obj.elements, print_readably))]"
if (obj is MalMap) return "{%(joinMapElements(obj.data, print_readably))}"
if (obj is MalNativeFn) return "#<MalNativeFn>"
if (obj is MalFn) return "#<MalFn>"
if (obj is MalAtom) return "(atom %(pr_str(obj.value, print_readably)))"
if (MalVal.isKeyword(obj)) return ":%(obj[1..-1])"
if (obj is String) return print_readably ? escape(obj) : obj
return obj.toString
}
}