diff --git a/lips/Dumper.lua b/lips/Dumper.lua index 9273103..1792b41 100644 --- a/lips/Dumper.lua +++ b/lips/Dumper.lua @@ -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 diff --git a/lips/Parser.lua b/lips/Parser.lua index a7ab8ad..2a760d0 100644 --- a/lips/Parser.lua +++ b/lips/Parser.lua @@ -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