This commit is contained in:
Andre Wagner
2026-05-05 09:54:42 -05:00
parent 8614f978ea
commit 60c55304b2
2 changed files with 6 additions and 1 deletions

View File

@@ -123,6 +123,7 @@ end
do do
local vm = VM:new() local vm = VM:new()
vm.debug = true
local bytecode = assemble [[ local bytecode = assemble [[
.func 0 .func 0
pushi 2 pushi 2

View File

@@ -134,6 +134,7 @@ function VM.new()
stack = Stack.new(), stack = Stack.new(),
code = Code.new(), code = Code.new(),
loc = {}, loc = {},
debug = false,
}, VM) }, VM)
end end
@@ -200,7 +201,8 @@ end
function VM:_step() function VM:_step()
local loc = self.loc[#self.loc] local loc = self.loc[#self.loc]
local op = self.code:next_instruction(loc.f_id, loc.pc) local op = self.code:next_instruction(loc.f_id, loc.pc)
print(loc.f_id .. ':' .. loc.pc .. ' ' .. op.operator .. ' ' .. (op.operand and op.operand or ''))
if self.debug then print('## ' .. loc.f_id .. ':' .. loc.pc .. ' ' .. op.operator .. ' ' .. (op.operand and op.operand or '')) end
if op.operator == 'pushi' then if op.operator == 'pushi' then
self:push_integer(op.operand) self:push_integer(op.operand)
@@ -215,6 +217,8 @@ function VM:_step()
error("Unknown operator '" .. tostring(op.operator) .. "'") error("Unknown operator '" .. tostring(op.operator) .. "'")
end end
if self.debug then print(self.stack:debug()) end
loc.pc = loc.pc + op.instruction_size loc.pc = loc.pc + op.instruction_size
end end