From 3cd1936de4c03306254c87ecb1db0055e9c20e28 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Thu, 16 Oct 2025 16:13:16 -0700 Subject: [PATCH] wip --- src/compiler/compiler.ts | 5 +++-- src/compiler/tests/compiler.test.ts | 7 +++++++ src/parser/highlight.ts | 4 +++- src/parser/tests/functions.test.ts | 17 ----------------- src/testSetup.ts | 14 ++++++++------ 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index aa821cf..8cc0836 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -17,8 +17,8 @@ import { getStringParts, } from '#compiler/utils' -const DEBUG = false -// const DEBUG = true +// const DEBUG = false +const DEBUG = true type Label = `.${string}` @@ -288,6 +288,7 @@ export class Compiler { instructions.push(['PUSH', positionalArgs.length]) instructions.push(['PUSH', namedArgs.length]) instructions.push(['CALL']) + return instructions } diff --git a/src/compiler/tests/compiler.test.ts b/src/compiler/tests/compiler.test.ts index afb59df..07c03b5 100644 --- a/src/compiler/tests/compiler.test.ts +++ b/src/compiler/tests/compiler.test.ts @@ -212,3 +212,10 @@ describe('Regex', () => { expect('//[unclosed//').toFailEvaluation() }) }) + +describe.only('native functions', () => { + test('print function', () => { + const add = (x: number, y: number) => x + y + expect(`add 5 9`).toEvaluateTo(14, { add }) + }) +}) diff --git a/src/parser/highlight.ts b/src/parser/highlight.ts index f873868..63fe0e0 100644 --- a/src/parser/highlight.ts +++ b/src/parser/highlight.ts @@ -5,11 +5,13 @@ export const highlighting = styleTags({ Number: tags.number, String: tags.string, Boolean: tags.bool, - fn: tags.keyword, + keyword: tags.keyword, end: tags.keyword, ':': tags.keyword, + Null: tags.keyword, Regex: tags.regexp, Operator: tags.operator, + Word: tags.variableName, Command: tags.function(tags.variableName), 'Params/Identifier': tags.definition(tags.variableName), Paren: tags.paren, diff --git a/src/parser/tests/functions.test.ts b/src/parser/tests/functions.test.ts index 80e4436..f24eaed 100644 --- a/src/parser/tests/functions.test.ts +++ b/src/parser/tests/functions.test.ts @@ -1,25 +1,8 @@ import { expect, describe, test } from 'bun:test' -import { afterEach } from 'bun:test' -import { resetCommandSource, setCommandSource } from '#editor/commands' -import { beforeEach } from 'bun:test' import '../shrimp.grammar' // Importing this so changes cause it to retest! describe('calling functions', () => { - beforeEach(() => { - setCommandSource(() => [ - { - command: 'echo', - args: [{ name: 'path', type: 'string' }], - execute: (p: any) => p, - }, - ]) - }) - - afterEach(() => { - resetCommandSource() - }) - test('call with no args', () => { expect('tail').toMatchTree(` FunctionCallOrIdentifier diff --git a/src/testSetup.ts b/src/testSetup.ts index d6cca27..56b851c 100644 --- a/src/testSetup.ts +++ b/src/testSetup.ts @@ -4,7 +4,7 @@ import { parser } from '#parser/shrimp' import { $ } from 'bun' import { assert, assertNever, errorMessage } from '#utils/utils' import { Compiler } from '#compiler/compiler' -import { VM, type Value } from 'reefvm' +import { run, VM, type Value } from 'reefvm' const regenerateParser = async () => { let generate = true @@ -33,7 +33,7 @@ declare module 'bun:test' { toMatchTree(expected: string): T toMatchExpression(expected: string): T toFailParse(): T - toEvaluateTo(expected: unknown): Promise + toEvaluateTo(expected: unknown, nativeFunctions?: Record): Promise toFailEvaluation(): Promise } } @@ -93,14 +93,16 @@ expect.extend({ } }, - async toEvaluateTo(received: unknown, expected: unknown) { + async toEvaluateTo( + received: unknown, + expected: unknown, + nativeFunctions: Record = {} + ) { assert(typeof received === 'string', 'toEvaluateTo can only be used with string values') try { const compiler = new Compiler(received) - const vm = new VM(compiler.bytecode) - await vm.run() - const result = await vm.run() + const result = await run(compiler.bytecode, nativeFunctions) let value = VMResultToValue(result) // Just treat regex as strings for comparison purposes