From cb1d9597f2d9c990ff291dbad6040948c9593f3f Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Mon, 29 Feb 2016 21:40:10 -0600 Subject: [PATCH] Allow `if {} else if {}` with DECORATE This is as opposed to `if {} else { if {} }`, which is what was required previously to express the same construct. --- src/thingdef/thingdef_states.cpp | 66 +++++++++++++++++++------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/thingdef/thingdef_states.cpp b/src/thingdef/thingdef_states.cpp index 50b03eb27..bf48e671d 100644 --- a/src/thingdef/thingdef_states.cpp +++ b/src/thingdef/thingdef_states.cpp @@ -441,6 +441,45 @@ static PPrototype *ReturnCheck(PPrototype *proto1, PPrototype *proto2, FScanner // //========================================================================== +static FxExpression *ParseIf(FScanner &sc, FState state, FString statestring, Baggage &bag, + PPrototype *&retproto, bool &lastwasret) +{ + FxExpression *add, *cond; + FxExpression *true_part, *false_part = NULL; + PPrototype *true_proto, *false_proto = NULL; + bool true_ret, false_ret = false; + sc.MustGetStringName("("); + cond = ParseExpression(sc, bag.Info); + sc.MustGetStringName(")"); + sc.MustGetStringName("{"); // braces are mandatory + true_part = ParseActions(sc, state, statestring, bag, true_proto, true_ret); + sc.MustGetString(); + if (sc.Compare("else")) + { + if (sc.CheckString("if")) + { + false_part = ParseIf(sc, state, statestring, bag, false_proto, false_ret); + } + else + { + sc.MustGetStringName("{"); // braces are still mandatory + false_part = ParseActions(sc, state, statestring, bag, false_proto, false_ret); + sc.MustGetString(); + } + } + add = new FxIfStatement(cond, true_part, false_part, sc); + retproto = ReturnCheck(retproto, true_proto, sc); + retproto = ReturnCheck(retproto, false_proto, sc); + // If one side does not end with a return, we don't consider the if statement + // to end with a return. If the else case is missing, it can never be considered + // as ending with a return. + if (true_ret && false_ret) + { + lastwasret = true; + } + return add; +} + FxExpression *ParseActions(FScanner &sc, FState state, FString statestring, Baggage &bag, PPrototype *&retproto, bool &endswithret) { @@ -467,32 +506,7 @@ FxExpression *ParseActions(FScanner &sc, FState state, FString statestring, Bagg lastwasret = false; if (sc.Compare("if")) { // Hangle an if statement - FxExpression *cond; - FxExpression *true_part, *false_part = NULL; - PPrototype *true_proto, *false_proto = NULL; - bool true_ret, false_ret = false; - sc.MustGetStringName("("); - cond = ParseExpression(sc, bag.Info); - sc.MustGetStringName(")"); - sc.MustGetStringName("{"); // braces are mandatory - true_part = ParseActions(sc, state, statestring, bag, true_proto, true_ret); - sc.MustGetString(); - if (sc.Compare("else")) - { - sc.MustGetStringName("{"); // braces are still mandatory - false_part = ParseActions(sc, state, statestring, bag, false_proto, false_ret); - sc.MustGetString(); - } - add = new FxIfStatement(cond, true_part, false_part, sc); - proto = ReturnCheck(proto, true_proto, sc); - proto = ReturnCheck(proto, false_proto, sc); - // If one side does not end with a return, we don't consider the if statement - // to end with a return. If the else case is missing, it can never be considered - // as ending with a return. - if (true_ret && false_ret) - { - lastwasret = true; - } + add = ParseIf(sc, state, statestring, bag, proto, lastwasret); } else if (sc.Compare("return")) { // Handle a return statement