shrimp/src/prelude/load.ts
2025-11-09 17:39:42 -08:00

31 lines
806 B
TypeScript

import { resolve } from 'path'
import { readFileSync } from 'fs'
import { Compiler } from '#compiler/compiler'
import { type Value, VM, Scope } from 'reefvm'
export const load = async function (this: VM, path: string): Promise<Record<string, Value>> {
const scope = this.scope
const pc = this.pc
let fullPath = resolve(path)
if (!path.includes('.')) fullPath += '.sh'
const code = readFileSync(fullPath, 'utf-8')
this.pc = this.instructions.length
this.scope = new Scope(scope)
const compiled = new Compiler(code)
this.appendBytecode(compiled.bytecode)
await this.continue()
const module: Record<string, Value> = {}
for (const [name, value] of this.scope.locals.entries())
module[name] = value
this.scope = scope
this.pc = pc
this.stopped = false
return module
}