1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-05-05 19:03:27 -07:00

reimplement defines

defines now resolve to the previous definition, rather than the lastmost.
also, defines are invisible to the parser now.
This commit is contained in:
Connor Olding 2016-01-14 17:08:56 -08:00
parent 61cd69004f
commit 659bec36f8
3 changed files with 31 additions and 26 deletions

View File

@ -14,7 +14,7 @@ function Muncher:advance()
self.tok = t.tok
self.fn = t.fn
self.line = t.line
return t.tt, t.tok
return t
end
function Muncher:is_EOL()

View File

@ -260,9 +260,6 @@ function Parser:parse(asm)
elseif self.tt == 'EOL' then
-- empty line
self:advance()
elseif self.tt == 'DEF' then
self:advance()
self:advance()
elseif self.tt == 'DIR' then
self:directive()
elseif self.tt == 'LABEL' then

View File

@ -14,40 +14,45 @@ function Preproc:process(tokens)
local plus_labels = {} -- constructed forwards
local minus_labels = {} -- constructed backwards
-- first pass: collect tokens, constants, and relative labels.
-- first pass: resolve defines, collect relative labels
local new_tokens = {}
self.i = 0
while self.i < #self.tokens do
local tt, tok = self:advance()
if tt == 'DEF' then
local tt2, tok2 = self:advance()
if tt2 ~= 'NUM' then
local t = self:advance()
if t.tt == nil then
error('Internal Error: missing token')
elseif t.tt == 'DEF' then
local t2 = self:advance()
if t2.tt ~= 'NUM' then
self:error('expected number for define')
end
defines[tok] = tok2
elseif tt == 'RELLABEL' then
if tok == '+' then
insert(plus_labels, self.i)
elseif tok == '-' then
insert(minus_labels, 1, self.i)
defines[t.tok] = t2.tok
elseif t.tt == 'DEFSYM' then
local tt = 'NUM'
local tok = defines[t.tok]
if tok == nil then
self:error('undefined define') -- uhhh nice wording
end
insert(new_tokens, {fn=t.fn, line=t.line, tt=tt, tok=tok})
elseif t.tt == 'RELLABEL' then
if t.tok == '+' then
insert(plus_labels, #new_tokens + 1)
elseif t.tok == '-' then
insert(minus_labels, 1, #new_tokens + 1)
else
error('Internal Error: unexpected token for relative label')
end
elseif tt == nil then
error('Internal Error: missing token')
insert(new_tokens, t)
else
insert(new_tokens, t)
end
end
-- resolve defines and relative labels
for i, t in ipairs(self.tokens) do
-- second pass: resolve relative labels
for i, t in ipairs(new_tokens) do
self.fn = t.fn
self.line = t.line
if t.tt == 'DEFSYM' then
t.tt = 'NUM'
t.tok = defines[t.tok]
if t.tok == nil then
self:error('undefined define') -- uhhh nice wording
end
elseif t.tt == 'RELLABEL' then
if t.tt == 'RELLABEL' then
t.tt = 'LABEL'
-- exploits the fact that user labels can't begin with a number
t.tok = tostring(i)
@ -78,12 +83,15 @@ function Preproc:process(tokens)
end
end
end
if seen ~= rel then
self:error('could not find appropriate relative label')
end
end
end
self.tokens = new_tokens
return self.tokens
end