1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-05-13 14:03:24 -07:00
lips/lips/init.lua

78 lines
2.0 KiB
Lua
Raw Normal View History

2016-01-13 09:51:29 -08:00
local lips = {
2015-11-20 16:21:13 -08:00
_DESCRIPTION = 'Assembles MIPS assembly files for the R4300i CPU.',
_URL = 'https://github.com/notwa/lips/',
2015-11-20 16:21:13 -08:00
_LICENSE = [[
Copyright (C) 2015,2016 Connor Olding
2015-11-20 16:21:13 -08:00
This program is licensed under the terms of the MIT License, and
is distributed without any warranty. You should have received a
copy of the license along with this program; see the file LICENSE.
]],
}
2016-01-15 11:15:02 -08:00
local util = require "lips.util"
local Parser = require "lips.Parser"
2016-01-13 09:51:29 -08:00
function lips.word_writer()
local buff = {}
2015-12-25 01:15:48 -08:00
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
2015-11-20 11:53:09 -08:00
end
end
2016-01-13 09:51:29 -08:00
function lips.assemble(fn_or_asm, writer, options)
-- assemble MIPS R4300i assembly code.
-- if fn_or_asm contains a newline; treat as assembly, otherwise load file.
-- returns error message on error, or nil on success.
2015-11-20 16:21:13 -08:00
fn_or_asm = tostring(fn_or_asm)
local default_writer = not writer
2016-01-13 09:51:29 -08:00
writer = writer or lips.word_writer()
options = options or {}
2015-11-20 14:00:02 -08:00
local function main()
local fn = nil
2015-12-18 23:42:54 -08:00
local asm
2015-11-20 16:21:13 -08:00
if fn_or_asm:find('[\r\n]') then
asm = fn_or_asm
else
fn = fn_or_asm
2016-01-15 11:15:02 -08:00
asm = util.readfile(fn)
2015-12-18 23:42:54 -08:00
options.path = fn:match(".*/")
2015-11-20 16:21:13 -08:00
end
2015-12-17 07:19:24 -08:00
local parser = Parser(writer, fn, options)
parser:parse(asm)
if default_writer then
writer()
end
end
2015-11-20 11:53:09 -08:00
if options.unsafe then
return main()
else
local ok, err = pcall(main)
return err
end
2015-11-20 11:53:09 -08:00
end
2015-11-20 16:21:13 -08:00
2016-01-13 09:51:29 -08:00
return setmetatable(lips, {
__call = function(self, ...)
return self.assemble(...)
2015-11-20 16:21:13 -08:00
end,
})