Lunatic: remove old gamevar/serialization code.

git-svn-id: https://svn.eduke32.com/eduke32@3771 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-05-15 18:32:52 +00:00
parent 3092fb20e7
commit f95967fad7
3 changed files with 2 additions and 142 deletions

View file

@ -1746,131 +1746,11 @@ gv = setmtonce(gv_, tmpmt)
defs_c.create_globals(_G)
---=== Game variables ===---
-- gamevarNames[name] is true if that gamevar was declared
local gamevarNames = {}
-- code for prohibiting initial assignments to create new variables,
-- based on 14.2 of PiL
function gamevar(name, initval) -- aka 'declare'
if (type(name) ~= "string") then
error("First argument to 'gamevar' must be a string", 2)
end
if (not string.match(name, "^[%a_][%w_]*$")) then
error("First argument to 'gamevar' must be a valid identifier", 2)
end
if (string.match(name, "^_")) then
error("Identifier names starting with an underscore are reserved", 2)
end
if (gamevarNames[name]) then
error(string.format("Duplicate declaration of identifier '%s'", name), 2)
end
if (rawget(G_, name) ~= nil) then
error(string.format("Identifier name '%s' is already used in the global environment", name), 2)
end
gamevarNames[name] = true
rawset(G_, name, initval or false)
end
---=== Serialization, from PiL with modifications ===---
local function basicSerialize(o)
if (type(o) == "number") then
if (o ~= o) then return "0/0" end -- nan
if (o == 1/0) then return "1/0" end -- inf
if (o == -1/0) then return "-1/0" end -- -inf
return tostring(o)
elseif (type(o) == "boolean") then
return tostring(o)
elseif (type(o) == "string") then
return string.format("%q", o)
end
end
-- will contain all gamevar tables as keys (and true as value)
local tmpgvtabs = {}
local function save(name, value, saved, strings)
saved = saved or {} -- initial value
strings[#strings+1] = name .. "="
local str = basicSerialize(value)
if (str ~= nil) then
strings[#strings+1] = str .. "\n"
elseif (type(value) == "table") then
if (saved[value]) then -- value already saved?
strings[#strings+1] = saved[value] .. "\n" -- use its previous name
else
saved[value] = name -- save name for next time
strings[#strings+1] = "{}\n" -- create a new table
for k,v in pairs(value) do -- save its fields
local keystr = basicSerialize(k)
if (keystr == nil) then
error("cannot save a " .. type(k) .. " as key of a table", 2);
end
local fieldname = string.format("%s[%s]", name, keystr)
if (type(v)=="table" and not tmpgvtabs[v]) then
error("cannot save \""..name..
"\": gamevar tables may only contain tables that are also gamevars", 2)
end
save(fieldname, v, saved, strings)
end
end
else
error("cannot save \""..name.."\", a " .. type(value), 2)
end
end
local function serializeGamevars()
local saved = {}
local strings = {}
for gvname,_ in pairs(gamevarNames) do
if (type(G_[gvname])=="table") then
tmpgvtabs[G_[gvname]] = true
end
end
-- TODO: catch errors in save()
for gvname,_ in pairs(gamevarNames) do
save(gvname, G_[gvname], saved, strings)
end
strings[#strings+1] = "\n"
tmpgvtabs = {}
return table.concat(strings)
end
local function loadGamevarsString(string)
--[=[
for gvname,_ in pairs(gamevarNames) do
G_[gvname] = nil;
end
gamevarNames = {}; -- clear gamevars
--]=]
assert(loadstring(string))()
end
-- REMOVE this for release
DBG_ = {}
DBG_.debug = require("debug")
DBG_.printkv = printkv
DBG_.loadstring = loadstring
DBG_.serializeGamevars = serializeGamevars
DBG_.loadGamevarsString = loadGamevarsString
DBG_.oom = function()
local s = "1"
for i=1,math.huge do

View file

@ -105,10 +105,6 @@ local g_cgopt = { ["no"]=false, ["debug-lineinfo"]=false, ["gendir"]=nil,
["cache-sap"]=false, ["error-nostate"]=true, }
local function csapp() return g_cgopt["cache-sap"] end
-- How many 'if' statements are following immediately each other,
-- needed to cope with CONs dangling-else resolution
local g_iflevel = 0
local g_ifelselevel = 0
-- Stack with *true* on top if the innermost block is a "whilevar*n".
local g_isWhile = {}
-- Sequence number of 'while' statements, used to implement CON "break" inside
@ -3124,8 +3120,6 @@ local function get_code_string(codetab, lineinfop)
end
function on.parse_begin()
g_iflevel = 0
g_ifelselevel = 0
g_isWhile = {}
g_have_file[g_filename] = true

View file

@ -31,27 +31,13 @@ local function checkfail(funcstr, expectedmsg)
end
end
--[[
---- check serialization ----
gamevar("ourvar")
gamevar("ourvar2")
-- test nans, infs, precision, subnorms, booleans
ourvar2 = { "asd"; 0/0, 1/0, -1/0, 0.12345678901234567, 1e-314, true }
ourvar = { ourvar2; 1, 2, 3, "qwe"; [true]=0, [false]=1 }
ourvar[#ourvar+1] = ourvar;
local gvstr = DBG_.serializeGamevars()
ourvar = -1
print("========== attempting to load string: ==========")
print(gvstr)
print("---------- (end string to load) ----------")
-- XXX: need to think about fully restoring state
DBG_.loadGamevarsString(gvstr)
print("ourvar[4]="..ourvar[4])
--]]
print('tweaking sector pals')
print('numsectors: ' .. gv.numsectors .. ' of ' .. gv.MAXSECTORS)