1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-11-14 18:19:03 -08:00

clean up some of my mess

This commit is contained in:
Connor Olding 2016-04-21 03:23:04 -07:00
parent fc153d5191
commit 694f09c9e0
7 changed files with 23 additions and 44 deletions

View file

@ -23,8 +23,7 @@ function Collector:statement(...)
return s return s
end end
function Collector:push_data(data, size) function Collector:push_data(datum, size)
-- FIXME: local 'data' name clashes with lips.data
--[[ pseudo-example: --[[ pseudo-example:
Statement{type='!DATA', Statement{type='!DATA',
{tt='BYTES', tok={0, 1, 2}}, {tt='BYTES', tok={0, 1, 2}},
@ -45,9 +44,9 @@ function Collector:push_data(data, size)
insert(self.statements, s) insert(self.statements, s)
end end
if type(data) == 'string' and size == 'WORD' then if type(datum) == 'string' and size == 'WORD' then
-- labels will be assembled to words -- labels will be assembled to words
insert(s, Token('LABEL', data)) insert(s, Token('LABEL', datum))
return return
end end
@ -66,7 +65,7 @@ function Collector:push_data(data, size)
insert(s, t) insert(s, t)
s:validate() s:validate()
end end
insert(t.tok, data) insert(t.tok, datum)
end end
function Collector:variable() function Collector:variable()

View file

@ -6,8 +6,6 @@ local unpack = unpack or table.unpack
local path = string.gsub(..., "[^.]+$", "") local path = string.gsub(..., "[^.]+$", "")
local data = require(path.."data") local data = require(path.."data")
local util = require(path.."util") local util = require(path.."util")
--local overrides = require(path.."overrides")
local Base = require(path.."Base")
local Token = require(path.."Token") local Token = require(path.."Token")
local Statement = require(path.."Statement") local Statement = require(path.."Statement")
local Reader = require(path.."Reader") local Reader = require(path.."Reader")
@ -290,11 +288,6 @@ function Dumper:assemble(s)
end end
end end
local assembled_directives = {
['!DATA'] = true,
['!ORG'] = true,
}
function Dumper:fill(length, content) function Dumper:fill(length, content)
self:validate(content, 8) self:validate(content, 8)
local bytes = {} local bytes = {}
@ -386,7 +379,7 @@ function Dumper:load(statements)
t.tok = {label} t.tok = {label}
end end
end end
self.pos = self.pos + s.length self.pos = self.pos + (s.length or util.measure_data(s))
insert(new_statements, s) insert(new_statements, s)
elseif s.type == '!ORG' then elseif s.type == '!ORG' then
self.pos = s[1].tok 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 -- TODO: have options insert .org and/or .base; pos is always 0 at start
self.pos = self.options.offset or 0 self.pos = self.options.offset or 0
for i, s in ipairs(self.statements) do for i, s in ipairs(self.statements) do
assert(assembled_directives[s.type], 'Internal Error: unassembled statement')
if s.type == '!DATA' then if s.type == '!DATA' then
for j, t in ipairs(s) do for j, t in ipairs(s) do
if t.tt == 'WORDS' then if t.tt == 'WORDS' then
@ -434,6 +426,8 @@ function Dumper:dump()
end end
elseif s.type == '!ORG' then elseif s.type == '!ORG' then
self.pos = s[1].tok self.pos = s[1].tok
else
error('Internal Error: cannot dump unassembled statement')
end end
end end
end end

View file

@ -1,7 +1,6 @@
local insert = table.insert local insert = table.insert
local path = string.gsub(..., "[^.]+$", "") local path = string.gsub(..., "[^.]+$", "")
local data = require(path.."data")
local Base = require(path.."Base") local Base = require(path.."Base")
local Token = require(path.."Token") local Token = require(path.."Token")
local Lexer = require(path.."Lexer") local Lexer = require(path.."Lexer")

View file

@ -3,8 +3,6 @@ local insert = table.insert
local path = string.gsub(..., "[^.]+$", "") local path = string.gsub(..., "[^.]+$", "")
local data = require(path.."data") local data = require(path.."data")
local overrides = require(path.."overrides") local overrides = require(path.."overrides")
local Base = require(path.."Base")
local Token = require(path.."Token")
local Statement = require(path.."Statement") local Statement = require(path.."Statement")
local Reader = require(path.."Reader") local Reader = require(path.."Reader")
@ -22,23 +20,6 @@ local function signs(s)
end end
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() local Preproc = Reader:extend()
function Preproc:init(options) function Preproc:init(options)
self.options = options or {} self.options = options or {}
@ -95,7 +76,7 @@ function Preproc:lookup(t)
end end
if seen ~= rel then 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
end end
else else
@ -149,7 +130,10 @@ function Preproc:process(statements)
elseif s.type == '!LABEL' then elseif s.type == '!LABEL' then
if s[1].tt == 'RELLABEL' then if s[1].tt == 'RELLABEL' then
local label = s[1].tok 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) local c = label:sub(1, 1)
if c == '+' then if c == '+' then
insert(self.plus_labels, rl) insert(self.plus_labels, rl)
@ -236,7 +220,6 @@ function Preproc:expand(statements)
self.fn = s.fn self.fn = s.fn
self.line = s.line self.line = s.line
if s.type:sub(1, 1) == '!' then if s.type:sub(1, 1) == '!' then
-- TODO
self:push(s) self:push(s)
else else
local name = s.type local name = s.type

View file

@ -3,7 +3,7 @@ local Base = require(path.."Base")
local Token = require(path.."Token") local Token = require(path.."Token")
local Reader = Base:extend() 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 -- Reader expects self.s to be set to a statement, and self.i to a token index

View file

@ -46,7 +46,6 @@ function Statement:validate(n)
for i, v in ipairs(self) do for i, v in ipairs(self) do
if util.parent(v) ~= Token then if util.parent(v) ~= Token then
self[i] = Token(self.fn, self.line, v) self[i] = Token(self.fn, self.line, v)
--error(('Internal Error: Statement[%i] is not a Token'):format(i), n)
end end
end end
end end

View file

@ -35,19 +35,24 @@ function Token:init(...)
else else
error('Internal Error: init takes 1, 3 or 4 arguments', 3) error('Internal Error: init takes 1, 3 or 4 arguments', 3)
end 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 if not self.fn then
error('Internal Error: tokens require a filename', 3) error('Internal Error: tokens require a filename', n)
end end
if not self.line then if not self.line then
error('Internal Error: tokens require a line number', 3) error('Internal Error: tokens require a line number', n)
end end
if not self.tt then if not self.tt then
error('Internal Error: token is missing a type', 3) error('Internal Error: token is missing a type', n)
end end
if not self.tok then if not self.tok then
error('Internal Error: token is missing a value', 3) error('Internal Error: token is missing a value', n)
end end
return self
end end
function Token:set(key, value) function Token:set(key, value)