diff --git a/bun.lock b/bun.lock index a89800c..79e3ae5 100644 --- a/bun.lock +++ b/bun.lock @@ -62,7 +62,7 @@ "hono": ["hono@4.10.4", "", {}, "sha512-YG/fo7zlU3KwrBL5vDpWKisLYiM+nVstBQqfr7gCPbSYURnNEP9BDxEMz8KfsDR9JX0lJWDRNc6nXX31v7ZEyg=="], - "reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#bffb83a5280a4d74e424c4e0f4fbd46f790227a3", { "peerDependencies": { "typescript": "^5" } }, "bffb83a5280a4d74e424c4e0f4fbd46f790227a3"], + "reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#f439c25742ed69bb141e92f41eeab8bbfd6ac12c", { "peerDependencies": { "typescript": "^5" } }, "f439c25742ed69bb141e92f41eeab8bbfd6ac12c"], "style-mod": ["style-mod@4.1.3", "", {}, "sha512-i/n8VsZydrugj3Iuzll8+x/00GH2vnYsk1eomD8QiRrSAeW6ItbCQDtfXCeJHd0iwiNagqjQkvpvREEPtW3IoQ=="], diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 9ce7bce..408bdc2 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -51,6 +51,7 @@ function processEscapeSeq(escapeSeq: string): string { export class Compiler { instructions: ProgramItem[] = [] + labelCount = 0 fnLabelCount = 0 ifLabelCount = 0 tryLabelCount = 0 @@ -367,7 +368,29 @@ export class Compiler { case terms.FunctionCallOrIdentifier: { if (node.firstChild?.type.id === terms.DotGet) { - return this.#compileNode(node.firstChild, input) + const instructions: ProgramItem[] = [] + const callLabel = `.call_dotget_${++this.labelCount}` + const afterLabel = `.after_dotget_${++this.labelCount}` + + instructions.push(...this.#compileNode(node.firstChild, input)) + instructions.push(['DUP']) + instructions.push(['TYPE']) + instructions.push(['PUSH', 'function']) + instructions.push(['EQ']) + instructions.push(['JUMP_IF_TRUE', callLabel]) + instructions.push(['DUP']) + instructions.push(['TYPE']) + instructions.push(['PUSH', 'native']) + instructions.push(['EQ']) + instructions.push(['JUMP_IF_TRUE', callLabel]) + instructions.push(['JUMP', afterLabel]) + instructions.push([`${callLabel}:`]) + instructions.push(['PUSH', 0]) + instructions.push(['PUSH', 0]) + instructions.push(['CALL']) + instructions.push([`${afterLabel}:`]) + + return instructions } return [['TRY_CALL', value]] diff --git a/src/compiler/tests/compiler.test.ts b/src/compiler/tests/compiler.test.ts index ad62392..263eeb7 100644 --- a/src/compiler/tests/compiler.test.ts +++ b/src/compiler/tests/compiler.test.ts @@ -110,7 +110,10 @@ describe('compiler', () => { }) test('function call with no args', () => { - expect(`bloop = do: 'bloop' end; bloop`).toEvaluateTo('bloop') + expect(`bloop = do: 'bleep' end; bloop`).toEvaluateTo('bleep') + expect(`bloop = [ go=do: 'bleep' end ]; bloop.go`).toEvaluateTo('bleep') + expect(`bloop = [ go=do: 'bleep' end ]; abc = do x: x end; abc (bloop.go)`).toEvaluateTo('bleep') + expect(`num = ((math.random) * 10 + 1) | math.floor; num >= 1 and num <= 10 `).toEvaluateTo(true) }) test('function call with if statement and multiple expressions', () => {