1
0
Fork 0
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:
Connor Olding 2016-04-10 06:52:20 -07:00
parent b8601031f3
commit a9b702114a
2 changed files with 15 additions and 1 deletions

View file

@ -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

View file

@ -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