bun run repl #2

Merged
probablycorey merged 6 commits from repl into main 2025-10-26 00:24:45 +00:00
Showing only changes of commit 664ba82199 - Show all commits

View File

@ -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}`)