1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-18 02:00:40 +03:00
mal/impls/js/printer.js
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

51 lines
1.4 KiB
JavaScript

// Node vs browser behavior
var printer = {};
if (typeof module !== 'undefined') {
var types = require('./types');
// map output/print to console.log
printer.println = exports.println = function () {
console.log.apply(console, arguments);
};
}
function _pr_str(obj, print_readably) {
if (typeof print_readably === 'undefined') { print_readably = true; }
var _r = print_readably;
var ot = types._obj_type(obj);
switch (ot) {
case 'list':
var ret = obj.map(function(e) { return _pr_str(e,_r); });
return "(" + ret.join(' ') + ")";
case 'vector':
var ret = obj.map(function(e) { return _pr_str(e,_r); });
return "[" + ret.join(' ') + "]";
case 'hash-map':
var ret = [];
for (var k in obj) {
ret.push(_pr_str(k,_r), _pr_str(obj[k],_r));
}
return "{" + ret.join(' ') + "}";
case 'string':
if (obj[0] === '\u029e') {
return ':' + obj.slice(1);
} else if (_r) {
return '"' + obj.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\n/g, "\\n") + '"'; // string
} else {
return obj;
}
case 'keyword':
return ':' + obj.slice(1);
case 'nil':
return "nil";
case 'atom':
return "(atom " + _pr_str(obj.val,_r) + ")";
default:
return obj.toString();
}
}
exports._pr_str = printer._pr_str = _pr_str;