-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_comments.lua
More file actions
32 lines (26 loc) · 886 Bytes
/
05_comments.lua
File metadata and controls
32 lines (26 loc) · 886 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
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, {})
-- 3. Tokenize a source string
local src = [[
start: mov 10 r0 # This is a comment
add r0 r1 ; This is another comment
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