From af53f5a825ae1fa1d3f109f54d4bb6e55fc5ce2b Mon Sep 17 00:00:00 2001 From: Leonard2 Date: Wed, 6 Jul 2016 00:32:26 +0200 Subject: [PATCH] Properly use the boolean type in function declarations This will get rid of useless casts like 'if (isPointerEqual(x))' It will also allow for proper casting in parameters like using a state as a boolean which is allowed in if statements for example --- src/thingdef/thingdef_parse.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/thingdef/thingdef_parse.cpp b/src/thingdef/thingdef_parse.cpp index e4488de45..c97930277 100644 --- a/src/thingdef/thingdef_parse.cpp +++ b/src/thingdef/thingdef_parse.cpp @@ -77,7 +77,7 @@ FxExpression *ParseParameter(FScanner &sc, PClassActor *cls, PType *type, bool c sc.MustGetString(); x = new FxConstant(FSoundID(sc.String), sc); } - else if (type == TypeSInt32 || type == TypeFloat64) + else if (type == TypeBool || type == TypeSInt32 || type == TypeFloat64) { x = ParseExpression (sc, cls, constant); if (constant && !x->isConstant()) @@ -85,8 +85,12 @@ FxExpression *ParseParameter(FScanner &sc, PClassActor *cls, PType *type, bool c sc.ScriptMessage("Default parameter must be constant."); FScriptPosition::ErrorCounter++; } - // Do automatic coercion between ints and floats. - if (type == TypeSInt32) + // Do automatic coercion between bools, ints and floats. + if (type == TypeBool) + { + x = new FxBoolCast(x); + } + else if (type == TypeSInt32) { x = new FxIntCast(x); } @@ -306,6 +310,9 @@ static void ParseArgListDef(FScanner &sc, PClassActor *cls, switch (sc.TokenType) { case TK_Bool: + type = TypeBool; + break; + case TK_Int: type = TypeSInt32; break; @@ -477,8 +484,11 @@ static void ParseNativeFunction(FScanner &sc, PClassActor *cls) sc.MustGetAnyToken(); switch (sc.TokenType) { - case TK_Int: case TK_Bool: + rets.Push(TypeBool); + break; + + case TK_Int: rets.Push(TypeSInt32); break; @@ -1064,7 +1074,11 @@ static void ParseActionDef (FScanner &sc, PClassActor *cls) // check for a return value do { - if (sc.CheckToken(TK_Int) || sc.CheckToken(TK_Bool)) + if (sc.CheckToken(TK_Bool)) + { + rets.Push(TypeBool); + } + else if (sc.CheckToken(TK_Int)) { rets.Push(TypeSInt32); }