From 4ba3eda57aa6fc4230695e7da7aed5f9d094e104 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 26 Feb 2026 21:39:11 -0700 Subject: [PATCH] More pretty printer stuff. fixing infix. fixing indents --- src/ast.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index 028c8c8..5b4ab12 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -173,6 +173,11 @@ export type AST = | Definition | Rebind +const infixOps: { [key: string]: string } = { + cat: '&', add: '+', sub: '-', mul: '*', div: '/', mod: '%', + pow: '^', eq: '==', neq: '!=', gt: '>', lt: '<', gte: '>=', lte: '<=' +}; + export function prettyPrint(ast: AST, indent = 0): string { const i = ' '.repeat(indent); @@ -195,6 +200,13 @@ export function prettyPrint(ast: AST, indent = 0): string { return ast.name; case 'apply': + // infix ops + if (ast.func.kind === 'variable' && ast.args.length === 2 && infixOps[ast.func.name]) { + const left = prettyPrint(ast.args[0], indent); + const right = prettyPrint(ast.args[1], indent); + return `${left} ${infixOps[ast.func.name]} ${right}`; + } + const func = prettyPrint(ast.func, indent); const args = ast.args.map(a => { const printed = prettyPrint(a, indent); @@ -204,6 +216,9 @@ export function prettyPrint(ast: AST, indent = 0): string { return printed; }).join(' '); + if (ast.func.kind === 'variable' || ast.func.kind === 'constructor') { + return `${func} ${args}` + } return `(${func} ${args})` case 'let': @@ -212,7 +227,8 @@ export function prettyPrint(ast: AST, indent = 0): string { case 'list': { const elems = ast.elements.map(e => prettyPrint(e, indent + 1)) - if (elems.length <= 1) return `[${elems.join(', ')}]`; + const oneLine = `[${elems.join(', ')}]`; + if (oneLine.length <= 60 || elems.length <= 1) return oneLine; const inner = elems.map(e => `${' '.repeat(indent + 1)}${e}`).join(',\n'); return `[\n${inner}\n${i}]`; } @@ -232,8 +248,8 @@ export function prettyPrint(ast: AST, indent = 0): string { const body = prettyPrint(ast.body, indent + 1); const isComplex = ast.body.kind === 'match' || ast.body.kind === 'let'; if (isComplex) { - return `${params} \\\n${body}` - + const ii = ' '.repeat(indent + 1); + return `${params} \\\n${ii}${body}` } return `${params} \\ ${body}` } @@ -257,7 +273,7 @@ export function prettyPrint(ast: AST, indent = 0): string { const needsParens = c.result.kind === 'match' || c.result.kind === 'let'; return `${i}| ${prettyPrintPattern(c.pattern)} \\ ${needsParens ? '(' : ''}${result}${needsParens ? ')' : ''}`; }).join('\n'); - return `${i}${expr}\n${cases}`; + return `${expr}\n${cases}`; case 'rebind': return `${prettyPrint(ast.target, indent)} := ${prettyPrint(ast.value, indent)}`;