1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-05-18 16:33:22 -07:00

use locals

This commit is contained in:
Connor Olding 2015-12-27 02:06:18 -08:00
parent 73920dd31d
commit 5f9be52d35

View File

@ -12,7 +12,12 @@ local assembler = {
]], ]],
} }
local byte = string.byte
local char = string.char
local find = string.find
local format = string.format
local floor = math.floor local floor = math.floor
local insert = table.insert
local Class = function(inherit) local Class = function(inherit)
local class = {} local class = {}
@ -505,7 +510,7 @@ function Parser:init(writer, fn, options)
end end
function Lexer:error(msg) function Lexer:error(msg)
error(string.format('%s:%d: Error: %s', self.fn, self.line, msg), 2) error(format('%s:%d: Error: %s', self.fn, self.line, msg), 2)
end end
function Lexer:nextc() function Lexer:nextc()
@ -522,22 +527,22 @@ function Lexer:nextc()
self.line = self.line + 1 self.line = self.line + 1
end end
self.ord = string.byte(self.asm, self.pos) self.ord = byte(self.asm, self.pos)
self.pos = self.pos + 1 self.pos = self.pos + 1
-- handle newlines; translate CRLF to LF -- handle newlines; translate CRLF to LF
if self.ord == 13 then if self.ord == 13 then
if self.pos <= #self.asm and string.byte(self.asm, self.pos) == 10 then if self.pos <= #self.asm and byte(self.asm, self.pos) == 10 then
self.pos = self.pos + 1 self.pos = self.pos + 1
end end
self.ord = 10 self.ord = 10
end end
self.chr = string.char(self.ord) self.chr = char(self.ord)
if self.pos <= #self.asm then if self.pos <= #self.asm then
self.ord2 = string.byte(self.asm, self.pos) self.ord2 = byte(self.asm, self.pos)
self.chr2 = string.char(self.ord2) self.chr2 = char(self.ord2)
self.chrchr = string.char(self.ord, self.ord2) self.chrchr = char(self.ord, self.ord2)
else else
self.ord2 = self.EOF self.ord2 = self.EOF
self.chr2 = '' self.chr2 = ''
@ -553,7 +558,7 @@ end
function Lexer:read_chars(pattern) function Lexer:read_chars(pattern)
local buff = '' local buff = ''
while string.find(self.chr, pattern) do while find(self.chr, pattern) do
buff = buff..self.chr buff = buff..self.chr
self:nextc() self:nextc()
end end
@ -820,7 +825,7 @@ function Lexer:lex(_yield)
end end
function Parser:error(msg) function Parser:error(msg)
error(string.format('%s:%d: Error: %s', self.fn, self.line, msg), 2) error(format('%s:%d: Error: %s', self.fn, self.line, msg), 2)
end end
function Parser:advance() function Parser:advance()
@ -1097,11 +1102,11 @@ function overrides.PUSH(self, name)
self:error("can't push a negative number of spaces") self:error("can't push a negative number of spaces")
end end
for i=1,self.tok do for i=1,self.tok do
table.insert(stack, '') insert(stack, '')
end end
self:advance() self:advance()
else else
table.insert(stack, self:register()) insert(stack, self:register())
end end
if not self:is_EOL() then if not self:is_EOL() then
self:optional_comma() self:optional_comma()
@ -1314,7 +1319,7 @@ function Parser:tokenize(asm)
t.tok = b t.tok = b
t.fn = c t.fn = c
t.line = d t.line = d
table.insert(self.tokens, t) insert(self.tokens, t)
return t.tt, t.tok, t.fn, t.line return t.tt, t.tok, t.fn, t.line
end end
@ -1389,7 +1394,7 @@ function Parser:parse(asm)
end end
function Dumper:error(msg) function Dumper:error(msg)
error(string.format('%s:%d: Error: %s', self.fn, self.line, msg), 2) error(format('%s:%d: Error: %s', self.fn, self.line, msg), 2)
end end
function Dumper:advance(by) function Dumper:advance(by)
@ -1398,7 +1403,7 @@ end
function Dumper:push_instruction(t) function Dumper:push_instruction(t)
t.kind = 'instruction' t.kind = 'instruction'
table.insert(self.commands, t) insert(self.commands, t)
self:advance(4) self:advance(4)
end end
@ -1435,7 +1440,7 @@ function Dumper:add_bytes(line, ...)
t[t.size] = b t[t.size] = b
end end
if not use_last then if not use_last then
table.insert(self.commands, t) insert(self.commands, t)
end end
self:advance(t.size) self:advance(t.size)
end end
@ -1458,7 +1463,7 @@ function Dumper:add_directive(line, name, a, b)
elseif name == 'ORG' then elseif name == 'ORG' then
t.kind = 'goto' t.kind = 'goto'
t.addr = a t.addr = a
table.insert(self.commands, t) insert(self.commands, t)
self.pos = a % 0x80000000 self.pos = a % 0x80000000
self:advance(0) self:advance(0)
elseif name == 'ALIGN' then elseif name == 'ALIGN' then
@ -1472,13 +1477,13 @@ function Dumper:add_directive(line, name, a, b)
local temp = self.pos + align - 1 local temp = self.pos + align - 1
t.skip = temp - (temp % align) - self.pos t.skip = temp - (temp % align) - self.pos
t.fill = t.fill or 0 t.fill = t.fill or 0
table.insert(self.commands, t) insert(self.commands, t)
self:advance(t.skip) self:advance(t.skip)
elseif name == 'SKIP' then elseif name == 'SKIP' then
t.kind = 'ahead' t.kind = 'ahead'
t.skip = a t.skip = a
t.fill = b t.fill = b
table.insert(self.commands, t) insert(self.commands, t)
self:advance(t.skip) self:advance(t.skip)
else else
self:error('unimplemented directive') self:error('unimplemented directive')