More pretty printer stuff. fixing infix. fixing indents

master
Dustin Swan 14 hours ago
parent 15f6124173
commit 4ba3eda57a
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -173,6 +173,11 @@ export type AST =
| Definition | Definition
| Rebind | 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 { export function prettyPrint(ast: AST, indent = 0): string {
const i = ' '.repeat(indent); const i = ' '.repeat(indent);
@ -195,6 +200,13 @@ export function prettyPrint(ast: AST, indent = 0): string {
return ast.name; return ast.name;
case 'apply': 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 func = prettyPrint(ast.func, indent);
const args = ast.args.map(a => { const args = ast.args.map(a => {
const printed = prettyPrint(a, indent); const printed = prettyPrint(a, indent);
@ -204,6 +216,9 @@ export function prettyPrint(ast: AST, indent = 0): string {
return printed; return printed;
}).join(' '); }).join(' ');
if (ast.func.kind === 'variable' || ast.func.kind === 'constructor') {
return `${func} ${args}`
}
return `(${func} ${args})` return `(${func} ${args})`
case 'let': case 'let':
@ -212,7 +227,8 @@ export function prettyPrint(ast: AST, indent = 0): string {
case 'list': { case 'list': {
const elems = ast.elements.map(e => prettyPrint(e, indent + 1)) 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'); const inner = elems.map(e => `${' '.repeat(indent + 1)}${e}`).join(',\n');
return `[\n${inner}\n${i}]`; 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 body = prettyPrint(ast.body, indent + 1);
const isComplex = ast.body.kind === 'match' || ast.body.kind === 'let'; const isComplex = ast.body.kind === 'match' || ast.body.kind === 'let';
if (isComplex) { if (isComplex) {
return `${params} \\\n${body}` const ii = ' '.repeat(indent + 1);
return `${params} \\\n${ii}${body}`
} }
return `${params} \\ ${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'; const needsParens = c.result.kind === 'match' || c.result.kind === 'let';
return `${i}| ${prettyPrintPattern(c.pattern)} \\ ${needsParens ? '(' : ''}${result}${needsParens ? ')' : ''}`; return `${i}| ${prettyPrintPattern(c.pattern)} \\ ${needsParens ? '(' : ''}${result}${needsParens ? ')' : ''}`;
}).join('\n'); }).join('\n');
return `${i}${expr}\n${cases}`; return `${expr}\n${cases}`;
case 'rebind': case 'rebind':
return `${prettyPrint(ast.target, indent)} := ${prettyPrint(ast.value, indent)}`; return `${prettyPrint(ast.target, indent)} := ${prettyPrint(ast.value, indent)}`;

Loading…
Cancel
Save