mirror of
https://github.com/notwa/lips
synced 2024-05-19 08:43:22 -07:00
refactor some utility functions
This commit is contained in:
parent
fb2e588f79
commit
3aa5d01a21
|
@ -208,7 +208,6 @@ function Collector:collect(tokens, fn)
|
||||||
end
|
end
|
||||||
|
|
||||||
for t in I do
|
for t in I do
|
||||||
print(t.tt, t.tok)
|
|
||||||
if t.tt == 'EOF' then
|
if t.tt == 'EOF' then
|
||||||
-- noop
|
-- noop
|
||||||
elseif t.tt == 'EOL' then
|
elseif t.tt == 'EOL' then
|
||||||
|
|
|
@ -37,53 +37,29 @@ function Parser:tokenize(asm)
|
||||||
assert(#tokens > 0, 'Internal Error: no tokens after preprocessing')
|
assert(#tokens > 0, 'Internal Error: no tokens after preprocessing')
|
||||||
|
|
||||||
local collector = Collector(self.options)
|
local collector = Collector(self.options)
|
||||||
self.statements = collector:collect(tokens, self.main_fn)
|
return collector:collect(tokens, self.main_fn)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:debug_dump()
|
function Parser:dump()
|
||||||
local boring = {
|
|
||||||
tt = true,
|
|
||||||
tok = true,
|
|
||||||
fn = true,
|
|
||||||
line = true,
|
|
||||||
}
|
|
||||||
for i, s in ipairs(self.statements) do
|
for i, s in ipairs(self.statements) do
|
||||||
local values = ''
|
print(s.line, s.type, s:dump())
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Parser:parse(asm)
|
function Parser:parse(asm)
|
||||||
self:tokenize(asm)
|
self.statements = self:tokenize(asm)
|
||||||
|
if self.options.debug_token then self:dump() end
|
||||||
if self.options.debug_token then self:debug_dump() end
|
|
||||||
|
|
||||||
local preproc = Preproc(self.options)
|
local preproc = Preproc(self.options)
|
||||||
self.statements = preproc:process(self.statements)
|
self.statements = preproc:process(self.statements)
|
||||||
|
if self.options.debug_pre then self:dump() end
|
||||||
if self.options.debug_pre then self:debug_dump() end
|
|
||||||
|
|
||||||
self.statements = preproc:expand(self.statements)
|
self.statements = preproc:expand(self.statements)
|
||||||
|
if self.options.debug_post then self:dump() end
|
||||||
if self.options.debug_post then self:debug_dump() end
|
|
||||||
|
|
||||||
local dumper = Dumper(self.writer, self.options)
|
local dumper = Dumper(self.writer, self.options)
|
||||||
self.statements = dumper:load(self.statements)
|
self.statements = dumper:load(self.statements)
|
||||||
|
if self.options.debug_asm then self:dump() end
|
||||||
if self.options.debug_asm then self:debug_dump() end
|
|
||||||
|
|
||||||
if self.options.labels then
|
if self.options.labels then
|
||||||
dumper:export_labels(self.options.labels)
|
dumper:export_labels(self.options.labels)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
local abs = math.abs
|
||||||
local insert = table.insert
|
local insert = table.insert
|
||||||
|
|
||||||
local path = string.gsub(..., "[^.]+$", "")
|
local path = string.gsub(..., "[^.]+$", "")
|
||||||
|
@ -6,20 +7,9 @@ local overrides = require(path.."overrides")
|
||||||
local Statement = require(path.."Statement")
|
local Statement = require(path.."Statement")
|
||||||
local Reader = require(path.."Reader")
|
local Reader = require(path.."Reader")
|
||||||
local Expression = require(path.."Expression")
|
local Expression = require(path.."Expression")
|
||||||
|
local util = require(path.."util")
|
||||||
|
|
||||||
local abs = math.abs
|
local signs = util.signs
|
||||||
|
|
||||||
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 Preproc = Reader:extend()
|
local Preproc = Reader:extend()
|
||||||
function Preproc:init(options)
|
function Preproc:init(options)
|
||||||
|
|
|
@ -50,4 +50,28 @@ function Statement:validate(n)
|
||||||
end
|
end
|
||||||
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
|
return Statement
|
||||||
|
|
|
@ -14,7 +14,7 @@ local function readfile(fn, binary)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function bitrange(x, lower, upper)
|
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
|
end
|
||||||
|
|
||||||
local function parent(t)
|
local function parent(t)
|
||||||
|
@ -25,6 +25,18 @@ local function parent(t)
|
||||||
return mt.__index
|
return mt.__index
|
||||||
end
|
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
|
-- http://stackoverflow.com/a/9279009
|
||||||
local loadcode
|
local loadcode
|
||||||
if setfenv and loadstring then -- 5.1, JIT
|
if setfenv and loadstring then -- 5.1, JIT
|
||||||
|
@ -77,6 +89,7 @@ return {
|
||||||
readfile = readfile,
|
readfile = readfile,
|
||||||
bitrange = bitrange,
|
bitrange = bitrange,
|
||||||
parent = parent,
|
parent = parent,
|
||||||
|
signs = signs,
|
||||||
loadcode = loadcode,
|
loadcode = loadcode,
|
||||||
measure_data = measure_data,
|
measure_data = measure_data,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user