More pretty printer
This commit is contained in:
parent
4904b7df7f
commit
295a8a5e24
3 changed files with 21 additions and 10 deletions
26
src/ast.ts
26
src/ast.ts
|
|
@ -178,6 +178,8 @@ const infixOps: { [key: string]: string } = {
|
|||
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 {
|
||||
const i = ' '.repeat(indent);
|
||||
|
||||
|
|
@ -201,25 +203,33 @@ export function prettyPrint(ast: AST, indent = 0): string {
|
|||
|
||||
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}`;
|
||||
if (isInfix(ast)) {
|
||||
const wrapIfNeeded = (a: AST) => {
|
||||
const printed = prettyPrint(a, indent);
|
||||
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 args = ast.args.map(a => {
|
||||
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;
|
||||
}).join(' ');
|
||||
|
||||
if (ast.func.kind === 'variable' || ast.func.kind === 'constructor') {
|
||||
return `${func} ${args}`
|
||||
if (ast.func.kind === 'lambda' || ast.func.kind === 'match' || ast.func.kind === 'let') {
|
||||
return `(${func} ${args})`
|
||||
}
|
||||
return `(${func} ${args})`
|
||||
return `${func} ${args}`
|
||||
|
||||
case 'let':
|
||||
const sep = indent === 0 ? '\n\n' : '\n';
|
||||
|
|
|
|||
|
|
@ -134,8 +134,8 @@ scrollable = config \
|
|||
| False \ []),
|
||||
...(showHBar
|
||||
| True \ [ui.positioned {
|
||||
x = c.h - 4,
|
||||
y = hBarX,
|
||||
x = hBarX,
|
||||
y = c.h - 4,
|
||||
child = ui.rect { h = 4, w = hBarWidth, color = "rgba(255,255,255,0.3)", radius = 2 }
|
||||
}]
|
||||
| False \ [])
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
textEditorBuffers = [];
|
||||
textEditor = name \
|
||||
# defaults = {};
|
||||
# c = { ...defaults, ...config };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue