diff --git a/src/ast.ts b/src/ast.ts index 5b4ab12..36cb678 100644 --- a/src/ast.ts +++ b/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'; diff --git a/src/cg/03-ui-components.cg b/src/cg/03-ui-components.cg index 5ca8f0e..d91ce72 100644 --- a/src/cg/03-ui-components.cg +++ b/src/cg/03-ui-components.cg @@ -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 \ []) diff --git a/src/cg/06-textEditor.cg b/src/cg/06-textEditor.cg index 215b632..c626821 100644 --- a/src/cg/06-textEditor.cg +++ b/src/cg/06-textEditor.cg @@ -1,3 +1,4 @@ +textEditorBuffers = []; textEditor = name \ # defaults = {}; # c = { ...defaults, ...config };