From 8c36fb07c02c0759ede64fc01eb537f69e924248 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Tue, 5 May 2026 14:04:30 -0500 Subject: [PATCH] . --- lua-temp/tests.lua | 45 ++++++++++++++++++++++--------------------- lua-temp/tyche-vm.lua | 30 ++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/lua-temp/tests.lua b/lua-temp/tests.lua index 130a9c0..0463f3a 100644 --- a/lua-temp/tests.lua +++ b/lua-temp/tests.lua @@ -75,44 +75,45 @@ end do local stack = VM.new().stack - stack:push(10) - stack:push(20) - stack:push(30) + stack:push({ type='integer', value=10 }) + stack:push({ type='integer', value=20 }) + stack:push({ type='integer', value=30 }) assert_eq(#stack, 3) - assert_eq(stack[0], 10) - assert_eq(stack[1], 20) - assert_eq(stack[-1], 30) - assert_eq(stack[-2], 20) + assert_eq(stack[0].value, 10) + assert_eq(stack[1].value, 20) + assert_eq(stack[-1].value, 30) + assert_eq(stack[-2].value, 20) stack:pop() stack:pop() + assert_eq(stack[-1].value, 10) stack:pop() assert_eq(#stack, 0) end do local stack = VM.new().stack - stack:push(10) - stack:push(20) + stack:push({ type='integer', value=10 }) + stack:push({ type='integer', value=20 }) stack:push_fp() - stack:push(30) - stack:push(40) - stack:push(50) + stack:push({ type='integer', value=30 }) + stack:push({ type='integer', value=40 }) + stack:push({ type='integer', value=50 }) assert_eq(#stack, 3) - assert_eq(stack[0], 30) - assert_eq(stack[1], 40) - assert_eq(stack[-1], 50) - assert_eq(stack[-2], 40) + assert_eq(stack[0].value, 30) + assert_eq(stack[1].value, 40) + assert_eq(stack[-1].value, 50) + assert_eq(stack[-2].value, 40) stack:pop_fp() assert_eq(#stack, 2) - assert_eq(stack[0], 10) - assert_eq(stack[1], 20) - assert_eq(stack[-1], 20) - assert_eq(stack[-2], 10) + assert_eq(stack[0].value, 10) + assert_eq(stack[1].value, 20) + assert_eq(stack[-1].value, 20) + assert_eq(stack[-2].value, 10) end ---------------------- @@ -123,7 +124,7 @@ end do local vm = VM:new() - vm.debug = true + -- vm.debug = true local bytecode = assemble [[ .func 0 pushi 2 @@ -139,7 +140,7 @@ do vm:call(0) 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) end diff --git a/lua-temp/tyche-vm.lua b/lua-temp/tyche-vm.lua index 909b474..42b96d9 100644 --- a/lua-temp/tyche-vm.lua +++ b/lua-temp/tyche-vm.lua @@ -1,5 +1,23 @@ 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 -- @@ -23,6 +41,7 @@ function Stack:top_fps() end function Stack:push(value) + assert(type(value) == 'table' and value.type) table.insert(self.stack, value) end @@ -87,7 +106,12 @@ end function Stack:debug() if #self.stack == 0 then return "empty" end 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) end @@ -164,8 +188,8 @@ function VM:stack_sz() return #self.stack end -function VM:is(idx, type) - return self.stack[idx].type == type +function VM:is(idx, type_) + return self.stack[idx].type == type_ end function VM:to_integer(idx)