foreachmap.lua: remove the need for a space for certain '.' accesses.

That is, now it's not necessary to write e.g. "io. write" -- .xxx is
translated to <what>[i].xxx only if it's a matching member name.

git-svn-id: https://svn.eduke32.com/eduke32@4105 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-10-16 19:43:01 +00:00
parent 78e8f476e1
commit 4cb9ab1d7c
3 changed files with 63 additions and 17 deletions

View file

@ -132,7 +132,7 @@ Writing and using modules
In Lunatic, like in Lua, a _module_ is a conceptually a sort of package that In Lunatic, like in Lua, a _module_ is a conceptually a sort of package that
unites related bits of functionality. Language-wise, it is simply a Lua unites related bits of functionality. Language-wise, it is simply a Lua
table holding its contents, as described and motivated at length in table holding its contents, as described and motivated at length in
_Programming in Lua_. _Programming in Lua_ (second edition).
The ``creation'' and ``usage'' sides of the modularity concept are reflected in The ``creation'' and ``usage'' sides of the modularity concept are reflected in
two functions known from Lua 5.1, `module` and `require`. The former is a two functions known from Lua 5.1, `module` and `require`. The former is a

View file

@ -10,6 +10,7 @@ local table = require "table"
local error = error local error = error
local assert = assert local assert = assert
local pairs = pairs
local print = print local print = print
local setmetatable = setmetatable local setmetatable = setmetatable
local tostring = tostring local tostring = tostring
@ -17,9 +18,8 @@ local tonumber = tonumber
module(...) module(...)
ffi.cdef[[ local STRUCTDEF = {
typedef struct sector = [[
{
int16_t wallptr, wallnum; int16_t wallptr, wallnum;
int32_t ceilingz, floorz; int32_t ceilingz, floorz;
uint16_t ceilingstat, floorstat; uint16_t ceilingstat, floorstat;
@ -31,10 +31,9 @@ typedef struct
uint8_t floorpal, floorxpanning, floorypanning; uint8_t floorpal, floorxpanning, floorypanning;
uint8_t visibility, filler; uint8_t visibility, filler;
int16_t lotag, hitag, extra; int16_t lotag, hitag, extra;
} sectortype; ]],
typedef struct wall = [[
{
int32_t x, y; int32_t x, y;
int16_t point2, nextwall, nextsector; int16_t point2, nextwall, nextsector;
uint16_t cstat; uint16_t cstat;
@ -42,10 +41,9 @@ typedef struct
int8_t shade; int8_t shade;
uint8_t pal, xrepeat, yrepeat, xpanning, ypanning; uint8_t pal, xrepeat, yrepeat, xpanning, ypanning;
int16_t lotag, hitag, extra; int16_t lotag, hitag, extra;
} walltype; ]],
typedef struct sprite = [[
{
int32_t x, y, z; int32_t x, y, z;
uint16_t cstat; uint16_t cstat;
int16_t picnum; int16_t picnum;
@ -56,8 +54,25 @@ typedef struct
int16_t sectnum, statnum; int16_t sectnum, statnum;
int16_t ang, owner, xvel, yvel, zvel; int16_t ang, owner, xvel, yvel, zvel;
int16_t lotag, hitag, extra; int16_t lotag, hitag, extra;
]],
}
ffi.cdef([[
typedef struct
{
]]..STRUCTDEF.sector..[[
} sectortype;
typedef struct
{
]]..STRUCTDEF.wall..[[
} walltype;
typedef struct
{
]]..STRUCTDEF.sprite..[[
} spritetype; } spritetype;
]] ]])
ffi.cdef[[ ffi.cdef[[
size_t fread(void *ptr, size_t size, size_t nmemb, void *stream); size_t fread(void *ptr, size_t size, size_t nmemb, void *stream);
@ -65,6 +80,24 @@ size_t fread(void *ptr, size_t size, size_t nmemb, void *stream);
local C = ffi.C local C = ffi.C
-- [<sector/wall/sprite>].<membername> = true
local is_member_tab = {
sector = {},
wall = {},
sprite = {},
}
do
for what, sdef in pairs(STRUCTDEF) do
for membname in string.gmatch(sdef, "([a-z0-9_]+)[,;]") do
is_member_tab[what][membname] = true
end
end
end
function ismember(what, membname)
return (is_member_tab[what][membname] ~= nil)
end
MAX = MAX =
{ {

View file

@ -16,19 +16,19 @@
-- forxcode example: print sprite numbers with lotag < -1 (lotag is signed for us), -- forxcode example: print sprite numbers with lotag < -1 (lotag is signed for us),
-- and for each matching sprite also print its lotag and picnum: -- and for each matching sprite also print its lotag and picnum:
-- $ ./findmaps.sh ~/.eduke32/ "sprite: .lotag < -1 :: io. write(', '.. .lotag .. ' ' .. .picnum)" -- $ ./findmaps.sh ~/.eduke32/ "sprite: .lotag < -1 :: io.write(', '.. .lotag .. ' ' .. .picnum)"
-- The local 'd' provides defs loaded from ../../names.h, example: -- The local 'd' provides defs loaded from ../../names.h, example:
-- $ ./findmaps.sh ~/.eduke32/ "sprite: .picnum>=d. CRACK1 and .picnum<=d. CRACK4" -- $ ./findmaps.sh ~/.eduke32/ "sprite: .picnum>=d.CRACK1 and .picnum<=d.CRACK4"
-- (The space between "d." and "CRACK" is because ".xxx" is translated to -- (Now: no space between "d." and "CRACK" is necessary ".xxx" is translated to
-- "sprite[<current>].xxx".) -- "sprite[<current>].xxx" only if it's the name of a sprite member.)
-- Print all V9 maps along with their number of bunches and max(ceilings of a bunch) -- Print all V9 maps along with their number of bunches and max(ceilings of a bunch)
-- $ prog='if (map.version==9) then print(map.numbunches.." ".. math.max(unpack(map.sectsperbunch[0],0)) .." "..fn) end' -- $ prog='if (map.version==9) then print(map.numbunches.." ".. math.max(unpack(map.sectsperbunch[0],0)) .." "..fn) end'
-- $ ./findmaps.sh ~/.eduke32 "$prog" |sort -n -k 2 -- $ ./findmaps.sh ~/.eduke32 "$prog" |sort -n -k 2
-- Print all MUSICANDSFX sprites that play sounds with bit 1 set. -- Print all MUSICANDSFX sprites that play sounds with bit 1 set.
-- ./findmaps.sh /g/Games/Eduke32c/grp 'sprite: .picnum==5 and eq(.lotag, {170, 186, 187, 279, 382, 347}) :: io. write(" ".. tostring(.lotag))' -- ./findmaps.sh /g/Games/Eduke32c/grp 'sprite: .picnum==5 and eq(.lotag, {170, 186, 187, 279, 382, 347}) :: io.write(" ".. tostring(.lotag))'
local B = require "build" local B = require "build"
local string = require "string" local string = require "string"
@ -62,6 +62,18 @@ function sum(tab, initidx)
return s return s
end end
local g_what
-- Maybe replace e.g. .nextwall --> wall[i].nextwall.
-- <what>: one of "sector", "wall" or "sprite"
-- <maybememb>: a potential member name prefixed by "."
local function maybe_complete_member(maybememb)
if (B.ismember(g_what, maybememb:sub(2))) then
return g_what.."[i]"..maybememb
else
return maybememb
end
end
local mod local mod
if (modname:sub(1,2) == "-e") then if (modname:sub(1,2) == "-e") then
local body = modname:sub(3) local body = modname:sub(3)
@ -69,9 +81,10 @@ if (modname:sub(1,2) == "-e") then
-- sector/wall/sprite finder shortcut -- sector/wall/sprite finder shortcut
local b, e, what = body:find("^([a-z]+)::?") local b, e, what = body:find("^([a-z]+)::?")
if (what) then if (what) then
g_what = what
local onlyfiles = (body:sub(e-1,e)=="::") -- "::" means "only list files" (like grep -l) local onlyfiles = (body:sub(e-1,e)=="::") -- "::" means "only list files" (like grep -l)
body = body:sub(e+1) -- clip off "bla::" body = body:sub(e+1) -- clip off "bla::"
body = body:gsub("%.[a-z]+", what.."[i]%0") -- e.g. .lotag --> sprite[i].lotag body = body:gsub("%.[a-z][a-z0-9]*", maybe_complete_member) -- e.g. .lotag --> sprite[i].lotag
local perxcode local perxcode
-- look for additional "print" code to be executed for each match -- look for additional "print" code to be executed for each match