start exporting a package API
This commit is contained in:
parent
cc06bdf2a7
commit
402748d1da
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "shrimp",
|
"name": "shrimp",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
|
"exports": "./src/index.ts",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
||||||
39
src/index.ts
Normal file
39
src/index.ts
Normal 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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user