This commit is contained in:
Corey Johnson 2025-10-16 16:13:16 -07:00
parent e8a1befdcc
commit 3cd1936de4
5 changed files with 21 additions and 26 deletions

View File

@ -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
}

View File

@ -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 })
})
})

View File

@ -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,

View File

@ -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

View File

@ -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<T>
toEvaluateTo(expected: unknown, nativeFunctions?: Record<string, Function>): Promise<T>
toFailEvaluation(): Promise<T>
}
}
@ -93,14 +93,16 @@ expect.extend({
}
},
async toEvaluateTo(received: unknown, expected: unknown) {
async toEvaluateTo(
received: unknown,
expected: unknown,
nativeFunctions: Record<string, Function> = {}
) {
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