start exporting a package API

This commit is contained in:
Chris Wanstrath 2025-11-03 13:26:53 -08:00
parent cc06bdf2a7
commit 402748d1da
2 changed files with 40 additions and 0 deletions

View File

@ -1,6 +1,7 @@
{
"name": "shrimp",
"version": "0.1.0",
"exports": "./src/index.ts",
"private": true,
"type": "module",
"scripts": {

39
src/index.ts Normal file
View File

@ -0,0 +1,39 @@
import { readFileSync } from 'fs'
import { VM, fromValue, type Bytecode } from 'reefvm'
import { Compiler } from '#compiler/compiler'
import { globals, 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> {
const code = readFileSync(path, 'utf-8')
return await runCode(code)
}
export async function runCode(code: string): Promise<any> {
const compiler = new Compiler(code, Object.keys(globals))
return await runBytecode(compiler.bytecode)
}
export async function runBytecode(bytecode: Bytecode): Promise<any> {
try {
const vm = new VM(bytecode, globals)
await vm.run()
return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!) : null
} catch (error: any) {
console.error(`${colors.red}Error:${colors.reset} ${error.message}`)
process.exit(1)
}
}
export function compileFile(path: string): Bytecode {
const code = readFileSync(path, 'utf-8')
return compileCode(code)
}
export function compileCode(code: string): Bytecode {
const compiler = new Compiler(code, Object.keys(globals))
return compiler.bytecode
}