Parsing bindings. adding AST pretty printer
This commit is contained in:
parent
1fc116f2fe
commit
0f0371461d
4 changed files with 64 additions and 42 deletions
36
src/ast.ts
36
src/ast.ts
|
|
@ -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}`
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue