Skip to content

Commit dc10532

Browse files
committed
Fix some mistakes
1 parent caf1127 commit dc10532

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/luasm.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ LuASM.version = "0.0.1"
3535
function LuASM:new(instructions, settings)
3636
-- Default settings
3737
setmetatable(settings, { __index = {
38-
separator = "[^,%s]+",
38+
separator = "^[ ,]+",
3939
label = "^([%a]+):%s*(.*)",
4040
comment = "[;#].*$",
41+
mnemonic = "^([%a]+)(.*)",
4142
syntax = {
4243
imm = "^[%d]+",
4344
reg = "^%a[%w]*",
@@ -110,6 +111,10 @@ function Tokenizer.get_next_line()
110111
return nil
111112
end
112113

114+
function Tokenizer:eol()
115+
return self.line:len() == 0
116+
end
117+
113118
--- @return boolean
114119
function Tokenizer:has_line()
115120
self.line = self:get_next_line()
@@ -123,16 +128,28 @@ function Tokenizer:get_label()
123128
return nil
124129
end
125130

126-
local label, rest = self.line:match(self.settings.label)
131+
local label, rest = self.line:match(self.luasm.settings.label)
127132

128133
if label ~= nil then
129-
self.line = rest
134+
self.line = trim(rest)
130135
self.cursor = self.cursor + #label
131136
end
132137

133138
return label
134139
end
135140

141+
--- @return boolean
142+
function Tokenizer:get_mnemonic()
143+
local mnemonic, rest = self.line:match(self.luasm.settings.mnemonic)
144+
145+
if mnemonic ~= nil then
146+
self.line = trim(rest)
147+
self.cursor = self.cursor + #mnemonic
148+
end
149+
150+
return mnemonic
151+
end
152+
136153
--- Creates a new tokenizer without a specific implementation.
137154
--- @return table A tokenizer instance (needs a concrete `get_next_line` implementation).
138155
function Tokenizer:new(luasm)
@@ -166,7 +183,7 @@ end
166183
--- @param input string The complete ASM source as a string.
167184
--- @return table Tokenizer instance.
168185
function LuASM:string_tokenizer(input)
169-
local tokenizer = Tokenizer:new()
186+
local tokenizer = Tokenizer:new(self)
170187

171188
tokenizer.input = input
172189
tokenizer.cursor = 1 -- byte index inside `input`
@@ -186,7 +203,7 @@ function LuASM:string_tokenizer(input)
186203

187204
-- Remove comment from the line
188205
if self.settings.comment ~= nil then
189-
line = line:gsub(self.settings.comment, "")
206+
line = trim(line:gsub(self.settings.comment, ""))
190207
end
191208

192209
tokenizer.cursor = endIndex + 1
@@ -280,7 +297,7 @@ function LuASM:parse(tokenizer)
280297
}
281298
end
282299

283-
if tokenizer:end_of_line() then
300+
if tokenizer:eol() then
284301
goto continue
285302
end
286303

0 commit comments

Comments
 (0)