mirror of
https://github.com/notwa/lips
synced 2024-05-18 16:33:22 -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:
parent
61cd69004f
commit
659bec36f8
|
@ -14,7 +14,7 @@ function Muncher:advance()
|
||||||
self.tok = t.tok
|
self.tok = t.tok
|
||||||
self.fn = t.fn
|
self.fn = t.fn
|
||||||
self.line = t.line
|
self.line = t.line
|
||||||
return t.tt, t.tok
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
function Muncher:is_EOL()
|
function Muncher:is_EOL()
|
||||||
|
|
|
@ -260,9 +260,6 @@ function Parser:parse(asm)
|
||||||
elseif self.tt == 'EOL' then
|
elseif self.tt == 'EOL' then
|
||||||
-- empty line
|
-- empty line
|
||||||
self:advance()
|
self:advance()
|
||||||
elseif self.tt == 'DEF' then
|
|
||||||
self:advance()
|
|
||||||
self:advance()
|
|
||||||
elseif self.tt == 'DIR' then
|
elseif self.tt == 'DIR' then
|
||||||
self:directive()
|
self:directive()
|
||||||
elseif self.tt == 'LABEL' then
|
elseif self.tt == 'LABEL' then
|
||||||
|
|
|
@ -14,40 +14,45 @@ function Preproc:process(tokens)
|
||||||
local plus_labels = {} -- constructed forwards
|
local plus_labels = {} -- constructed forwards
|
||||||
local minus_labels = {} -- constructed backwards
|
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
|
self.i = 0
|
||||||
while self.i < #self.tokens do
|
while self.i < #self.tokens do
|
||||||
local tt, tok = self:advance()
|
local t = self:advance()
|
||||||
if tt == 'DEF' then
|
if t.tt == nil then
|
||||||
local tt2, tok2 = self:advance()
|
error('Internal Error: missing token')
|
||||||
if tt2 ~= 'NUM' then
|
elseif t.tt == 'DEF' then
|
||||||
|
local t2 = self:advance()
|
||||||
|
if t2.tt ~= 'NUM' then
|
||||||
self:error('expected number for define')
|
self:error('expected number for define')
|
||||||
end
|
end
|
||||||
defines[tok] = tok2
|
defines[t.tok] = t2.tok
|
||||||
elseif tt == 'RELLABEL' then
|
elseif t.tt == 'DEFSYM' then
|
||||||
if tok == '+' then
|
local tt = 'NUM'
|
||||||
insert(plus_labels, self.i)
|
local tok = defines[t.tok]
|
||||||
elseif tok == '-' then
|
if tok == nil then
|
||||||
insert(minus_labels, 1, self.i)
|
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
|
else
|
||||||
error('Internal Error: unexpected token for relative label')
|
error('Internal Error: unexpected token for relative label')
|
||||||
end
|
end
|
||||||
elseif tt == nil then
|
insert(new_tokens, t)
|
||||||
error('Internal Error: missing token')
|
else
|
||||||
|
insert(new_tokens, t)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- resolve defines and relative labels
|
-- second pass: resolve relative labels
|
||||||
for i, t in ipairs(self.tokens) do
|
for i, t in ipairs(new_tokens) do
|
||||||
self.fn = t.fn
|
self.fn = t.fn
|
||||||
self.line = t.line
|
self.line = t.line
|
||||||
if t.tt == 'DEFSYM' then
|
if t.tt == 'RELLABEL' 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
|
|
||||||
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
|
||||||
t.tok = tostring(i)
|
t.tok = tostring(i)
|
||||||
|
@ -78,12 +83,15 @@ function Preproc:process(tokens)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if seen ~= rel then
|
if seen ~= rel then
|
||||||
self:error('could not find appropriate relative label')
|
self:error('could not find appropriate relative label')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.tokens = new_tokens
|
||||||
|
|
||||||
return self.tokens
|
return self.tokens
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user