Compare commits

..

5 Commits

Author SHA1 Message Date
57b582a0c2 it's alive 2025-11-03 13:34:32 -08:00
32c47cfd24 old syntax 2025-11-03 13:34:32 -08:00
a161679903 insanity 2025-11-03 13:34:31 -08:00
da77bf1686 what have i done 2025-11-03 13:34:26 -08:00
458371b802 ./bin/shrimp parse file 2025-11-03 13:34:19 -08:00

View File

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