1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-05-16 23:53:22 -07:00

refactor label resolution into its own method

This commit is contained in:
Connor Olding 2016-11-27 16:17:32 -08:00
parent 29ae63ed34
commit 437b816f85

View File

@ -36,7 +36,10 @@ function Preproc:lookup(t)
if t.tok == nil then if t.tok == nil then
self:error('undefined variable', name) self:error('undefined variable', name)
end end
elseif self.do_labels and t.tt == 'RELLABELSYM' or t.tt == 'RELLABEL' then end
end
function Preproc:resolve(t)
if t.tt == 'RELLABEL' then if t.tt == 'RELLABEL' then
t.tt = 'LABEL' t.tt = 'LABEL'
-- exploits the fact that user labels can't begin with a number -- exploits the fact that user labels can't begin with a number
@ -82,10 +85,6 @@ function Preproc:lookup(t)
self:error('could not find appropriate relative label', t.tok) self:error('could not find appropriate relative label', t.tok)
end end
end end
else
return false
end
return true
end end
function Preproc:check(s, i, tt) function Preproc:check(s, i, tt)
@ -115,7 +114,6 @@ function Preproc:process(statements)
self.variables = {} self.variables = {}
self.plus_labels = {} -- constructed forwards self.plus_labels = {} -- constructed forwards
self.minus_labels = {} -- constructed backwards self.minus_labels = {} -- constructed backwards
self.do_labels = false
-- first pass: resolve variables and collect relative labels -- first pass: resolve variables and collect relative labels
local new_statements = {} local new_statements = {}
@ -152,10 +150,9 @@ function Preproc:process(statements)
end end
-- second pass: resolve relative labels -- second pass: resolve relative labels
self.do_labels = true
for s in self:iter(new_statements) do for s in self:iter(new_statements) do
for j, t in ipairs(s) do for j, t in ipairs(s) do
self:lookup(t) self:resolve(t)
end end
end end