2012-04-07 20:10:03 +00:00
|
|
|
-- LunaCON CON to Lunatic translator
|
|
|
|
-- requires LPeg, http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html
|
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
local require = require
|
2012-04-07 20:10:03 +00:00
|
|
|
local lpeg = require("lpeg")
|
|
|
|
|
2013-01-01 15:24:28 +00:00
|
|
|
local bit
|
2012-12-09 13:24:28 +00:00
|
|
|
local math = require("math")
|
|
|
|
local string = require("string")
|
|
|
|
local table = require("table")
|
|
|
|
|
2012-12-09 13:24:39 +00:00
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
local arg = arg
|
|
|
|
|
|
|
|
local assert = assert
|
2013-01-13 16:40:07 +00:00
|
|
|
local ipairs = ipairs
|
2012-12-09 13:24:28 +00:00
|
|
|
local pairs = pairs
|
|
|
|
local pcall = pcall
|
|
|
|
local print = print
|
|
|
|
local tonumber = tonumber
|
2012-12-23 19:24:16 +00:00
|
|
|
local tostring = tostring
|
2012-12-09 13:24:28 +00:00
|
|
|
local type = type
|
2012-12-28 17:18:02 +00:00
|
|
|
local unpack = unpack
|
2012-12-09 13:24:28 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
-- non-nil if running from EDuke32
|
|
|
|
-- (read_into_string~=nil iff string.dump==nil)
|
|
|
|
local read_into_string = read_into_string
|
2013-01-01 15:24:22 +00:00
|
|
|
local ffi, ffiC
|
2012-12-29 15:21:24 +00:00
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
if (string.dump) then
|
2013-01-01 15:24:28 +00:00
|
|
|
bit = require("bit")
|
|
|
|
-- For Rio Lua:
|
2013-01-02 22:33:37 +00:00
|
|
|
-- bit = { bor=function() return 0 end }
|
2012-06-17 19:45:33 +00:00
|
|
|
require("strict")
|
2013-01-01 15:24:22 +00:00
|
|
|
else
|
2013-01-01 15:24:28 +00:00
|
|
|
bit = require("bit")
|
2013-01-01 15:24:22 +00:00
|
|
|
ffi = require("ffi")
|
|
|
|
ffiC = ffi.C
|
2012-06-17 19:45:33 +00:00
|
|
|
end
|
|
|
|
|
2012-04-29 20:54:06 +00:00
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
module("lunacon")
|
2012-12-09 13:24:28 +00:00
|
|
|
|
|
|
|
|
2012-06-17 19:45:33 +00:00
|
|
|
-- I think that the "too many pending calls/choices" is unavoidable in general.
|
|
|
|
-- This limit is of course still arbitrary, but writing long if/else cascades
|
2012-04-29 20:54:06 +00:00
|
|
|
-- in CON isn't pretty either (though sometimes necessary because nested switches
|
|
|
|
-- don't work?)
|
|
|
|
-- See also: http://lua-users.org/lists/lua-l/2010-03/msg00086.html
|
|
|
|
lpeg.setmaxstack(1024);
|
|
|
|
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
local Pat, Set, Range, Var = lpeg.P, lpeg.S, lpeg.R, lpeg.V
|
2012-07-01 22:11:17 +00:00
|
|
|
local POS = lpeg.Cp
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
-- CON language definitions (among other things, all keywords pattern).
|
2012-11-30 18:57:40 +00:00
|
|
|
local conl = require("con_lang")
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
local function match_until(matchsp, untilsp) -- (!untilsp matchsp)* in PEG
|
|
|
|
-- sp: string or pattern
|
|
|
|
return (matchsp - Pat(untilsp))^0
|
|
|
|
end
|
|
|
|
|
2012-11-30 18:57:40 +00:00
|
|
|
local format = string.format
|
|
|
|
|
2012-06-10 18:56:18 +00:00
|
|
|
local function printf(fmt, ...)
|
2012-11-30 18:57:40 +00:00
|
|
|
print(format(fmt, ...))
|
2012-06-10 18:56:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
---=== semantic action functions ===---
|
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
local inf = 1/0
|
2012-06-17 19:45:33 +00:00
|
|
|
local NaN = 0/0
|
2012-06-10 18:56:21 +00:00
|
|
|
|
|
|
|
-- Last keyword position, for error diagnosis.
|
|
|
|
local g_lastkwpos = nil
|
|
|
|
local g_lastkw = nil
|
|
|
|
local g_badids = {} -- maps bad id strings to 'true'
|
|
|
|
|
|
|
|
local g_recurslevel = -1 -- 0: base CON file, >0 included
|
2012-06-13 23:13:26 +00:00
|
|
|
local g_filename = "???"
|
2012-06-10 18:56:21 +00:00
|
|
|
local g_directory = "" -- with trailing slash if not empty
|
2012-12-09 13:24:28 +00:00
|
|
|
local g_maxerrors = 20
|
2012-06-10 18:56:21 +00:00
|
|
|
local g_numerrors = 0
|
|
|
|
|
2012-12-03 18:24:33 +00:00
|
|
|
-- Warning options. Key names are the same as cmdline options, e.g.
|
|
|
|
-- -Wno-bad-identifier for disabling the "bad identifier" warning.
|
2012-12-09 13:24:28 +00:00
|
|
|
local g_warn = { ["not-redefined"]=true, ["bad-identifier"]=true,
|
|
|
|
["number-conversion"]=true, }
|
2012-12-03 18:24:33 +00:00
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
-- Code generation options.
|
2013-01-19 18:28:40 +00:00
|
|
|
local g_cgopt = { ["no"]=false, ["_incomplete"]=false, }
|
2013-01-13 16:40:32 +00:00
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
-- How many 'if' statements are following immediately each other,
|
|
|
|
-- needed to cope with CONs dangling-else resolution
|
2013-01-02 22:33:37 +00:00
|
|
|
local g_iflevel = 0
|
|
|
|
local g_ifelselevel = 0
|
2012-07-01 22:11:17 +00:00
|
|
|
|
|
|
|
---=== Code generation ===---
|
2013-01-13 16:40:32 +00:00
|
|
|
local GVFLAG = {
|
|
|
|
PERPLAYER=1, PERACTOR=2, PERX_MASK=3,
|
|
|
|
SYSTEM = 0x00000800,
|
|
|
|
READONLY = 0x00001000,
|
|
|
|
}
|
2013-01-13 16:40:21 +00:00
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
-- CON --> mangled Lua function name, also existence check:
|
|
|
|
local g_funcname = {}
|
2013-01-13 16:40:32 +00:00
|
|
|
-- [identifier] = { name=<mangled name / code>, flags=<gamevar flags> }
|
2013-01-13 16:40:21 +00:00
|
|
|
local g_gamevar = {}
|
2013-01-05 13:09:31 +00:00
|
|
|
|
2012-12-28 17:18:06 +00:00
|
|
|
local g_have_file = {} -- [filename]=true
|
2012-07-01 22:11:17 +00:00
|
|
|
local g_curcode = nil -- a table of string pieces or other "gencode" tables
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
-- [{actor, event, actor}num]=gencode_table
|
|
|
|
local g_code = { actor={}, event={}, loadactor={} }
|
2012-06-13 23:13:26 +00:00
|
|
|
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
local function ACS(s) return "actor[_aci]"..s end
|
|
|
|
local function SPS(s) return "sprite[_aci]"..s end
|
|
|
|
local function PLS(s) return "player[_pli]"..s end
|
|
|
|
|
|
|
|
|
2012-06-10 18:56:18 +00:00
|
|
|
local function getlinecol(pos) end -- fwd-decl
|
|
|
|
|
2012-12-28 17:18:06 +00:00
|
|
|
local function new_initial_codetab()
|
|
|
|
return {
|
|
|
|
"local _con, _bit, _math = require'con', require'bit', require'math';",
|
2013-01-02 22:33:37 +00:00
|
|
|
"local sector, sprite, actor, player = sector, sprite, actor, player;",
|
2013-01-19 18:28:40 +00:00
|
|
|
"local gameactor, gameevent, _gv = gameactor, gameevent, gv;"
|
2012-12-28 17:18:06 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
-- Creates the table of predefined game variables.
|
|
|
|
-- KEEPINSYNC gamevars.c: Gv_AddSystemVars()
|
|
|
|
local function new_initial_gvartab()
|
|
|
|
local wmembers = conl.wdata_members
|
|
|
|
local MAX_WEAPONS = ffiC and ffiC.MAX_WEAPONS or 12
|
|
|
|
|
2013-01-19 18:28:40 +00:00
|
|
|
local RW = function(varname)
|
|
|
|
return { name=varname, flags=GVFLAG.SYSTEM }
|
|
|
|
end
|
|
|
|
|
|
|
|
local RO = function(varname)
|
|
|
|
return { name=varname, flags=GVFLAG.SYSTEM+GVFLAG.READONLY }
|
|
|
|
end
|
|
|
|
|
|
|
|
local gamevar = {
|
|
|
|
-- XXX: TODO: THISACTOR can mean different things in some contexts,
|
|
|
|
-- e.g. sector[THISACTOR] is sector[sprite[<current actor>].sectnum]
|
|
|
|
THISACTOR = RO "_aci",
|
|
|
|
|
|
|
|
-- TODO
|
|
|
|
RETURN = RW "_RETURN_",
|
|
|
|
|
|
|
|
xdim = RO "_gv.xdim",
|
|
|
|
ydim = RO "_gv.ydim",
|
|
|
|
|
|
|
|
numsectors = RO "_gv.numsectors",
|
|
|
|
NUMSECTORS = RO "_gv.numsectors",
|
|
|
|
NUMWALLS = RO "_gv.numwalls",
|
|
|
|
|
|
|
|
camerax = RW "_gv.cam.pos.x",
|
|
|
|
cameray = RW "_gv.cam.pos.y",
|
|
|
|
cameraz = RW "_gv.cam.pos.z",
|
|
|
|
cameraang = RW "_gv.cam.ang",
|
|
|
|
camerahoriz = RW "_gv.cam.horiz",
|
|
|
|
camerasect = RW "_gv.cam.sect",
|
|
|
|
cameradist = RW "_gv.cam.dist",
|
|
|
|
cameraclock = RW "_gv.cam.clock",
|
|
|
|
}
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
for w=0,MAX_WEAPONS-1 do
|
|
|
|
for i=1,#wmembers do
|
|
|
|
local member = wmembers[i]:gsub(".* ","") -- strip e.g. "int32_t "
|
|
|
|
local name = format("WEAPON%d_%s", w, member:upper())
|
|
|
|
|
|
|
|
local code = format(PLS":weapon(%d).%s", w, member)
|
|
|
|
|
|
|
|
gamevar[name] = { name=code, flags=GVFLAG.PERPLAYER+GVFLAG.SYSTEM }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return gamevar
|
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function reset_codegen()
|
2013-01-05 13:09:31 +00:00
|
|
|
g_funcname = {}
|
2013-01-13 16:40:32 +00:00
|
|
|
g_gamevar = new_initial_gvartab()
|
2013-01-05 13:09:31 +00:00
|
|
|
|
2012-12-28 17:18:06 +00:00
|
|
|
g_have_file = {}
|
|
|
|
g_curcode = new_initial_codetab()
|
2013-01-13 16:40:21 +00:00
|
|
|
g_code.actor, g_code.event, g_code.loadactor = {}, {}, {}
|
2012-12-28 17:18:06 +00:00
|
|
|
|
|
|
|
g_recurslevel = -1
|
|
|
|
g_numerrors = 0
|
2012-07-01 22:11:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function addcode(x)
|
2012-11-30 18:57:40 +00:00
|
|
|
assert(type(x)=="string" or type(x)=="table")
|
2012-07-01 22:11:17 +00:00
|
|
|
g_curcode[#g_curcode+1] = x
|
|
|
|
end
|
|
|
|
|
|
|
|
local function addcodef(fmt, ...)
|
2012-11-30 18:57:40 +00:00
|
|
|
addcode(format(fmt, ...))
|
2012-07-01 22:11:17 +00:00
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:19 +00:00
|
|
|
local function add_code_and_end(codetab, endstr)
|
|
|
|
assert(type(codetab)=="table")
|
|
|
|
addcode(codetab)
|
|
|
|
addcode(endstr)
|
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function on_actor_end(usertype, tsamm, codetab)
|
|
|
|
local tilenum = tsamm[1]
|
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
local str = ""
|
|
|
|
for i=2,math.min(#tsamm,4) do
|
|
|
|
if ((i==3 or i==4) and tsamm[i]=="0") then
|
|
|
|
-- HACK, gameactor() currently doesn't support literals
|
|
|
|
tsamm[i] = "'NO'"
|
|
|
|
end
|
|
|
|
str = str .. tostring(tsamm[i])..","
|
|
|
|
end
|
|
|
|
if (#tsamm==5) then
|
2012-12-28 17:18:02 +00:00
|
|
|
local flags = bit.bor(unpack(tsamm, 5))
|
2012-12-23 19:24:16 +00:00
|
|
|
str = str .. flags..","
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: usertype (is non-nil only for 'useractor')
|
|
|
|
addcodef("gameactor(%d,%sfunction(_aci, _pli, _dist)", tilenum, str)
|
2013-01-05 13:09:19 +00:00
|
|
|
add_code_and_end(codetab, "end)")
|
2012-07-01 22:11:17 +00:00
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
g_code.actor[tilenum] = codetab
|
2012-07-01 22:11:17 +00:00
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
local BAD_ID_CHARS0 = "_/\\*?" -- allowed 1st identifier chars
|
|
|
|
local BAD_ID_CHARS1 = "_/\\*-+?" -- allowed following identifier chars
|
|
|
|
|
|
|
|
local function mangle_name(name, prefix)
|
|
|
|
-- NOTE: the underscore char has to be replaced too, because we're using
|
|
|
|
-- it as the escape char.
|
|
|
|
for i=1,#BAD_ID_CHARS1 do
|
|
|
|
name = name:gsub(BAD_ID_CHARS1:sub(i,i), "_"..i)
|
|
|
|
end
|
|
|
|
return prefix..name
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_state_begin(statename)
|
|
|
|
local ourname = mangle_name(statename, "F")
|
|
|
|
g_funcname[statename] = ourname
|
|
|
|
return ourname
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_state_end(funcname, codetab)
|
|
|
|
addcodef("local function %s(_aci, _pli, _dist)", funcname)
|
2013-01-05 13:09:19 +00:00
|
|
|
add_code_and_end(codetab, "end")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_event_end(eventidx, codetab)
|
|
|
|
addcodef("gameevent(%d, function (_aci, _pli, _dist)", eventidx)
|
|
|
|
add_code_and_end(codetab, "end)")
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
g_code.event[eventidx] = codetab
|
2012-12-03 18:24:33 +00:00
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
----------
|
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
local function linecolstr(pos)
|
|
|
|
local line, col = getlinecol(pos)
|
2012-11-30 18:57:40 +00:00
|
|
|
return format("%d:%d", line, col)
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
local function increment_numerrors()
|
|
|
|
g_numerrors = g_numerrors+1
|
|
|
|
if (g_numerrors == g_maxerrors) then
|
|
|
|
g_numerrors = inf
|
|
|
|
printf("Too many errors (%d), aborting...", g_maxerrors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function perrprintf(pos, fmt, ...)
|
|
|
|
printf("%s %s: error: "..fmt, g_filename, linecolstr(pos), ...)
|
2012-12-09 13:24:28 +00:00
|
|
|
increment_numerrors()
|
2012-07-01 22:11:17 +00:00
|
|
|
end
|
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
local function errprintf(fmt, ...)
|
|
|
|
if (g_lastkwpos) then
|
2012-07-01 22:11:17 +00:00
|
|
|
perrprintf(g_lastkwpos, fmt, ...)
|
2012-06-10 18:56:21 +00:00
|
|
|
else
|
2012-06-13 23:13:26 +00:00
|
|
|
printf("%s ???: error: "..fmt, g_filename, ...)
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
2012-12-09 13:24:28 +00:00
|
|
|
increment_numerrors()
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function pwarnprintf(pos, fmt, ...)
|
|
|
|
printf("%s %s: warning: "..fmt, g_filename, linecolstr(pos), ...)
|
|
|
|
end
|
|
|
|
|
2012-06-13 23:13:26 +00:00
|
|
|
local function warnprintf(fmt, ...)
|
|
|
|
if (g_lastkwpos) then
|
2012-07-01 22:11:17 +00:00
|
|
|
pwarnprintf(g_lastkwpos, fmt, ...)
|
2012-06-13 23:13:26 +00:00
|
|
|
else
|
|
|
|
printf("%s ???: warning: "..fmt, g_filename, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function parse_number(pos, numstr)
|
2013-01-05 13:09:31 +00:00
|
|
|
local num = tonumber((numstr:gsub("h$", "")))
|
2012-06-10 18:56:18 +00:00
|
|
|
|
2012-08-02 10:52:35 +00:00
|
|
|
if (num==nil or num < -0x80000000 or num > 0xffffffff) then
|
2012-07-01 22:11:17 +00:00
|
|
|
perrprintf(pos, "number %s out of the range of a 32-bit integer", numstr)
|
2012-06-17 19:45:33 +00:00
|
|
|
num = NaN
|
2012-06-13 23:13:26 +00:00
|
|
|
elseif (num >= 0x80000000 and numstr:sub(1,2):lower()~="0x") then
|
2012-12-09 13:24:28 +00:00
|
|
|
if (g_warn["number-conversion"]) then
|
|
|
|
pwarnprintf(pos, "number %s converted to a negative one", numstr)
|
|
|
|
end
|
2012-07-25 18:56:18 +00:00
|
|
|
num = num-(0xffffffff+1)
|
2012-06-10 18:56:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return num
|
|
|
|
end
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- Mapping of various "define" types to the respective number of members and
|
|
|
|
-- vice versa
|
|
|
|
local LABEL = { MOVE=2, AI=3, ACTION=5, [2]="move", [3]="ai", [5]="action",
|
|
|
|
NUMBER=1, [1]="number" }
|
2012-06-17 19:45:43 +00:00
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- Function names in the 'con' module
|
|
|
|
local LABEL_FUNCNAME = { [2]="move", [3]="ai", [5]="action" }
|
2012-06-17 19:45:43 +00:00
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
local g_labeldef = {} -- Lua numbers for numbers, strings for composites
|
|
|
|
local g_labeltype = {}
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-06-17 19:45:33 +00:00
|
|
|
local function reset_labels()
|
2012-12-28 17:18:06 +00:00
|
|
|
g_badids = {}
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- NO is also a valid `move', `ai' or `action', but they are handled
|
|
|
|
-- separately in lookup_composite().
|
2013-01-19 18:28:40 +00:00
|
|
|
g_labeldef = {
|
|
|
|
NO = 0,
|
|
|
|
-- NOTE: these are read-only gamevars in C-CON
|
|
|
|
CLIPMASK0 = 65536+1, -- blocking
|
|
|
|
CLIPMASK1 = (256*65536)+64, -- hittable
|
|
|
|
}
|
|
|
|
|
|
|
|
g_labeltype = {
|
|
|
|
NO = LABEL.NUMBER,
|
|
|
|
CLIPMASK0 = LABEL.NUMBER,
|
|
|
|
CLIPMASK1 = LABEL.NUMBER,
|
|
|
|
}
|
2012-11-25 13:18:46 +00:00
|
|
|
|
|
|
|
-- Initialize default defines.
|
2012-11-30 18:57:40 +00:00
|
|
|
for i=1,#conl.labels do
|
|
|
|
for label, val in pairs(conl.labels[i]) do
|
2012-06-17 19:45:33 +00:00
|
|
|
g_labeldef[label] = val
|
2012-11-25 13:18:46 +00:00
|
|
|
g_labeltype[label] = LABEL.NUMBER
|
2012-06-17 19:45:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function lookup_defined_label(pos, maybe_minus_str, identifier)
|
2012-06-10 18:56:21 +00:00
|
|
|
local num = g_labeldef[identifier]
|
|
|
|
|
|
|
|
if (num == nil) then
|
2012-07-01 22:11:17 +00:00
|
|
|
perrprintf(pos, "label \"%s\" is not defined", identifier)
|
2012-06-17 19:45:37 +00:00
|
|
|
return -inf -- return a number for type cleanness
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
if (g_labeltype[identifier] ~= LABEL.NUMBER) then
|
|
|
|
perrprintf(pos, "label \"%s\" is not a `define'd number", identifier)
|
2012-06-17 19:45:43 +00:00
|
|
|
return -inf
|
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
assert(type(num)=="number")
|
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
return (maybe_minus_str=="" and 1 or -1) * num
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
local function do_define_label(identifier, num)
|
2012-11-25 13:18:46 +00:00
|
|
|
local oldtype = g_labeltype[identifier]
|
2012-06-17 19:45:43 +00:00
|
|
|
local oldval = g_labeldef[identifier]
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
if (oldval) then
|
2012-11-25 13:18:46 +00:00
|
|
|
if (oldtype ~= LABEL.NUMBER) then
|
|
|
|
errprintf("refusing to overwrite `%s' label \"%s\" with a `define'd number",
|
|
|
|
LABEL[oldtype], identifier)
|
|
|
|
else
|
2012-11-30 18:57:40 +00:00
|
|
|
-- conl.labels[...]: don't warn for wrong PROJ_ redefinitions
|
2012-12-03 18:24:33 +00:00
|
|
|
if (g_warn["not-redefined"]) then
|
2013-01-05 13:09:19 +00:00
|
|
|
if (oldval ~= num and conl.PROJ[identifier]==nil) then
|
2012-12-03 18:24:33 +00:00
|
|
|
warnprintf("label \"%s\" not redefined with new value %d (old: %d)",
|
|
|
|
identifier, num, oldval)
|
|
|
|
end
|
2012-11-25 13:18:46 +00:00
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
2012-11-25 13:18:46 +00:00
|
|
|
else
|
2013-01-13 16:40:21 +00:00
|
|
|
if (g_gamevar[identifier]) then
|
|
|
|
warnprintf("symbol `%s' already used for game variable", identifier)
|
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- New definition of a label
|
|
|
|
g_labeldef[identifier] = num
|
|
|
|
g_labeltype[identifier] = LABEL.NUMBER
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
local function check_composite_literal(labeltype, pos, num)
|
|
|
|
if (num==0 or num==1) then
|
|
|
|
return (num==0) and "0" or "1"
|
|
|
|
else
|
|
|
|
perrprintf(pos, "literal `%s' number must be either 0 or 1", LABEL[labeltype])
|
|
|
|
return "_INVALIT"
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local function lookup_composite(labeltype, pos, identifier)
|
2012-06-17 19:45:43 +00:00
|
|
|
if (identifier=="NO") then
|
2012-11-25 13:18:46 +00:00
|
|
|
-- NO is a special case and is valid for move, action and ai,
|
|
|
|
-- being the same as passing a literal 0.
|
|
|
|
return "0"
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local val = g_labeldef[identifier]
|
|
|
|
|
|
|
|
if (val == nil) then
|
2012-07-01 22:11:17 +00:00
|
|
|
perrprintf(pos, "label \"%s\" is not defined", identifier)
|
2012-11-25 13:18:46 +00:00
|
|
|
return "_NOTDEF"
|
|
|
|
elseif (g_labeltype[identifier] ~= labeltype) then
|
2012-12-28 17:18:06 +00:00
|
|
|
if (identifier=="randomangle" and labeltype==LABEL.MOVE) then
|
|
|
|
pwarnprintf(pos, "label \"randomangle\" is not a `move' value, assuming 0")
|
|
|
|
return "0"
|
|
|
|
else
|
|
|
|
perrprintf(pos, "label \"%s\" is not a%s `%s' value", identifier,
|
|
|
|
labeltype==LABEL.MOVE and "" or "n", LABEL[labeltype])
|
|
|
|
return "_WRONGTYPE"
|
|
|
|
end
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- Generate a quoted identifier name.
|
2012-11-30 18:57:40 +00:00
|
|
|
val = format("%q", identifier)
|
2012-11-25 13:18:46 +00:00
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
|
|
|
local function do_define_composite(labeltype, identifier, ...)
|
2012-11-25 13:18:46 +00:00
|
|
|
local oldtype = g_labeltype[identifier]
|
2012-06-17 19:45:43 +00:00
|
|
|
local oldval = g_labeldef[identifier]
|
|
|
|
|
|
|
|
if (oldval) then
|
2012-11-25 13:18:46 +00:00
|
|
|
if (oldtype ~= labeltype) then
|
2012-06-17 19:45:43 +00:00
|
|
|
errprintf("refusing to overwrite `%s' label \"%s\" with a `%s' value",
|
2012-11-25 13:18:46 +00:00
|
|
|
LABEL[oldtype], identifier, LABEL[labeltype])
|
|
|
|
else
|
|
|
|
warnprintf("duplicate `%s' definition of \"%s\" ignored",
|
|
|
|
LABEL[labeltype], identifier)
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- Fill up omitted arguments with zeros.
|
|
|
|
local isai = (labeltype == LABEL.AI)
|
|
|
|
local args = {...}
|
|
|
|
for i=#args+1,labeltype do
|
|
|
|
-- passing nil to con.ai will make the action/move the null one
|
|
|
|
args[i] = (isai and i<=2) and "nil" or 0
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
if (isai) then
|
|
|
|
assert(type(args[1])=="string")
|
|
|
|
assert(type(args[2])=="string")
|
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
-- OR together the flags
|
2012-11-25 13:18:46 +00:00
|
|
|
for i=#args,LABEL.AI+1, -1 do
|
|
|
|
-- TODO: check?
|
|
|
|
args[LABEL.AI] = bit.bor(args[LABEL.AI], args[i])
|
|
|
|
args[i] = nil
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-25 13:18:46 +00:00
|
|
|
-- Make a string out of that.
|
|
|
|
for i=1+(isai and 2 or 0),#args do
|
2012-11-30 18:57:40 +00:00
|
|
|
args[i] = format("%d", args[i])
|
2012-11-25 13:18:46 +00:00
|
|
|
end
|
|
|
|
|
2012-11-30 18:57:40 +00:00
|
|
|
addcodef("_con.%s(%q,%s)", LABEL_FUNCNAME[labeltype], identifier, table.concat(args, ","))
|
2012-11-25 13:18:46 +00:00
|
|
|
|
|
|
|
g_labeldef[identifier] = ""
|
|
|
|
g_labeltype[identifier] = labeltype
|
2012-06-17 19:45:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
local function parse(contents) end -- fwd-decl
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local function do_include_file(dirname, filename)
|
|
|
|
assert(type(filename)=="string")
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
if (g_have_file[filename] ~= nil) then
|
|
|
|
printf("[%d] Fatal error: infinite loop including \"%s\"", filename)
|
|
|
|
g_numerrors = inf
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local contents
|
|
|
|
|
|
|
|
if (read_into_string) then
|
|
|
|
-- running from EDuke32
|
|
|
|
contents = read_into_string(filename)
|
|
|
|
else
|
|
|
|
-- running stand-alone
|
2012-06-10 18:56:21 +00:00
|
|
|
local io = require("io")
|
|
|
|
|
|
|
|
local fd, msg = io.open(dirname..filename)
|
|
|
|
if (fd == nil) then
|
|
|
|
-- strip up to and including first slash:
|
|
|
|
filename = string.gsub(filename, "^.-/", "")
|
|
|
|
fd, msg = io.open(dirname..filename)
|
|
|
|
|
|
|
|
if (fd == nil) then
|
2012-06-17 19:45:43 +00:00
|
|
|
printf("[%d] Fatal error: couldn't open %s", g_recurslevel, msg)
|
2012-06-10 18:56:21 +00:00
|
|
|
g_numerrors = inf
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
contents = fd:read("*all")
|
2012-06-10 18:56:21 +00:00
|
|
|
fd:close()
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
if (contents == nil) then
|
|
|
|
-- maybe that file name turned out to be a directory or other
|
|
|
|
-- special file accidentally
|
|
|
|
printf("[%d] Fatal error: couldn't read from \"%s\"",
|
|
|
|
g_recurslevel, dirname..filename)
|
|
|
|
g_numerrors = inf
|
|
|
|
return
|
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
printf("%s[%d] Translating file \"%s\"", (g_recurslevel==-1 and "\n---- ") or "",
|
|
|
|
g_recurslevel+1, dirname..filename);
|
2012-06-17 19:45:43 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local oldfilename = g_filename
|
|
|
|
g_filename = filename
|
|
|
|
parse(contents)
|
|
|
|
g_filename = oldfilename
|
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
-- Table of various outer command handling functions.
|
|
|
|
local Cmd = {}
|
|
|
|
|
|
|
|
function Cmd.include(filename)
|
2012-12-29 15:21:24 +00:00
|
|
|
do_include_file(g_directory, filename)
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
|
|
|
|
2012-06-17 19:45:37 +00:00
|
|
|
--- Per-module game data
|
|
|
|
local g_data = {}
|
2012-11-30 18:57:40 +00:00
|
|
|
local EPMUL = conl.MAXLEVELS
|
2012-06-17 19:45:37 +00:00
|
|
|
|
|
|
|
local function reset_gamedata()
|
|
|
|
g_data = {}
|
|
|
|
|
|
|
|
-- [EPMUL*ep + lev] = { ptime=<num>, dtime=<num>, fn=<str>, name=<str> }
|
2012-06-17 19:45:41 +00:00
|
|
|
g_data.level = {}
|
|
|
|
-- [ep] = <str>
|
|
|
|
g_data.volname = {}
|
|
|
|
-- [skillnum] = <str>
|
|
|
|
g_data.skillname = {}
|
|
|
|
-- [quotenum] = <str>
|
|
|
|
g_data.quote = {}
|
|
|
|
-- table of length 26 or 30 containg numbers
|
|
|
|
g_data.startup = {}
|
|
|
|
-- [soundnum] = { fn=<str>, params=<table of length 5> }
|
|
|
|
g_data.sound = {}
|
|
|
|
-- [volnum] = <table of length numlevels (<= MAXLEVELS) of <str>>
|
|
|
|
g_data.music = {}
|
2012-06-17 19:45:37 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.definelevelname(vol, lev, fn, ptstr, dtstr, levname)
|
2012-11-30 18:57:40 +00:00
|
|
|
if (vol < 0 or vol >= conl.MAXVOLUMES) then
|
2012-06-17 19:45:37 +00:00
|
|
|
errprintf("volume number exceeds maximum volume count.")
|
2012-06-17 19:45:41 +00:00
|
|
|
return
|
2012-06-17 19:45:37 +00:00
|
|
|
end
|
|
|
|
|
2012-11-30 18:57:40 +00:00
|
|
|
if (lev < 0 or lev >= conl.MAXLEVELS) then
|
2012-06-17 19:45:37 +00:00
|
|
|
errprintf("level number exceeds maximum number of levels per episode.")
|
2012-06-17 19:45:41 +00:00
|
|
|
return
|
2012-06-17 19:45:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: Bcorrectfilename(fn)
|
|
|
|
|
|
|
|
local function secs(tstr)
|
|
|
|
local m, s = string.match(tstr, ".+:.+")
|
|
|
|
m, s = tonumber(m), tonumber(s)
|
|
|
|
return (m and s) and m*60+s or 0
|
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:19 +00:00
|
|
|
local map = {
|
|
|
|
ptime=secs(ptstr), dtime=secs(dtstr), fn="/"..fn, name=levname
|
2012-06-17 19:45:37 +00:00
|
|
|
}
|
2013-01-05 13:09:19 +00:00
|
|
|
|
|
|
|
if (ffi) then
|
|
|
|
ffiC.C_DefineLevelName(vol, lev, map.fn, map.ptime, map.dtime, map.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
g_data.level[EPMUL*vol+lev] = map
|
2012-06-17 19:45:37 +00:00
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
local function defineXname(what, ffiCfuncname, X, name)
|
|
|
|
name = name:upper()
|
|
|
|
if (ffi) then
|
|
|
|
ffiC[ffiCfuncname](X, name)
|
|
|
|
if (#name > 32) then
|
|
|
|
warnprintf("%s %d name truncated to 32 characters.", what, X)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return name
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.defineskillname(skillnum, name)
|
2012-11-30 18:57:40 +00:00
|
|
|
if (skillnum < 0 or skillnum >= conl.MAXSKILLS) then
|
2013-01-05 13:09:31 +00:00
|
|
|
errprintf("skill number is negative or exceeds maximum skill count.")
|
2012-06-17 19:45:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
name = defineXname("skill", "C_DefineSkillName", skillnum, name)
|
|
|
|
g_data.skillname[skillnum] = name
|
2012-06-17 19:45:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.definevolumename(vol, name)
|
2012-11-30 18:57:40 +00:00
|
|
|
if (vol < 0 or vol >= conl.MAXVOLUMES) then
|
2012-06-17 19:45:41 +00:00
|
|
|
errprintf("volume number is negative or exceeds maximum volume count.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:31 +00:00
|
|
|
name = defineXname("volume", "C_DefineVolumeName", vol, name)
|
2013-01-05 13:09:19 +00:00
|
|
|
g_data.volname[vol] = name
|
|
|
|
end
|
|
|
|
|
|
|
|
-- strip whitespace from front and back
|
|
|
|
local function stripws(str)
|
|
|
|
return str:match("^%s*(.*)%s*$")
|
2012-06-17 19:45:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.definequote(qnum, quotestr)
|
2013-01-01 15:24:28 +00:00
|
|
|
--[[
|
2012-06-17 19:45:41 +00:00
|
|
|
-- have the INT_MAX limit simply for some sanity
|
2013-01-01 15:24:28 +00:00
|
|
|
if (not (qnum >= 0 and <= 0x7fffffff)) then
|
2012-06-17 19:45:41 +00:00
|
|
|
errprintf("quote number is negative or exceeds limit of INT32_MAX.")
|
|
|
|
return
|
|
|
|
end
|
2013-01-01 15:24:28 +00:00
|
|
|
--]]
|
|
|
|
if (not (qnum >= 0 and qnum < conl.MAXQUOTES)) then
|
|
|
|
errprintf("quote number is negative or exceeds limit of %d.", conl.MAXQUOTES-1)
|
|
|
|
return
|
|
|
|
end
|
2012-06-17 19:45:41 +00:00
|
|
|
|
2013-01-05 13:09:19 +00:00
|
|
|
quotestr = stripws(quotestr)
|
2013-01-01 15:24:28 +00:00
|
|
|
|
|
|
|
if (ffi) then
|
|
|
|
if (#quotestr >= conl.MAXQUOTELEN) then
|
|
|
|
warnprintf("quote %d truncated to %d characters.", conl.MAXQUOTELEN-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
ffiC.C_DefineQuote(qnum, quotestr)
|
|
|
|
end
|
|
|
|
|
|
|
|
g_data.quote[qnum] = quotestr
|
2012-06-17 19:45:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.gamestartup(...)
|
2012-12-29 15:21:24 +00:00
|
|
|
local args = {...}
|
2012-06-17 19:45:41 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
if (#args ~= 26 and #args ~= 30) then
|
2012-06-17 19:45:41 +00:00
|
|
|
errprintf("must pass either 26 (1.3D) or 30 (1.5) values")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-01-01 15:24:22 +00:00
|
|
|
if (ffi) then
|
2012-12-29 15:21:24 +00:00
|
|
|
-- running from EDuke32
|
2013-01-01 15:24:22 +00:00
|
|
|
if (#args == 30) then
|
2012-12-29 15:21:24 +00:00
|
|
|
ffiC.g_scriptVersion = 14
|
|
|
|
end
|
|
|
|
local params = ffi.new("int32_t [30]", args)
|
|
|
|
ffiC.G_DoGameStartup(params)
|
|
|
|
end
|
|
|
|
|
|
|
|
g_data.startup = args -- TODO: sanity-check them
|
2012-06-17 19:45:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.definesound(sndnum, fn, ...)
|
2013-01-01 15:24:28 +00:00
|
|
|
if (not (sndnum >= 0 and sndnum < conl.MAXSOUNDS)) then
|
2013-01-05 13:09:19 +00:00
|
|
|
errprintf("sound number is negative or exceeds sound limit of %d", conl.MAXSOUNDS-1)
|
2012-06-17 19:45:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local params = {...} -- TODO: sanity-check them
|
2013-01-01 15:24:22 +00:00
|
|
|
if (ffi) then
|
|
|
|
local cparams = ffi.new("int32_t [5]", params)
|
|
|
|
assert(type(fn)=="string")
|
|
|
|
ffiC.C_DefineSound(sndnum, fn, cparams)
|
|
|
|
end
|
2012-06-17 19:45:41 +00:00
|
|
|
|
|
|
|
g_data.sound[sndnum] = { fn=fn, params=params }
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.music(volnum, ...)
|
2013-01-05 13:09:19 +00:00
|
|
|
if (not (volnum >= 0 and volnum < conl.MAXVOLUMES+1)) then
|
|
|
|
-- NOTE: MAXVOLUMES is OK, since it's MapInfo[(MAXVOLUMES+1)*MAXLEVELS]
|
|
|
|
errprintf("volume number is negative or exceeds MAXVOLUMES=%d", conl.MAXVOLUMES)
|
2012-06-17 19:45:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local filenames = {...}
|
|
|
|
|
2012-11-30 18:57:40 +00:00
|
|
|
if (#filenames > conl.MAXLEVELS) then
|
|
|
|
warnprintf("ignoring extraneous %d music file names", #filenames-conl.MAXLEVELS)
|
|
|
|
for i=conl.MAXLEVELS+1,#filenames do
|
2012-06-17 19:45:41 +00:00
|
|
|
filenames[i] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-05 13:09:19 +00:00
|
|
|
if (ffi) then
|
|
|
|
for i=1,#filenames do
|
|
|
|
assert(type(filenames[i])=="string")
|
|
|
|
ffiC.C_DefineMusic(volnum, i-1, "/"..filenames[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-17 19:45:41 +00:00
|
|
|
g_data.music[volnum] = filenames
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
--- GAMEVARS / GAMEARRAYS
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
function Cmd.gamevar(identifier, initval, flags)
|
2013-01-13 16:40:21 +00:00
|
|
|
local invalid_code = "local _INVALIDGV"
|
|
|
|
|
|
|
|
if (bit.band(flags, bit.bnot(GVFLAG.PERX_MASK)) ~= 0) then
|
|
|
|
-- TODO: a couple of the presumably safe ones
|
|
|
|
errprintf("gamevar flags other than PERPLAYER or PERACTOR: NYI or forbidden")
|
|
|
|
return invalid_code
|
|
|
|
end
|
|
|
|
|
|
|
|
if (flags==GVFLAG.PERPLAYER+GVFLAG.PERACTOR) then
|
|
|
|
errprintf("invalid gamevar flags: must be either PERPLAYER or PERACTOR, not both")
|
|
|
|
return invalid_code
|
|
|
|
end
|
|
|
|
|
|
|
|
local ogv = g_gamevar[identifier]
|
|
|
|
|
|
|
|
if (ogv ~= nil) then
|
|
|
|
if (ogv.flags ~= flags) then
|
|
|
|
errprintf("duplicate gamevar definition `%s' has different flags", identifier)
|
|
|
|
return invalid_code
|
|
|
|
else
|
|
|
|
warnprintf("duplicate gamevar definition `%s' ignored", identifier)
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local ltype = g_labeltype[identifier]
|
|
|
|
if (ltype ~= nil) then
|
|
|
|
warnprintf("symbol `%s' already used for a defined %s", identifier, LABEL[ltype])
|
|
|
|
end
|
|
|
|
|
|
|
|
local gv = { name=mangle_name(identifier, "V"), flags=flags }
|
|
|
|
g_gamevar[identifier] = gv
|
|
|
|
|
|
|
|
-- TODO: Write gamevar system on the Lunatic side and hook it up.
|
|
|
|
-- TODO: per-player gamevars
|
|
|
|
if (flags==GVFLAG.PERACTOR) then
|
2013-01-13 16:40:32 +00:00
|
|
|
addcodef("local %s=_con.peractorvar(%d)", gv.name, initval)
|
2013-01-13 16:40:21 +00:00
|
|
|
else
|
2013-01-13 16:40:32 +00:00
|
|
|
addcodef("local %s=%d", gv.name, initval)
|
2013-01-13 16:40:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
local function lookup_gamevar(identifier, writable)
|
2013-01-13 16:40:21 +00:00
|
|
|
local gv = g_gamevar[identifier]
|
|
|
|
|
|
|
|
if (gv == nil) then
|
|
|
|
errprintf("symbol `%s' is not a game variable", identifier)
|
|
|
|
return "_INVALIDGV"
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
if (writable and bit.band(gv.flags, GVFLAG.READONLY) ~= 0) then
|
|
|
|
errprintf("variable `%s' is read-only", identifier)
|
|
|
|
return "_READONLYGV"
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
if (gv.flags==GVFLAG.PERACTOR) then
|
|
|
|
return format("%s[_aci]", gv.name)
|
|
|
|
else
|
|
|
|
return gv.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function maybe_gamevar_Cmt(subj, pos, identifier)
|
|
|
|
if (g_gamevar[identifier]) then
|
|
|
|
return true, lookup_gamevar(identifier)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
----==== patterns ====----
|
|
|
|
|
|
|
|
---- basic ones
|
2012-04-29 20:54:06 +00:00
|
|
|
-- Windows, *nix and Mac newlines all exist in the wild!
|
|
|
|
local newline = "\r"*Pat("\n")^-1 + "\n"
|
|
|
|
local EOF = Pat(-1)
|
2012-04-07 20:10:03 +00:00
|
|
|
local anychar = Pat(1)
|
|
|
|
-- comments
|
|
|
|
local comment = "/*" * match_until(anychar, "*/") * "*/"
|
|
|
|
local linecomment = "//" * match_until(anychar, newline)
|
|
|
|
local whitespace = Var("whitespace")
|
|
|
|
local sp0 = whitespace^0
|
2012-04-29 20:54:06 +00:00
|
|
|
-- This "WS+" pattern matches EOF too, so that a forgotten newline at EOF is
|
|
|
|
-- properly handled
|
|
|
|
local sp1 = whitespace^1 + EOF
|
2012-04-07 20:10:03 +00:00
|
|
|
local alpha = Range("AZ", "az") -- locale?
|
|
|
|
local alphanum = alpha + Range("09")
|
2012-04-29 20:54:06 +00:00
|
|
|
--local alnumtok = alphanum + Set("{}/\\*-_.") -- see isaltok() in gamedef.c
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
--- Basic lexical elements ("tokens"). See the final grammar ("Grammar") for
|
|
|
|
--- their definitions.
|
2013-01-13 16:40:26 +00:00
|
|
|
local tok =
|
|
|
|
{
|
|
|
|
maybe_minus = (Pat("-") * sp0)^-1,
|
|
|
|
number = Var("t_number"),
|
|
|
|
|
|
|
|
-- Valid identifier names are disjunct from keywords!,
|
|
|
|
-- XXX: CON is more permissive with identifier name characters:,
|
|
|
|
identifier = Var("t_identifier"),
|
|
|
|
-- This one matches keywords, too:,
|
|
|
|
identifier_all = Var("t_identifier_all"),
|
|
|
|
define = Var("t_define"),
|
|
|
|
move = Var("t_move"),
|
|
|
|
ai = Var("t_ai"),
|
|
|
|
action = Var("t_action"),
|
|
|
|
|
|
|
|
-- NOTE: no chance to whitespace and double quotes in filenames:,
|
|
|
|
filename = lpeg.C((anychar-Set(" \t\r\n\""))^1),
|
|
|
|
newline_term_str = match_until(anychar, newline),
|
|
|
|
|
|
|
|
-- new-style inline arrays and structures:,
|
|
|
|
arrayexp = Var("t_arrayexp"),
|
|
|
|
|
|
|
|
rvar = Var("t_rvar"),
|
|
|
|
wvar = Var("t_wvar"),
|
|
|
|
|
|
|
|
-- for definelevelname,
|
|
|
|
time = lpeg.C(alphanum*alphanum^-1*":"*alphanum*alphanum^-1),
|
|
|
|
}
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
---- helper patterns / pattern constructing functions
|
2013-01-13 16:40:26 +00:00
|
|
|
local maybe_quoted_filename = ('"' * tok.filename * '"' + tok.filename)
|
2012-04-29 20:54:06 +00:00
|
|
|
-- empty string is handled too; we must not eat the newline then!
|
2012-06-10 18:56:21 +00:00
|
|
|
local newline_term_string = (#newline + EOF)*lpeg.Cc("")
|
2013-01-13 16:40:26 +00:00
|
|
|
+ (whitespace-newline)^1 * lpeg.C(tok.newline_term_str)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
-- (sp1 * tok.define) repeated exactly n times
|
2012-04-07 20:10:03 +00:00
|
|
|
local function n_defines(n) -- works well only for small n
|
|
|
|
local pat = Pat(true)
|
|
|
|
for i=1,n do
|
2013-01-13 16:40:26 +00:00
|
|
|
pat = sp1 * tok.define * pat
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
|
|
|
return pat
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
local D, R, W, I, AC, MV, AI = -1, -2, -3, -4, -5, -6, -7
|
2013-01-13 16:40:26 +00:00
|
|
|
local TOKEN_PATTERN = { [D]=tok.define, [R]=tok.rvar, [W]=tok.wvar, [I]=tok.identifier,
|
|
|
|
[AC]=tok.action, [MV]=tok.move, [AI]=tok.ai }
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
-- Generic command pattern, types given by varargs.
|
|
|
|
-- The command name to be matched is attached later.
|
|
|
|
-- Example:
|
|
|
|
-- "command" writtenvar readvar def def: gencmd(W,R,D,D)
|
2013-01-13 16:40:26 +00:00
|
|
|
-- --> sp1 * tok.wvar * sp1 * tok.rvar * sp1 * tok.define * sp1 * tok.define
|
2012-04-07 20:10:03 +00:00
|
|
|
-- "command_with_no_args": gencmd()
|
|
|
|
-- --> Pat(true)
|
|
|
|
local function cmd(...)
|
|
|
|
local pat = Pat(true)
|
|
|
|
local vartypes = {...}
|
|
|
|
|
|
|
|
for i=1,#vartypes do
|
2012-06-17 19:45:43 +00:00
|
|
|
pat = pat * sp1 * assert(TOKEN_PATTERN[vartypes[i]])
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return pat
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- The command names will be attached to the front of the patterns later!
|
|
|
|
|
|
|
|
--== Top level CON commands ==--
|
2012-04-29 20:54:06 +00:00
|
|
|
-- XXX: many of these are also allowed inside actors/states/events in CON.
|
2013-01-13 16:40:26 +00:00
|
|
|
local Couter = {
|
2012-04-07 20:10:03 +00:00
|
|
|
--- 1. Preprocessor
|
2013-01-13 16:40:26 +00:00
|
|
|
include = sp1 * maybe_quoted_filename / Cmd.include,
|
2012-04-07 20:10:03 +00:00
|
|
|
includedefault = cmd(),
|
2012-06-10 18:56:21 +00:00
|
|
|
define = cmd(I,D) / do_define_label,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
--- 2. Defines and Meta-Settings
|
|
|
|
dynamicremap = cmd(),
|
2013-01-13 16:40:26 +00:00
|
|
|
setcfgname = sp1 * tok.filename,
|
|
|
|
setdefname = sp1 * tok.filename,
|
2012-04-07 20:10:03 +00:00
|
|
|
setgamename = newline_term_string,
|
|
|
|
|
|
|
|
precache = cmd(D,D,D),
|
2012-12-09 13:24:39 +00:00
|
|
|
scriptsize = cmd(D)
|
|
|
|
/ "", -- no-op
|
2012-04-07 20:10:03 +00:00
|
|
|
cheatkeys = cmd(D,D),
|
|
|
|
|
|
|
|
definecheat = newline_term_string, -- XXX: actually tricker syntax (TS)
|
|
|
|
definegamefuncname = newline_term_string, -- XXX: TS?
|
|
|
|
definegametype = n_defines(2) * newline_term_string,
|
2013-01-13 16:40:26 +00:00
|
|
|
definelevelname = n_defines(2) * sp1 * tok.filename * sp1 * tok.time * sp1 * tok.time *
|
|
|
|
newline_term_string / Cmd.definelevelname,
|
|
|
|
defineskillname = sp1 * tok.define * newline_term_string / Cmd.defineskillname,
|
|
|
|
definevolumename = sp1 * tok.define * newline_term_string / Cmd.definevolumename,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
definequote = sp1 * tok.define * newline_term_string / Cmd.definequote,
|
2012-04-07 20:10:03 +00:00
|
|
|
defineprojectile = cmd(D,D,D),
|
2013-01-13 16:40:26 +00:00
|
|
|
definesound = sp1 * tok.define * sp1 * maybe_quoted_filename * n_defines(5) / Cmd.definesound,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-06-17 19:45:37 +00:00
|
|
|
-- NOTE: gamevar.ogg and the like is OK, too
|
2013-01-13 16:40:26 +00:00
|
|
|
music = sp1 * tok.define * match_until(sp1 * tok.filename, sp1 * conl.keyword * sp1) / Cmd.music,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
--- 3. Game Settings
|
2012-12-03 18:24:30 +00:00
|
|
|
-- gamestartup has 26/30 fixed defines, depending on 1.3D/1.5 version:
|
2013-01-13 16:40:26 +00:00
|
|
|
gamestartup = (sp1 * tok.define)^26 / Cmd.gamestartup,
|
2012-04-07 20:10:03 +00:00
|
|
|
spritenopal = cmd(D),
|
|
|
|
spritenoshade = cmd(D),
|
|
|
|
spritenvg = cmd(D),
|
|
|
|
spriteshadow = cmd(D),
|
|
|
|
|
|
|
|
spriteflags = cmd(D,D), -- also see inner
|
|
|
|
|
|
|
|
--- 4. Game Variables / Arrays
|
2013-01-13 16:40:26 +00:00
|
|
|
gamevar = cmd(I,D,D) / Cmd.gamevar,
|
2012-04-07 20:10:03 +00:00
|
|
|
gamearray = cmd(I,D),
|
|
|
|
|
|
|
|
--- 5. Top level commands that are also run-time commands
|
2013-01-13 16:40:26 +00:00
|
|
|
move = sp1 * tok.identifier * (sp1 * tok.define)^-2 / -- hvel, vvel
|
2012-11-25 13:18:46 +00:00
|
|
|
function(...) do_define_composite(LABEL.MOVE, ...) end,
|
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
-- startframe, numframes, viewtype, incval, delay:
|
2013-01-13 16:40:26 +00:00
|
|
|
action = sp1 * tok.identifier * (sp1 * tok.define)^-5 /
|
2012-06-17 19:45:43 +00:00
|
|
|
function(...) do_define_composite(LABEL.ACTION, ...) end,
|
|
|
|
|
|
|
|
-- action, move, flags...:
|
2013-01-13 16:40:26 +00:00
|
|
|
ai = sp1 * tok.identifier * (sp1 * tok.action *
|
|
|
|
(sp1 * tok.move * (sp1 * tok.define)^0)^-1
|
2012-06-17 19:45:43 +00:00
|
|
|
)^-1 /
|
|
|
|
function(...) do_define_composite(LABEL.AI, ...) end,
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
--- 6. Deprecated TLCs
|
|
|
|
betaname = newline_term_string,
|
|
|
|
enhanced = cmd(D),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
--== Run time CON commands ==--
|
|
|
|
--- 1. Gamevar Operators
|
|
|
|
local varop = cmd(W,D)
|
|
|
|
local varvarop = cmd(W,R)
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
local function varopf(op)
|
|
|
|
if (#op == 1) then
|
|
|
|
return varop / ("%1=%1"..op.."%2")
|
|
|
|
else
|
|
|
|
return varop / ("%1="..op.."(%1,%2)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function varvaropf(op)
|
|
|
|
if (#op == 1) then
|
|
|
|
return varvarop / ("%1=%1"..op.."%2")
|
|
|
|
else
|
|
|
|
return varvarop / ("%1="..op.."(%1,%2)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-29 20:54:06 +00:00
|
|
|
-- Allow nesting... stuff like
|
|
|
|
-- ifvarl actorvar[sprite[THISACTOR].owner].burning 0
|
|
|
|
-- is kinda breaking the classic "no array nesting" rules
|
2012-12-28 17:18:02 +00:00
|
|
|
-- (if there ever were any) but making our life harder else.
|
2013-01-13 16:40:26 +00:00
|
|
|
local arraypat = sp0 * "[" * sp0 * tok.rvar * sp0 * "]"
|
2012-04-29 20:54:06 +00:00
|
|
|
|
|
|
|
-- Have to bite the bullet here and list actor/player members with second parameters,
|
|
|
|
-- even though it's ugly to make it part of the syntax. Also, stuff like
|
|
|
|
-- actor[xxx].loogiex parm2 x
|
|
|
|
-- will be wrongly accepted at the parsing stage because we don't discriminate between
|
|
|
|
-- actor and player (but it will be rejected later).
|
|
|
|
local parm2memberpat = (Pat("htg_t") + "loogiex" + "loogiey" + "ammo_amount" +
|
2013-01-13 16:40:26 +00:00
|
|
|
"weaprecs" + "gotweapon" + "pals" + "max_ammo_amount") * sp1 * tok.rvar
|
2012-04-07 20:10:03 +00:00
|
|
|
-- The member name must match keywords, too (_all), because e.g. cstat is a member
|
2012-04-29 20:54:06 +00:00
|
|
|
-- of sprite[].
|
2013-01-13 16:40:26 +00:00
|
|
|
local memberpat = sp0 * "." * sp0 * (parm2memberpat + tok.identifier_all)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
local getstructcmd = -- get<structname>[<idx>].<member> (<parm2>)? <<var>>
|
|
|
|
-- existence of a second parameter is determined later
|
|
|
|
-- This is wrong, (sp1 id)? will match (sp1 wvar) if there's no 2nd param:
|
2013-01-13 16:40:26 +00:00
|
|
|
-- arraypat * memberpat * (sp1 * tok.identifier)^-1 * sp1 * tok.wvar
|
|
|
|
arraypat * memberpat * sp1 * (tok.rvar * sp1 * tok.wvar + tok.wvar)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
local setstructcmd = -- set<structname>[<idx>].<member> (<parm2>)? <var>
|
|
|
|
-- existence of a second parameter is determined later
|
2013-01-13 16:40:26 +00:00
|
|
|
arraypat * memberpat * sp1 * (tok.rvar * sp1 * tok.rvar + tok.rvar)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
local getperxvarcmd = -- get<actor/player>var[<idx>].<member> <<var>>
|
2013-01-13 16:40:26 +00:00
|
|
|
arraypat * memberpat * sp1 * tok.wvar
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
local setperxvarcmd = -- set<actor/player>var[<idx>].<member> <var>
|
2013-01-13 16:40:26 +00:00
|
|
|
arraypat * memberpat * sp1 * tok.rvar
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
-- Various inner command handling functions.
|
|
|
|
local handle =
|
|
|
|
{
|
|
|
|
palfrom = function(...)
|
|
|
|
local v = {...}
|
|
|
|
return format(PLS":_palfrom(%d,%d,%d,%d)",
|
|
|
|
v[1] or 0, v[2] or 0, v[3] or 0, v[4] or 0)
|
|
|
|
end,
|
|
|
|
|
|
|
|
move = function(mv, ...)
|
|
|
|
local flags = {...}
|
|
|
|
return format(ACS":set_move(%s,%d)", mv, (flags[1] and bit.bor(...)) or 0)
|
|
|
|
end,
|
|
|
|
|
|
|
|
debug = function(val)
|
|
|
|
return format("print('%s:%d: debug %d')", g_filename, getlinecol(g_lastkwpos), val)
|
|
|
|
end,
|
|
|
|
|
|
|
|
state = function(statename)
|
|
|
|
if (g_funcname[statename]==nil) then
|
|
|
|
errprintf("state `%s' not found.", statename)
|
|
|
|
return "_NULLSTATE()"
|
|
|
|
end
|
|
|
|
return format("%s(_aci,_pli,_dist)", g_funcname[statename])
|
|
|
|
end,
|
|
|
|
}
|
2013-01-05 13:09:31 +00:00
|
|
|
|
2012-06-17 19:45:37 +00:00
|
|
|
-- NOTE about prefixes: most is handled by all_alt_pattern(), however commands
|
|
|
|
-- that have no arguments and that are prefixes of other commands MUST be
|
|
|
|
-- suffixed with a "* #sp1" pattern.
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
local Cinner = {
|
2012-04-07 20:10:03 +00:00
|
|
|
-- these can appear anywhere in the script
|
2012-11-30 18:57:40 +00:00
|
|
|
["break"] = cmd()
|
2012-12-23 19:24:25 +00:00
|
|
|
/ "do return end",
|
2012-12-03 18:24:25 +00:00
|
|
|
["return"] = cmd() -- NLCF
|
2013-01-05 13:09:31 +00:00
|
|
|
/ "_con.longjmp()",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-11-30 18:57:40 +00:00
|
|
|
state = cmd(I)
|
2013-01-13 16:40:26 +00:00
|
|
|
/ handle.state,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
--- 1. get*, set*
|
|
|
|
getactor = getstructcmd,
|
|
|
|
getinput = getstructcmd,
|
|
|
|
getplayer = getstructcmd,
|
|
|
|
getprojectile = getstructcmd,
|
|
|
|
getsector = getstructcmd,
|
|
|
|
getthisprojectile = getstructcmd,
|
|
|
|
gettspr = getstructcmd,
|
2012-04-29 20:54:06 +00:00
|
|
|
-- NOTE: {get,set}userdef is the only struct that can be accessed without
|
2012-06-03 16:11:22 +00:00
|
|
|
-- an "array part", e.g. H266MOD has "setuserdef .weaponswitch 0" (space
|
2012-04-29 20:54:06 +00:00
|
|
|
-- between keyword and "." is mandatory)
|
2013-01-13 16:40:26 +00:00
|
|
|
getuserdef = (arraypat + sp1) * memberpat * sp1 * (tok.rvar * sp1 * tok.wvar + tok.wvar),
|
2012-04-29 20:54:06 +00:00
|
|
|
-- getuserdef = getstructcmd,
|
2012-04-07 20:10:03 +00:00
|
|
|
getwall = getstructcmd,
|
|
|
|
|
|
|
|
getactorvar = getperxvarcmd,
|
|
|
|
getplayervar = getperxvarcmd,
|
|
|
|
|
|
|
|
setactor = setstructcmd,
|
|
|
|
setinput = setstructcmd,
|
|
|
|
setplayer = setstructcmd,
|
|
|
|
setprojectile = setstructcmd,
|
|
|
|
setsector = setstructcmd,
|
|
|
|
setthisprojectile = setstructcmd,
|
|
|
|
settspr = setstructcmd,
|
2013-01-13 16:40:26 +00:00
|
|
|
setuserdef = (arraypat + sp1) * memberpat * sp1 * (tok.rvar * sp1 * tok.wvar + tok.rvar),
|
2012-04-29 20:54:06 +00:00
|
|
|
-- setuserdef = setstructcmd,
|
2012-04-07 20:10:03 +00:00
|
|
|
setwall = setstructcmd,
|
|
|
|
|
|
|
|
setactorvar = setperxvarcmd,
|
|
|
|
setplayervar = setperxvarcmd,
|
|
|
|
|
2012-04-22 19:10:46 +00:00
|
|
|
setsprite = cmd(R,R,R,R),
|
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
setvarvar = varvarop / "%1=%2",
|
|
|
|
addvarvar = varvaropf "+",
|
|
|
|
subvarvar = varvaropf "-",
|
|
|
|
mulvarvar = varvaropf "*",
|
|
|
|
divvarvar = varvaropf "_con._div",
|
|
|
|
modvarvar = varvaropf "_con._mod",
|
|
|
|
andvarvar = varvaropf "_bit.band",
|
|
|
|
orvarvar = varvaropf "_bit.bor",
|
|
|
|
xorvarvar = varvaropf "_bit.bxor",
|
|
|
|
randvarvar = varvarop / "%1=_con._rand(%2)",
|
|
|
|
|
|
|
|
setvar = varop / "%1=%2",
|
|
|
|
addvar = varopf "+",
|
|
|
|
subvar = varopf "-",
|
|
|
|
mulvar = varopf "*",
|
|
|
|
divvar = varopf "_con._div",
|
|
|
|
modvar = varopf "_con._mod",
|
|
|
|
andvar = varopf "_bit.band",
|
|
|
|
orvar = varopf "_bit.bor",
|
|
|
|
xorvar = varopf "_bit.bxor",
|
|
|
|
randvar = varop / "%1=_con._rand(%2)",
|
|
|
|
shiftvarl = varopf "_bit.lshift",
|
|
|
|
shiftvarr = varopf "_bit.rshift",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
--- 2. Math operations
|
|
|
|
sqrt = cmd(R,W),
|
|
|
|
calchypotenuse = cmd(W,R,R),
|
|
|
|
sin = cmd(W,R),
|
|
|
|
cos = cmd(W,R),
|
2012-04-22 19:10:46 +00:00
|
|
|
mulscale = cmd(W,R,R,R),
|
2012-04-07 20:10:03 +00:00
|
|
|
getangle = cmd(W,R,R),
|
|
|
|
getincangle = cmd(W,R,R),
|
|
|
|
|
|
|
|
--- 3. Actors
|
2012-11-30 18:57:46 +00:00
|
|
|
action = cmd(AC)
|
|
|
|
/ ACS":set_action(%1)",
|
|
|
|
ai = cmd(AI)
|
|
|
|
/ ACS":set_ai(%1)",
|
2013-01-13 16:40:26 +00:00
|
|
|
move = sp1 * tok.move * (sp1 * tok.define)^0
|
|
|
|
/ handle.move,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
cactor = cmd(D)
|
2012-11-30 18:57:50 +00:00
|
|
|
/ SPS":set_picnum(%1)",
|
2012-11-30 18:57:46 +00:00
|
|
|
count = cmd(D)
|
|
|
|
/ ACS":set_count(%1)",
|
2012-12-03 18:24:25 +00:00
|
|
|
cstator = cmd(D)
|
|
|
|
/ (SPS".cstat=_bit.bor(%1,"..SPS".cstat)"),
|
|
|
|
cstat = cmd(D)
|
|
|
|
/ SPS".cstat=%1",
|
2012-12-03 18:24:30 +00:00
|
|
|
clipdist = cmd(D)
|
|
|
|
/ SPS".clipdist=%1",
|
2012-12-03 18:24:25 +00:00
|
|
|
sizeto = cmd(D,D)
|
2012-12-29 15:21:32 +00:00
|
|
|
/ "_con._sizeto(_aci,%1,%2)", -- TODO: see control.lua:_sizeto
|
2012-11-30 18:57:46 +00:00
|
|
|
sizeat = cmd(D,D)
|
|
|
|
/ (SPS".xrepeat,"..SPS".yrepeat=%1,%2"),
|
|
|
|
strength = cmd(D)
|
|
|
|
/ SPS".extra=%1",
|
|
|
|
addstrength = cmd(D)
|
|
|
|
/ (SPS".extra="..SPS".extra+%1"),
|
2013-01-05 13:09:31 +00:00
|
|
|
spritepal = cmd(D)
|
|
|
|
/ SPS".pal=%1",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-12-03 18:24:30 +00:00
|
|
|
hitradius = cmd(D,D,D,D,D)
|
2013-01-01 15:24:22 +00:00
|
|
|
/ "_con._A_RadiusDamage(_aci,%1,%2,%3,%4,%5)",
|
2012-04-07 20:10:03 +00:00
|
|
|
hitradiusvar = cmd(R,R,R,R,R),
|
|
|
|
|
|
|
|
-- some commands taking read vars
|
|
|
|
eshootvar = cmd(R),
|
|
|
|
espawnvar = cmd(R),
|
|
|
|
qspawnvar = cmd(R),
|
|
|
|
eqspawnvar = cmd(R),
|
|
|
|
operaterespawns = cmd(R),
|
|
|
|
operatemasterswitches = cmd(R),
|
|
|
|
checkactivatormotion = cmd(R),
|
|
|
|
time = cmd(R), -- no-op
|
|
|
|
inittimer = cmd(R),
|
|
|
|
lockplayer = cmd(R),
|
|
|
|
shootvar = cmd(R),
|
|
|
|
quake = cmd(R),
|
|
|
|
jump = cmd(R),
|
|
|
|
cmenu = cmd(R),
|
|
|
|
soundvar = cmd(R),
|
|
|
|
globalsoundvar = cmd(R),
|
|
|
|
stopsoundvar = cmd(R),
|
|
|
|
soundoncevar = cmd(R),
|
|
|
|
angoffvar = cmd(R),
|
|
|
|
checkavailweapon = cmd(R),
|
|
|
|
checkavailinven = cmd(R),
|
|
|
|
guniqhudid = cmd(R),
|
|
|
|
savegamevar = cmd(R),
|
|
|
|
readgamevar = cmd(R),
|
|
|
|
userquote = cmd(R),
|
2013-01-05 13:09:31 +00:00
|
|
|
echo = cmd(R) / "_con._echo(%1)",
|
2012-04-07 20:10:03 +00:00
|
|
|
starttrackvar = cmd(R),
|
|
|
|
clearmapstate = cmd(R),
|
|
|
|
activatecheat = cmd(R),
|
|
|
|
setgamepalette = cmd(R),
|
|
|
|
|
|
|
|
-- some commands taking defines
|
2012-12-03 18:24:25 +00:00
|
|
|
addammo = cmd(D,D) -- NLCF
|
|
|
|
/ format("if (%s) then _con.longjmp() end", PLS":addammo(%1,%2)"),
|
|
|
|
addweapon = cmd(D,D) -- NLCF
|
|
|
|
/ format("if (%s) then _con.longjmp() end", PLS":addweapon(%1,%2)"),
|
|
|
|
debris = cmd(D,D)
|
2012-12-28 17:18:02 +00:00
|
|
|
/ "_con._debris(_aci, %1, %2)",
|
2012-12-03 18:24:25 +00:00
|
|
|
addinventory = cmd(D,D)
|
|
|
|
/ PLS":addinventory(%1,%2)",
|
2012-12-03 18:24:30 +00:00
|
|
|
guts = cmd(D,D)
|
2012-12-28 17:18:02 +00:00
|
|
|
/ "_con._A_DoGuts(_aci,%1,%2)",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
-- cont'd
|
2012-11-30 18:57:46 +00:00
|
|
|
addkills = cmd(D)
|
|
|
|
/ (PLS".actors_killed="..PLS".actors_killed+%1;"..ACS".actorstayput=-1"),
|
2012-12-03 18:24:25 +00:00
|
|
|
addphealth = cmd(D)
|
2013-01-06 18:56:45 +00:00
|
|
|
/ format("_con._addphealth(%s,_aci,%%1)", PLS""),
|
2012-12-09 13:24:39 +00:00
|
|
|
angoff = cmd(D)
|
|
|
|
/ "spritext[_aci].angoff=%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
debug = cmd(D)
|
2013-01-13 16:40:26 +00:00
|
|
|
/ handle.debug,
|
2012-12-03 18:24:30 +00:00
|
|
|
endofgame = cmd(D)
|
|
|
|
/ "_con._endofgame(_pli,%1)",
|
2012-04-07 20:10:03 +00:00
|
|
|
eqspawn = cmd(D),
|
|
|
|
espawn = cmd(D),
|
2012-12-03 18:24:25 +00:00
|
|
|
globalsound = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ "_con._globalsound(_pli,%1)",
|
2012-12-28 17:18:02 +00:00
|
|
|
lotsofglass = cmd(D)
|
|
|
|
/ "_con._A_SpawnGlass(_aci,%1)",
|
2012-12-03 18:24:30 +00:00
|
|
|
mail = cmd(D)
|
2012-12-28 17:18:02 +00:00
|
|
|
/ "_con._spawnmany(_aci,4410,%1)", -- TODO: dyntile
|
2012-12-03 18:24:30 +00:00
|
|
|
money = cmd(D)
|
2012-12-28 17:18:02 +00:00
|
|
|
/ "_con._spawnmany(_aci,1233,%1)", -- TODO: dyntile
|
2012-12-03 18:24:30 +00:00
|
|
|
paper = cmd(D)
|
2012-12-28 17:18:02 +00:00
|
|
|
/ "_con._spawnmany(_aci,4460,%1)", -- TODO: dyntile
|
|
|
|
qspawn = cmd(D)
|
|
|
|
/ "_con.spawn(_aci,%1,true)",
|
2012-12-03 18:24:30 +00:00
|
|
|
quote = cmd(D)
|
2013-01-01 15:24:28 +00:00
|
|
|
/ "_con._quote(_pli,%1)",
|
2012-04-07 20:10:03 +00:00
|
|
|
savenn = cmd(D),
|
|
|
|
save = cmd(D),
|
2012-12-03 18:24:30 +00:00
|
|
|
sleeptime = cmd(D)
|
|
|
|
/ ACS".timetosleep=%1",
|
2013-01-02 22:33:37 +00:00
|
|
|
soundonce = cmd(D)
|
|
|
|
/ "_con._soundonce(_aci,%1)",
|
2012-12-03 18:24:25 +00:00
|
|
|
sound = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ "_con._sound(_aci,%1)",
|
2012-12-28 17:18:02 +00:00
|
|
|
spawn = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ "_con.spawn(_aci,%1)",
|
2012-12-03 18:24:25 +00:00
|
|
|
stopsound = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ "_con._stopsound(_aci,%1)",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
eshoot = cmd(D),
|
|
|
|
ezshoot = cmd(R,D),
|
|
|
|
ezshootvar = cmd(R,R),
|
2012-12-03 18:24:25 +00:00
|
|
|
shoot = cmd(D)
|
2013-01-19 18:28:40 +00:00
|
|
|
/ "_con._A_Shoot(_aci,%1)",
|
2012-04-07 20:10:03 +00:00
|
|
|
zshoot = cmd(R,D),
|
|
|
|
zshootvar = cmd(R,R),
|
|
|
|
|
2012-12-03 18:24:25 +00:00
|
|
|
fall = cmd()
|
|
|
|
/ "_con._VM_FallSprite(_aci)",
|
2012-12-09 13:24:39 +00:00
|
|
|
flash = cmd()
|
2013-01-02 22:33:37 +00:00
|
|
|
/ format("_con._flash(%s,%s)", SPS"", PLS""),
|
2012-12-03 18:24:30 +00:00
|
|
|
getlastpal = cmd()
|
|
|
|
/ "_con._getlastpal(_aci)",
|
2012-12-28 17:18:02 +00:00
|
|
|
insertspriteq = cmd()
|
|
|
|
/ "_con._addtodelqueue(_aci)",
|
2012-12-03 18:24:25 +00:00
|
|
|
killit = cmd() -- NLCF
|
|
|
|
/ "_con.killit()",
|
2012-12-23 19:24:16 +00:00
|
|
|
mikesnd = cmd()
|
2013-01-02 22:33:37 +00:00
|
|
|
/ format("_con._soundonce(_aci,%s)", SPS".yvel"),
|
2012-12-03 18:24:30 +00:00
|
|
|
nullop = cmd()
|
|
|
|
/ "", -- NOTE: really generate no code
|
|
|
|
pkick = cmd()
|
2012-12-09 13:24:39 +00:00
|
|
|
/ format("_con._pkick(%s,%s)", PLS"", ACS""),
|
2012-12-03 18:24:25 +00:00
|
|
|
pstomp = cmd()
|
2013-01-13 16:40:21 +00:00
|
|
|
/ PLS":stomp(_aci)",
|
2012-11-30 18:57:46 +00:00
|
|
|
resetactioncount = cmd()
|
|
|
|
/ ACS":reset_acount()",
|
|
|
|
resetcount = cmd()
|
|
|
|
/ ACS":set_count(0)",
|
2012-12-03 18:24:25 +00:00
|
|
|
resetplayer = cmd() -- NLCF
|
|
|
|
/ "if (_con._VM_ResetPlayer2(_pli,_aci)) then _con.longjmp() end",
|
2012-12-03 18:24:30 +00:00
|
|
|
respawnhitag = cmd()
|
2013-01-02 22:33:37 +00:00
|
|
|
/ format("_con._respawnhitag(%s)", SPS""),
|
2012-11-30 18:57:46 +00:00
|
|
|
tip = cmd()
|
|
|
|
/ PLS".tipincs=26",
|
2012-12-03 18:24:30 +00:00
|
|
|
tossweapon = cmd()
|
|
|
|
/ "", -- TODO
|
|
|
|
wackplayer = cmd()
|
|
|
|
/ PLS":wack()",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-04-22 19:10:46 +00:00
|
|
|
-- player/sprite searching
|
|
|
|
findplayer = cmd(W),
|
|
|
|
findotherplayer = cmd(W),
|
2012-04-07 20:10:03 +00:00
|
|
|
findnearspritezvar = cmd(D,R,R,W),
|
|
|
|
findnearspritez = cmd(D,D,D,W),
|
|
|
|
findnearsprite3dvar = cmd(D,R,W),
|
|
|
|
findnearsprite3d = cmd(D,D,W),
|
2012-04-22 19:10:46 +00:00
|
|
|
findnearspritevar = cmd(D,R,W),
|
2012-04-07 20:10:03 +00:00
|
|
|
findnearsprite = cmd(D,D,W),
|
|
|
|
findnearactorzvar = cmd(D,R,R,W),
|
|
|
|
findnearactorz = cmd(D,D,D,W),
|
|
|
|
findnearactor3dvar = cmd(D,R,W),
|
|
|
|
findnearactor3d = cmd(D,D,W),
|
2012-04-22 19:10:46 +00:00
|
|
|
findnearactorvar = cmd(D,R,W),
|
2012-04-07 20:10:03 +00:00
|
|
|
findnearactor = cmd(D,D,W),
|
|
|
|
|
|
|
|
-- quotes
|
2013-01-13 16:40:26 +00:00
|
|
|
qsprintf = sp1 * tok.rvar * sp1 * tok.rvar * (sp1 * tok.rvar)^-32,
|
2012-04-07 20:10:03 +00:00
|
|
|
qgetsysstr = cmd(R,R),
|
|
|
|
qstrcat = cmd(R,R),
|
|
|
|
qstrcpy = cmd(R,R),
|
|
|
|
qstrlen = cmd(R,R),
|
|
|
|
qstrncat = cmd(R,R),
|
|
|
|
qsubstr = cmd(R,R),
|
|
|
|
|
|
|
|
-- array stuff
|
2013-01-13 16:40:26 +00:00
|
|
|
copy = sp1 * tok.identifier * arraypat * sp1 * tok.identifier * arraypat * sp1 * tok.rvar,
|
|
|
|
setarray = sp1 * tok.identifier * arraypat * sp1 * tok.rvar,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
activatebysector = cmd(R,R),
|
2012-05-06 20:22:56 +00:00
|
|
|
addlogvar = cmd(R),
|
2012-06-17 19:45:37 +00:00
|
|
|
addlog = cmd() * #sp1,
|
2013-01-06 18:56:45 +00:00
|
|
|
addweaponvar = cmd(R,R), -- NLCF
|
2012-04-07 20:10:03 +00:00
|
|
|
cansee = cmd(R,R,R,R,R,R,R,R,W),
|
|
|
|
canseespr = cmd(R,R,W),
|
|
|
|
changespritesect = cmd(R,R),
|
|
|
|
changespritestat = cmd(R,R),
|
|
|
|
clipmove = cmd(W,W,W,R,W,R,R,R,R,R,R),
|
|
|
|
clipmovenoslide = cmd(W,W,W,R,W,R,R,R,R,R,R),
|
|
|
|
displayrand = cmd(W),
|
|
|
|
displayrandvar = cmd(W,D),
|
|
|
|
displayrandvarvar = cmd(W,R),
|
2012-04-22 19:10:46 +00:00
|
|
|
dist = cmd(W,R,R),
|
2012-04-07 20:10:03 +00:00
|
|
|
dragpoint = cmd(R,R,R),
|
|
|
|
hitscan = cmd(R,R,R,R,R,R,R,W,W,W,W,W,W,R), -- 7R 6W 1R
|
|
|
|
|
|
|
|
-- screen text and numbers display
|
|
|
|
gametext = cmd(R,R,R,R,R,R,R,R,R,R,R), -- 11 R
|
|
|
|
gametextz = cmd(R,R,R,R,R,R,R,R,R,R,R,R), -- 12 R
|
|
|
|
digitalnumber = cmd(R,R,R,R,R,R,R,R,R,R,R), -- 11R
|
2012-04-29 20:54:06 +00:00
|
|
|
digitalnumberz = cmd(R,R,R,R,R,R,R,R,R,R,R,R), -- 12R
|
2012-04-07 20:10:03 +00:00
|
|
|
minitext = cmd(R,R,R,R,R),
|
|
|
|
|
2012-04-22 19:10:46 +00:00
|
|
|
ldist = cmd(W,R,R),
|
2012-04-07 20:10:03 +00:00
|
|
|
lineintersect = cmd(R,R,R,R,R,R,R,R,R,R,W,W,W,W), -- 10R 4W
|
|
|
|
rayintersect = cmd(R,R,R,R,R,R,R,R,R,R,W,W,W,W), -- 10R 4W
|
|
|
|
loadmapstate = cmd(),
|
|
|
|
savemapstate = cmd(),
|
|
|
|
movesprite = cmd(R,R,R,R,R,W),
|
|
|
|
neartag = cmd(R,R,R,R,R,W,W,W,W,R,R),
|
2012-04-29 20:54:06 +00:00
|
|
|
operateactivators = cmd(R,R),
|
|
|
|
operatesectors = cmd(R,R),
|
2013-01-13 16:40:26 +00:00
|
|
|
palfrom = (sp1 * tok.define)^-4
|
|
|
|
/ handle.palfrom,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-12-03 18:24:30 +00:00
|
|
|
operate = cmd() * #sp1
|
|
|
|
/ "_con._operate(_aci)",
|
2012-04-29 20:54:06 +00:00
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
myos = cmd(R,R,R,R,R),
|
2012-04-29 20:54:06 +00:00
|
|
|
myosx = cmd(R,R,R,R,R),
|
2012-04-07 20:10:03 +00:00
|
|
|
myospal = cmd(R,R,R,R,R,R),
|
|
|
|
myospalx = cmd(R,R,R,R,R,R),
|
|
|
|
|
|
|
|
headspritesect = cmd(R,R),
|
|
|
|
headspritestat = cmd(R,R),
|
|
|
|
nextspritesect = cmd(R,R),
|
|
|
|
nextspritestat = cmd(R,R),
|
|
|
|
prevspritesect = cmd(R,R),
|
|
|
|
prevspritestat = cmd(R,R),
|
|
|
|
|
|
|
|
readarrayfromfile = cmd(I,D),
|
|
|
|
writearraytofile = cmd(I,D),
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
redefinequote = sp1 * tok.define * newline_term_string
|
2013-01-05 13:09:19 +00:00
|
|
|
/ function(qnum, qstr) return format("_con._definequote(%d,%q)", qnum, stripws(qstr)) end,
|
2012-04-07 20:10:03 +00:00
|
|
|
resizearray = cmd(I,R),
|
|
|
|
getarraysize = cmd(I,W),
|
|
|
|
rotatepoint = cmd(R,R,R,R,R,W,W),
|
|
|
|
rotatesprite = cmd(R,R,R,R,R,R,R,R,R,R,R,R), -- 12R
|
|
|
|
rotatesprite16 = cmd(R,R,R,R,R,R,R,R,R,R,R,R), -- 12R
|
|
|
|
sectorofwall = cmd(W,R,R),
|
|
|
|
sectclearinterpolation = cmd(R),
|
|
|
|
sectsetinterpolation = cmd(R),
|
|
|
|
|
|
|
|
sectgethitag = cmd(),
|
|
|
|
sectgetlotag = cmd(),
|
|
|
|
spgethitag = cmd(),
|
|
|
|
spgetlotag = cmd(),
|
|
|
|
|
|
|
|
showview = cmd(R,R,R,R,R,R,R,R,R,R), -- 10R
|
|
|
|
showviewunbiased = cmd(R,R,R,R,R,R,R,R,R,R), -- 10R
|
|
|
|
smaxammo = cmd(R,R),
|
|
|
|
gmaxammo = cmd(R,W),
|
|
|
|
spriteflags = cmd(R), -- also see outer
|
|
|
|
ssp = cmd(R,R),
|
|
|
|
startlevel = cmd(R,R),
|
|
|
|
starttrack = cmd(D),
|
|
|
|
stopactorsound = cmd(R,R),
|
|
|
|
stopallsounds = cmd(),
|
|
|
|
updatesector = cmd(R,R,W),
|
|
|
|
updatesectorz = cmd(R,R,R,W),
|
|
|
|
|
|
|
|
getactorangle = cmd(W),
|
|
|
|
setactorangle = cmd(R),
|
|
|
|
getplayerangle = cmd(W),
|
|
|
|
setplayerangle = cmd(R),
|
|
|
|
getangletotarget = cmd(W),
|
|
|
|
|
|
|
|
getceilzofslope = cmd(R,R,R,W),
|
|
|
|
getflorzofslope = cmd(R,R,R,W),
|
|
|
|
getcurraddress = cmd(W), -- XXX
|
|
|
|
getkeyname = cmd(R,R,R),
|
|
|
|
getpname = cmd(R,R),
|
|
|
|
gettextureceiling = cmd(),
|
|
|
|
gettexturefloor = cmd(),
|
|
|
|
getticks = cmd(W),
|
|
|
|
gettimedate = cmd(W,W,W,W,W,W,W,W),
|
|
|
|
getzrange = cmd(R,R,R,R,W,W,W,W,R,R),
|
|
|
|
|
|
|
|
setactorsoundpitch = cmd(R,R,R),
|
|
|
|
setaspect = cmd(R,R),
|
|
|
|
}
|
|
|
|
|
|
|
|
local Cif = {
|
2012-11-30 18:57:46 +00:00
|
|
|
ifai = cmd(AI)
|
|
|
|
/ ACS":has_ai(%1)",
|
|
|
|
ifaction = cmd(AC)
|
|
|
|
/ ACS":has_action(%1)",
|
|
|
|
ifmove = cmd(MV)
|
|
|
|
/ ACS":has_move(%1)",
|
|
|
|
|
|
|
|
ifrnd = cmd(D)
|
2012-12-03 18:24:25 +00:00
|
|
|
/ "_con.rnd(%1)",
|
|
|
|
ifpdistl = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ function(val) return "_dist<"..val, "", "_con._sleepcheck(_aci,_dist)" end,
|
2012-12-03 18:24:25 +00:00
|
|
|
ifpdistg = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ function(val) return "_dist>"..val, "", "_con._sleepcheck(_aci,_dist)" end,
|
2012-11-30 18:57:46 +00:00
|
|
|
ifactioncount = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ ACS":get_acount()>=%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifcount = cmd(D)
|
2013-01-02 22:33:37 +00:00
|
|
|
/ ACS":get_count()>=%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifactor = cmd(D)
|
2012-12-09 13:24:28 +00:00
|
|
|
/ SPS".picnum==%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifstrength = cmd(D)
|
2012-12-09 13:24:28 +00:00
|
|
|
/ SPS".extra<=%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifspawnedby = cmd(D)
|
2012-12-09 13:24:28 +00:00
|
|
|
/ ACS".picnum==%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifwasweapon = cmd(D)
|
2012-12-09 13:24:28 +00:00
|
|
|
/ ACS".picnum==%1",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifgapzl = cmd(D) -- factor into a con.* function?
|
|
|
|
/ format("_bit.arshift(%s-%s,8)<%%1", ACS".floorz", ACS".ceilingz"),
|
|
|
|
iffloordistl = cmd(D)
|
|
|
|
/ format("(%s-%s)<=256*%%1", ACS".floorz", SPS".z"),
|
|
|
|
ifceilingdistl = cmd(D)
|
|
|
|
/ format("(%s-%s)<=256*%%1", SPS".z", ACS".ceilingz"),
|
|
|
|
ifphealthl = cmd(D)
|
|
|
|
/ format("sprite[%s].extra<%%1", PLS".i"),
|
|
|
|
ifspritepal = cmd(D)
|
|
|
|
/ SPS".pal==%1",
|
2012-12-03 18:24:30 +00:00
|
|
|
ifgotweaponce = cmd(D)
|
|
|
|
/ "false", -- TODO? (multiplayer only)
|
|
|
|
ifangdiffl = cmd(D)
|
|
|
|
/ format("_con._angdiffabs(%s,%s)<=%%1", PLS".ang", SPS".ang"),
|
2012-12-03 18:24:25 +00:00
|
|
|
ifsound = cmd(D)
|
2013-01-19 18:28:40 +00:00
|
|
|
/ "_con._soundplaying(_aci,%1)",
|
2012-12-03 18:24:30 +00:00
|
|
|
-- vvv TODO: this is not correct for GET_ACCESS or GET_SHIELD.
|
|
|
|
ifpinventory = cmd(D,D)
|
2012-12-09 13:24:39 +00:00
|
|
|
/ format("_con._getinventory(%s,%%1,_aci)~=%%2", PLS""),
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-19 18:28:40 +00:00
|
|
|
ifvarl = cmd(R,D)
|
|
|
|
/ "%1<%2",
|
|
|
|
ifvarg = cmd(R,D)
|
|
|
|
/ "%1>%2",
|
2013-01-05 13:09:19 +00:00
|
|
|
ifvare = cmd(R,D)
|
|
|
|
/ "%1==%2",
|
2013-01-19 18:28:40 +00:00
|
|
|
ifvarn = cmd(R,D)
|
|
|
|
/ "%1~=%2",
|
|
|
|
ifvarand = cmd(R,D)
|
|
|
|
/ "_bit.band(%1,%2)~=0",
|
|
|
|
ifvaror = cmd(R,D)
|
|
|
|
/ "_bit.bor(%1,%2)~=0",
|
|
|
|
ifvarxor = cmd(R,D)
|
|
|
|
/ "_bit.bxor(%1,%2)~=0",
|
|
|
|
ifvareither = cmd(R,D)
|
|
|
|
/ "%1~=0 or %2~=0",
|
|
|
|
|
|
|
|
ifvarvarl = cmd(R,R)
|
|
|
|
/ "%1<%2",
|
|
|
|
ifvarvarg = cmd(R,R)
|
|
|
|
/ "%1>%2",
|
|
|
|
ifvarvare = cmd(R,R)
|
|
|
|
/ "%1==%2",
|
|
|
|
ifvarvarn = cmd(R,R)
|
|
|
|
/ "%1~=%2",
|
|
|
|
ifvarvarand = cmd(R,R)
|
|
|
|
/ "_bit.band(%1,%2)~=0",
|
|
|
|
ifvarvaror = cmd(R,R)
|
|
|
|
/ "_bit.bor(%1,%2)~=0",
|
|
|
|
ifvarvarxor = cmd(R,R)
|
|
|
|
/ "_bit.bxor(%1,%2)~=0",
|
|
|
|
ifvarvareither = cmd(R,R)
|
|
|
|
/ "%1~=0 or %2~=0",
|
|
|
|
|
|
|
|
ifactorsound = cmd(R,R)
|
|
|
|
/ "_con._soundplaying(%1,%2)",
|
2012-04-29 20:54:06 +00:00
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
ifp = (sp1 * tok.define)^1
|
2012-12-09 13:24:39 +00:00
|
|
|
/ function (...) return format("_con._ifp(%d,_pli,_aci)", bit.bor(...)) end,
|
2012-12-03 18:24:25 +00:00
|
|
|
ifsquished = cmd()
|
|
|
|
/ "false", -- TODO
|
2012-04-29 20:54:06 +00:00
|
|
|
ifserver = cmd(),
|
2012-12-03 18:24:30 +00:00
|
|
|
ifrespawn = cmd()
|
2012-12-09 13:24:39 +00:00
|
|
|
/ format("_con._checkrespawn(%s)", SPS""),
|
2012-11-30 18:57:46 +00:00
|
|
|
ifoutside = cmd()
|
|
|
|
/ format("_bit.band(sector[%s].ceilingstat,1)~=0", SPS".sectnum"),
|
2012-12-03 18:24:25 +00:00
|
|
|
ifonwater = cmd()
|
2012-12-23 19:24:16 +00:00
|
|
|
/ format("sector[%s].lotag==1 and _math.abs(%s-sector[%s].floorz)<32*256",
|
2012-12-03 18:24:25 +00:00
|
|
|
SPS".sectnum", SPS".z", SPS".sectnum"),
|
2012-12-03 18:24:30 +00:00
|
|
|
ifnotmoving = cmd()
|
|
|
|
/ "_bit.band(actor[_aci].movflag,49152)>16384",
|
2012-12-23 19:24:16 +00:00
|
|
|
ifnosounds = cmd()
|
2013-01-02 22:33:37 +00:00
|
|
|
/ "not _con._ianysound()",
|
2012-11-30 18:57:46 +00:00
|
|
|
ifmultiplayer = cmd()
|
|
|
|
/ "false", -- TODO?
|
|
|
|
ifinwater = cmd()
|
|
|
|
/ format("sector[%s].lotag==2", SPS".sectnum"),
|
2012-12-03 18:24:30 +00:00
|
|
|
ifinspace = cmd()
|
2012-12-09 13:24:39 +00:00
|
|
|
/ format("_con._checkspace(%s,false)", SPS".sectnum"),
|
2012-12-03 18:24:30 +00:00
|
|
|
ifinouterspace = cmd()
|
2012-12-09 13:24:39 +00:00
|
|
|
/ format("_con._checkspace(%s,true)", SPS".sectnum"),
|
2012-11-30 18:57:46 +00:00
|
|
|
ifhitweapon = cmd()
|
2013-01-01 15:24:22 +00:00
|
|
|
/ "_con._A_IncurDamage(_aci)>=0",
|
2012-12-03 18:24:30 +00:00
|
|
|
ifhitspace = cmd()
|
|
|
|
/ "_con._testkey(_pli,29)", -- XXX
|
2012-11-30 18:57:46 +00:00
|
|
|
ifdead = cmd()
|
|
|
|
/ SPS".extra<=0",
|
2012-04-29 20:54:06 +00:00
|
|
|
ifclient = cmd(),
|
2012-12-03 18:24:25 +00:00
|
|
|
ifcanshoottarget = cmd()
|
2012-12-09 13:24:39 +00:00
|
|
|
/ "_con._canshoottarget(_dist,_aci)",
|
2013-01-02 22:33:37 +00:00
|
|
|
ifcanseetarget = cmd() -- XXX: 1536 is SLEEPTIME
|
|
|
|
/ function() return format("_con._canseetarget(%s,%s)", SPS"", PLS""), ACS".timetosleep=1536" end,
|
2012-12-09 13:24:39 +00:00
|
|
|
ifcansee = cmd() * #sp1
|
|
|
|
/ format("_con._cansee(_aci,%s)", PLS""),
|
2012-12-03 18:24:30 +00:00
|
|
|
ifbulletnear = cmd()
|
|
|
|
/ "_con._bulletnear(_aci)",
|
|
|
|
ifawayfromwall = cmd()
|
|
|
|
/ format("_con._awayfromwall(%s,108)", SPS""),
|
2012-11-30 18:57:46 +00:00
|
|
|
ifactornotstayput = cmd()
|
|
|
|
/ ACS".actorstayput==-1",
|
2012-04-07 20:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-22 19:10:46 +00:00
|
|
|
----==== Tracing and reporting ====----
|
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
-- g_newlineidxs will contain the 1-based file offsets to "\n" characters
|
|
|
|
local g_newlineidxs = {}
|
2012-04-22 19:10:46 +00:00
|
|
|
|
|
|
|
-- Returns index into the sorted table tab such that
|
|
|
|
-- tab[index] <= searchelt < tab[index+1].
|
|
|
|
-- Preconditions:
|
2012-06-10 18:56:18 +00:00
|
|
|
-- tab[i] < tab[i+1] for 0 <= i < #tab
|
|
|
|
-- tab[0] <= searchelt < tab[#tab]
|
2012-05-13 16:04:45 +00:00
|
|
|
-- If #tab is less than 2, returns 0. This plays nicely with newline index
|
|
|
|
-- tables like { [0]=0, [1]=len+1 }, e.g. if the file doesn't contain any.
|
2012-04-22 19:10:46 +00:00
|
|
|
local function bsearch(tab, searchelt)
|
|
|
|
-- printf("bsearch(tab, %d)", searchelt)
|
2012-06-10 18:56:18 +00:00
|
|
|
local l, r = 0, #tab
|
2012-04-22 19:10:46 +00:00
|
|
|
local i
|
|
|
|
|
2012-05-13 16:04:45 +00:00
|
|
|
if (r < 2) then
|
|
|
|
return 0
|
2012-04-22 19:10:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
while (l ~= r) do
|
|
|
|
i = l + math.ceil((r-l)/2) -- l < i <= r
|
|
|
|
assert(l < i and i <= r)
|
|
|
|
local elt = tab[i]
|
|
|
|
-- printf("l=%d tab[%d]=%d r=%d", l, i, elt, r)
|
|
|
|
|
|
|
|
if (searchelt == elt) then
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
|
|
|
if (searchelt < elt) then
|
|
|
|
r = i-1
|
|
|
|
else -- (searchelt > elt)
|
|
|
|
l = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- printf("return tab[%d]=%d", l, tab[l])
|
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
2012-06-10 18:56:18 +00:00
|
|
|
function getlinecol(pos) -- local
|
2012-11-25 13:18:46 +00:00
|
|
|
assert(type(pos)=="number")
|
2012-06-10 18:56:21 +00:00
|
|
|
local line = bsearch(g_newlineidxs, pos)
|
|
|
|
assert(line and g_newlineidxs[line]<=pos and pos<g_newlineidxs[line+1])
|
|
|
|
local col = pos-g_newlineidxs[line]
|
2012-05-13 16:04:45 +00:00
|
|
|
return line+1, col-1
|
2012-04-22 19:10:46 +00:00
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
-- A generic trace function, prints a position together with the match content.
|
|
|
|
-- The 'doit' parameter can be used to temporarily enable/disable a particular
|
|
|
|
-- tracing function.
|
2012-04-22 19:10:46 +00:00
|
|
|
local function TraceFunc(pat, label, doit)
|
2012-07-01 22:11:17 +00:00
|
|
|
assert(doit ~= nil)
|
2012-04-07 20:10:03 +00:00
|
|
|
pat = Pat(pat)
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
if (doit) then
|
2012-04-22 19:10:46 +00:00
|
|
|
local function tfunc(subj, pos, a)
|
2012-06-10 18:56:21 +00:00
|
|
|
printf("%s:%s: %s", linecolstr(pos), label, a)
|
2012-04-22 19:10:46 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
pat = lpeg.Cmt(pat, tfunc)
|
2012-04-29 20:54:06 +00:00
|
|
|
elseif (label=="kw") then -- HACK
|
|
|
|
local function tfunc(subj, pos, a)
|
|
|
|
g_lastkwpos = pos
|
|
|
|
g_lastkw = a
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
-- XXX: is there a better way?
|
|
|
|
pat = lpeg.Cmt(pat, tfunc)
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
2012-04-29 20:54:06 +00:00
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
return pat
|
|
|
|
end
|
|
|
|
|
2012-06-10 18:56:18 +00:00
|
|
|
local function BadIdent(pat)
|
2012-04-29 20:54:06 +00:00
|
|
|
local function tfunc(subj, pos, a)
|
2012-12-03 18:24:33 +00:00
|
|
|
if (g_warn["bad-identifier"] and not g_badids[a]) then
|
2012-06-13 23:13:26 +00:00
|
|
|
warnprintf("bad identifier: %s", a)
|
2012-04-29 20:54:06 +00:00
|
|
|
g_badids[a] = true
|
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
return true
|
2012-04-29 20:54:06 +00:00
|
|
|
end
|
|
|
|
return lpeg.Cmt(Pat(pat), tfunc)
|
|
|
|
end
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
-- These are tracers for specific patterns which can be disabled
|
|
|
|
-- if desired.
|
2012-04-29 20:54:06 +00:00
|
|
|
local function Keyw(kwname) return TraceFunc(kwname, "kw", false) end
|
|
|
|
local function NotKeyw(text) return TraceFunc(text, "!kw", false) end
|
|
|
|
local function Ident(idname) return TraceFunc(idname, "id", false) end
|
|
|
|
local function Stmt(cmdpat) return TraceFunc(cmdpat, "st", false) end
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-05-13 16:04:45 +00:00
|
|
|
--local function Temp(kwname) return TraceFunc(kwname, "temp", true) end
|
2013-01-13 16:40:26 +00:00
|
|
|
--Cinner["myosx"] = Temp(Cinner["myosx"])
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-04-22 19:10:46 +00:00
|
|
|
----==== Translator continued ====----
|
2012-07-01 22:11:17 +00:00
|
|
|
local function after_inner_cmd_Cmt(subj, pos, ...)
|
|
|
|
local capts = {...}
|
|
|
|
|
|
|
|
if (g_numerrors == inf) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if (type(capts[1])=="string" and capts[2]==nil) then
|
2012-11-30 18:57:46 +00:00
|
|
|
return true, capts[1] .."--"..linecolstr(pos) --TEMP
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function after_if_cmd_Cmt(subj, pos, ...)
|
|
|
|
local capts = {...}
|
|
|
|
|
|
|
|
if (g_numerrors == inf) then
|
|
|
|
return nil
|
|
|
|
end
|
2013-01-19 18:28:40 +00:00
|
|
|
|
|
|
|
if (not g_cgopt["_incomplete"] and capts[1] ~= nil) then
|
2013-01-02 22:33:37 +00:00
|
|
|
assert(#capts <= 3)
|
|
|
|
for i=1,#capts do
|
|
|
|
assert(type(capts[i]=="string"))
|
|
|
|
end
|
|
|
|
return true, unpack(capts, 1, #capts)
|
2012-07-01 22:11:17 +00:00
|
|
|
end
|
2013-01-19 18:28:40 +00:00
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function after_cmd_Cmt(subj, pos, ...)
|
2012-06-10 18:56:21 +00:00
|
|
|
if (g_numerrors == inf) then
|
|
|
|
-- print("Aborting parsing...")
|
|
|
|
return nil -- make the match fail, bail out of parsing
|
|
|
|
end
|
|
|
|
|
|
|
|
return true -- don't return any captures
|
|
|
|
end
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
-- attach the command names at the front!
|
2012-11-30 18:57:46 +00:00
|
|
|
local function attachnames(kwtab, matchtimefunc)
|
2012-04-07 20:10:03 +00:00
|
|
|
for cmdname,cmdpat in pairs(kwtab) do
|
2012-06-17 19:45:37 +00:00
|
|
|
-- The match-time function capture at the end is so that every
|
2012-06-10 18:56:18 +00:00
|
|
|
-- command acts as a barrier to captures to prevent stack overflow (and
|
|
|
|
-- to make lpeg.match return a subject position at the end)
|
2012-11-30 18:57:46 +00:00
|
|
|
kwtab[cmdname] = lpeg.Cmt(Keyw(cmdname) * cmdpat, matchtimefunc)
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
attachnames(Couter, after_cmd_Cmt)
|
|
|
|
attachnames(Cinner, after_inner_cmd_Cmt)
|
2012-11-30 18:57:46 +00:00
|
|
|
attachnames(Cif, after_if_cmd_Cmt)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
|
2012-06-17 19:45:37 +00:00
|
|
|
-- Takes one or more tables and +'s all its patterns together in reverse
|
|
|
|
-- lexicographical order.
|
|
|
|
-- Each such PATTAB must be a table that maps command names to their patterns.
|
2012-04-22 19:10:46 +00:00
|
|
|
local function all_alt_pattern(...)
|
2012-06-17 19:45:37 +00:00
|
|
|
local cmds = {}
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
local args = {...}
|
2012-06-17 19:45:37 +00:00
|
|
|
assert(#args <= 2)
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
for argi=1,#args do
|
|
|
|
local pattab = args[argi]
|
2012-06-17 19:45:37 +00:00
|
|
|
|
|
|
|
-- pairs() iterates in undefined order, so we first fill in the names...
|
|
|
|
for cmdname,_ in pairs(pattab) do
|
|
|
|
cmds[#cmds+1] = cmdname
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
|
|
|
end
|
2012-06-17 19:45:37 +00:00
|
|
|
|
|
|
|
-- ...and then sort them in ascending lexicographical order
|
|
|
|
table.sort(cmds)
|
|
|
|
|
|
|
|
local pat = Pat(false)
|
|
|
|
|
|
|
|
for i=1,#cmds do
|
|
|
|
local ourpat = args[1][cmds[i]] or args[2][cmds[i]]
|
|
|
|
-- shorter commands go at the end!
|
|
|
|
pat = pat + ourpat
|
|
|
|
end
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
return pat
|
|
|
|
end
|
|
|
|
|
|
|
|
-- actor ORGANTIC is greeting!
|
2012-07-01 22:11:17 +00:00
|
|
|
local function warn_on_lonely_else(pos)
|
|
|
|
pwarnprintf(pos, "found `else' with no `if'")
|
2012-06-22 21:40:01 +00:00
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
local con_inner_command = all_alt_pattern(Cinner)
|
2012-06-17 19:45:37 +00:00
|
|
|
local con_if_begs = all_alt_pattern(Cif)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
local lone_else = (POS() * "else" * sp1)/warn_on_lonely_else
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
local stmt_list = Var("stmt_list")
|
|
|
|
-- possibly empty statement list:
|
2012-07-01 22:11:17 +00:00
|
|
|
local stmt_list_or_eps = lpeg.Ct((stmt_list * sp1)^-1)
|
2012-04-29 20:54:06 +00:00
|
|
|
local stmt_list_nosp_or_eps = (stmt_list * (sp1 * stmt_list)^0)^-1
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-06-17 19:45:43 +00:00
|
|
|
-- common to actor and useractor: <name/tilenum> [<strength> [<action> [<move> [<flags>... ]]]]
|
2013-01-13 16:40:26 +00:00
|
|
|
local common_actor_end = sp1 * lpeg.Ct(tok.define *
|
|
|
|
(sp1 * tok.define *
|
|
|
|
(sp1 * tok.action *
|
|
|
|
(sp1 * tok.move *
|
|
|
|
(sp1 * tok.define)^0
|
2012-06-17 19:45:43 +00:00
|
|
|
)^-1
|
|
|
|
)^-1
|
2012-06-22 21:40:01 +00:00
|
|
|
)^-1)
|
2012-07-01 22:11:17 +00:00
|
|
|
* sp1 * stmt_list_or_eps * "enda"
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
--== block delimiters (no recursion) ==--
|
2013-01-13 16:40:26 +00:00
|
|
|
local Cblock = {
|
2012-04-07 20:10:03 +00:00
|
|
|
-- actor (...)
|
2012-07-01 22:11:17 +00:00
|
|
|
actor = lpeg.Cc(nil) * common_actor_end / on_actor_end,
|
2012-04-07 20:10:03 +00:00
|
|
|
-- useractor <actortype> (...)
|
2013-01-13 16:40:26 +00:00
|
|
|
useractor = sp1 * tok.define * common_actor_end / on_actor_end,
|
2012-06-22 21:40:01 +00:00
|
|
|
-- eventloadactor <name/tilenum>
|
2013-01-13 16:40:26 +00:00
|
|
|
eventloadactor = lpeg.Cc(nil) * sp1 * lpeg.Ct(tok.define)
|
2012-07-01 22:11:17 +00:00
|
|
|
* sp1 * stmt_list_or_eps * "enda" / on_actor_end,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
onevent = sp1 * tok.define * sp1 * stmt_list_or_eps * "endevent"
|
2013-01-05 13:09:19 +00:00
|
|
|
/ on_event_end,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
state = sp1 * (tok.identifier/on_state_begin) * sp1 * stmt_list_or_eps * "ends"
|
2012-12-03 18:24:33 +00:00
|
|
|
/ on_state_end,
|
2012-04-07 20:10:03 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
attachnames(Cblock, after_cmd_Cmt)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
|
2012-04-29 20:54:06 +00:00
|
|
|
local t_good_identifier = Range("AZ", "az", "__") * Range("AZ", "az", "__", "09")^0
|
|
|
|
|
|
|
|
-- CON isaltok also has chars in "{}.", but these could potentially
|
2012-11-30 18:57:40 +00:00
|
|
|
-- interfere with *CON* syntax. The "]" is so that the number in e.g. array[80]
|
2012-04-29 20:54:06 +00:00
|
|
|
-- isn't considered a broken identifier.
|
|
|
|
-- "-" is somewhat problematic, but we allow it only as 2nd and up character, so
|
|
|
|
-- there's no ambiguity with unary minus. (Commands must be separated by spaces
|
|
|
|
-- in CON, so a trailing "-" is "OK", too.)
|
|
|
|
-- This is broken in itself, so we ought to make a compatibility/modern CON switch.
|
2013-01-13 16:40:26 +00:00
|
|
|
local t_broken_identifier = BadIdent(-((tok.number + t_good_identifier) * (sp1 + Set("[]:"))) *
|
2013-01-05 13:09:31 +00:00
|
|
|
(alphanum + Set(BAD_ID_CHARS0)) * (alphanum + Set(BAD_ID_CHARS1))^0)
|
2012-04-29 20:54:06 +00:00
|
|
|
|
2013-01-02 22:33:37 +00:00
|
|
|
-- These two tables hold code to be inserted at a later point: either at
|
|
|
|
-- the end of the "if" body, or the end of the whole "if [else]" block.
|
|
|
|
-- For CON interpreter patterns like these:
|
|
|
|
-- VM_CONDITIONAL(<condition>);
|
|
|
|
-- <do_something_afterwards>
|
|
|
|
-- (Still not the same if the body returns or jumps out)
|
|
|
|
local g_endIfCode = {}
|
|
|
|
local g_endIfElseCode = {}
|
|
|
|
|
|
|
|
local function add_deferred_code(tab, lev, str)
|
|
|
|
if (str ~= nil) then
|
|
|
|
assert(type(str)=="string")
|
|
|
|
tab[lev] = str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_deferred_code(tab, lev, code)
|
|
|
|
if (tab[lev]) then
|
|
|
|
code = code..tab[lev]
|
|
|
|
tab[lev] = nil
|
|
|
|
end
|
|
|
|
return code
|
|
|
|
end
|
|
|
|
|
|
|
|
local function begin_if_fn(condstr, endifstr, endifelsestr)
|
2012-11-30 18:57:46 +00:00
|
|
|
condstr = condstr or "TODO"
|
|
|
|
assert(type(condstr)=="string")
|
2013-01-01 15:24:28 +00:00
|
|
|
|
2013-01-02 22:33:37 +00:00
|
|
|
add_deferred_code(g_endIfCode, g_iflevel, endifstr)
|
|
|
|
add_deferred_code(g_endIfElseCode, g_ifelselevel, endifelsestr)
|
|
|
|
|
|
|
|
g_iflevel = g_iflevel+1
|
|
|
|
g_ifelselevel = g_ifelselevel+1
|
2013-01-01 15:24:28 +00:00
|
|
|
|
2012-11-30 18:57:46 +00:00
|
|
|
return format("if (%s) then", condstr)
|
2012-06-13 23:13:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function end_if_fn()
|
2013-01-02 22:33:37 +00:00
|
|
|
g_iflevel = g_iflevel-1
|
|
|
|
local code = get_deferred_code(g_endIfCode, g_iflevel, "")
|
|
|
|
if (code ~= "") then
|
2013-01-01 15:24:28 +00:00
|
|
|
return code
|
|
|
|
end
|
2012-06-13 23:13:26 +00:00
|
|
|
end
|
|
|
|
|
2013-01-02 22:33:37 +00:00
|
|
|
local function end_if_else_fn()
|
|
|
|
g_ifelselevel = g_ifelselevel-1
|
|
|
|
return get_deferred_code(g_endIfElseCode, g_ifelselevel, "end ")
|
|
|
|
end
|
|
|
|
|
2012-06-13 23:13:26 +00:00
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
--- The final grammar!
|
|
|
|
local Grammar = Pat{
|
|
|
|
-- The starting symbol.
|
|
|
|
-- A translation unit is a (possibly empty) sequence of outer CON
|
|
|
|
-- commands, separated by at least one whitespace which may be
|
|
|
|
-- omitted at the EOF.
|
2013-01-13 16:40:26 +00:00
|
|
|
sp0 * (all_alt_pattern(Couter, Cblock) * sp1)^0,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-06-10 18:56:18 +00:00
|
|
|
-- Some often-used terminals follow. These appear here because we're
|
|
|
|
-- hitting a limit with LPeg else.
|
2012-04-07 20:10:03 +00:00
|
|
|
-- http://lua-users.org/lists/lua-l/2008-11/msg00462.html
|
2012-12-28 17:18:02 +00:00
|
|
|
|
2012-05-13 16:04:45 +00:00
|
|
|
-- NOTE: NW demo (NWSNOW.CON) contains a Ctrl-Z char (decimal 26)
|
2012-04-29 20:54:06 +00:00
|
|
|
whitespace = Set(" \t\r\26") + newline + Set("(),;") + comment + linecomment,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
t_number = POS() * lpeg.C(
|
2013-01-13 16:40:26 +00:00
|
|
|
tok.maybe_minus * ((Pat("0x") + "0X") * Range("09", "af", "AF")^1 * Pat("h")^-1
|
2013-01-13 16:40:21 +00:00
|
|
|
+ Range("09")^1)
|
|
|
|
) / parse_number,
|
|
|
|
|
2012-04-29 20:54:06 +00:00
|
|
|
t_identifier_all = t_broken_identifier + t_good_identifier,
|
2012-11-30 18:57:40 +00:00
|
|
|
-- NOTE: -conl.keyword alone would be wrong, e.g. "state breakobject":
|
2012-04-07 20:10:03 +00:00
|
|
|
-- NOTE 2: The + "[" is so that stuff like
|
|
|
|
-- getactor[THISACTOR].x x
|
|
|
|
-- getactor[THISACTOR].y y
|
|
|
|
-- is parsed correctly. (Compared with this:)
|
|
|
|
-- getactor[THISACTOR].x x
|
|
|
|
-- getactor [THISACTOR].y y
|
|
|
|
-- This is in need of cleanup!
|
2013-01-13 16:40:26 +00:00
|
|
|
t_identifier = -NotKeyw(conl.keyword * (sp1 + "[")) * lpeg.C(tok.identifier_all),
|
|
|
|
t_define = (POS() * lpeg.C(tok.maybe_minus) * tok.identifier / lookup_defined_label) + tok.number,
|
2012-07-01 22:11:17 +00:00
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
-- Defines and constants can take the place of vars that are only read.
|
2013-01-13 16:40:26 +00:00
|
|
|
-- XXX: now, when tok.rvar fails, the tok.define failure message is printed.
|
|
|
|
t_rvar = tok.arrayexp + lpeg.Cmt(tok.identifier, maybe_gamevar_Cmt) + tok.define,
|
2013-01-13 16:40:21 +00:00
|
|
|
-- not so with written-to vars:
|
2013-01-13 16:40:32 +00:00
|
|
|
t_wvar = tok.arrayexp + (tok.identifier / function(id) return lookup_gamevar(id, true) end),
|
2013-01-13 16:40:21 +00:00
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
t_move =
|
2013-01-13 16:40:26 +00:00
|
|
|
POS()*tok.identifier / function(...) return lookup_composite(LABEL.MOVE, ...) end +
|
|
|
|
POS()*tok.number / function(...) return check_composite_literal(LABEL.MOVE, ...) end,
|
2012-07-01 22:11:17 +00:00
|
|
|
|
|
|
|
t_ai =
|
2013-01-13 16:40:26 +00:00
|
|
|
POS()*tok.identifier / function(...) return lookup_composite(LABEL.AI, ...) end +
|
|
|
|
POS()*tok.number / function(...) return check_composite_literal(LABEL.AI, ...) end,
|
2012-06-17 19:45:43 +00:00
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
t_action =
|
2013-01-13 16:40:26 +00:00
|
|
|
POS()*tok.identifier / function(...) return lookup_composite(LABEL.ACTION, ...) end +
|
|
|
|
POS()*tok.number / function(...) return check_composite_literal(LABEL.ACTION, ...) end,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:26 +00:00
|
|
|
t_arrayexp = tok.identifier * arraypat * memberpat^-1,
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-04-29 20:54:06 +00:00
|
|
|
-- SWITCH
|
2013-01-13 16:40:26 +00:00
|
|
|
switch_stmt = Keyw("switch") * sp1 * tok.rvar *
|
2012-04-29 20:54:06 +00:00
|
|
|
(Var("case_block") + Var("default_block"))^0 * sp1 * "endswitch",
|
|
|
|
|
|
|
|
-- NOTE: some old DNWMD has "case: PIGCOP". I don't think I'll allow that.
|
2013-01-13 16:40:26 +00:00
|
|
|
case_block = (sp1 * Keyw("case") * sp1 * tok.define/"XXX_CASE" * (sp0*":")^-1)^1 * sp1 *
|
2012-04-29 20:54:06 +00:00
|
|
|
stmt_list_nosp_or_eps, -- * "break",
|
|
|
|
|
|
|
|
default_block = sp1 * Keyw("default") * (sp0*":"*sp0 + sp1) * stmt_list_nosp_or_eps, -- * "break",
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-05 13:09:25 +00:00
|
|
|
optional_else = (sp1 * lpeg.C("else") * sp1 * Var("single_stmt"))^-1,
|
|
|
|
|
|
|
|
if_else_bodies = Var("single_stmt2") * (Pat("")/end_if_fn) * Var("optional_else"),
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
if_stmt = con_if_begs/begin_if_fn * sp1
|
2013-01-05 13:09:25 +00:00
|
|
|
* Var("if_else_bodies")
|
|
|
|
* (Pat("")/end_if_else_fn),
|
|
|
|
|
|
|
|
if_stmt2 = con_if_begs/begin_if_fn * sp1
|
|
|
|
* (-con_if_begs * Var("single_stmt") * (Pat("")/end_if_fn)
|
|
|
|
+ Var("if_else_bodies"))
|
2013-01-02 22:33:37 +00:00
|
|
|
* (Pat("")/end_if_else_fn),
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-06-13 23:13:26 +00:00
|
|
|
-- TODO?: SST TC has "state ... else ends"
|
2013-01-13 16:40:26 +00:00
|
|
|
while_stmt = Keyw("whilevarvarn") * sp1 * tok.rvar * sp1 * tok.rvar * sp1 * Var("single_stmt")
|
|
|
|
+ Keyw("whilevarn") * sp1 * tok.rvar * sp1 * tok.define/"WHILE_XXX" * sp1 * Var("single_stmt"),
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-05 13:09:25 +00:00
|
|
|
stmt_common = Keyw("{") * sp1 * "}" -- space separation of commands in CON is for a reason!
|
|
|
|
+ Keyw("{") * sp1 * stmt_list * sp1 * "}"
|
|
|
|
+ con_inner_command + Var("switch_stmt") + Var("while_stmt"),
|
|
|
|
|
|
|
|
single_stmt = Stmt( lone_else^-1 * (Var("stmt_common") + Var("if_stmt")) ),
|
|
|
|
single_stmt2 = Stmt( lone_else^-1 * (Var("stmt_common") + Var("if_stmt2")) ),
|
2012-04-07 20:10:03 +00:00
|
|
|
|
|
|
|
-- a non-empty statement/command list
|
|
|
|
stmt_list = Var("single_stmt") * (sp1 * Var("single_stmt"))^0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-22 19:10:46 +00:00
|
|
|
local function setup_newlineidxs(contents)
|
2012-06-10 18:56:21 +00:00
|
|
|
local newlineidxs = {}
|
2012-04-22 19:10:46 +00:00
|
|
|
for i in string.gmatch(contents, "()\n") do
|
|
|
|
newlineidxs[#newlineidxs+1] = i
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
2012-05-13 16:04:45 +00:00
|
|
|
if (#newlineidxs == 0) then
|
|
|
|
-- try CR only (old Mac)
|
|
|
|
for i in string.gmatch(contents, "()\r") do
|
|
|
|
newlineidxs[#newlineidxs+1] = i
|
|
|
|
end
|
|
|
|
-- if (#newlineidxs > 0) then print('CR-only lineends detected.') end
|
|
|
|
end
|
2012-04-29 20:54:06 +00:00
|
|
|
-- dummy newlines at beginning and end
|
|
|
|
newlineidxs[#newlineidxs+1] = #contents+1
|
|
|
|
newlineidxs[0] = 0
|
2012-06-10 18:56:21 +00:00
|
|
|
|
|
|
|
return newlineidxs
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
---
|
|
|
|
local function do_flatten_codetab(code, intotab)
|
|
|
|
for i=1,math.huge do
|
|
|
|
local elt = code[i]
|
|
|
|
|
|
|
|
if (type(elt)=="string") then
|
2012-11-30 18:57:40 +00:00
|
|
|
intotab[#intotab+1] = elt
|
2012-07-01 22:11:17 +00:00
|
|
|
elseif (type(elt)=="table") then
|
|
|
|
do_flatten_codetab(elt, intotab)
|
|
|
|
else
|
|
|
|
assert(elt==nil)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return a "string buffer" table that can be table.concat'ed
|
|
|
|
-- to get the code string.
|
|
|
|
local function flatten_codetab(codetab)
|
|
|
|
local tmpcode = {}
|
|
|
|
do_flatten_codetab(codetab, tmpcode)
|
|
|
|
return tmpcode
|
|
|
|
end
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local function get_code_string(codetab)
|
|
|
|
return table.concat(flatten_codetab(g_curcode), "\n")
|
|
|
|
end
|
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
local function on_parse_begin()
|
|
|
|
g_iflevel = 0
|
|
|
|
g_ifelselevel = 0
|
|
|
|
g_have_file[g_filename] = true
|
|
|
|
|
|
|
|
-- set up new state
|
|
|
|
-- TODO: pack into one "parser state" table?
|
|
|
|
g_lastkw, g_lastkwpos, g_numerrors = nil, nil, 0
|
|
|
|
g_recurslevel = g_recurslevel+1
|
|
|
|
end
|
|
|
|
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
---=== EXPORTED FUNCTIONS ===---
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
function parse(contents) -- local
|
|
|
|
-- save outer state
|
|
|
|
local lastkw, lastkwpos, numerrors = g_lastkw, g_lastkwpos, g_numerrors
|
|
|
|
local newlineidxs = g_newlineidxs
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2013-01-13 16:40:21 +00:00
|
|
|
on_parse_begin()
|
2012-06-13 23:13:26 +00:00
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
g_newlineidxs = setup_newlineidxs(contents)
|
|
|
|
|
2012-12-28 17:18:06 +00:00
|
|
|
addcodef("-- BEGIN %s", g_filename)
|
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
local idx = lpeg.match(Grammar, contents)
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
if (not idx) then
|
2012-06-10 18:56:21 +00:00
|
|
|
printf("[%d] Match failed.", g_recurslevel)
|
2012-06-17 19:45:37 +00:00
|
|
|
g_numerrors = inf
|
2012-05-13 16:05:16 +00:00
|
|
|
elseif (idx == #contents+1) then
|
2012-06-10 18:56:21 +00:00
|
|
|
if (g_numerrors ~= 0) then
|
|
|
|
printf("[%d] Matched whole contents (%d errors).",
|
|
|
|
g_recurslevel, g_numerrors)
|
|
|
|
elseif (g_recurslevel==0) then
|
2012-12-28 17:18:06 +00:00
|
|
|
printf("[0] Matched whole contents.")
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
else
|
|
|
|
local i, col = getlinecol(idx)
|
2012-06-10 18:56:21 +00:00
|
|
|
local bi, ei = g_newlineidxs[i-1]+1, g_newlineidxs[i]-1
|
2012-04-07 20:10:03 +00:00
|
|
|
|
2012-12-28 17:18:06 +00:00
|
|
|
printf("[%d] Match succeeded up to line %d, col %d (pos=%d, len=%d)",
|
|
|
|
g_recurslevel, i, col, idx, #contents)
|
2012-06-17 19:45:37 +00:00
|
|
|
g_numerrors = inf
|
2012-04-22 19:10:46 +00:00
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
-- printf("Line goes from %d to %d", bi, ei)
|
2012-06-10 18:56:21 +00:00
|
|
|
local suffix = ""
|
|
|
|
if (ei-bi > 76) then
|
|
|
|
ei = bi+76
|
|
|
|
suffix = " (...)"
|
|
|
|
end
|
2012-12-28 17:18:06 +00:00
|
|
|
printf("%s%s", string.sub(contents, bi, ei), suffix)
|
2012-04-22 19:10:46 +00:00
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
if (g_lastkwpos) then
|
|
|
|
i, col = getlinecol(g_lastkwpos)
|
|
|
|
printf("Last keyword was at line %d, col %d: %s", i, col, g_lastkw)
|
2012-04-29 20:54:06 +00:00
|
|
|
end
|
|
|
|
end
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-12-28 17:18:06 +00:00
|
|
|
addcodef("-- END %s", g_filename)
|
2012-06-10 18:56:21 +00:00
|
|
|
g_recurslevel = g_recurslevel-1
|
|
|
|
|
|
|
|
-- restore outer state
|
|
|
|
g_lastkw, g_lastkwpos = lastkw, lastkwpos
|
|
|
|
g_numerrors = (g_numerrors==inf and inf) or numerrors
|
|
|
|
g_newlineidxs = newlineidxs
|
2012-04-07 20:10:03 +00:00
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2012-12-03 18:24:33 +00:00
|
|
|
local function handle_cmdline_arg(str)
|
|
|
|
if (str:sub(1,1)=="-") then
|
|
|
|
if (#str == 1) then
|
|
|
|
printf("Warning: input from stdin not supported")
|
|
|
|
else
|
|
|
|
local ok = false
|
|
|
|
local kind = str:sub(2,2)
|
|
|
|
|
|
|
|
if (kind=="W" and #str >= 3) then
|
|
|
|
-- warnings
|
|
|
|
local val = true
|
|
|
|
local warnstr = str:sub(3)
|
|
|
|
|
|
|
|
if (#warnstr >= 4 and warnstr:sub(1,3)=="no-") then
|
|
|
|
val = false
|
|
|
|
warnstr = warnstr:sub(4)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (type(g_warn[warnstr])=="boolean") then
|
|
|
|
g_warn[warnstr] = val
|
|
|
|
ok = true
|
|
|
|
end
|
2013-01-13 16:40:32 +00:00
|
|
|
elseif (str:sub(2)=="fno") then
|
|
|
|
-- Disable printing code.
|
|
|
|
g_cgopt["no"] = true
|
2013-01-19 18:28:40 +00:00
|
|
|
ok = true
|
|
|
|
elseif (str:sub(2)=="f_incomplete") then
|
|
|
|
-- TEMP
|
|
|
|
g_cgopt["_incomplete"] = true
|
|
|
|
ok = true
|
2012-12-03 18:24:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (not ok) then
|
|
|
|
printf("Warning: Unrecognized option %s", str)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
local function reset_all()
|
|
|
|
reset_labels()
|
|
|
|
reset_gamedata()
|
|
|
|
reset_codegen()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function print_on_failure(msg)
|
|
|
|
if (g_lastkwpos ~= nil) then
|
|
|
|
printf("LAST KEYWORD POSITION: %s, %s", linecolstr(g_lastkwpos), g_lastkw)
|
|
|
|
end
|
|
|
|
print(msg)
|
|
|
|
end
|
|
|
|
|
2012-12-09 13:24:28 +00:00
|
|
|
if (string.dump) then
|
2012-12-29 15:21:24 +00:00
|
|
|
-- running stand-alone
|
2012-12-03 18:24:33 +00:00
|
|
|
|
|
|
|
local i = 1
|
|
|
|
while (arg[i]) do
|
|
|
|
if (handle_cmdline_arg(arg[i])) then
|
|
|
|
table.remove(arg, i)
|
|
|
|
else
|
|
|
|
i = i+1
|
|
|
|
end
|
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
|
|
|
|
for argi=1,#arg do
|
|
|
|
local filename = arg[argi]
|
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
reset_all()
|
2012-06-10 18:56:21 +00:00
|
|
|
|
2012-06-17 19:45:33 +00:00
|
|
|
g_directory = filename:match("(.*/)") or ""
|
2012-06-10 18:56:21 +00:00
|
|
|
filename = filename:sub(#g_directory+1, -1)
|
|
|
|
|
2012-07-01 22:11:17 +00:00
|
|
|
-- NOTE: xpcall isn't useful here since the traceback won't give us
|
|
|
|
-- anything inner to the lpeg.match call
|
2012-06-10 18:56:21 +00:00
|
|
|
local ok, msg = pcall(do_include_file, g_directory, filename)
|
2012-11-25 13:18:46 +00:00
|
|
|
-- ^v Swap commenting (comment top, uncomment bottom line) to get backtraces
|
|
|
|
-- local ok, msg = true, do_include_file(g_directory, filename)
|
2012-07-01 22:11:17 +00:00
|
|
|
|
2012-06-10 18:56:21 +00:00
|
|
|
if (not ok) then
|
2012-12-29 15:21:24 +00:00
|
|
|
print_on_failure(msg)
|
2012-06-10 18:56:21 +00:00
|
|
|
end
|
2012-07-01 22:11:17 +00:00
|
|
|
|
2013-01-13 16:40:32 +00:00
|
|
|
if (not g_cgopt["no"]) then
|
2012-12-28 17:18:06 +00:00
|
|
|
local file = require("io").stderr
|
2012-11-30 18:57:40 +00:00
|
|
|
file:write(format("-- GENERATED CODE for \"%s\":\n", filename))
|
2012-12-29 15:21:24 +00:00
|
|
|
file:write(get_code_string(g_curcode))
|
2012-11-25 13:18:46 +00:00
|
|
|
file:write("\n")
|
2012-07-01 22:11:17 +00:00
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
end
|
2012-12-29 15:21:24 +00:00
|
|
|
else
|
|
|
|
-- running from EDuke32
|
2013-01-13 16:40:07 +00:00
|
|
|
function compile(filenames)
|
2012-12-29 15:21:24 +00:00
|
|
|
-- TODO: pathsearchmode=1 set in G_CompileScripts
|
2013-01-13 16:40:07 +00:00
|
|
|
|
2012-12-29 15:21:24 +00:00
|
|
|
reset_all()
|
2013-01-13 16:40:07 +00:00
|
|
|
|
|
|
|
for _, fname in ipairs(filenames) do
|
|
|
|
local ok, msg = pcall(do_include_file, "", fname)
|
|
|
|
if (not ok) then
|
|
|
|
print_on_failure(msg)
|
|
|
|
return nil
|
|
|
|
end
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2013-01-13 16:40:07 +00:00
|
|
|
|
|
|
|
return get_code_string(g_curcode)
|
2012-12-29 15:21:24 +00:00
|
|
|
end
|
2012-05-13 16:05:16 +00:00
|
|
|
end
|