1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-05-14 22:33:23 -07:00
lips/lips/util.lua

24 lines
536 B
Lua
Raw Normal View History

2016-01-15 11:15:02 -08:00
local floor = math.floor
local open = io.open
2016-04-10 04:03:00 -07:00
local function readfile(fn, binary)
local mode = binary and 'rb' or 'r'
local f = open(fn, mode)
2016-01-15 11:15:02 -08:00
if not f then
2016-04-10 04:03:00 -07:00
local kind = binary and 'binary' or 'assembly'
error('could not open '..kind..' file for reading: '..tostring(fn), 2)
2016-01-15 11:15:02 -08:00
end
2016-04-10 04:03:00 -07:00
local data = f:read('*a')
2016-01-15 11:15:02 -08:00
f:close()
2016-04-10 04:03:00 -07:00
return data
2016-01-15 11:15:02 -08:00
end
local function bitrange(x, lower, upper)
return floor(x/2^lower) % 2^(upper - lower + 1)
end
return {
readfile = readfile,
bitrange = bitrange,
}