diff --git a/lua-temp/TODO.md b/lua-temp/TODO.md index 1fa0c36..d4ece75 100644 --- a/lua-temp/TODO.md +++ b/lua-temp/TODO.md @@ -9,7 +9,7 @@ Progress of the Lua port: - [x] Calling functions - [x] Calling functions with parameters - [ ] Control flow - - [ ] Lablels in Assembly + - [ ] Labels in Assembly - [ ] Recursion - [ ] Strings - [ ] From constants diff --git a/lua-temp/tests.lua b/lua-temp/tests.lua index 0af4028..c5eb8d4 100644 --- a/lua-temp/tests.lua +++ b/lua-temp/tests.lua @@ -71,6 +71,31 @@ do TEST "Parser" assert_eq(found, expected) 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 -- @@ -137,7 +162,7 @@ local function arith(a, b, op) end -do TEST("VM: basic") +do TEST "VM: basic" local vm = VM:new() -- vm.debug = true local bytecode = assemble [[ @@ -159,7 +184,7 @@ do TEST("VM: basic") assert_eq(vm:to_integer(-1), 5) 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, 'sub'):to_integer(-1), -3) 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) end -do TEST("VM: local variables") +do TEST "VM: local variables" local vm = VM:new():load(assemble([[ .func 0 pushv 2 ; local a, b @@ -197,7 +222,7 @@ do TEST("VM: local variables") assert_eq(vm:to_integer(-1), 3) end -do TEST("VM: functions") +do TEST "VM: functions" local vm = VM:new():load(assemble([[ .func 0 pushf 1 diff --git a/lua-temp/tyche-as.lua b/lua-temp/tyche-as.lua index 29d78e6..0fd8c40 100644 --- a/lua-temp/tyche-as.lua +++ b/lua-temp/tyche-as.lua @@ -17,7 +17,7 @@ local function assemble(source) local line = line:gsub("%s*;.*$", "") -- remove comments line = line:match("^%s*(.-)%s*$") -- trim - if #line == 1 then goto continue end + if #line == 0 then goto continue end if line == ".const" then section = 'const' @@ -28,6 +28,7 @@ local function assemble(source) current_f_id = f_id elseif section == 'const' then 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 proto.constants[tonumber(k)] = line:match('"(.*)"') else @@ -39,6 +40,7 @@ local function assemble(source) table.insert(proto.functions[current_f_id], { inst, tonumber(par) }) else 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 }) end end