add /bytecode

This commit is contained in:
Chris Wanstrath 2025-10-25 10:32:36 -07:00
parent d7f613f2e4
commit 664ba82199

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bun #!/usr/bin/env bun
import { Compiler } from '../src/compiler/compiler' import { Compiler } from '../src/compiler/compiler'
import { VM, type Value, Scope } from 'reefvm' import { VM, type Value, Scope, bytecodeToString } from 'reefvm'
import * as readline from 'node:readline' import * as readline from 'node:readline'
const colors = { const colors = {
@ -18,7 +18,7 @@ const colors = {
} }
async function repl() { async function repl() {
const commands = ['/clear', '/reset', '/vars', '/funcs', '/history', '/exit', '/quit'] const commands = ['/clear', '/reset', '/vars', '/funcs', '/history', '/bytecode', '/exit', '/quit']
function completer(line: string): [string[], string] { function completer(line: string): [string[], string] {
if (line.startsWith('/')) { if (line.startsWith('/')) {
@ -101,6 +101,20 @@ async function repl() {
return return
} }
if (trimmed === '/bytecode') {
if (!vm || codeHistory.length === 0) {
console.log(`\n${colors.dim}No history. Type some things.${colors.reset}`)
} else {
console.log(`\n${colors.bright}Bytecode:${colors.reset}`)
console.log(bytecodeToString({
instructions: vm.instructions,
constants: vm.constants
}))
}
rl.prompt()
return
}
codeHistory.push(trimmed) codeHistory.push(trimmed)
try { try {
@ -210,6 +224,7 @@ function showWelcome() {
console.log(` ${colors.bright}/vars${colors.reset} - Show all variables`) console.log(` ${colors.bright}/vars${colors.reset} - Show all variables`)
console.log(` ${colors.bright}/funcs${colors.reset} - Show all functions`) console.log(` ${colors.bright}/funcs${colors.reset} - Show all functions`)
console.log(` ${colors.bright}/history${colors.reset} - Show code history`) console.log(` ${colors.bright}/history${colors.reset} - Show code history`)
console.log(` ${colors.bright}/bytecode${colors.reset} - Show compiled bytecode`)
console.log(` ${colors.bright}/exit${colors.reset} - Quit REPL`) console.log(` ${colors.bright}/exit${colors.reset} - Quit REPL`)
console.log(`\nExamples:`) console.log(`\nExamples:`)
console.log(` ${colors.cyan}5 + 10${colors.reset}`) console.log(` ${colors.cyan}5 + 10${colors.reset}`)