forked from defunkt/ReefVM
cli
This commit is contained in:
parent
0a7a3dcfdb
commit
2f2a8fe9f2
14
bin/reef
Executable file
14
bin/reef
Executable file
|
|
@ -0,0 +1,14 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
import { toBytecode } from "../src/bytecode"
|
||||
import { run } from "../src/index"
|
||||
import { toJs } from "../src/value"
|
||||
|
||||
const file = Bun.argv[2]
|
||||
if (!file || !await Bun.file(file).exists()) {
|
||||
console.error("usage: reef <path/to/bytecode.reef>")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const value = await run(toBytecode(await Bun.file(file).text()))
|
||||
console.log(toJs(value))
|
||||
3
examples/add.reef
Normal file
3
examples/add.reef
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
PUSH 1
|
||||
PUSH 2
|
||||
ADD
|
||||
12
src/value.ts
12
src/value.ts
|
|
@ -119,4 +119,16 @@ export function isEqual(a: Value, b: Value): boolean {
|
|||
}
|
||||
case 'function': return false // functions never equal
|
||||
}
|
||||
}
|
||||
|
||||
export function toJs(v: Value): any {
|
||||
switch (v.type) {
|
||||
case 'null': return null
|
||||
case 'boolean': return v.value
|
||||
case 'number': return v.value
|
||||
case 'string': return v.value
|
||||
case 'array': return v.value.map(toJs)
|
||||
case 'dict': return Object.fromEntries(v.value.entries().map(([k, v]) => [k, toJs(v)]))
|
||||
case 'function': return '<function>'
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user