From e56f4cb6a60bc965db2601310b946c6907418606 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Sun, 16 Jun 2024 15:04:20 +0100 Subject: [PATCH 1/7] if statements Changed: if check on vals now return true if the value has a string value %val = "test me" if(%val) will now return true since %val is not null Script side: string checks for "true" and "false" will now be parsed as integer values of 1 and 0. TEST VIGOUROUSLY --- Engine/source/console/console.h | 2 +- Engine/source/console/torquescript/CMDscan.cpp | 12 ++++++++++++ Engine/source/console/torquescript/CMDscan.l | 12 ++++++++++++ Engine/source/console/torquescript/astNodes.cpp | 16 +++++++++++++--- .../source/console/torquescript/compiledEval.cpp | 1 + 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Engine/source/console/console.h b/Engine/source/console/console.h index df921c694..8ac3f2f4c 100644 --- a/Engine/source/console/console.h +++ b/Engine/source/console/console.h @@ -245,7 +245,7 @@ public: if (type == ConsoleValueType::cvSTEntry) return s == StringTable->EmptyString() ? 0 : dAtoi(s); if (type == ConsoleValueType::cvString) - return dStrcmp(s, "") == 0 ? 0 : dAtoi(s); + return dStrcmp(s, "") == 0 ? 0 : dIsdigit(*s) ? dAtoi(s) : s == StringTable->EmptyString() ? 0 : 1; return dAtoi(getConsoleData()); } diff --git a/Engine/source/console/torquescript/CMDscan.cpp b/Engine/source/console/torquescript/CMDscan.cpp index 4497cb961..b533658f0 100644 --- a/Engine/source/console/torquescript/CMDscan.cpp +++ b/Engine/source/console/torquescript/CMDscan.cpp @@ -2827,6 +2827,18 @@ static int Sc_ScanString(int ret) if(!collapseEscape(CMDtext+1)) return -1; + const char* scannedStr = CMDtext + 1; + + if (dStrcmp(scannedStr, "true") == 0) { + CMDlval.i = MakeToken(1, yylineno); + return INTCONST; + } + + if (dStrcmp(scannedStr, "false") == 0) { + CMDlval.i = MakeToken(0, yylineno); + return INTCONST; + } + dsize_t bufferLen = dStrlen( CMDtext ); char* buffer = ( char* ) consoleAlloc( bufferLen ); dStrcpy( buffer, CMDtext + 1, bufferLen ); diff --git a/Engine/source/console/torquescript/CMDscan.l b/Engine/source/console/torquescript/CMDscan.l index aa3a72733..88246a3b5 100644 --- a/Engine/source/console/torquescript/CMDscan.l +++ b/Engine/source/console/torquescript/CMDscan.l @@ -377,6 +377,18 @@ static int Sc_ScanString(int ret) if(!collapseEscape(CMDtext+1)) return -1; + const char* scannedStr = CMDtext + 1; + + if (dStrcmp(scannedStr, "true") == 0) { + CMDlval.i = MakeToken(1, yylineno); + return INTCONST; + } + + if (dStrcmp(scannedStr, "false") == 0) { + CMDlval.i = MakeToken(0, yylineno); + return INTCONST; + } + dsize_t bufferLen = dStrlen( CMDtext ); char* buffer = ( char* ) consoleAlloc( bufferLen ); dStrcpy( buffer, CMDtext + 1, bufferLen ); diff --git a/Engine/source/console/torquescript/astNodes.cpp b/Engine/source/console/torquescript/astNodes.cpp index cc911477d..173f9ab53 100644 --- a/Engine/source/console/torquescript/astNodes.cpp +++ b/Engine/source/console/torquescript/astNodes.cpp @@ -200,7 +200,9 @@ U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip) U32 endifIp, elseIp; addBreakLine(codeStream); - if (testExpr->getPreferredType() == TypeReqUInt) + TypeReq testType = testExpr->getPreferredType(); + + if (testType == TypeReqUInt) { integer = true; } @@ -209,8 +211,16 @@ U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip) integer = false; } - ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat); - codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT); + if (testType == TypeReqString) + { + ip = testExpr->compile(codeStream, ip, TypeReqString); + codeStream.emit(OP_JMPIFNOT); + } + else + { + ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat); + codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT); + } if (elseBlock) { diff --git a/Engine/source/console/torquescript/compiledEval.cpp b/Engine/source/console/torquescript/compiledEval.cpp index 3d765c470..337c71354 100644 --- a/Engine/source/console/torquescript/compiledEval.cpp +++ b/Engine/source/console/torquescript/compiledEval.cpp @@ -1144,6 +1144,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi ip++; break; } + ip = code[ip]; break; case OP_JMPIFF: From d6a79e4f5ba0930cf1c5e49d0aaeec0526c429aa Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Sun, 16 Jun 2024 20:01:47 +0100 Subject: [PATCH 2/7] if statement treat "true" as a bool in getInt check (inside if statements for strings) no longer convert all "true" and "false" to ints --- Engine/source/console/console.h | 12 +++++++++++- Engine/source/console/torquescript/CMDscan.cpp | 12 ------------ Engine/source/console/torquescript/CMDscan.l | 12 ------------ 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Engine/source/console/console.h b/Engine/source/console/console.h index 8ac3f2f4c..279f0dffc 100644 --- a/Engine/source/console/console.h +++ b/Engine/source/console/console.h @@ -245,7 +245,17 @@ public: if (type == ConsoleValueType::cvSTEntry) return s == StringTable->EmptyString() ? 0 : dAtoi(s); if (type == ConsoleValueType::cvString) - return dStrcmp(s, "") == 0 ? 0 : dIsdigit(*s) ? dAtoi(s) : s == StringTable->EmptyString() ? 0 : 1; + { + if (dStrcmp(s, "false") == 0) { + return 0; + } + else if (dStrcmp(s, "true") == 0) { + return 1; + } + + return dIsdigit(*s) ? dAtoi(s) : s == StringTable->EmptyString() ? 0 : 1; + } + return dAtoi(getConsoleData()); } diff --git a/Engine/source/console/torquescript/CMDscan.cpp b/Engine/source/console/torquescript/CMDscan.cpp index b533658f0..4497cb961 100644 --- a/Engine/source/console/torquescript/CMDscan.cpp +++ b/Engine/source/console/torquescript/CMDscan.cpp @@ -2827,18 +2827,6 @@ static int Sc_ScanString(int ret) if(!collapseEscape(CMDtext+1)) return -1; - const char* scannedStr = CMDtext + 1; - - if (dStrcmp(scannedStr, "true") == 0) { - CMDlval.i = MakeToken(1, yylineno); - return INTCONST; - } - - if (dStrcmp(scannedStr, "false") == 0) { - CMDlval.i = MakeToken(0, yylineno); - return INTCONST; - } - dsize_t bufferLen = dStrlen( CMDtext ); char* buffer = ( char* ) consoleAlloc( bufferLen ); dStrcpy( buffer, CMDtext + 1, bufferLen ); diff --git a/Engine/source/console/torquescript/CMDscan.l b/Engine/source/console/torquescript/CMDscan.l index 88246a3b5..aa3a72733 100644 --- a/Engine/source/console/torquescript/CMDscan.l +++ b/Engine/source/console/torquescript/CMDscan.l @@ -377,18 +377,6 @@ static int Sc_ScanString(int ret) if(!collapseEscape(CMDtext+1)) return -1; - const char* scannedStr = CMDtext + 1; - - if (dStrcmp(scannedStr, "true") == 0) { - CMDlval.i = MakeToken(1, yylineno); - return INTCONST; - } - - if (dStrcmp(scannedStr, "false") == 0) { - CMDlval.i = MakeToken(0, yylineno); - return INTCONST; - } - dsize_t bufferLen = dStrlen( CMDtext ); char* buffer = ( char* ) consoleAlloc( bufferLen ); dStrcpy( buffer, CMDtext + 1, bufferLen ); From d8411b4a58b94dbbec86323821c30ff7c7479eb9 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Sun, 16 Jun 2024 20:02:57 +0100 Subject: [PATCH 3/7] Update console.h case insensitive --- Engine/source/console/console.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/source/console/console.h b/Engine/source/console/console.h index 279f0dffc..07c57d1b5 100644 --- a/Engine/source/console/console.h +++ b/Engine/source/console/console.h @@ -246,10 +246,10 @@ public: return s == StringTable->EmptyString() ? 0 : dAtoi(s); if (type == ConsoleValueType::cvString) { - if (dStrcmp(s, "false") == 0) { + if (dStricmp(s, "false") == 0) { return 0; } - else if (dStrcmp(s, "true") == 0) { + else if (dStricmp(s, "true") == 0) { return 1; } From 0d4c335231ef67ef86ed41178660ca27b8c99040 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Sun, 16 Jun 2024 23:05:42 +0100 Subject: [PATCH 4/7] test working test without scanstring changes --- Engine/source/console/console.h | 11 +---------- Engine/source/console/torquescript/astNodes.cpp | 4 ++-- Engine/source/console/torquescript/compiledEval.cpp | 8 ++++++++ Engine/source/console/torquescript/compiler.h | 1 + 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Engine/source/console/console.h b/Engine/source/console/console.h index 07c57d1b5..b3163e5c5 100644 --- a/Engine/source/console/console.h +++ b/Engine/source/console/console.h @@ -245,16 +245,7 @@ public: if (type == ConsoleValueType::cvSTEntry) return s == StringTable->EmptyString() ? 0 : dAtoi(s); if (type == ConsoleValueType::cvString) - { - if (dStricmp(s, "false") == 0) { - return 0; - } - else if (dStricmp(s, "true") == 0) { - return 1; - } - - return dIsdigit(*s) ? dAtoi(s) : s == StringTable->EmptyString() ? 0 : 1; - } + return dStrcmp(s, "") == 0 ? 0 : dAtoi(s); return dAtoi(getConsoleData()); } diff --git a/Engine/source/console/torquescript/astNodes.cpp b/Engine/source/console/torquescript/astNodes.cpp index 173f9ab53..00e2d2d67 100644 --- a/Engine/source/console/torquescript/astNodes.cpp +++ b/Engine/source/console/torquescript/astNodes.cpp @@ -211,10 +211,10 @@ U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip) integer = false; } - if (testType == TypeReqString) + if (testType == TypeReqString || testType == TypeReqNone) { ip = testExpr->compile(codeStream, ip, TypeReqString); - codeStream.emit(OP_JMPIFNOT); + codeStream.emit(OP_JMPSTRING); } else { diff --git a/Engine/source/console/torquescript/compiledEval.cpp b/Engine/source/console/torquescript/compiledEval.cpp index 337c71354..73a972c96 100644 --- a/Engine/source/console/torquescript/compiledEval.cpp +++ b/Engine/source/console/torquescript/compiledEval.cpp @@ -1163,6 +1163,14 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi } ip = code[ip]; break; + case OP_JMPSTRING: + if (stack[_STK--].getBool()) + { + ip++; + break; + } + ip = code[ip]; + break; case OP_JMPIFNOT_NP: if (stack[_STK].getInt()) { diff --git a/Engine/source/console/torquescript/compiler.h b/Engine/source/console/torquescript/compiler.h index e1cfbbed9..8d271e7fb 100644 --- a/Engine/source/console/torquescript/compiler.h +++ b/Engine/source/console/torquescript/compiler.h @@ -64,6 +64,7 @@ namespace Compiler OP_JMPIFNOT, OP_JMPIFF, OP_JMPIF, + OP_JMPSTRING, OP_JMPIFNOT_NP, OP_JMPIF_NP, // 10 OP_JMP, From 54d0da6690cc1c803fdf00cc328eeb7952f8cef0 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Tue, 18 Jun 2024 15:10:24 +0100 Subject: [PATCH 5/7] Update stringFunctions.h changes to dAtob from az --- Engine/source/core/strings/stringFunctions.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Engine/source/core/strings/stringFunctions.h b/Engine/source/core/strings/stringFunctions.h index d0c91b734..d1556f7a6 100644 --- a/Engine/source/core/strings/stringFunctions.h +++ b/Engine/source/core/strings/stringFunctions.h @@ -259,9 +259,23 @@ extern S32 dStrcmp(const UTF16 *str1, const UTF16 *str2); extern S32 dStrnatcmp( const char* str1, const char* str2 ); extern S32 dStrnatcasecmp( const char* str1, const char* str2 ); -inline bool dAtob(const char *str) +inline bool dAtob(const char* str) { - return !dStricmp(str, "true") || dAtof(str); + if (str && str[0] != '\0') + { + if (dStricmp(str, "0") == 0) + return false; + if (dStricmp(str, "0.0") == 0) + return false; + if (dStricmp(str, "0.0f") == 0) + return false; + if (dStricmp(str, "null") == 0) + return false; + if (dStricmp(str, "false") == 0) + return false; + return true; + } + return false; } bool dStrEqual(const char* str1, const char* str2); From fed83cdb8f51048186e679e00648d86d1180024b Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Tue, 18 Jun 2024 15:15:25 +0100 Subject: [PATCH 6/7] naming change enum to OP_JMPIFNOTSTRING (same name as others doing similar for different types) place case with other ifnot statements --- .../source/console/torquescript/compiledEval.cpp | 16 ++++++++-------- Engine/source/console/torquescript/compiler.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Engine/source/console/torquescript/compiledEval.cpp b/Engine/source/console/torquescript/compiledEval.cpp index 73a972c96..94ddcab80 100644 --- a/Engine/source/console/torquescript/compiledEval.cpp +++ b/Engine/source/console/torquescript/compiledEval.cpp @@ -1145,6 +1145,14 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi break; } + ip = code[ip]; + break; + case OP_JMPNOTSTRING: + if (stack[_STK--].getBool()) + { + ip++; + break; + } ip = code[ip]; break; case OP_JMPIFF: @@ -1163,14 +1171,6 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi } ip = code[ip]; break; - case OP_JMPSTRING: - if (stack[_STK--].getBool()) - { - ip++; - break; - } - ip = code[ip]; - break; case OP_JMPIFNOT_NP: if (stack[_STK].getInt()) { diff --git a/Engine/source/console/torquescript/compiler.h b/Engine/source/console/torquescript/compiler.h index 8d271e7fb..74737c773 100644 --- a/Engine/source/console/torquescript/compiler.h +++ b/Engine/source/console/torquescript/compiler.h @@ -62,9 +62,9 @@ namespace Compiler OP_JMPIFFNOT, OP_JMPIFNOT, + OP_JMPNOTSTRING, OP_JMPIFF, OP_JMPIF, - OP_JMPSTRING, OP_JMPIFNOT_NP, OP_JMPIF_NP, // 10 OP_JMP, From b0181cc56a9a7158c34a7deb7a6b01d9441410fc Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Tue, 18 Jun 2024 15:23:52 +0100 Subject: [PATCH 7/7] Update astNodes.cpp missed naming --- Engine/source/console/torquescript/astNodes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/source/console/torquescript/astNodes.cpp b/Engine/source/console/torquescript/astNodes.cpp index 00e2d2d67..17d96c961 100644 --- a/Engine/source/console/torquescript/astNodes.cpp +++ b/Engine/source/console/torquescript/astNodes.cpp @@ -214,7 +214,7 @@ U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip) if (testType == TypeReqString || testType == TypeReqNone) { ip = testExpr->compile(codeStream, ip, TypeReqString); - codeStream.emit(OP_JMPSTRING); + codeStream.emit(OP_JMPNOTSTRING); } else {