From 4c672c61ede3a561264587e7639e939b7d65299a Mon Sep 17 00:00:00 2001 From: QuickWrite <54590845+QuickWrite@users.noreply.github.com> Date: Mon, 1 Sep 2025 18:56:39 +0200 Subject: [PATCH 1/2] Add ability to specify comments --- src/luasm.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/luasm.lua b/src/luasm.lua index bbf4524..60f9505 100644 --- a/src/luasm.lua +++ b/src/luasm.lua @@ -37,6 +37,7 @@ function LuASM:new(instructions, settings) setmetatable(settings, { __index = { separator = "[^,%s]+", label = "^([%a]+):%s*(.*)", + comment = "[;#].*$", syntax = { imm = "^[%d]+", reg = "^%a[%w]*", @@ -228,6 +229,11 @@ function LuASM:parse(tokenizer) if token ~= nil then + -- Remove comments + if self.settings.comment ~= nil then + token = token:gsub(self.settings.comment, "") + end + --[[ This is very basic label processing as labels could be nested and there could be priorities assigned with labels. From dca8edbae7b29ea24d97b1ba13d0d89e969ae6b7 Mon Sep 17 00:00:00 2001 From: QuickWrite <54590845+QuickWrite@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:06:28 +0200 Subject: [PATCH 2/2] Add examples for comments --- examples/05_comments.lua | 32 +++++++++++++++++++++++++++++++ examples/06_custom_comments.lua | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 examples/05_comments.lua create mode 100644 examples/06_custom_comments.lua diff --git a/examples/05_comments.lua b/examples/05_comments.lua new file mode 100644 index 0000000..ab35a9c --- /dev/null +++ b/examples/05_comments.lua @@ -0,0 +1,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 = LuASM.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 diff --git a/examples/06_custom_comments.lua b/examples/06_custom_comments.lua new file mode 100644 index 0000000..0356909 --- /dev/null +++ b/examples/06_custom_comments.lua @@ -0,0 +1,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 = LuASM.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