More pretty printer

master
Dustin Swan 3 days ago
parent 4904b7df7f
commit 295a8a5e24
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -178,6 +178,8 @@ const infixOps: { [key: string]: string } = {
pow: '^', eq: '==', neq: '!=', gt: '>', lt: '<', gte: '>=', lte: '<=' pow: '^', eq: '==', neq: '!=', gt: '>', lt: '<', gte: '>=', lte: '<='
}; };
const isInfix = (a: AST) => a.kind === 'apply' && a.func.kind === 'variable' && a.args.length === 2 && infixOps[a.func.name];
export function prettyPrint(ast: AST, indent = 0): string { export function prettyPrint(ast: AST, indent = 0): string {
const i = ' '.repeat(indent); const i = ' '.repeat(indent);
@ -201,25 +203,33 @@ export function prettyPrint(ast: AST, indent = 0): string {
case 'apply': case 'apply':
// infix ops // infix ops
if (ast.func.kind === 'variable' && ast.args.length === 2 && infixOps[ast.func.name]) { if (isInfix(ast)) {
const left = prettyPrint(ast.args[0], indent); const wrapIfNeeded = (a: AST) => {
const right = prettyPrint(ast.args[1], indent); const printed = prettyPrint(a, indent);
return `${left} ${infixOps[ast.func.name]} ${right}`; if (a.kind === 'apply' || a.kind === 'lambda' || a.kind === 'match' || a.kind === 'let') {
return `(${printed})`;
}
return `${printed}`;
}
const left = wrapIfNeeded(ast.args[0]);
const right = wrapIfNeeded(ast.args[1]);
return `${left} ${infixOps[(ast.func as Variable).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);
if (a.kind === 'lambda' || a.kind === 'match' || a.kind === 'let' || a.kind === 'rebind') { if (a.kind === 'lambda' || a.kind === 'match' || a.kind === 'let' || a.kind === 'rebind' || a.kind === 'apply') {
return `(${printed})`; return `(${printed})`;
} }
return printed; return printed;
}).join(' '); }).join(' ');
if (ast.func.kind === 'variable' || ast.func.kind === 'constructor') { if (ast.func.kind === 'lambda' || ast.func.kind === 'match' || ast.func.kind === 'let') {
return `${func} ${args}` return `(${func} ${args})`
} }
return `(${func} ${args})` return `${func} ${args}`
case 'let': case 'let':
const sep = indent === 0 ? '\n\n' : '\n'; const sep = indent === 0 ? '\n\n' : '\n';

@ -134,8 +134,8 @@ scrollable = config \
| False \ []), | False \ []),
...(showHBar ...(showHBar
| True \ [ui.positioned { | True \ [ui.positioned {
x = c.h - 4, x = hBarX,
y = hBarX, y = c.h - 4,
child = ui.rect { h = 4, w = hBarWidth, color = "rgba(255,255,255,0.3)", radius = 2 } child = ui.rect { h = 4, w = hBarWidth, color = "rgba(255,255,255,0.3)", radius = 2 }
}] }]
| False \ []) | False \ [])

@ -1,3 +1,4 @@
textEditorBuffers = [];
textEditor = name \ textEditor = name \
# defaults = {}; # defaults = {};
# c = { ...defaults, ...config }; # c = { ...defaults, ...config };

Loading…
Cancel
Save