From 0678d3eb589e38bcc92f17c56f999b6b8c89dbb5 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 26 Apr 2016 21:34:05 -0700 Subject: [PATCH] move word_writer to lips.writers, add others --- lips/init.lua | 23 ++----------- lips/writers.lua | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 lips/writers.lua diff --git a/lips/init.lua b/lips/init.lua index f7f6242..13bd35a 100644 --- a/lips/init.lua +++ b/lips/init.lua @@ -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() diff --git a/lips/writers.lua b/lips/writers.lua new file mode 100644 index 0000000..45bf6d8 --- /dev/null +++ b/lips/writers.lua @@ -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