DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / es6 / printer.mjs
1 import { _list_Q, _keyword_Q, Vector, Atom } from './types'
2
3 export function pr_str(obj, print_readably) {
4 if (typeof print_readably === 'undefined') { print_readably = true }
5 var _r = print_readably
6 if (_list_Q(obj)) {
7 return "(" + obj.map(e => pr_str(e,_r)).join(' ') + ")"
8 } else if (obj instanceof Vector) {
9 return "[" + obj.map(e => pr_str(e,_r)).join(' ') + "]"
10 } else if (obj instanceof Map) {
11 var ret = []
12 for (let [k,v] of obj) {
13 ret.push(pr_str(k,_r), pr_str(v,_r))
14 }
15 return "{" + ret.join(' ') + "}"
16 } else if (typeof obj === "string") {
17 if (_keyword_Q(obj)) {
18 return ':' + obj.slice(1)
19 } else if (_r) {
20 return '"' + obj.replace(/\\/g, "\\\\")
21 .replace(/"/g, '\\"')
22 .replace(/\n/g, "\\n") + '"'
23 } else {
24 return obj
25 }
26 } else if (typeof obj === 'symbol') {
27 return Symbol.keyFor(obj)
28 } else if (obj === null) {
29 return "nil"
30 } else if (obj instanceof Atom) {
31 return "(atom " + pr_str(obj.val,_r) + ")"
32 } else {
33 return obj.toString()
34 }
35 }