Parsing bindings. adding AST pretty printer

This commit is contained in:
Dustin Swan 2026-02-01 00:43:01 -07:00
parent 1fc116f2fe
commit 0f0371461d
No known key found for this signature in database
GPG key ID: 30D46587E2100467
4 changed files with 64 additions and 42 deletions

View file

@ -122,3 +122,39 @@ export type AST =
| Definition
| TypeDef
| Import
export function prettyPrint(ast: AST, indent = 0): string {
const i = ' '.repeat(indent);
switch (ast.kind) {
case 'literal':
return `${i}${ast.value.value}`;
case 'variable':
return `${i}${ast.name}`;
case 'constructor':
return `${i}${ast.name}`;
case 'apply':
const func = prettyPrint(ast.func, 0);
const args = ast.args.map(a => prettyPrint(a, 0)).join(' ');
return `${i}(${func} ${args})`
case 'let':
return `${i}let ${ast.name} = \n${prettyPrint(ast.value, indent + 1)}\n${i}in\n${prettyPrint(ast.body, indent + 1)}`
case 'list':
const elems = ast.elements.map(e => prettyPrint(e, 0)).join(', ');
return `${i}[${elems}]`;
case 'record':
const fields = Object.entries(ast.fields)
.map(([k, v]) => `${k} = ${prettyPrint(v, 0)}`)
.join(', ');
return `${i}{${fields}}`;
default:
return `${i}${ast.kind}`
}
}