From 87a34ba1ff595812c9248736b7b595dbd57ed7de Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 20 Dec 2015 15:06:35 +0100 Subject: [PATCH] Refactor CBotString::LoadString --- src/CBot/CBotKeywordStrings.cpp | 109 ++++++++++++++++++++++ src/CBot/CBotKeywordStrings.h | 10 ++ src/CBot/CBotProgram.cpp | 4 +- src/CBot/CBotString.cpp | 111 ---------------------- src/CBot/CBotString.h | 17 ---- src/CBot/CBotToken.cpp | 9 +- src/CBot/CBotVar/CBotVarBoolean.cpp | 8 +- src/CBot/CBotVar/CBotVarFloat.cpp | 8 +- src/CBot/CBotVar/CBotVarInt.cpp | 9 +- src/CBot/CBotVar/CBotVarString.cpp | 11 +-- src/CBot/CMakeLists.txt | 138 ++++++++++++++-------------- 11 files changed, 208 insertions(+), 226 deletions(-) create mode 100644 src/CBot/CBotKeywordStrings.cpp create mode 100644 src/CBot/CBotKeywordStrings.h diff --git a/src/CBot/CBotKeywordStrings.cpp b/src/CBot/CBotKeywordStrings.cpp new file mode 100644 index 00000000..e1a45fa5 --- /dev/null +++ b/src/CBot/CBotKeywordStrings.cpp @@ -0,0 +1,109 @@ +#include "CBot/CBotKeywordStrings.h" + +#include + +//! \brief Keeps the string corresponding to keyword ID +// Map is filled with id-string pars that are needed for CBot language parsing +static const std::map s_keywordString = { + {ID_IF, "if"}, + {ID_ELSE, "else"}, + {ID_WHILE, "while"}, + {ID_DO, "do"}, + {ID_FOR, "for"}, + {ID_BREAK, "break"}, + {ID_CONTINUE, "continue"}, + {ID_SWITCH, "switch"}, + {ID_CASE, "case"}, + {ID_DEFAULT, "default"}, + {ID_TRY, "try"}, + {ID_THROW, "throw"}, + {ID_CATCH, "catch"}, + {ID_FINALLY, "finally"}, + {ID_TXT_AND, "and"}, + {ID_TXT_OR, "or"}, + {ID_TXT_NOT, "not"}, + {ID_RETURN, "return"}, + {ID_CLASS, "class"}, + {ID_EXTENDS, "extends"}, + {ID_SYNCHO, "synchronized"}, + {ID_NEW, "new"}, + {ID_PUBLIC, "public"}, + {ID_EXTERN, "extern"}, + {ID_STATIC, "static"}, + {ID_PROTECTED, "protected"}, + {ID_PRIVATE, "private"}, + {ID_INT, "int"}, + {ID_FLOAT, "float"}, + {ID_BOOLEAN, "boolean"}, + {ID_STRING, "string"}, + {ID_VOID, "void"}, + {ID_BOOL, "bool"}, + {ID_TRUE, "true"}, + {ID_FALSE, "false"}, + {ID_NULL, "null"}, + {ID_NAN, "nan"}, + {ID_OPENPAR, "("}, + {ID_CLOSEPAR, ")"}, + {ID_OPBLK, "{"}, + {ID_CLBLK, "}"}, + {ID_SEP, ";"}, + {ID_COMMA, ","}, + {ID_DOTS, ":"}, + {ID_DOT, "."}, + {ID_OPBRK, "["}, + {ID_CLBRK, "]"}, + {ID_DBLDOTS, "::"}, + {ID_LOGIC, "?"}, + {ID_ADD, "+"}, + {ID_SUB, "-"}, + {ID_MUL, "*"}, + {ID_DIV, "/"}, + {ID_ASS, "="}, + {ID_ASSADD, "+="}, + {ID_ASSSUB, "-="}, + {ID_ASSMUL, "*="}, + {ID_ASSDIV, "/="}, + {ID_ASSOR, "|="}, + {ID_ASSAND, "&="}, + {ID_ASSXOR, "^="}, + {ID_ASSSL, "<<="}, + {ID_ASSSR, ">>>="}, + {ID_ASSASR, ">>="}, + {ID_SL, "<<"}, + {ID_SR, ">>"}, + {ID_ASR, ">>"}, + {ID_INC, "++"}, + {ID_DEC, "--"}, + {ID_LO, "<"}, + {ID_HI, ">"}, + {ID_LS, "<="}, + {ID_HS, ">="}, + {ID_EQ, "=="}, + {ID_NE, "!="}, + {ID_AND, "&"}, + {ID_XOR, "^"}, + {ID_OR, "|"}, + {ID_LOG_AND, "&&"}, + {ID_LOG_OR, "||"}, + {ID_LOG_NOT, "!"}, + {ID_NOT, "~"}, + {ID_MODULO, "%"}, + {ID_POWER, "**"}, + {ID_ASSMODULO, "%="}, + {TX_UNDEF, "undefined"}, + {TX_NAN, "not a number"} +}; + +static const char emptyString[] = ""; + +const char* LoadString(EID id) +{ + if (s_keywordString.find(id) != s_keywordString.end()) + { + return s_keywordString.at(id); + } + else + { + return emptyString; + } +} \ No newline at end of file diff --git a/src/CBot/CBotKeywordStrings.h b/src/CBot/CBotKeywordStrings.h new file mode 100644 index 00000000..b328b787 --- /dev/null +++ b/src/CBot/CBotKeywordStrings.h @@ -0,0 +1,10 @@ +#pragma once + +#include "CBotEnums.h" + +/** + * \brief LoadString Maps given ID to its string equivalent. + * \param id Provided identifier. + * \return String if found, else NullString. + */ +const char* LoadString(EID id); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 5ad91194..80c93276 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -30,6 +30,7 @@ #include "CBot/CBotInstr/CBotFunction.h" #include "StringFunctions.h" +#include "CBotKeywordStrings.h" // Local include @@ -357,9 +358,8 @@ bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) //////////////////////////////////////////////////////////////////////////////// CBotString CBotProgram::GetErrorText(int code) { - CBotString TextError; + CBotString TextError = LoadString(static_cast(code)); - TextError.LoadString( code ); if (TextError.IsEmpty()) { char buf[100]; diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index fc1cefd8..2459f39c 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -21,104 +21,11 @@ #include "CBot/CBotString.h" -//strings management #include #include #include #include -//Map is filled with id-string pars that are needed for CBot language parsing -const std::map CBotString::s_keywordString = -{ - {ID_IF, "if"}, - {ID_ELSE, "else"}, - {ID_WHILE, "while"}, - {ID_DO, "do"}, - {ID_FOR, "for"}, - {ID_BREAK, "break"}, - {ID_CONTINUE, "continue"}, - {ID_SWITCH, "switch"}, - {ID_CASE, "case"}, - {ID_DEFAULT, "default"}, - {ID_TRY, "try"}, - {ID_THROW, "throw"}, - {ID_CATCH, "catch"}, - {ID_FINALLY, "finally"}, - {ID_TXT_AND, "and"}, - {ID_TXT_OR, "or"}, - {ID_TXT_NOT, "not"}, - {ID_RETURN, "return"}, - {ID_CLASS, "class"}, - {ID_EXTENDS, "extends"}, - {ID_SYNCHO, "synchronized"}, - {ID_NEW, "new"}, - {ID_PUBLIC, "public"}, - {ID_EXTERN, "extern"}, - {ID_STATIC, "static"}, - {ID_PROTECTED, "protected"}, - {ID_PRIVATE, "private"}, - {ID_INT, "int"}, - {ID_FLOAT, "float"}, - {ID_BOOLEAN, "boolean"}, - {ID_STRING, "string"}, - {ID_VOID, "void"}, - {ID_BOOL, "bool"}, - {ID_TRUE, "true"}, - {ID_FALSE, "false"}, - {ID_NULL, "null"}, - {ID_NAN, "nan"}, - {ID_OPENPAR, "("}, - {ID_CLOSEPAR, ")"}, - {ID_OPBLK, "{"}, - {ID_CLBLK, "}"}, - {ID_SEP, ";"}, - {ID_COMMA, ","}, - {ID_DOTS, ":"}, - {ID_DOT, "."}, - {ID_OPBRK, "["}, - {ID_CLBRK, "]"}, - {ID_DBLDOTS, "::"}, - {ID_LOGIC, "?"}, - {ID_ADD, "+"}, - {ID_SUB, "-"}, - {ID_MUL, "*"}, - {ID_DIV, "/"}, - {ID_ASS, "="}, - {ID_ASSADD, "+="}, - {ID_ASSSUB, "-="}, - {ID_ASSMUL, "*="}, - {ID_ASSDIV, "/="}, - {ID_ASSOR, "|="}, - {ID_ASSAND, "&="}, - {ID_ASSXOR, "^="}, - {ID_ASSSL, "<<="}, - {ID_ASSSR, ">>>="}, - {ID_ASSASR, ">>="}, - {ID_SL, "<<"}, - {ID_SR, ">>"}, - {ID_ASR, ">>"}, - {ID_INC, "++"}, - {ID_DEC, "--"}, - {ID_LO, "<"}, - {ID_HI, ">"}, - {ID_LS, "<="}, - {ID_HS, ">="}, - {ID_EQ, "=="}, - {ID_NE, "!="}, - {ID_AND, "&"}, - {ID_XOR, "^"}, - {ID_OR, "|"}, - {ID_LOG_AND, "&&"}, - {ID_LOG_OR, "||"}, - {ID_LOG_NOT, "!"}, - {ID_NOT, "~"}, - {ID_MODULO, "%"}, - {ID_POWER, "**"}, - {ID_ASSMODULO, "%="}, - {TX_UNDEF, "undefined"}, - {TX_NAN, "not a number"} -}; - CBotString::CBotString() { m_str = ""; @@ -202,12 +109,6 @@ void CBotString::MakeLower() boost::to_lower(m_str); } -bool CBotString::LoadString(unsigned int id) -{ - m_str = MapIdToString(static_cast(id)); - return !m_str.empty(); -} - const CBotString& CBotString::operator=(const CBotString& stringSrc) { @@ -333,15 +234,3 @@ const char* CBotString::CStr() const if (this == nullptr) return emptyString; // TODO: can this be removed? return m_str.c_str(); } - -const char * CBotString::MapIdToString(EID id) -{ - if (s_keywordString.find(id) != s_keywordString.end()) - { - return s_keywordString.at(id); - } - else - { - return emptyString; - } -} diff --git a/src/CBot/CBotString.h b/src/CBot/CBotString.h index 1628e07e..efe2d9d9 100644 --- a/src/CBot/CBotString.h +++ b/src/CBot/CBotString.h @@ -122,13 +122,6 @@ public: */ int ReverseFind(const char* lpsz); - /** - * \brief LoadString Load the string associate with the id. - * \param id The id to load. - * \return True if the id exist false otherwise. - */ - bool LoadString(unsigned int id); - /** * \brief Mid Return a part of a string from a starting index and until * the end of the string with a limited size. @@ -198,14 +191,4 @@ private: //! \brief String std::string m_str; - - //! \brief Keeps the string corresponding to keyword ID - static const std::map s_keywordString; - - /** - * \brief MapIdToString Maps given ID to its string equivalent. - * \param id Provided identifier. - * \return String if found, else NullString. - */ - static const char * MapIdToString(EID id); }; diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index e1465ad1..baef6977 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBot/CBotToken.h" +#include "CBotKeywordStrings.h" // Local include @@ -480,14 +481,14 @@ void CBotToken::LoadKeyWords() int i, n = 0; i = TokenKeyWord; //start with keywords of the language - while (s.LoadString(i)) + while (!(s = LoadString(static_cast(i))).IsEmpty()) { m_ListKeyWords.push_back(s); m_ListIdKeyWords[n++] = i++; } i = TokenKeyDeclare; //keywords of declarations - while (s.LoadString(i)) + while (!(s = LoadString(static_cast(i))).IsEmpty()) { m_ListKeyWords.push_back(s); m_ListIdKeyWords[n++] = i++; @@ -495,14 +496,14 @@ void CBotToken::LoadKeyWords() i = TokenKeyVal; //keywords of values - while (s.LoadString(i)) + while (!(s = LoadString(static_cast(i))).IsEmpty()) { m_ListKeyWords.push_back(s); m_ListIdKeyWords[n++] = i++; } i = TokenKeyOp; //operators - while (s.LoadString(i)) + while (!(s = LoadString(static_cast(i))).IsEmpty()) { m_ListKeyWords.push_back(s); m_ListIdKeyWords[n++] = i++; diff --git a/src/CBot/CBotVar/CBotVarBoolean.cpp b/src/CBot/CBotVar/CBotVarBoolean.cpp index 2ae7e6b3..7a177e09 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.cpp +++ b/src/CBot/CBotVar/CBotVarBoolean.cpp @@ -21,8 +21,8 @@ #include "CBot/CBotVar/CBotVarBoolean.h" #include "CBot/CBotEnums.h" - #include "CBot/CBotUtils.h" +#include "CBot/CBotKeywordStrings.h" // Local include @@ -98,16 +98,16 @@ CBotString CBotVarBoolean::GetValString() if ( m_binit == CBotVar::InitType::UNDEF ) { - res.LoadString(TX_UNDEF); + res = LoadString(TX_UNDEF); return res; } if ( m_binit == CBotVar::InitType::IS_NAN ) { - res.LoadString(TX_NAN); + res = LoadString(TX_NAN); return res; } - ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); + ret = LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); return ret; } diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp index d23fca46..29dbdae4 100644 --- a/src/CBot/CBotVar/CBotVarFloat.cpp +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -21,8 +21,8 @@ #include "CBot/CBotVar/CBotVarFloat.h" #include "CBot/CBotEnums.h" - #include "CBot/CBotToken.h" +#include "CBot/CBotKeywordStrings.h" #include "CBot/CBotUtils.h" @@ -99,13 +99,11 @@ CBotString CBotVarFloat::GetValString() if ( m_binit == CBotVar::InitType::UNDEF ) { - res.LoadString(TX_UNDEF); - return res; + return LoadString(TX_UNDEF); } if ( m_binit == CBotVar::InitType::IS_NAN ) { - res.LoadString(TX_NAN); - return res; + return LoadString(TX_NAN); } char buffer[300]; diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp index f584997f..52b9e5ce 100644 --- a/src/CBot/CBotVar/CBotVarInt.cpp +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -21,10 +21,9 @@ #include "CBot/CBotVar/CBotVarInt.h" #include "CBot/CBotEnums.h" - #include "CBot/CBotToken.h" - #include "CBot/CBotUtils.h" +#include "CBot/CBotKeywordStrings.h" // Local include @@ -102,14 +101,12 @@ CBotString CBotVarInt::GetValString() if ( m_binit == CBotVar::InitType::UNDEF ) { - res.LoadString(TX_UNDEF); - return res; + return LoadString(TX_UNDEF); } if ( m_binit == CBotVar::InitType::IS_NAN ) { - res.LoadString(TX_NAN); - return res; + return LoadString(TX_NAN); } char buffer[300]; diff --git a/src/CBot/CBotVar/CBotVarString.cpp b/src/CBot/CBotVar/CBotVarString.cpp index 25f39a6c..425733f5 100644 --- a/src/CBot/CBotVar/CBotVarString.cpp +++ b/src/CBot/CBotVar/CBotVarString.cpp @@ -21,10 +21,9 @@ #include "CBot/CBotVar/CBotVarString.h" #include "CBot/CBotEnums.h" - #include "CBot/CBotToken.h" - #include "CBot/CBotUtils.h" +#include "CBot/CBotKeywordStrings.h" // Local include @@ -77,15 +76,11 @@ CBotString CBotVarString::GetValString() { if ( m_binit == CBotVar::InitType::UNDEF ) { - CBotString res; - res.LoadString(TX_UNDEF); - return res; + return LoadString(TX_UNDEF); } if ( m_binit == CBotVar::InitType::IS_NAN ) { - CBotString res; - res.LoadString(TX_NAN); - return res; + return LoadString(TX_NAN); } return m_val; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 33e47afe..3f07eb5a 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,73 +1,73 @@ set(SOURCES - CBotUtils.cpp - CBotFileUtils.cpp - CBotClass.cpp - CBotProgram.cpp - CBotStack.cpp - CBotCStack.cpp - CBotString.cpp - CBotToken.cpp - CBotCall.cpp - CBotDefParam.cpp - CBotCallMethode.cpp - CBotTypResult.cpp - StringFunctions.cpp - CBotInstr/CBotInstr.cpp - CBotInstr/CBotInstrUtils.cpp - CBotInstr/CBotWhile.cpp - CBotInstr/CBotDo.cpp - CBotInstr/CBotFor.cpp - CBotInstr/CBotListExpression.cpp - CBotInstr/CBotSwitch.cpp - CBotInstr/CBotCase.cpp - CBotInstr/CBotBreak.cpp - CBotInstr/CBotTry.cpp - CBotInstr/CBotCatch.cpp - CBotInstr/CBotThrow.cpp - CBotInstr/CBotExprAlpha.cpp - CBotInstr/CBotExprNum.cpp - CBotInstr/CBotNew.cpp - CBotInstr/CBotExprNan.cpp - CBotInstr/CBotExprNull.cpp - CBotInstr/CBotExprBool.cpp - CBotInstr/CBotLeftExprVar.cpp - CBotInstr/CBotPreIncExpr.cpp - CBotInstr/CBotPostIncExpr.cpp - CBotInstr/CBotExprVar.cpp - CBotInstr/CBotInstrMethode.cpp - CBotInstr/CBotInstrCall.cpp - CBotInstr/CBotListInstr.cpp - CBotInstr/CBotBlock.cpp - CBotInstr/CBotExprUnaire.cpp - CBotInstr/CBotParExpr.cpp - CBotInstr/CBotBoolExpr.cpp - CBotInstr/CBotLogicExpr.cpp - CBotInstr/CBotTwoOpExpr.cpp - CBotInstr/CBotExpression.cpp - CBotInstr/CBotIndexExpr.cpp - CBotInstr/CBotFieldExpr.cpp - CBotInstr/CBotLeftExpr.cpp - CBotInstr/CBotCondition.cpp - CBotInstr/CBotClassInst.cpp - CBotInstr/CBotIString.cpp - CBotInstr/CBotFloat.cpp - CBotInstr/CBotBoolean.cpp - CBotInstr/CBotEmpty.cpp - CBotInstr/CBotReturn.cpp - CBotInstr/CBotIf.cpp - CBotInstr/CBotListArray.cpp - CBotInstr/CBotInstArray.cpp - CBotInstr/CBotInt.cpp - CBotInstr/CBotFunction.cpp - CBotVar/CBotVarArray.cpp - CBotVar/CBotVarPointer.cpp - CBotVar/CBotVarClass.cpp - CBotVar/CBotVarBoolean.cpp - CBotVar/CBotVarString.cpp - CBotVar/CBotVarFloat.cpp - CBotVar/CBotVarInt.cpp - CBotVar/CBotVar.cpp -) + CBotUtils.cpp + CBotFileUtils.cpp + CBotClass.cpp + CBotProgram.cpp + CBotStack.cpp + CBotCStack.cpp + CBotString.cpp + CBotToken.cpp + CBotCall.cpp + CBotDefParam.cpp + CBotCallMethode.cpp + CBotTypResult.cpp + StringFunctions.cpp + CBotInstr/CBotInstr.cpp + CBotInstr/CBotInstrUtils.cpp + CBotInstr/CBotWhile.cpp + CBotInstr/CBotDo.cpp + CBotInstr/CBotFor.cpp + CBotInstr/CBotListExpression.cpp + CBotInstr/CBotSwitch.cpp + CBotInstr/CBotCase.cpp + CBotInstr/CBotBreak.cpp + CBotInstr/CBotTry.cpp + CBotInstr/CBotCatch.cpp + CBotInstr/CBotThrow.cpp + CBotInstr/CBotExprAlpha.cpp + CBotInstr/CBotExprNum.cpp + CBotInstr/CBotNew.cpp + CBotInstr/CBotExprNan.cpp + CBotInstr/CBotExprNull.cpp + CBotInstr/CBotExprBool.cpp + CBotInstr/CBotLeftExprVar.cpp + CBotInstr/CBotPreIncExpr.cpp + CBotInstr/CBotPostIncExpr.cpp + CBotInstr/CBotExprVar.cpp + CBotInstr/CBotInstrMethode.cpp + CBotInstr/CBotInstrCall.cpp + CBotInstr/CBotListInstr.cpp + CBotInstr/CBotBlock.cpp + CBotInstr/CBotExprUnaire.cpp + CBotInstr/CBotParExpr.cpp + CBotInstr/CBotBoolExpr.cpp + CBotInstr/CBotLogicExpr.cpp + CBotInstr/CBotTwoOpExpr.cpp + CBotInstr/CBotExpression.cpp + CBotInstr/CBotIndexExpr.cpp + CBotInstr/CBotFieldExpr.cpp + CBotInstr/CBotLeftExpr.cpp + CBotInstr/CBotCondition.cpp + CBotInstr/CBotClassInst.cpp + CBotInstr/CBotIString.cpp + CBotInstr/CBotFloat.cpp + CBotInstr/CBotBoolean.cpp + CBotInstr/CBotEmpty.cpp + CBotInstr/CBotReturn.cpp + CBotInstr/CBotIf.cpp + CBotInstr/CBotListArray.cpp + CBotInstr/CBotInstArray.cpp + CBotInstr/CBotInt.cpp + CBotInstr/CBotFunction.cpp + CBotVar/CBotVarArray.cpp + CBotVar/CBotVarPointer.cpp + CBotVar/CBotVarClass.cpp + CBotVar/CBotVarBoolean.cpp + CBotVar/CBotVarString.cpp + CBotVar/CBotVarFloat.cpp + CBotVar/CBotVarInt.cpp + CBotVar/CBotVar.cpp + CBotKeywordStrings.cpp CBotKeywordStrings.h) # Includes set(LOCAL_INCLUDES