diff --git a/bin/repl b/bin/repl index 8236590..6954db7 100755 --- a/bin/repl +++ b/bin/repl @@ -1,7 +1,7 @@ #!/usr/bin/env bun 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' const colors = { @@ -18,7 +18,7 @@ const colors = { } 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] { if (line.startsWith('/')) { @@ -101,6 +101,20 @@ async function repl() { 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) try { @@ -210,6 +224,7 @@ function showWelcome() { console.log(` ${colors.bright}/vars${colors.reset} - Show all variables`) console.log(` ${colors.bright}/funcs${colors.reset} - Show all functions`) 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(`\nExamples:`) console.log(` ${colors.cyan}5 + 10${colors.reset}`)