2016-01-13 11:20:28 -08:00
|
|
|
local floor = math.floor
|
|
|
|
local open = io.open
|
|
|
|
|
|
|
|
local function Class(inherit)
|
|
|
|
local class = {}
|
|
|
|
local mt_obj = {__index = class}
|
|
|
|
local mt_class = {
|
|
|
|
__call = function(self, ...)
|
|
|
|
local obj = setmetatable({}, mt_obj)
|
|
|
|
obj:init(...)
|
|
|
|
return obj
|
|
|
|
end,
|
|
|
|
__index = inherit,
|
|
|
|
}
|
|
|
|
|
|
|
|
return setmetatable(class, mt_class)
|
|
|
|
end
|
|
|
|
|
2016-04-10 06:07:06 -07:00
|
|
|
local function readfile(fn, binary)
|
|
|
|
local mode = binary and 'rb' or 'r'
|
|
|
|
local f = open(fn, mode)
|
2016-01-13 11:20:28 -08:00
|
|
|
if not f then
|
2016-04-10 06:07:06 -07:00
|
|
|
local kind = binary and 'binary' or 'assembly'
|
|
|
|
error('could not open '..kind..' file for reading: '..tostring(fn), 2)
|
2016-01-13 11:20:28 -08:00
|
|
|
end
|
2016-04-10 06:07:06 -07:00
|
|
|
local data = f:read('*a')
|
2016-01-13 11:20:28 -08:00
|
|
|
f:close()
|
2016-04-10 06:07:06 -07:00
|
|
|
return data
|
2016-01-13 11:20:28 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function bitrange(x, lower, upper)
|
|
|
|
return floor(x/2^lower) % 2^(upper - lower + 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
Class = Class,
|
|
|
|
readfile = readfile,
|
|
|
|
bitrange = bitrange,
|
|
|
|
}
|