1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 02:27:10 +03:00
mal/ts/printer.ts
2017-02-25 14:16:51 +09:00

43 lines
1.3 KiB
TypeScript

import { Node, MalType } from "./types";
export function prStr(v: MalType, printReadably = true): string {
switch (v.type) {
case Node.List:
return `(${v.list.map(v => prStr(v, printReadably)).join(" ")})`;
case Node.Vector:
return `[${v.list.map(v => prStr(v, printReadably)).join(" ")}]`;
case Node.HashMap:
let result = "{";
for (const [key, value] of v.entries()) {
if (result !== "{") {
result += " ";
}
result += `${prStr(key, printReadably)} ${prStr(value, printReadably)}`;
}
result += "}";
return result;
case Node.Number:
case Node.Symbol:
case Node.Boolean:
return `${v.v}`;
case Node.String:
if (printReadably) {
const str = v.v
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\n/g, "\\n");
return `"${str}"`;
} else {
return v.v;
}
case Node.Nil:
return "nil";
case Node.Keyword:
return `:${v.v}`;
case Node.Function:
return "#<function>";
case Node.Atom:
return `(atom ${prStr(v.v, printReadably)})`;
}
}