Skip to content

Commit 18fe18f

Browse files
authored
Merge pull request #3 from QuickWrite/2-add-comments
Add comments The syntax of the comments can be specified through a setting called comment: local asm = LuASM:new(instructions, { comment = ";.*$" }) This will now allow for comments like this: start: mov 10 r0 ; This is a comment add r0 r1 jmp start
2 parents 49272ed + dca8edb commit 18fe18f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-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

src/luasm.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function LuASM:new(instructions, settings)
3737
setmetatable(settings, { __index = {
3838
separator = "[^,%s]+",
3939
label = "^([%a]+):%s*(.*)",
40+
comment = "[;#].*$",
4041
syntax = {
4142
imm = "^[%d]+",
4243
reg = "^%a[%w]*",
@@ -228,6 +229,11 @@ function LuASM:parse(tokenizer)
228229

229230
if token ~= nil then
230231

232+
-- Remove comments
233+
if self.settings.comment ~= nil then
234+
token = token:gsub(self.settings.comment, "")
235+
end
236+
231237
--[[
232238
This is very basic label processing as labels could be
233239
nested and there could be priorities assigned with labels.

0 commit comments

Comments
 (0)