From 83fad9a68f327f6edde7949cba29263e3a090cb3 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Wed, 29 Oct 2025 15:04:23 -0700 Subject: [PATCH] fix repl error recovery --- bin/repl | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/bin/repl b/bin/repl index 052f481..c6ff98c 100755 --- a/bin/repl +++ b/bin/repl @@ -144,19 +144,33 @@ async function repl() { return } - codeHistory.push(trimmed) - try { const compiler = new Compiler(trimmed, Object.keys(globals)) - vm.appendBytecode(compiler.bytecode) + // Save VM state before appending bytecode, in case execution fails + const savedInstructions = [...vm.instructions] + const savedConstants = [...vm.constants] + const savedPc = vm.pc + const savedScope = vm.scope + const savedStopped = vm.stopped - const result = await vm.continue() + try { + vm.appendBytecode(compiler.bytecode) + const result = await vm.continue() - console.log(`${colors.dim}=>${colors.reset} ${formatValue(result)}`) + codeHistory.push(trimmed) + console.log(`${colors.dim}=>${colors.reset} ${formatValue(result)}`) + } catch (error: any) { + vm.instructions = savedInstructions + vm.constants = savedConstants + vm.pc = savedPc + vm.scope = savedScope + vm.stopped = savedStopped + + console.log(`\n${colors.red}Error:${colors.reset} ${error.message}`) + } } catch (error: any) { console.log(`\n${colors.red}Error:${colors.reset} ${error.message}`) - codeHistory.pop() } rl.prompt()