From c320dba4d0a3c3e800ce73362c3671935caec478 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 28 Dec 2014 15:40:04 +0000 Subject: [PATCH] Lunatic translator: add stand-alone option -fnames, generating names.h file. git-svn-id: https://svn.eduke32.com/eduke32@4860 1a8010ca-5511-0410-912e-c29ae57300e0 --- .../eduke32/source/lunatic/doc/lunacon.txt | 5 ++ polymer/eduke32/source/lunatic/lunacon.lua | 50 ++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/polymer/eduke32/source/lunatic/doc/lunacon.txt b/polymer/eduke32/source/lunatic/doc/lunacon.txt index ca2113eb1..7510f70bb 100644 --- a/polymer/eduke32/source/lunatic/doc/lunacon.txt +++ b/polymer/eduke32/source/lunatic/doc/lunacon.txt @@ -171,6 +171,11 @@ Disable printing out the generated code, but validate whether it is syntactically legal Lua code. A failure of this check represents a bug with LunaCON itself. +`-fnames` (stand-alone only):: +Instead of generating Lua code, output all ++define++d names that were ever +passed as tile number to `actor`, `useractor` or `eventloadactor` in a form +suitable for the `names.h` file read by Mapster32. + `-ferror-nostate` (default: on):: If enabled, an attempt to call a `state` that was not previously defined results in an error. Otherwise, a warning is issued and no code is generated diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index f185aec7b..c6323f6a8 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -130,6 +130,13 @@ local g_cgopt = { ["no"]=false, ["debug-lineinfo"]=false, ["gendir"]=nil, ["error-nonlocal-userdef"]=true, ["error-negative-tag-write"]=false, } +if (string.dump) then + g_cgopt["names"] = false +end + +-- For -fnames mode. +local g_actorTileToName = {} + local function csapp() return g_cgopt["cache-sap"] end local function handle_cmdline_arg(str) @@ -537,10 +544,30 @@ for i=4,7 do end +-- Table of functions doing various lookups (label, gamevar, ...) +local lookup = {} + +-- For -fnames mode. +function on.fnames_tilenum_label(tilenum) + if (g_cgopt["names"] and type(tilenum)=="string") then + -- may be a string (define label) + -- HANDLE_RAWDEFINE + local pos, minus, label = tilenum:match("(.-):(.-):(.+)") + local realtilenum = lookup.defined_label(tonumber(pos), minus, label) + + g_actorTileToName[realtilenum] = label + return true + end +end + function on.actor_end(pos, usertype, tsamm, codetab) local tilenum = tsamm[1] local flags = 0 + if (on.fnames_tilenum_label(tilenum)) then + return + end + if (usertype ~= nil) then -- useractor if (not (bit.band(usertype, bit.bnot(7)) == 0)) then perrprintf(pos, "invalid usertype: must be bitwise OR of 1, 2 and/or 4") @@ -631,6 +658,10 @@ function on.event_end(pos, eventidx, codetab) end function on.eventloadactor_end(pos, tilenum, codetab) + if (on.fnames_tilenum_label(tilenum)) then + return + end + -- Translate eventloadactor into a chained EVENT_LOADACTOR block paddcodef(pos, "gameevent{'LOADACTOR', function (_aci,_pli,_dist)") addcode(get_cache_sap_code()) @@ -799,9 +830,6 @@ function reset.labels() end end --- Table of functions doing various lookups (label, gamevar, ...) -local lookup = {} - function lookup.defined_label(pos, maybe_minus_str, identifier) local num = g_labeldef[identifier] @@ -1374,6 +1402,7 @@ function Cmd.definesound(sndlabel, fn, ...) local sndnum if (type(sndlabel)=="string") then + -- HANDLE_RAWDEFINE local pos, minus, label = sndlabel:match("(.-):(.-):(.+)") sndnum = lookup.defined_label(tonumber(pos), minus, label) @@ -1707,8 +1736,11 @@ local tok = identifier = Var("t_identifier"), -- This one matches keywords, too: identifier_all = Var("t_identifier_all"), + define = Var("t_define"), rawdefine = Var("t_rawdefine"), + actordefine = g_cgopt["names"] and Var("t_rawdefine") or Var("t_define"), + move = Var("t_move"), ai = Var("t_ai"), action = Var("t_action"), @@ -3275,7 +3307,7 @@ local stmt_list_nosp_or_eps = lpeg.Ct((stmt_list * (sp1 * stmt_list)^0)^-1) local common = {} -- common to actor and useractor: [ [ [ [... ]]]] -common.actor_end = sp1 * lpeg.Ct(tok.define * +common.actor_end = sp1 * lpeg.Ct(tok.actordefine * (sp1 * tok.define * (sp1 * tok.action * (sp1 * tok.move * @@ -3303,7 +3335,7 @@ local Cblock = { -- useractor (...) useractor = POS() * sp1 * tok.define * common.actor_end / on.actor_end, -- eventloadactor - eventloadactor = POS() * sp1 * tok.define * sp1 * stmt_list_or_eps * "enda" + eventloadactor = POS() * sp1 * tok.actordefine * sp1 * stmt_list_or_eps * "enda" / on.eventloadactor_end, onevent = POS() * sp1 * tok.define * sp1 * stmt_list_or_eps * "endevent" @@ -3897,7 +3929,13 @@ if (string.dump) then g_directory, filename, errmsg)) end - if (lineinfo) then + if (g_cgopt["names"]) then + for i=0,C.MAXTILES-1 do + if (g_actorTileToName[i]) then + msgfile:write(format("#define %s %d\n", g_actorTileToName[i], i)) + end + end + elseif (lineinfo) then for i=1,#lineinfo.llines do msgfile:write(format("%d -> %s:%d\n", i, lineinfo:getfline(i))) end