From 0fae9a0b3779e13be71250e333c0c4ce270e2767 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Wed, 6 May 2026 10:49:45 -0500 Subject: [PATCH] . --- lua-temp/TODO.md | 6 ++++-- lua-temp/tests.lua | 16 ++++++++++++++++ lua-temp/tyche-vm.lua | 31 ++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lua-temp/TODO.md b/lua-temp/TODO.md index 665f749..7f64cee 100644 --- a/lua-temp/TODO.md +++ b/lua-temp/TODO.md @@ -3,10 +3,11 @@ Progress of the Lua port: - [x] Assembler - [x] Basic VM execution - [x] Logic/arithmetic expressions -- [ ] Functions +- [ ] Variables - [ ] Local variables +- [ ] Functions - [ ] Calling functions - - [ ] Globals + - [ ] Calling functions with parameters - [ ] Control flow - [ ] Lablels in Assembly - [ ] Recursion @@ -19,6 +20,7 @@ Progress of the Lua port: - [ ] Garbage collection - [ ] Metatables - [ ] Real +- [ ] Globals - [ ] Error handling - [ ] Stack traces in case of errors - [ ] Closures/upvalues diff --git a/lua-temp/tests.lua b/lua-temp/tests.lua index e78f62a..cc65f37 100644 --- a/lua-temp/tests.lua +++ b/lua-temp/tests.lua @@ -181,4 +181,20 @@ do TEST("VM: logic/arithmetic") assert_eq(arith(20, 3, 'mod'):to_integer(-1), 2) end +do TEST("VM: local variables") + local vm = VM:new():load(assemble([[ + .func 0 + pushv 2 ; local a, b + pushi 3 ; a = 3 + set 0 + pushi 4 ; b = 4 + set 1 + dupv 0 ; return a + ret + ]])):call(0) + + assert_eq(vm:stack_sz(), 1) + assert_eq(vm:to_integer(-1), 3) +end + print('End.') \ No newline at end of file diff --git a/lua-temp/tyche-vm.lua b/lua-temp/tyche-vm.lua index fea35ed..9cc1782 100644 --- a/lua-temp/tyche-vm.lua +++ b/lua-temp/tyche-vm.lua @@ -21,6 +21,8 @@ function format_value(v) return '"' .. v.value .. '"' elseif v.type == 'function' then return '@' .. tostring(v.value) + elseif v.type == 'nil' then + return 'nil' else return pprint.format(v) end @@ -49,7 +51,8 @@ function Stack:top_fps() end function Stack:push(value) - assert(type(value) == 'table' and TYPE_MAP[value.type]) + assert(type(value) == 'table', "invalid value format (expected { type='...', value=... })") + assert(TYPE_MAP[value.type], "missing field 'type' in value") table.insert(self.stack, value) end @@ -208,6 +211,11 @@ function VM.new() }, VM) end +function VM:set_debug(b) + self.debug = b + return self +end + -- -- code management -- @@ -226,6 +234,10 @@ function VM:push_integer(n) self.stack:push({ type = 'integer', value = n }) end +function VM:push_nil() + self.stack:push({ type = 'nil' }) +end + -- -- information -- @@ -281,6 +293,23 @@ function VM:_step() if op.operator == 'pushi' then self:push_integer(op.operand) + -- + -- local variables + -- + + elseif op.operator == 'pushv' then + for i=1,op.operand do + self:push_nil() + end + + elseif op.operator == 'set' then + local a = self.stack:pop() + self.stack[op.operand + 1] = a + + elseif op.operator == 'dupv' then + local a = self.stack[op.operand + 1] + self.stack:push(a) + -- -- logic/arithmetic operations --