diff --git a/lips/Collector.lua b/lips/Collector.lua index 1fb0724..9db3f7b 100644 --- a/lips/Collector.lua +++ b/lips/Collector.lua @@ -208,7 +208,6 @@ function Collector:collect(tokens, fn) end for t in I do - print(t.tt, t.tok) if t.tt == 'EOF' then -- noop elseif t.tt == 'EOL' then diff --git a/lips/Parser.lua b/lips/Parser.lua index 66d2766..353100f 100644 --- a/lips/Parser.lua +++ b/lips/Parser.lua @@ -37,53 +37,29 @@ function Parser:tokenize(asm) assert(#tokens > 0, 'Internal Error: no tokens after preprocessing') local collector = Collector(self.options) - self.statements = collector:collect(tokens, self.main_fn) + return collector:collect(tokens, self.main_fn) end -function Parser:debug_dump() - local boring = { - tt = true, - tok = true, - fn = true, - line = true, - } +function Parser:dump() for i, s in ipairs(self.statements) do - local values = '' - for j, t in ipairs(s) do - local tok = t.tok - if type(tok) == 'number' then - tok = ("$%X"):format(tok) - end - values = values..'\t'..t.tt..'('..tostring(tok)..')' - for k, v in pairs(t) do - if not boring[k] then - values = values..'['..k..'='..tostring(v)..']' - end - end - end - values = values:sub(2) - print(s.line, s.type, values) + print(s.line, s.type, s:dump()) end end function Parser:parse(asm) - self:tokenize(asm) - - if self.options.debug_token then self:debug_dump() end + self.statements = self:tokenize(asm) + if self.options.debug_token then self:dump() end local preproc = Preproc(self.options) self.statements = preproc:process(self.statements) - - if self.options.debug_pre then self:debug_dump() end + if self.options.debug_pre then self:dump() end self.statements = preproc:expand(self.statements) - - if self.options.debug_post then self:debug_dump() end + if self.options.debug_post then self:dump() end local dumper = Dumper(self.writer, self.options) self.statements = dumper:load(self.statements) - - if self.options.debug_asm then self:debug_dump() end + if self.options.debug_asm then self:dump() end if self.options.labels then dumper:export_labels(self.options.labels) diff --git a/lips/Preproc.lua b/lips/Preproc.lua index 3f5dd96..d5def64 100644 --- a/lips/Preproc.lua +++ b/lips/Preproc.lua @@ -1,3 +1,4 @@ +local abs = math.abs local insert = table.insert local path = string.gsub(..., "[^.]+$", "") @@ -6,20 +7,9 @@ local overrides = require(path.."overrides") local Statement = require(path.."Statement") local Reader = require(path.."Reader") local Expression = require(path.."Expression") +local util = require(path.."util") -local abs = math.abs - -local function signs(s) - local start, end_ = s:find('[+-]+') - if start ~= 1 then - return 0 - end - if s:sub(1, 1) == '+' then - return end_ - elseif s:sub(1, 1) == '-' then - return -end_ - end -end +local signs = util.signs local Preproc = Reader:extend() function Preproc:init(options) diff --git a/lips/Statement.lua b/lips/Statement.lua index 384d17a..e9cb4ea 100644 --- a/lips/Statement.lua +++ b/lips/Statement.lua @@ -50,4 +50,28 @@ function Statement:validate(n) end end +local boring = { + tt = true, + tok = true, + fn = true, + line = true, +} + +function Statement:dump() + local values = '' + for j, t in ipairs(self) do + local tok = t.tok + if type(tok) == 'number' then + tok = ("$%X"):format(tok) + end + values = values..'\t'..t.tt..'('..tostring(tok)..')' + for k, v in pairs(t) do + if not boring[k] then + values = values..'['..k..'='..tostring(v)..']' + end + end + end + return values:sub(2) +end + return Statement diff --git a/lips/util.lua b/lips/util.lua index 145ecca..9006399 100644 --- a/lips/util.lua +++ b/lips/util.lua @@ -14,7 +14,7 @@ local function readfile(fn, binary) end local function bitrange(x, lower, upper) - return floor(x/2^lower) % 2^(upper - lower + 1) + return floor(x / 2^lower) % 2^(upper - lower + 1) end local function parent(t) @@ -25,6 +25,18 @@ local function parent(t) return mt.__index end +local function signs(s) + local start, end_ = s:find('[+-]+') + if start ~= 1 then + return 0 + end + if s:sub(1, 1) == '+' then + return end_ + elseif s:sub(1, 1) == '-' then + return -end_ + end +end + -- http://stackoverflow.com/a/9279009 local loadcode if setfenv and loadstring then -- 5.1, JIT @@ -77,6 +89,7 @@ return { readfile = readfile, bitrange = bitrange, parent = parent, + signs = signs, loadcode = loadcode, measure_data = measure_data, }