Remove gensym, inc and or from step files.
[jackhill/mal.git] / ts / printer.ts
CommitLineData
5bb7479d 1import { Node, MalType } from "./types";
f406f88b 2
3export function prStr(v: MalType, printReadably = true): string {
4 switch (v.type) {
5bb7479d 5 case Node.List:
dfe70453 6 return `(${v.list.map(v => prStr(v, printReadably)).join(" ")})`;
5bb7479d 7 case Node.Vector:
dfe70453 8 return `[${v.list.map(v => prStr(v, printReadably)).join(" ")}]`;
5bb7479d 9 case Node.HashMap:
f406f88b 10 let result = "{";
10f8aa84 11 for (const [key, value] of v.entries()) {
f406f88b 12 if (result !== "{") {
13 result += " ";
14 }
dfe70453 15 result += `${prStr(key, printReadably)} ${prStr(value, printReadably)}`;
f406f88b 16 }
17 result += "}";
18 return result;
5bb7479d 19 case Node.Number:
20 case Node.Symbol:
21 case Node.Boolean:
f406f88b 22 return `${v.v}`;
5bb7479d 23 case Node.String:
f406f88b 24 if (printReadably) {
25 const str = v.v
26 .replace(/\\/g, "\\\\")
27 .replace(/"/g, '\\"')
28 .replace(/\n/g, "\\n");
29 return `"${str}"`;
30 } else {
31 return v.v;
32 }
6071876f 33 case Node.Nil:
f406f88b 34 return "nil";
5bb7479d 35 case Node.Keyword:
10f8aa84 36 return `:${v.v}`;
5bb7479d 37 case Node.Function:
dfe70453 38 return "#<function>";
5bb7479d 39 case Node.Atom:
555f7fc7 40 return `(atom ${prStr(v.v, printReadably)})`;
f406f88b 41 }
42}