From 694f09c9e013d475fd3c67a591c53adf57ca2747 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 21 Apr 2016 03:23:04 -0700 Subject: [PATCH] clean up some of my mess --- lips/Collector.lua | 9 ++++----- lips/Dumper.lua | 12 +++--------- lips/Parser.lua | 1 - lips/Preproc.lua | 27 +++++---------------------- lips/Reader.lua | 2 +- lips/Statement.lua | 1 - lips/Token.lua | 15 ++++++++++----- 7 files changed, 23 insertions(+), 44 deletions(-) diff --git a/lips/Collector.lua b/lips/Collector.lua index d090e5f..a4ce389 100644 --- a/lips/Collector.lua +++ b/lips/Collector.lua @@ -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() diff --git a/lips/Dumper.lua b/lips/Dumper.lua index b6b721b..dde559b 100644 --- a/lips/Dumper.lua +++ b/lips/Dumper.lua @@ -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 diff --git a/lips/Parser.lua b/lips/Parser.lua index 9bcd08c..78c66f6 100644 --- a/lips/Parser.lua +++ b/lips/Parser.lua @@ -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") diff --git a/lips/Preproc.lua b/lips/Preproc.lua index 9bc5805..7a09e22 100644 --- a/lips/Preproc.lua +++ b/lips/Preproc.lua @@ -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 diff --git a/lips/Reader.lua b/lips/Reader.lua index 25a417c..8c31484 100644 --- a/lips/Reader.lua +++ b/lips/Reader.lua @@ -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 diff --git a/lips/Statement.lua b/lips/Statement.lua index e8e97c6..384d17a 100644 --- a/lips/Statement.lua +++ b/lips/Statement.lua @@ -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 diff --git a/lips/Token.lua b/lips/Token.lua index ce83e9b..a3ccc4a 100644 --- a/lips/Token.lua +++ b/lips/Token.lua @@ -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)