mirror of
https://github.com/notwa/lips
synced 2024-05-16 07:03:23 -07:00
allow expressions in variable definitions
This commit is contained in:
parent
067267c313
commit
de2618bb61
|
@ -48,7 +48,7 @@ function Expander:pop(kind)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Expander:expand(statements)
|
function Expander:expand(statements)
|
||||||
-- fourth pass: expand pseudo-instructions and register arguments
|
-- third pass: expand pseudo-instructions and register arguments
|
||||||
self.statements = {}
|
self.statements = {}
|
||||||
for i, s in ipairs(statements) do
|
for i, s in ipairs(statements) do
|
||||||
self.s = s
|
self.s = s
|
||||||
|
|
|
@ -96,21 +96,12 @@ function Preproc:resolve(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Preproc:check(s, i, tt)
|
function Preproc:check(s, i, tt)
|
||||||
s = s or self.s
|
|
||||||
i = i or self.i
|
|
||||||
local t = s[i]
|
local t = s[i]
|
||||||
if t == nil then
|
if t == nil then
|
||||||
local err = ("expected another argument for %s at position %i"):format(self.s.type, self.i)
|
local err = ("expected another argument for %s at position %i"):format(self.s.type, self.i)
|
||||||
self:error(err)
|
self:error(err)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.fn = t.fn
|
|
||||||
self.line = t.line
|
|
||||||
|
|
||||||
if t.tt ~= tt then
|
|
||||||
self:lookup(t)
|
|
||||||
end
|
|
||||||
|
|
||||||
if t.tt ~= tt then
|
if t.tt ~= tt then
|
||||||
local err = ("argument %i of %s expected type %s"):format(i, s.type, tt)
|
local err = ("argument %i of %s expected type %s"):format(i, s.type, tt)
|
||||||
self:error(err, t.tt)
|
self:error(err, t.tt)
|
||||||
|
@ -118,14 +109,30 @@ function Preproc:check(s, i, tt)
|
||||||
return t.tok
|
return t.tok
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Preproc:evaluate(t)
|
||||||
|
if t.tt == 'EXPR' then
|
||||||
|
local result, err = self.expr:eval(t.tok)
|
||||||
|
if err then
|
||||||
|
self:error('failed to evaulate ('..t.tok..')', err)
|
||||||
|
end
|
||||||
|
t.tt = 'NUM'
|
||||||
|
t.tok = result
|
||||||
|
end
|
||||||
|
self:lookup(t)
|
||||||
|
end
|
||||||
|
|
||||||
function Preproc:process(statements)
|
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.expr = Expression(self.variables)
|
||||||
|
|
||||||
-- first pass: resolve variables and collect relative labels
|
-- first pass: resolve variables and collect relative labels
|
||||||
local new_statements = {}
|
local new_statements = {}
|
||||||
for s in self:iter(statements) do
|
for s in self:iter(statements) do
|
||||||
|
for j, t in ipairs(s) do
|
||||||
|
self:evaluate(t)
|
||||||
|
end
|
||||||
if s.type == '!VAR' then
|
if s.type == '!VAR' then
|
||||||
local a = self:check(s, 1, 'VAR')
|
local a = self:check(s, 1, 'VAR')
|
||||||
local b = self:check(s, 2, 'NUM')
|
local b = self:check(s, 2, 'NUM')
|
||||||
|
@ -148,9 +155,6 @@ function Preproc:process(statements)
|
||||||
end
|
end
|
||||||
insert(new_statements, s)
|
insert(new_statements, s)
|
||||||
else
|
else
|
||||||
for j, t in ipairs(s) do
|
|
||||||
self:lookup(t)
|
|
||||||
end
|
|
||||||
insert(new_statements, s)
|
insert(new_statements, s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -162,21 +166,6 @@ function Preproc:process(statements)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- third pass: evaluate constant expressions
|
|
||||||
for s in self:iter(new_statements) do
|
|
||||||
for j, t in ipairs(s) do
|
|
||||||
if t.tt == 'EXPR' then
|
|
||||||
local expr = Expression()
|
|
||||||
local result, err = expr:eval(t.tok)
|
|
||||||
if err then
|
|
||||||
self:error('failed to evaulate ('..t.tok..')', err)
|
|
||||||
end
|
|
||||||
t.tt = 'NUM'
|
|
||||||
t.tok = result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return new_statements
|
return new_statements
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user