-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_custom_comments.lua
More file actions
34 lines (28 loc) · 904 Bytes
/
06_custom_comments.lua
File metadata and controls
34 lines (28 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
local LuASM = require("luasm")
-- 1. Define the instruction set
local instructions = {
LuASM.instruction("mov", {"imm", "reg"}, {}),
LuASM.instruction("mov", {"reg", "reg"}, {}),
LuASM.instruction("add", {"reg", "reg"}, {}),
LuASM.instruction("jmp", {"label"}, {}),
}
-- 2. Create a runner (use default settings)
local asm = LuASM:new(instructions, {
comment = "=.*$"
})
-- 3. Tokenize a source string
local src = [[
start: mov 10 r0 = My custom comment
add r0 r1 = This just works
jmp start
]]
local tokenizer = asm:string_tokenizer(src)
-- 4. Parse
local result = asm:parse(tokenizer)
print("Lines parsed:", result.parsed_lines)
for name, info in pairs(result.labels) do
print("Label: " .. name .. " -> line: " .. info.location)
end
for i, instr in ipairs(result.instructions) do
print(i, instr.op) -- currently just the instruction name
end