shrimp/src/prelude/load.ts

29 lines
770 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
const fullPath = resolve(path) + '.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
}