This commit is contained in:
Andre Wagner
2026-05-05 14:04:30 -05:00
parent 60c55304b2
commit 8c36fb07c0
2 changed files with 50 additions and 25 deletions

View File

@@ -75,44 +75,45 @@ end
do do
local stack = VM.new().stack local stack = VM.new().stack
stack:push(10) stack:push({ type='integer', value=10 })
stack:push(20) stack:push({ type='integer', value=20 })
stack:push(30) stack:push({ type='integer', value=30 })
assert_eq(#stack, 3) assert_eq(#stack, 3)
assert_eq(stack[0], 10) assert_eq(stack[0].value, 10)
assert_eq(stack[1], 20) assert_eq(stack[1].value, 20)
assert_eq(stack[-1], 30) assert_eq(stack[-1].value, 30)
assert_eq(stack[-2], 20) assert_eq(stack[-2].value, 20)
stack:pop() stack:pop()
stack:pop() stack:pop()
assert_eq(stack[-1].value, 10)
stack:pop() stack:pop()
assert_eq(#stack, 0) assert_eq(#stack, 0)
end end
do do
local stack = VM.new().stack local stack = VM.new().stack
stack:push(10) stack:push({ type='integer', value=10 })
stack:push(20) stack:push({ type='integer', value=20 })
stack:push_fp() stack:push_fp()
stack:push(30) stack:push({ type='integer', value=30 })
stack:push(40) stack:push({ type='integer', value=40 })
stack:push(50) stack:push({ type='integer', value=50 })
assert_eq(#stack, 3) assert_eq(#stack, 3)
assert_eq(stack[0], 30) assert_eq(stack[0].value, 30)
assert_eq(stack[1], 40) assert_eq(stack[1].value, 40)
assert_eq(stack[-1], 50) assert_eq(stack[-1].value, 50)
assert_eq(stack[-2], 40) assert_eq(stack[-2].value, 40)
stack:pop_fp() stack:pop_fp()
assert_eq(#stack, 2) assert_eq(#stack, 2)
assert_eq(stack[0], 10) assert_eq(stack[0].value, 10)
assert_eq(stack[1], 20) assert_eq(stack[1].value, 20)
assert_eq(stack[-1], 20) assert_eq(stack[-1].value, 20)
assert_eq(stack[-2], 10) assert_eq(stack[-2].value, 10)
end end
---------------------- ----------------------
@@ -123,7 +124,7 @@ end
do do
local vm = VM:new() local vm = VM:new()
vm.debug = true -- vm.debug = true
local bytecode = assemble [[ local bytecode = assemble [[
.func 0 .func 0
pushi 2 pushi 2
@@ -139,7 +140,7 @@ do
vm:call(0) vm:call(0)
assert_eq(vm:stack_sz(), 1) assert_eq(vm:stack_sz(), 1)
assert_eq(vm:is(-1, 'integer')) assert_eq(vm:is(-1, 'integer'), true)
assert_eq(vm:to_integer(-1), 5) assert_eq(vm:to_integer(-1), 5)
end end

View File

@@ -1,5 +1,23 @@
local pprint = require('pprint') local pprint = require('pprint')
----------------------
-- --
-- UTIL --
-- --
----------------------
function format_value(v)
if v.type == 'integer' or v.type == 'real' then
return tostring(v.value)
elseif v.type == 'string' then
return '"' .. v.value .. '"'
elseif v.type == 'function' then
return '@' .. tostring(v.value)
else
return pprint.format(v)
end
end
---------------------- ----------------------
-- -- -- --
-- STACK -- -- STACK --
@@ -23,6 +41,7 @@ function Stack:top_fps()
end end
function Stack:push(value) function Stack:push(value)
assert(type(value) == 'table' and value.type)
table.insert(self.stack, value) table.insert(self.stack, value)
end end
@@ -87,7 +106,12 @@ end
function Stack:debug() function Stack:debug()
if #self.stack == 0 then return "empty" end if #self.stack == 0 then return "empty" end
local ss = {} local ss = {}
for _,v in ipairs(self.stack) do table.insert(ss, '[' .. pprint.pformat(v) .. '] ') end for i,v in ipairs(self.stack) do
for _,fp in pairs(self.fps) do
if i == fp then table.insert(ss, '^ ') end
end
table.insert(ss, '[' .. format_value(v) .. '] ')
end
return table.concat(ss) return table.concat(ss)
end end
@@ -164,8 +188,8 @@ function VM:stack_sz()
return #self.stack return #self.stack
end end
function VM:is(idx, type) function VM:is(idx, type_)
return self.stack[idx].type == type return self.stack[idx].type == type_
end end
function VM:to_integer(idx) function VM:to_integer(idx)