start of item writing
This commit is contained in:
		
							
								
								
									
										122
									
								
								splubp.p8
									
									
									
									
									
								
							
							
						
						
									
										122
									
								
								splubp.p8
									
									
									
									
									
								
							| @@ -4,7 +4,8 @@ __lua__ | |||||||
| -- splubp data transport | -- splubp data transport | ||||||
| -- by kistaro windrider | -- by kistaro windrider | ||||||
|  |  | ||||||
| -- main | -- shrinko-8 hints | ||||||
|  | DEBUG = true --[[const]] | ||||||
|  |  | ||||||
| -- >8 | -- >8 | ||||||
| -- utilities | -- utilities | ||||||
| @@ -44,54 +45,107 @@ function mknew(tt) | |||||||
|  return tt |  return tt | ||||||
| end | end | ||||||
|  |  | ||||||
| function set(t) | function trim(s) | ||||||
|  local ret = {} |  local f, e = 1, #s | ||||||
|  for v in all(t) do |  while (f <= e and s[f]==" ") f += 1 | ||||||
|   ret[v]=true |  while (e >= f and s[e]==" ") e -= 1 | ||||||
|  |  if (f<e) return sub(s,f,e) | ||||||
|  |  return "" | ||||||
|  | end | ||||||
|  |  | ||||||
|  | function shatter(s) | ||||||
|  |  local ret ={} | ||||||
|  |  for line in all(split(s, "\n")) do | ||||||
|  |   local row = {} | ||||||
|  |   for cell in all(split(split(line, "--")[1], " ")) do | ||||||
|  |    cell = trim(cell) | ||||||
|  |    if (#cell > 0) add(row, cell) | ||||||
|  |   end | ||||||
|  |   if (#row > 0) add(ret, row) | ||||||
|  end |  end | ||||||
|  return ret |  return ret | ||||||
| end | end | ||||||
|  |  | ||||||
| whitespace = set(split" ,\t,\n") | function crush(s) | ||||||
|  |  ret = "" | ||||||
| function trim(s) |  for line in shatter(s) do | ||||||
|  local f, e = 1, #s |   for tok in line do | ||||||
|  while (f <= e and whitespace[s[f]]) f += 1 |    ret..=tok.." " | ||||||
|  while (e >= f and whitespace[s[e]]) e -= 1 |   end | ||||||
|  if (f<e) return sub(s,f,e) |   ret = sub(ret, 1, -2).."\n" | ||||||
|  |  end | ||||||
|  |  if (#ret > 0) return sub(ret, 1, -2) | ||||||
|  return "" |  return "" | ||||||
| end | end | ||||||
|  |  | ||||||
|  | -- >8 | ||||||
|  | -- common | ||||||
|  |  | ||||||
|  | --[[preserve-keys]] splubp = mknew{ | ||||||
|  |  ptr = 0x8000, | ||||||
|  |  init = function(self) | ||||||
|  |   -- fill from ffile or saved file | ||||||
|  |   self.fmts = {} | ||||||
|  |   if (not self.ffile) self.ffile, self.ptr = self:e() | ||||||
|  |   for f in all(split(self.ffile, "===", false)) do | ||||||
|  |    local tok = shatter(f) | ||||||
|  |    assert(#tok > 1, "not enough rows in "..tostr(tok)) -- debug | ||||||
|  |    assert(#tok[1] == 1 and tok[1][1][1] == ".", "bad extension row: "..tostr(tok[1])) -- debug | ||||||
|  |    self.fmts[sub(deli(tok, 1)[1], 1)] = tok | ||||||
|  |   end | ||||||
|  |  end, | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function splubp:e() | ||||||
|  |  local ret = chr(peek(self.ptr + 2, %self.ptr)) | ||||||
|  |  return ret, self.ptr + 2 + #ret | ||||||
|  | end | ||||||
|  |  | ||||||
| -- >8 | -- >8 | ||||||
| -- writer | -- writer | ||||||
|  |  | ||||||
| splubp_writer = mknew{ | function splubp:emit_fmts() | ||||||
|  init = function(self) |  self.ptr = self:emit_e(self.ffile) | ||||||
|   -- fill from single file | end | ||||||
|   self.fmts = self.fmts or {} |  | ||||||
|   if self.ffile then | function splubp:emit_item(obj, filename) | ||||||
|    for f in all(split(self.ffile, "===", false)) do |  debug_last_emit = obj -- global debug | ||||||
|     local ext, fmt = split(f, "---") |  filename = filename or obj._file | ||||||
|     self.fmts[trim(ext)] = trim(fmt) |  obj._addr = self.ptr | ||||||
|    end |  self.ptr = self:write_s(filename) | ||||||
|  |  for row in all(self.fmts[split(filename, ".")[2]]) do | ||||||
|  |   local alt = self.wdirectives[row[1]] | ||||||
|  |   if alt then | ||||||
|  |    self.ptr = alt(self, obj, row) | ||||||
|  |   else | ||||||
|  |    self.ptr = self:write_fmt(obj[row[1]], row, 2) | ||||||
|   end |   end | ||||||
|  end |  end | ||||||
| } | end | ||||||
|  |  | ||||||
|  | -- TODO: splubp.wdirectives -- a | ||||||
|  | --       table mapping non-key | ||||||
|  | --       special values to | ||||||
|  | --       functions to write them      | ||||||
|  | -- TODO: write_fmt: recursive | ||||||
|  | --       writing thing | ||||||
|  |  | ||||||
|  | function splubp:write_s(s) | ||||||
|  |  poke(self.ptr, #s, ord(s, 1, #s)) | ||||||
|  |  return self.ptr + 1 + #s | ||||||
|  | ) | ||||||
|  | end | ||||||
|  |  | ||||||
|  | function splubp:write_e(s) | ||||||
|  |  poke2(self.ptr, #s) | ||||||
|  |  poke(self.ptr+2, ord(s, 1, #s)) | ||||||
|  |  return self.ptr + 2 + #s | ||||||
|  | end | ||||||
|  |  | ||||||
| -->8 | -->8 | ||||||
| -- loader | -- loader | ||||||
|  |  | ||||||
| -- essay: `e` directive | function splubp:s() | ||||||
| -- reads int16 char count, |  local ret = chr(peek(self.ptr + 1, @self.ptr)) | ||||||
| -- then parses that many bytes |  return ret, self.ptr + 1 + #ret | ||||||
| -- into a string (via chr). |  | ||||||
| -- returns string, new read offset. |  | ||||||
| function splubp_read_essay(addr) |  | ||||||
|  local cs, n = {}, %addr |  | ||||||
|  for a2 = addr+2, addr+n+1 do |  | ||||||
|   add(cs, @a2) |  | ||||||
|  end |  | ||||||
|  return chr(unpack(cs)), addr+2+n |  | ||||||
| end | end | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user