major code reuse improvement, more ops
This commit is contained in:
104
splubp.p8
104
splubp.p8
@@ -89,36 +89,65 @@ end
|
|||||||
if (not self.ffile) self.ffile, self.ptr = self:e()
|
if (not self.ffile) self.ffile, self.ptr = self:e()
|
||||||
for f in all(split(self.ffile, "===", false)) do
|
for f in all(split(self.ffile, "===", false)) do
|
||||||
local tok = shatter(f)
|
local tok = shatter(f)
|
||||||
assert(#tok > 1, "not enough rows in "..tostr(tok)) -- debug
|
if DEBUG then
|
||||||
assert(#tok[1] == 1 and tok[1][1][1] == ".", "bad extension row: "..tostr(tok[1])) -- debug
|
assert(#tok > 1, "not enough rows in "..tostr(tok))
|
||||||
self.fmts[sub(deli(tok, 1)[1], 1)] = tok
|
assert(#tok[1] == 1, "bad extension row: "..tostr(tok[1]))
|
||||||
|
end
|
||||||
|
self.fmts[deli(tok, 1)[1]] = tok
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function splubp:ppi(n)
|
||||||
|
self.ptr += n
|
||||||
|
return self.ptr - n
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:op()
|
||||||
|
self.cri += 1
|
||||||
|
return self.row[self.cri]
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:c(n)
|
||||||
|
n = n or 1
|
||||||
|
return peek(self:ppi(n), n)
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:i()
|
||||||
|
return %self:ppi(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:n()
|
||||||
|
return $self:ppi(4)
|
||||||
|
end
|
||||||
|
|
||||||
function splubp:e()
|
function splubp:e()
|
||||||
local ret = chr(peek(self.ptr + 2, %self.ptr))
|
return chr(self:c(self:i()))
|
||||||
return ret, self.ptr + 2 + #ret
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- >8
|
-- >8
|
||||||
-- writer
|
-- writer
|
||||||
|
|
||||||
function splubp:emit_fmts()
|
function splubp:emit_fmts()
|
||||||
self.ptr = self:emit_e(self.ffile)
|
self:write_e(self.ffile)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- execute next op (write data)
|
||||||
|
function splubp:wnx(...)
|
||||||
|
return self["write_"..self:op()](self, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function splubp:emit_item(obj, filename)
|
function splubp:emit_item(obj, filename)
|
||||||
debug_last_emit = obj -- global debug
|
debug_last_emit = obj -- global debug
|
||||||
filename = filename or obj._file
|
filename = filename or obj._file
|
||||||
obj._addr = self.ptr
|
obj._addr = self.ptr
|
||||||
self.ptr = self:write_s(filename)
|
self:write_s(filename)
|
||||||
for row in all(self.fmts[split(filename, ".")[2]]) do
|
for row in all(self.fmts[split(filename, ".")[2]]) do
|
||||||
local alt = self.wdirectives[row[1]]
|
local alt = self.wdirectives[row[1]]
|
||||||
if alt then
|
if alt then
|
||||||
self.ptr = alt(self, obj, row)
|
alt(self, obj, row)
|
||||||
else
|
else
|
||||||
self.ptr = self:write_fmt(obj[row[1]], row, 2)
|
self:write_fmt(obj[row[1]], row, 2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -130,22 +159,61 @@ end
|
|||||||
-- TODO: write_fmt: recursive
|
-- TODO: write_fmt: recursive
|
||||||
-- writing thing
|
-- writing thing
|
||||||
|
|
||||||
|
-- Write each argument as one
|
||||||
|
-- byte at self.ptr, then
|
||||||
|
-- increment self.ptr by the
|
||||||
|
-- number of bytes written.
|
||||||
|
function splubp:write_c(...)
|
||||||
|
poke(self:ppi(#{...}), ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Like etch but it takes only
|
||||||
|
-- a single 2-byte int.
|
||||||
|
function splubp:write_i(i)
|
||||||
|
poke2(self:ppi(2), i)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Like etch but it takes only
|
||||||
|
-- a single 4-byte number.
|
||||||
|
function splubp:write_n(n)
|
||||||
|
poke4(self:ppi(4), n)
|
||||||
|
end
|
||||||
|
|
||||||
function splubp:write_s(s)
|
function splubp:write_s(s)
|
||||||
poke(self.ptr, #s, ord(s, 1, #s))
|
self:write_c(#s, ord(s, 1, #s))
|
||||||
return self.ptr + 1 + #s
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function splubp:write_e(s)
|
function splubp:write_e(s)
|
||||||
poke2(self.ptr, #s)
|
self:write_i(#s)
|
||||||
poke(self.ptr+2, ord(s, 1, #s))
|
self:write_c(ord(s,1,#s))
|
||||||
return self.ptr + 2 + #s
|
end
|
||||||
|
|
||||||
|
splubp.write_b = splubp.write_c
|
||||||
|
|
||||||
|
function splubp:write_shr(n)
|
||||||
|
n <<= self:op()
|
||||||
|
self:wnx(n)
|
||||||
end
|
end
|
||||||
|
|
||||||
-->8
|
-->8
|
||||||
-- loader
|
-- loader
|
||||||
|
|
||||||
function splubp:s()
|
-- execute next op (read)
|
||||||
local ret = chr(peek(self.ptr + 1, @self.ptr))
|
function splubp:rnx()
|
||||||
return ret, self.ptr + 1 + #ret
|
return self[self:op()](self)
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:s()
|
||||||
|
return chr(self:c(self:c()))
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:b()
|
||||||
|
local ret = self:c()
|
||||||
|
if (ret < 128) return ret
|
||||||
|
return ret-256
|
||||||
|
end
|
||||||
|
|
||||||
|
function splubp:shr()
|
||||||
|
local amt = self:op()
|
||||||
|
return self:rnx() >>> amt
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user