1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-11-12 22:39:03 -08:00

refactor some utility functions

This commit is contained in:
Connor Olding 2016-11-27 13:33:37 -08:00
parent fb2e588f79
commit 3aa5d01a21
5 changed files with 49 additions and 47 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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,
}