mirror of
https://github.com/notwa/lips
synced 2024-11-14 09:59:03 -08:00
allow importing/exporting of labels
This commit is contained in:
parent
b8601031f3
commit
a9b702114a
2 changed files with 15 additions and 1 deletions
|
@ -12,7 +12,7 @@ function Dumper:init(writer, fn, options)
|
|||
self.writer = writer
|
||||
self.fn = fn or '(string)'
|
||||
self.options = options or {}
|
||||
self.labels = {}
|
||||
self.labels = setmetatable({}, {__index=options.labels})
|
||||
self.commands = {}
|
||||
self.pos = options.offset or 0
|
||||
self.lastcommand = nil
|
||||
|
@ -22,6 +22,17 @@ function Dumper:error(msg)
|
|||
error(format('%s:%d: Error: %s', self.fn, self.line, msg), 2)
|
||||
end
|
||||
|
||||
function Dumper:export_labels(t)
|
||||
for k, v in pairs(self.labels) do
|
||||
-- only return valid labels; those that don't begin with a number
|
||||
-- (relative labels are invalid)
|
||||
if not tostring(k):sub(1, 1):find('%d') then
|
||||
t[k] = v
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function Dumper:advance(by)
|
||||
self.pos = self.pos + by
|
||||
end
|
||||
|
|
|
@ -276,6 +276,9 @@ function Parser:parse(asm)
|
|||
self:error('unexpected token (unknown instruction?)')
|
||||
end
|
||||
end
|
||||
if self.options.labels then
|
||||
self.dumper:export_labels(self.options.labels)
|
||||
end
|
||||
return self.dumper:dump()
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue