mirror of
https://github.com/notwa/lips
synced 2024-11-14 09:29:03 -08:00
clean up some of my mess
This commit is contained in:
parent
fc153d5191
commit
694f09c9e0
7 changed files with 23 additions and 44 deletions
|
@ -23,8 +23,7 @@ function Collector:statement(...)
|
|||
return s
|
||||
end
|
||||
|
||||
function Collector:push_data(data, size)
|
||||
-- FIXME: local 'data' name clashes with lips.data
|
||||
function Collector:push_data(datum, size)
|
||||
--[[ pseudo-example:
|
||||
Statement{type='!DATA',
|
||||
{tt='BYTES', tok={0, 1, 2}},
|
||||
|
@ -45,9 +44,9 @@ function Collector:push_data(data, size)
|
|||
insert(self.statements, s)
|
||||
end
|
||||
|
||||
if type(data) == 'string' and size == 'WORD' then
|
||||
if type(datum) == 'string' and size == 'WORD' then
|
||||
-- labels will be assembled to words
|
||||
insert(s, Token('LABEL', data))
|
||||
insert(s, Token('LABEL', datum))
|
||||
return
|
||||
end
|
||||
|
||||
|
@ -66,7 +65,7 @@ function Collector:push_data(data, size)
|
|||
insert(s, t)
|
||||
s:validate()
|
||||
end
|
||||
insert(t.tok, data)
|
||||
insert(t.tok, datum)
|
||||
end
|
||||
|
||||
function Collector:variable()
|
||||
|
|
|
@ -6,8 +6,6 @@ local unpack = unpack or table.unpack
|
|||
local path = string.gsub(..., "[^.]+$", "")
|
||||
local data = require(path.."data")
|
||||
local util = require(path.."util")
|
||||
--local overrides = require(path.."overrides")
|
||||
local Base = require(path.."Base")
|
||||
local Token = require(path.."Token")
|
||||
local Statement = require(path.."Statement")
|
||||
local Reader = require(path.."Reader")
|
||||
|
@ -290,11 +288,6 @@ function Dumper:assemble(s)
|
|||
end
|
||||
end
|
||||
|
||||
local assembled_directives = {
|
||||
['!DATA'] = true,
|
||||
['!ORG'] = true,
|
||||
}
|
||||
|
||||
function Dumper:fill(length, content)
|
||||
self:validate(content, 8)
|
||||
local bytes = {}
|
||||
|
@ -386,7 +379,7 @@ function Dumper:load(statements)
|
|||
t.tok = {label}
|
||||
end
|
||||
end
|
||||
self.pos = self.pos + s.length
|
||||
self.pos = self.pos + (s.length or util.measure_data(s))
|
||||
insert(new_statements, s)
|
||||
elseif s.type == '!ORG' then
|
||||
self.pos = s[1].tok
|
||||
|
@ -406,7 +399,6 @@ function Dumper:dump()
|
|||
-- TODO: have options insert .org and/or .base; pos is always 0 at start
|
||||
self.pos = self.options.offset or 0
|
||||
for i, s in ipairs(self.statements) do
|
||||
assert(assembled_directives[s.type], 'Internal Error: unassembled statement')
|
||||
if s.type == '!DATA' then
|
||||
for j, t in ipairs(s) do
|
||||
if t.tt == 'WORDS' then
|
||||
|
@ -434,6 +426,8 @@ function Dumper:dump()
|
|||
end
|
||||
elseif s.type == '!ORG' then
|
||||
self.pos = s[1].tok
|
||||
else
|
||||
error('Internal Error: cannot dump unassembled statement')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
local insert = table.insert
|
||||
|
||||
local path = string.gsub(..., "[^.]+$", "")
|
||||
local data = require(path.."data")
|
||||
local Base = require(path.."Base")
|
||||
local Token = require(path.."Token")
|
||||
local Lexer = require(path.."Lexer")
|
||||
|
|
|
@ -3,8 +3,6 @@ local insert = table.insert
|
|||
local path = string.gsub(..., "[^.]+$", "")
|
||||
local data = require(path.."data")
|
||||
local overrides = require(path.."overrides")
|
||||
local Base = require(path.."Base")
|
||||
local Token = require(path.."Token")
|
||||
local Statement = require(path.."Statement")
|
||||
local Reader = require(path.."Reader")
|
||||
|
||||
|
@ -22,23 +20,6 @@ local function signs(s)
|
|||
end
|
||||
end
|
||||
|
||||
local function unsigns(n)
|
||||
if n > 0 then
|
||||
return string.rep('+', n)
|
||||
elseif n < 0 then
|
||||
return string.rep('-', -n)
|
||||
else
|
||||
return ''
|
||||
end
|
||||
end
|
||||
|
||||
local function RelativeLabel(index, name)
|
||||
return {
|
||||
index = index,
|
||||
name = name,
|
||||
}
|
||||
end
|
||||
|
||||
local Preproc = Reader:extend()
|
||||
function Preproc:init(options)
|
||||
self.options = options or {}
|
||||
|
@ -95,7 +76,7 @@ function Preproc:lookup(t)
|
|||
end
|
||||
|
||||
if seen ~= rel then
|
||||
self:error('could not find appropriate relative label', unsigns(rel))
|
||||
self:error('could not find appropriate relative label', t.tok)
|
||||
end
|
||||
end
|
||||
else
|
||||
|
@ -149,7 +130,10 @@ function Preproc:process(statements)
|
|||
elseif s.type == '!LABEL' then
|
||||
if s[1].tt == 'RELLABEL' then
|
||||
local label = s[1].tok
|
||||
local rl = RelativeLabel(#new_statements + 1, label:sub(2))
|
||||
local rl = {
|
||||
index = #new_statements + 1,
|
||||
name = label:sub(2)
|
||||
}
|
||||
local c = label:sub(1, 1)
|
||||
if c == '+' then
|
||||
insert(self.plus_labels, rl)
|
||||
|
@ -236,7 +220,6 @@ function Preproc:expand(statements)
|
|||
self.fn = s.fn
|
||||
self.line = s.line
|
||||
if s.type:sub(1, 1) == '!' then
|
||||
-- TODO
|
||||
self:push(s)
|
||||
else
|
||||
local name = s.type
|
||||
|
|
|
@ -3,7 +3,7 @@ local Base = require(path.."Base")
|
|||
local Token = require(path.."Token")
|
||||
|
||||
local Reader = Base:extend()
|
||||
-- no init method
|
||||
-- no base init method
|
||||
|
||||
-- Reader expects self.s to be set to a statement, and self.i to a token index
|
||||
|
||||
|
|
|
@ -46,7 +46,6 @@ function Statement:validate(n)
|
|||
for i, v in ipairs(self) do
|
||||
if util.parent(v) ~= Token then
|
||||
self[i] = Token(self.fn, self.line, v)
|
||||
--error(('Internal Error: Statement[%i] is not a Token'):format(i), n)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,19 +35,24 @@ function Token:init(...)
|
|||
else
|
||||
error('Internal Error: init takes 1, 3 or 4 arguments', 3)
|
||||
end
|
||||
self:validate(1)
|
||||
return self
|
||||
end
|
||||
|
||||
function Token:validate(n)
|
||||
n = (n or 0) + 3 -- depth for error message
|
||||
if not self.fn then
|
||||
error('Internal Error: tokens require a filename', 3)
|
||||
error('Internal Error: tokens require a filename', n)
|
||||
end
|
||||
if not self.line then
|
||||
error('Internal Error: tokens require a line number', 3)
|
||||
error('Internal Error: tokens require a line number', n)
|
||||
end
|
||||
if not self.tt then
|
||||
error('Internal Error: token is missing a type', 3)
|
||||
error('Internal Error: token is missing a type', n)
|
||||
end
|
||||
if not self.tok then
|
||||
error('Internal Error: token is missing a value', 3)
|
||||
error('Internal Error: token is missing a value', n)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function Token:set(key, value)
|
||||
|
|
Loading…
Reference in a new issue