Added [elseif] support, Lua part. Also included sanity checks for missing [then] tags inside [if]

This commit is contained in:
Elvish_Hunter 2014-04-03 12:19:05 +02:00
parent c9321455b2
commit 2cff4662e2

View file

@ -339,15 +339,45 @@ local function if_while_handler(max_iter, pass, fail, cfg)
end
end
local function if_handler(cfg)
if_while_handler(1, "then", "else", cfg)
end
local function while_handler(cfg)
if_while_handler(65536, "do", nil, cfg)
end
wml_actions["if"] = if_handler
-- since if and while are Lua keywords, we can't create functions with such names
-- instead, we store the following anonymous functions directly into
-- the table, using the [] operator, rather than by using the point syntax
wml_actions["if"] = function( cfg )
-- raise error if [then] is missing
if not helper.get_child( cfg, "then" ) then
helper.wml_error "[if] missing required [then] tag"
end
if wesnoth.eval_conditional( cfg ) then -- evalutate [if] tag
for then_child in helper.child_range ( cfg, "then" ) do
handle_event_commands( then_child )
end
return -- stop after executing [then] tags
else
for elseif_child in helper.child_range ( cfg, "elseif" ) do
-- there's no point in executing [elseif] without [then]
if not helper.get_child( elseif_child, "then" ) then
helper.wml_error "[elseif] missing required [then] tag"
end
if wesnoth.eval_conditional( elseif_child ) then -- we'll evalutate the [elseif] tags one by one
for then_tag in helper.child_range( elseif_child, "then" ) do
handle_event_commands( then_tag )
end
return -- stop on first matched condition
end
end
-- no matched condition, try the [else] tags
for else_child in helper.child_range ( cfg, "else" ) do
handle_event_commands( else_child )
end
end
end
wml_actions["while"] = while_handler
function wml_actions.switch(cfg)