Lunatic: gasping for some air.

That is, fix some oversights introduced in the preceding runs.
listglobals.sh is a helper to find global accesses.
Translator: add "number-conversion" warning option, max. error limit.

git-svn-id: https://svn.eduke32.com/eduke32@3256 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-12-09 13:24:28 +00:00
parent 2212e4e6f8
commit 541fca6dac
4 changed files with 79 additions and 49 deletions

View File

@ -18,6 +18,8 @@ local cansee = defs_c.cansee
local neartag = defs_c.neartag
local inside = defs_c.inside
-- NOTE FOR RELEASE: the usually global stuff like "sprite" etc. ought to be
-- accessed as locals here
module(...)
@ -148,6 +150,8 @@ end
local function P_AddAmmo(ps, weap, amount)
if (not have_ammo_at_max(ps, weap)) then
local curamount = ps:get_ammo_amount(weap)
local maxamount = ps:get_max_ammo_amount(weap)
-- NOTE: no clamping towards the bottom
ps:set_ammo_amount(weap, math.min(curamount+amount, maxamount))
end
@ -156,7 +160,7 @@ end
local function P_AddWeaponAmmoCommon(ps, weap, amount)
P_AddAmmo(ps, weap, amount)
if (ps.curr_weapon==KNEE_WEAPON and have_weapon(weap)) then
if (ps.curr_weapon==ffiC.KNEE_WEAPON and have_weapon(weap)) then
ffiC.P_AddWeaponMaybeSwitch(ps, weap);
end
end
@ -281,11 +285,11 @@ end
-- The return value is true iff the ammo was at the weapon's max.
-- In that case, no action is taken.
function _addammo(ps, weapon, amount)
function _addammo(ps, weap, amount)
return have_ammo_at_max(ps, weap) or P_AddWeaponAmmoCommon(ps, weap, amount)
end
function _addweapon(ps, weapon, amount)
function _addweapon(ps, weap, amount)
if (weap >= ffiC.MAX_WEAPONS+0ULL) then
error("Invalid weapon ID "..weap, 2)
end

View File

@ -1,8 +1,7 @@
-- INTERNAL
-- definitions of BUILD and game types for the Lunatic Interpreter
_EDUKE32_LUNATIC = true
local require = require
local ffi = require("ffi")
local ffiC = ffi.C
@ -10,7 +9,22 @@ local bit = bit
local string = string
local table = table
local math = math
local assert = assert
local error = error
local ipairs = ipairs
local loadstring = loadstring
local pairs = pairs
local rawget = rawget
local rawset = rawset
local setmetatable = setmetatable
local setfenv = setfenv
local tonumber = tonumber
local type = type
local print = print
local tostring = tostring
--== First, load the definitions common to the game's and editor's Lua interface.
@ -43,29 +57,13 @@ end
-- load the common definitions!
local defs_c = require("defs_common")
local cansee = defs_c.cansee
local strip_const = defs_c.strip_const
local setmtonce = defs_c.setmtonce
---=== EDuke32 game definitions ===---
-- array -> element flattening inside structs,
-- e.g. int32_t a[4] --> int32_t _a0, _a1, _a2, _a3;
local function repeat_n_elts(type, namebase, num)
local strbuf = { "const "..type.." " }
for i=1,num do
local str = namebase..tostring(i-1)
if (i < num) then
str = str..","
end
strbuf[#strbuf+1] = str
end
strbuf[#strbuf+1] = ";"
return table.concat(strbuf)
end
---- game structs ----
ffi.cdef[[
enum dukeinv_t {
@ -449,17 +447,6 @@ int32_t kread(int32_t handle, void *buffer, int32_t leng);
]]
local assert = assert
local error = error
local ipairs = ipairs
local loadstring = loadstring
local pairs = pairs
local rawget = rawget
local rawset = rawset
local setmetatable = setmetatable
local setfenv = setfenv
local type = type
-- http://lua-users.org/wiki/SandBoxes says "potentially unsafe"
-- as it allows to see implementations of functions.
--local string_dump = string.dump
@ -614,7 +601,7 @@ local actor_mt = {
local oa = a
a = ffi.cast(actor_ptr_ct, a)
if (type(mov)=="string") then
if (type(ai)=="string") then
ai = AI[ai];
end
-- TODO: literal number AIs?
@ -634,7 +621,7 @@ local actor_mt = {
has_ai = function(a, ai)
a = ffi.cast(actor_ptr_ct, a)
if (type(mov)=="string") then
if (type(ai)=="string") then
ai = AI[ai];
end
if (ffi.istype(con_ai_ct, ai)) then
@ -957,7 +944,6 @@ G_._VERSION = _VERSION
G_._G = G_
--- non-default data and functions
G_._EDUKE32_LUNATIC = _EDUKE32_LUNATIC
G_.gameevent = gameevent -- included in lunatic.c
G_.gameactor = gameactor -- included in lunatic.c
G_.player = player -- from above

View File

@ -0,0 +1,12 @@
#!/bin/sh
if [ -z "$1" ]; then
# S or G can be used to display SETGLOBALs or GETGLOBALs, respectively
echo "Usage: $0 <file.lua> [S|G]"
exit 1;
fi
# Strip LuaJIT specific syntax
sed -r -e "s/[0-9]+U?LL/0/g" "$1" | luac -p -l - | grep "$2ETGLOBAL" |
# mark where the new module environment starts
sed -e "s/; module/; module ____________________/"

View File

@ -1,13 +1,31 @@
-- LunaCON CON to Lunatic translator
-- requires LPeg, http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html
local require = require
local lpeg = require("lpeg")
if (not _EDUKE32_LUNATIC) then
local bit = require("bit")
local math = require("math")
local string = require("string")
local table = require("table")
local arg = arg
local assert = assert
local pairs = pairs
local pcall = pcall
local print = print
local tonumber = tonumber
local type = type
if (string.dump) then
require("strict")
end
module(...)
-- 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
-- in CON isn't pretty either (though sometimes necessary because nested switches
@ -48,11 +66,13 @@ local g_badids = {} -- maps bad id strings to 'true'
local g_recurslevel = -1 -- 0: base CON file, >0 included
local g_filename = "???"
local g_directory = "" -- with trailing slash if not empty
local g_maxerrors = 20
local g_numerrors = 0
-- Warning options. Key names are the same as cmdline options, e.g.
-- -Wno-bad-identifier for disabling the "bad identifier" warning.
local g_warn = { ["not-redefined"]=true, ["bad-identifier"]=true, }
local g_warn = { ["not-redefined"]=true, ["bad-identifier"]=true,
["number-conversion"]=true, }
-- How many 'if' statements are following immediately each other,
-- needed to cope with CONs dangling-else resolution
@ -113,8 +133,17 @@ local function linecolstr(pos)
return format("%d:%d", line, col)
end
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
local function perrprintf(pos, fmt, ...)
printf("%s %s: error: "..fmt, g_filename, linecolstr(pos), ...)
increment_numerrors()
end
local function errprintf(fmt, ...)
@ -123,7 +152,7 @@ local function errprintf(fmt, ...)
else
printf("%s ???: error: "..fmt, g_filename, ...)
end
g_numerrors = g_numerrors+1
increment_numerrors()
end
local function pwarnprintf(pos, fmt, ...)
@ -145,7 +174,9 @@ local function parse_number(pos, numstr)
perrprintf(pos, "number %s out of the range of a 32-bit integer", numstr)
num = NaN
elseif (num >= 0x80000000 and numstr:sub(1,2):lower()~="0x") then
if (g_warn["number-conversion"]) then
pwarnprintf(pos, "number %s converted to a negative one", numstr)
end
num = num-(0xffffffff+1)
end
@ -306,7 +337,7 @@ local function parse(contents) end -- fwd-decl
local function do_include_file(dirname, filename) end
local function cmd_include(filename) end
if (_EDUKE32_LUNATIC) then
if (not string.dump) then
-- NOT IMPLEMENTED
else
function do_include_file(dirname, filename)
@ -1064,13 +1095,13 @@ local Cif = {
ifcount = cmd(D)
/ ACS":get_count()==%1",
ifactor = cmd(D)
/ SPS".picnum==%d",
/ SPS".picnum==%1",
ifstrength = cmd(D)
/ SPS".extra<=%d",
/ SPS".extra<=%1",
ifspawnedby = cmd(D)
/ ACS".picnum==%d",
/ ACS".picnum==%1",
ifwasweapon = cmd(D)
/ ACS".picnum==%d",
/ ACS".picnum==%1",
ifgapzl = cmd(D) -- factor into a con.* function?
/ format("_bit.arshift(%s-%s,8)<%%1", ACS".floorz", ACS".ceilingz"),
iffloordistl = cmd(D)
@ -1158,7 +1189,6 @@ local Cif = {
----==== Tracing and reporting ====----
local string = require("string")
-- g_newlineidxs will contain the 1-based file offsets to "\n" characters
local g_newlineidxs = {}
@ -1492,8 +1522,6 @@ local Grammar = Pat{
}
local math = require("math")
local function setup_newlineidxs(contents)
local newlineidxs = {}
for i in string.gmatch(contents, "()\n") do
@ -1637,7 +1665,7 @@ local function handle_cmdline_arg(str)
end
end
if (not _EDUKE32_LUNATIC) then
if (string.dump) then
--- stand-alone
local i = 1