use works more like fn, for now

This commit is contained in:
Chris Wanstrath 2025-10-28 22:20:03 -07:00
parent f25ec024c2
commit bf1196bf96
2 changed files with 29 additions and 15 deletions

View File

@ -78,7 +78,7 @@ export const globalFunctions = {
}, },
// modules // modules
use: async function (this: VM, path: string) { use: async function (this: VM, path: string): Promise<Record<string, Value>> {
const scope = this.scope const scope = this.scope
const pc = this.pc const pc = this.pc
@ -92,16 +92,16 @@ export const globalFunctions = {
await this.continue() await this.continue()
const module: Map<string, Value> = new Map const module: Record<string, Value> = {}
for (const [name, value] of this.scope.locals.entries()) for (const [name, value] of this.scope.locals.entries())
module.set(name, value) module[name] = value
this.scope = scope this.scope = scope
this.pc = pc this.pc = pc
this.stopped = false this.stopped = false
this.scope.set(parse(fullPath).name, { type: 'dict', value: module }) return module
} },
} }
export function formatValue(value: Value, inner = false): string { export function formatValue(value: Value, inner = false): string {

View File

@ -4,25 +4,39 @@ import { globalFunctions } from '#prelude'
describe('use', () => { describe('use', () => {
test(`imports all a file's functions`, async () => { test(`imports all a file's functions`, async () => {
expect(` expect(`
use ./src/prelude/tests/math math = use ./src/prelude/tests/math
dbl = math | at double math.double 4
dbl 4
`).toEvaluateTo(8, globalFunctions) `).toEvaluateTo(8, globalFunctions)
expect(` expect(`
use ./src/prelude/tests/math math = use ./src/prelude/tests/math
math | at pi math.double (math.double 4)
`).toEvaluateTo(16, globalFunctions)
expect(`
math = use ./src/prelude/tests/math
dbl = math.double
dbl (dbl 2)
`).toEvaluateTo(8, globalFunctions)
expect(`
math = use ./src/prelude/tests/math
math.pi
`).toEvaluateTo(3.14, globalFunctions) `).toEvaluateTo(3.14, globalFunctions)
expect(` expect(`
use ./src/prelude/tests/math math = use ./src/prelude/tests/math
math | at 🥧 math | at 🥧
`).toEvaluateTo(3.14159265359, globalFunctions) `).toEvaluateTo(3.14159265359, globalFunctions)
expect(` expect(`
use ./src/prelude/tests/math math = use ./src/prelude/tests/math
call = do x y: x y end math.🥧
call (math | at add1) 5 `).toEvaluateTo(3.14159265359, globalFunctions)
expect(`
math = use ./src/prelude/tests/math
math.add1 5
`).toEvaluateTo(6, globalFunctions) `).toEvaluateTo(6, globalFunctions)
}) })
}) })