move word_writer to lips.writers, add others

This commit is contained in:
Connor Olding 2016-04-26 21:34:05 -07:00
parent 027b5d9deb
commit 0678d3eb58
2 changed files with 87 additions and 21 deletions

View File

@ -14,26 +14,7 @@ local path = string.gsub(..., "%.init$", "").."."
local util = require(path.."util")
local Parser = require(path.."Parser")
function lips.word_writer()
local buff = {}
local max = -1
return function(pos, b)
if pos then
buff[pos] = ("%02X"):format(b)
if pos > max then
max = pos
end
elseif max >= 0 then
for i=0, max, 4 do
local a = buff[i+0] or '00'
local b = buff[i+1] or '00'
local c = buff[i+2] or '00'
local d = buff[i+3] or '00'
print(a..b..c..d)
end
end
end
end
lips.writers = require(path.."writers")
function lips.assemble(fn_or_asm, writer, options)
-- assemble MIPS R4300i assembly code.
@ -41,7 +22,7 @@ function lips.assemble(fn_or_asm, writer, options)
-- returns error message on error, or nil on success.
fn_or_asm = tostring(fn_or_asm)
local default_writer = not writer
writer = writer or lips.word_writer()
writer = writer or lips.writers.make_word()
options = options or {}
local function main()

85
lips/writers.lua Normal file
View File

@ -0,0 +1,85 @@
local writers = {}
function writers.make_word()
local buff = {}
local max = -1
return function(pos, b)
if pos then
buff[pos] = ("%02X"):format(b)
if pos > max then
max = pos
end
elseif max >= 0 then
for i=0, max, 4 do
local a = buff[i+0] or '00'
local b = buff[i+1] or '00'
local c = buff[i+2] or '00'
local d = buff[i+3] or '00'
print(a..b..c..d)
end
end
end
end
function writers.make_verbose()
local buff = {}
local max = -1
return function(pos, b)
if pos then
buff[pos] = b
if pos > max then
max = pos
end
elseif max >= 0 then
for i=0, max, 4 do
local a = buff[i+0] or nil
local b = buff[i+1] or nil
local c = buff[i+2] or nil
local d = buff[i+3] or nil
if a or b or c or d then
a = a and ("%02X"):format(a) or '--'
b = b and ("%02X"):format(b) or '--'
c = c and ("%02X"):format(c) or '--'
d = d and ("%02X"):format(d) or '--'
print(('%08X %s'):format(i, a..b..c..d))
end
end
end
end
end
function writers.make_tester()
local buff = {}
local max = -1
return function(pos, b)
if pos then
buff[pos] = b
if pos > max then
max = pos
end
elseif max >= 0 then
local s = ''
local last_i = 0
for i=0, max, 4 do
local a = buff[i+0] or nil
local b = buff[i+1] or nil
local c = buff[i+2] or nil
local d = buff[i+3] or nil
if a or b or c or d then
a = a and ("%02X"):format(a) or '--'
b = b and ("%02X"):format(b) or '--'
c = c and ("%02X"):format(c) or '--'
d = d and ("%02X"):format(d) or '--'
if last_i ~= i - 4 then
s = s..('@%08X\n'):format(i)
end
s = s..a..b..c..d.."\n"
last_i = i
end
end
return s
end
end
end
return writers