Skip to content

Commit dca8edb

Browse files
committed
Add examples for comments
1 parent 4c672c6 commit dca8edb

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

examples/05_comments.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local LuASM = require("luasm")
2+
3+
-- 1. Define the instruction set
4+
local instructions = {
5+
LuASM.instruction("mov", {"imm", "reg"}, {}),
6+
LuASM.instruction("mov", {"reg", "reg"}, {}),
7+
LuASM.instruction("add", {"reg", "reg"}, {}),
8+
LuASM.instruction("jmp", {"label"}, {}),
9+
}
10+
11+
-- 2. Create a runner (use default settings)
12+
local asm = LuASM:new(instructions, {})
13+
14+
-- 3. Tokenize a source string
15+
local src = [[
16+
start: mov 10 r0 # This is a comment
17+
add r0 r1 ; This is another comment
18+
jmp start
19+
]]
20+
local tokenizer = LuASM.string_tokenizer(src)
21+
22+
-- 4. Parse
23+
local result = asm:parse(tokenizer)
24+
25+
print("Lines parsed:", result.parsed_lines)
26+
for name, info in pairs(result.labels) do
27+
print("Label: " .. name .. " -> line: " .. info.location)
28+
end
29+
30+
for i, instr in ipairs(result.instructions) do
31+
print(i, instr.op) -- currently just the instruction name
32+
end

examples/06_custom_comments.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local LuASM = require("luasm")
2+
3+
-- 1. Define the instruction set
4+
local instructions = {
5+
LuASM.instruction("mov", {"imm", "reg"}, {}),
6+
LuASM.instruction("mov", {"reg", "reg"}, {}),
7+
LuASM.instruction("add", {"reg", "reg"}, {}),
8+
LuASM.instruction("jmp", {"label"}, {}),
9+
}
10+
11+
-- 2. Create a runner (use default settings)
12+
local asm = LuASM:new(instructions, {
13+
comment = "=.*$"
14+
})
15+
16+
-- 3. Tokenize a source string
17+
local src = [[
18+
start: mov 10 r0 = My custom comment
19+
add r0 r1 = This just works
20+
jmp start
21+
]]
22+
local tokenizer = LuASM.string_tokenizer(src)
23+
24+
-- 4. Parse
25+
local result = asm:parse(tokenizer)
26+
27+
print("Lines parsed:", result.parsed_lines)
28+
for name, info in pairs(result.labels) do
29+
print("Label: " .. name .. " -> line: " .. info.location)
30+
end
31+
32+
for i, instr in ipairs(result.instructions) do
33+
print(i, instr.op) -- currently just the instruction name
34+
end

0 commit comments

Comments
 (0)