1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 02:45:44 +03:00
mal/es6/printer.js
Joel Martin a05c086f05 ES6: more use of ES6, simplifications, newer babel.
- Use Vector class derived from Array
- Use Array/Vector.from for initializing/cloning of Array/Vector
- Remove most semi-colon line endings
- More use of arrow functions
- Use Object.assign to copy properties in _malfunc and function
  cloning.
- Remove or inline a bunch of types.js functions that don't really
  need to be separate functions: _obj_type, _sequential_Q, _symbol,
  _symbol_Q, _vector, _vector_Q, _hash_map, _hash_map_Q
- Simplify dependency list in Makefile
- Remove some separate core.js functions by moving them into the
  core_ns declaration: _nth, keys, vals, with_meta.

With node 7, babel is mostly just used for translating imports into
CommonJS requires for node.
2017-02-10 23:02:30 -06:00

36 lines
1.2 KiB
JavaScript

import { _symbol, _list_Q, Vector, Atom } from './types'
export function pr_str(obj, print_readably) {
if (typeof print_readably === 'undefined') { print_readably = true }
var _r = print_readably
if (_list_Q(obj)) {
return "(" + obj.map(e => pr_str(e,_r)).join(' ') + ")"
} else if (obj instanceof Vector) {
return "[" + obj.map(e => pr_str(e,_r)).join(' ') + "]"
} else if (obj instanceof Map) {
var ret = []
for (let [k,v] of obj) {
ret.push(pr_str(k,_r), pr_str(v,_r))
}
return "{" + ret.join(' ') + "}"
} else if (typeof obj === "string") {
if (obj[0] === '\u029e') {
return ':' + obj.slice(1)
} else if (_r) {
return '"' + obj.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\n/g, "\\n") + '"'
} else {
return obj
}
} else if (typeof obj === 'symbol') {
return Symbol.keyFor(obj)
} else if (obj === null) {
return "nil"
} else if (obj instanceof Atom) {
return "(atom " + pr_str(obj.val,_r) + ")"
} else {
return obj.toString()
}
}