mirror of
https://github.com/kanaka/mal.git
synced 2024-09-19 09:38:28 +03:00
8a19f60386
- 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.
51 lines
1.4 KiB
JavaScript
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;
|
|
|