Skip to content

Commit 184c5b4

Browse files
committed
Add custom arguments example
1 parent 95c4738 commit 184c5b4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

examples/03_custom_arguments.lua

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

0 commit comments

Comments
 (0)