This commit is contained in:
Andre Wagner
2026-05-07 09:20:22 -05:00
parent 8f851a330e
commit 4a23c52781
3 changed files with 33 additions and 6 deletions

View File

@@ -9,7 +9,7 @@ Progress of the Lua port:
- [x] Calling functions - [x] Calling functions
- [x] Calling functions with parameters - [x] Calling functions with parameters
- [ ] Control flow - [ ] Control flow
- [ ] Lablels in Assembly - [ ] Labels in Assembly
- [ ] Recursion - [ ] Recursion
- [ ] Strings - [ ] Strings
- [ ] From constants - [ ] From constants

View File

@@ -71,6 +71,31 @@ do TEST "Parser"
assert_eq(found, expected) assert_eq(found, expected)
end end
--do TEST "Parser: labels"
--
-- local source = [[
-- .func 0
-- jmp %my_label
-- pushi 3
-- %my_label:
-- ret ]]
--
-- local expected = {
-- constants = {},
-- functions = {
-- [0] = {
-- { "jmp", "%my_label" },
-- { "pushi", 3 },
-- { "ret", labels = { "%my_label" } },
-- }
-- }
-- }
--
-- local found = assemble(source)
-- pprint(found)
-- assert_eq(found, expected)
--end
---------------------- ----------------------
-- -- -- --
-- STACK -- -- STACK --
@@ -137,7 +162,7 @@ local function arith(a, b, op)
end end
do TEST("VM: basic") do TEST "VM: basic"
local vm = VM:new() local vm = VM:new()
-- vm.debug = true -- vm.debug = true
local bytecode = assemble [[ local bytecode = assemble [[
@@ -159,7 +184,7 @@ do TEST("VM: basic")
assert_eq(vm:to_integer(-1), 5) assert_eq(vm:to_integer(-1), 5)
end end
do TEST("VM: logic/arithmetic") do TEST "VM: logic/arithmetic"
assert_eq(arith(2, 5, 'sum'):to_integer(-1), 7) assert_eq(arith(2, 5, 'sum'):to_integer(-1), 7)
assert_eq(arith(2, 5, 'sub'):to_integer(-1), -3) assert_eq(arith(2, 5, 'sub'):to_integer(-1), -3)
assert_eq(arith(2, 5, 'mul'):to_integer(-1), 10) assert_eq(arith(2, 5, 'mul'):to_integer(-1), 10)
@@ -181,7 +206,7 @@ do TEST("VM: logic/arithmetic")
assert_eq(arith(20, 3, 'mod'):to_integer(-1), 2) assert_eq(arith(20, 3, 'mod'):to_integer(-1), 2)
end end
do TEST("VM: local variables") do TEST "VM: local variables"
local vm = VM:new():load(assemble([[ local vm = VM:new():load(assemble([[
.func 0 .func 0
pushv 2 ; local a, b pushv 2 ; local a, b
@@ -197,7 +222,7 @@ do TEST("VM: local variables")
assert_eq(vm:to_integer(-1), 3) assert_eq(vm:to_integer(-1), 3)
end end
do TEST("VM: functions") do TEST "VM: functions"
local vm = VM:new():load(assemble([[ local vm = VM:new():load(assemble([[
.func 0 .func 0
pushf 1 pushf 1

View File

@@ -17,7 +17,7 @@ local function assemble(source)
local line = line:gsub("%s*;.*$", "") -- remove comments local line = line:gsub("%s*;.*$", "") -- remove comments
line = line:match("^%s*(.-)%s*$") -- trim line = line:match("^%s*(.-)%s*$") -- trim
if #line == 1 then goto continue end if #line == 0 then goto continue end
if line == ".const" then if line == ".const" then
section = 'const' section = 'const'
@@ -28,6 +28,7 @@ local function assemble(source)
current_f_id = f_id current_f_id = f_id
elseif section == 'const' then elseif section == 'const' then
local k, v = line:match("^%s*(%d+)%s*:%s*(.+)$") local k, v = line:match("^%s*(%d+)%s*:%s*(.+)$")
if not k then error("Invalid row for constant") end
if v:sub(1, 1) == '"' then if v:sub(1, 1) == '"' then
proto.constants[tonumber(k)] = line:match('"(.*)"') proto.constants[tonumber(k)] = line:match('"(.*)"')
else else
@@ -39,6 +40,7 @@ local function assemble(source)
table.insert(proto.functions[current_f_id], { inst, tonumber(par) }) table.insert(proto.functions[current_f_id], { inst, tonumber(par) })
else else
local inst = line:match("^%s*(%a+)%s*$") local inst = line:match("^%s*(%a+)%s*$")
if not inst then error("Invalid row for function") end
table.insert(proto.functions[current_f_id], { inst }) table.insert(proto.functions[current_f_id], { inst })
end end
end end