From 023bfb2caa63685d48917de350f3e51161c5b1ff Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Mon, 13 Oct 2025 16:38:11 -0700 Subject: [PATCH] getting there bro --- src/compiler/compiler.ts | 23 +++++++++++------------ src/compiler/utils.ts | 7 ++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 262b91e..7cdd77d 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -25,6 +25,7 @@ export class Compiler { fnLabels = new Map() ifLabelCount = 0 bytecode: Bytecode + pipeCounter = 0 constructor(public input: string) { try { @@ -38,12 +39,6 @@ export class Compiler { this.#compileCst(cst, input) - throw new CompilerError( - 'I am a very long fake error to test scrolling and other things this is super long\nand has multiple lines\nand just keeps going and going and going', - 20, - 25 - ) - // Add the labels for (const [label, labelInstructions] of this.fnLabels) { this.instructions.push([`${label}:`]) @@ -146,7 +141,7 @@ export class Compiler { } case terms.FunctionDef: { - const { paramNames, bodyNode } = getFunctionDefParts(node, input) + const { paramNames, bodyNodes } = getFunctionDefParts(node, input) const instructions: ProgramItem[] = [] const functionLabel: Label = `.func_${this.fnLabels.size}` const bodyInstructions: ProgramItem[] = [] @@ -157,7 +152,9 @@ export class Compiler { this.fnLabels.set(functionLabel, bodyInstructions) instructions.push(['MAKE_FUNCTION', paramNames, functionLabel]) - bodyInstructions.push(...this.#compileNode(bodyNode, input)) + bodyNodes.forEach((bodyNode) => { + bodyInstructions.push(...this.#compileNode(bodyNode, input)) + }) return instructions } @@ -311,8 +308,10 @@ export class Compiler { const instructions: ProgramItem[] = [] instructions.push(...this.#compileNode(pipedFunctionCall, input)) + this.pipeCounter++ + const pipeValName = `_pipe_value_${this.pipeCounter}` pipeReceivers.forEach((pipeReceiver) => { - instructions.push(['STORE', '_pipe_value']) + instructions.push(['STORE', pipeValName]) const { identifierNode, namedArgs, positionalArgs } = getFunctionCallParts( pipeReceiver, @@ -333,12 +332,12 @@ export class Compiler { // If no underscore is explicitly used, add the piped value as the first positional arg if (shouldPushPositionalArg) { - instructions.push(['LOAD', '_pipe_value']) + instructions.push(['LOAD', pipeValName]) } positionalArgs.forEach((arg) => { if (arg.type.id === terms.Underscore) { - instructions.push(['LOAD', '_pipe_value']) + instructions.push(['LOAD', pipeValName]) } else { instructions.push(...this.#compileNode(arg, input)) } @@ -348,7 +347,7 @@ export class Compiler { const { name, valueNode } = getNamedArgParts(arg, input) instructions.push(['PUSH', name]) if (valueNode.type.id === terms.Underscore) { - instructions.push(['LOAD', '_pipe_value']) + instructions.push(['LOAD', pipeValName]) } else { instructions.push(...this.#compileNode(valueNode, input)) } diff --git a/src/compiler/utils.ts b/src/compiler/utils.ts index c308331..b1dfc11 100644 --- a/src/compiler/utils.ts +++ b/src/compiler/utils.ts @@ -59,9 +59,9 @@ export const getAssignmentParts = (node: SyntaxNode) => { export const getFunctionDefParts = (node: SyntaxNode, input: string) => { const children = getAllChildren(node) - const [fnKeyword, paramsNode, colon, bodyNode] = children + const [fnKeyword, paramsNode, colon, ...bodyNodes] = children - if (!fnKeyword || !paramsNode || !colon || !bodyNode) { + if (!fnKeyword || !paramsNode || !colon || !bodyNodes) { throw new CompilerError( `FunctionDef expected 5 children, got ${children.length}`, node.from, @@ -80,7 +80,8 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => { return input.slice(param.from, param.to) }) - return { paramNames, bodyNode } + const bodyWithoutEnd = bodyNodes.slice(0, -1) + return { paramNames, bodyNodes: bodyWithoutEnd } } export const getFunctionCallParts = (node: SyntaxNode, input: string) => {