Move language files into dedicated folder
This commit is contained in:
parent
3d1cd89067
commit
1b406899e0
15 changed files with 7 additions and 343 deletions
|
|
@ -1,55 +0,0 @@
|
|||
// src/debug/value_string.ts
|
||||
|
||||
import { Value, Env, Closure, EnvFrame } from '../value';
|
||||
import { exprToString, productPatternToString } from './expr_show';
|
||||
|
||||
export function valueToString(val: Value): string {
|
||||
switch (val.tag) {
|
||||
case "number": return val.value.toString();
|
||||
case "string": return `"${val.value}"`;
|
||||
case "tag": return `#${val.tag_name}`;
|
||||
case "tagged": return `#${val.tag_name} ${valueToString(val.value)}`;
|
||||
case "tuple": return `(${val.values.map(valueToString).join(", ")})`;
|
||||
case "record": {
|
||||
const entries = Array.from(val.fields.entries())
|
||||
.map(([k, v]) => `${k} = ${valueToString(v)}`)
|
||||
.join(", ");
|
||||
return `{ ${entries} }`;
|
||||
}
|
||||
case "closure": return closureToString(val.closure);
|
||||
}
|
||||
}
|
||||
|
||||
function closureToString(c: Closure): string {
|
||||
const params = c.parameters.map(productPatternToString).join(", ");
|
||||
const envStr = envToString(c.env);
|
||||
// We represent the closure as the code + a summary of its captured scope
|
||||
return `fn { ${params} . ${exprToString(c.body)} } [captured: ${envStr}]`;
|
||||
}
|
||||
|
||||
function envToString(env: Env): string {
|
||||
if (env.tag === "nil") return "∅";
|
||||
|
||||
const frames: string[] = [];
|
||||
let current: Env = env;
|
||||
|
||||
while (current.tag === "frame") {
|
||||
frames.push(frameToString(current.frame));
|
||||
current = current.parent;
|
||||
}
|
||||
|
||||
// Shows stack from inner-most to outer-most
|
||||
return frames.join(" ⮕ ");
|
||||
}
|
||||
|
||||
function frameToString(frame: EnvFrame): string {
|
||||
const entries = Array.from(frame.entries());
|
||||
if (entries.length === 0) return "{}";
|
||||
|
||||
const formattedEntries = entries.map(([name, val]) => {
|
||||
// We call valueToString here to show the actual data
|
||||
return `${name} = ${valueToString(val)}`;
|
||||
});
|
||||
|
||||
return `{ ${formattedEntries.join(", ")} }`;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue