Compare commits

..

6 Commits

Author SHA1 Message Date
4fc1a965eb it's alive 2025-11-03 13:45:01 -08:00
6cb4d69088 old syntax 2025-11-03 13:45:01 -08:00
75a304bf18 insanity 2025-11-03 13:45:01 -08:00
7cf7d7de48 what have i done 2025-11-03 13:45:01 -08:00
120244003c ./bin/shrimp parse file 2025-11-03 13:45:01 -08:00
Chris Wanstrath
90a1f63847 allow passing in extra globals 2025-11-03 13:44:12 -08:00

View File

@ -1,25 +1,24 @@
import { readFileSync } from 'fs'
import { VM, fromValue, type Bytecode } from 'reefvm'
import { Compiler } from '#compiler/compiler'
import { globals, colors } from '#prelude'
import { globals as shrimpGlobals, colors } from '#prelude'
export { Compiler } from '#compiler/compiler'
export { parser } from '#parser/shrimp'
export { globals } from '#prelude'
export async function runFile(path: string): Promise<any> {
export async function runFile(path: string, globals?: Record<string, any>): Promise<any> {
const code = readFileSync(path, 'utf-8')
return await runCode(code)
return await runCode(code, globals)
}
export async function runCode(code: string): Promise<any> {
const compiler = new Compiler(code, Object.keys(globals))
return await runBytecode(compiler.bytecode)
export async function runCode(code: string, globals?: Record<string, any>): Promise<any> {
return await runBytecode(compileCode(code, globals), globals)
}
export async function runBytecode(bytecode: Bytecode): Promise<any> {
export async function runBytecode(bytecode: Bytecode, globals?: Record<string, any>): Promise<any> {
try {
const vm = new VM(bytecode, globals)
const vm = new VM(bytecode, Object.assign({}, shrimpGlobals, globals))
await vm.run()
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!) : null
} catch (error: any) {
@ -28,12 +27,13 @@ export async function runBytecode(bytecode: Bytecode): Promise<any> {
}
}
export function compileFile(path: string): Bytecode {
export function compileFile(path: string, globals?: Record<string, any>): Bytecode {
const code = readFileSync(path, 'utf-8')
return compileCode(code)
return compileCode(code, globals)
}
export function compileCode(code: string): Bytecode {
const compiler = new Compiler(code, Object.keys(globals))
export function compileCode(code: string, globals?: Record<string, any>): Bytecode {
const globalNames = [...Object.keys(shrimpGlobals), ...(globals ? Object.keys(globals) : [])]
const compiler = new Compiler(code, globalNames)
return compiler.bytecode
}