From b42c1376e5efa8def4c6e3621f6a69714102c5b9 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 17:49:25 +0100 Subject: [PATCH 01/91] Move CBotToken class in its own header and source files. --- src/CBot/CBotDll.h | 152 +--------------------- src/CBot/CBotToken.cpp | 78 ++++++------ src/CBot/CBotToken.h | 276 +++++++++++++++++++++++++++++++++++++--- src/CBot/CMakeLists.txt | 22 ++-- src/script/script.cpp | 1 + 5 files changed, 310 insertions(+), 219 deletions(-) diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 57ee15b7..dbeaf8f8 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -26,8 +26,13 @@ #pragma once -#include +// Modules inlcude #include "resource.h" + +// Local include + +// Global include +#include #include #include @@ -990,151 +995,6 @@ public: }; -#define MAXDEFNUM 1000 // limited number of DefineNum - -///////////////////////////////////////////////////////////////////////////////////// -// Token management (tokens) - -#define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord) -#define TokenTypNum 2 // number -#define TokenTypString 3 // string -#define TokenTypVar 4 // a variable name -#define TokenTypDef 5 // value according DefineNum - -#define TokenKeyWord 2000 // keywords of the language -#define TokenKeyDeclare 2100 // keywords of declarations (int, float,..) -#define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan) -#define TokenKeyOp 2300 // operators - -/** - * \class CBotToken - * Responsible for token management - */ -class CBotToken -{ -private: - static - CBotStringArray m_ListKeyWords; // list of keywords of language - static - int m_ListIdKeyWords[200]; // the corresponding codes - - static - CBotStringArray m_ListKeyDefine; // names defined by a DefineNum - static - long m_ListKeyNums[MAXDEFNUM]; // the ​​associated values - -private: - CBotToken* m_next; // following in the list - CBotToken* m_prev; - int m_type; // type of Token - long m_IdKeyWord; // number of the keyword if it is a - // or value of the "define" - - CBotString m_Text; // word found as token - CBotString m_Sep; // following separators - - int m_start; // position in the original text (program) - int m_end; // the same for the end of the token - - /** - * \brief Check whether given parameter is a keyword - */ - static - int GetKeyWords(const char* w); // is it a keyword? - static - bool GetKeyDefNum(const char* w, CBotToken* &token); - - /** - * \brief Loads the list of keywords - */ - static - void LoadKeyWords(); - -public: - /** - * \brief Constructors - */ - CBotToken(); - CBotToken(const CBotToken* pSrc); - CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0); - CBotToken(const char* mot, const char* sep = nullptr); - - /** - * \brief Destructor - */ - ~CBotToken(); - /** - * \brief Returns the type of token - */ - int GetType(); - - /** - * \brief makes the string corresponding to this token - */ - CBotString& GetString(); - - /** - * \brief makes the following separator token - */ - CBotString& GetSep(); - - /** - * \brief position of the beginning in the text - */ - int GetStart(); - /** - * \brief end position in the text - */ - int GetEnd(); - - /** - * \brief gives the next token in the list - */ - CBotToken* GetNext(); - /** - * \brief gives the previous token in a list - */ - CBotToken* GetPrev(); - - /** - * \brief transforms the entire program - */ - static - CBotToken* CompileTokens(const char* p, int& error); - - /** - * \brief releases the list - */ - static - void Delete(CBotToken* pToken); // libère la liste - - - // fonctions non utiles en export - static - bool DefineNum(const char* name, long val); - void SetString(const char* name); - - void SetPos(int start, int end); - long GetIdKey(); - /** - * \brief adds a token (a copy) - */ - void AddNext(CBotToken* p); - - /** - * finds the next token - */ - static - CBotToken* NextToken(char* &program, int& error, bool first = false); - - const CBotToken& - operator=(const CBotToken& src); - - static - void Free(); -}; - - /* //////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index 12529467..2b06c0ff 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -17,24 +17,21 @@ * along with this program. If not, see http://gnu.org/licenses */ - -////////////////////////////////////////////////////////////////// -// Managing Tokens -// the text of a program is first transformed -// into a sequence of tokens for easy interpretation -// it will only treat the case as an error -// where there is an illegal character in a string - - +// Modules inlcude #include "CBot.h" + +// Local include + +// Global include #include +//////////////////////////////////////////////////////////////////////////////// CBotStringArray CBotToken::m_ListKeyWords; int CBotToken::m_ListIdKeyWords[200]; CBotStringArray CBotToken::m_ListKeyDefine; long CBotToken::m_ListKeyNums[MAXDEFNUM]; -//! contructors +//////////////////////////////////////////////////////////////////////////////// CBotToken::CBotToken() { m_next = nullptr; @@ -43,6 +40,7 @@ CBotToken::CBotToken() m_IdKeyWord = -1; } +//////////////////////////////////////////////////////////////////////////////// CBotToken::CBotToken(const CBotToken* pSrc) { m_next = nullptr; @@ -71,6 +69,7 @@ CBotToken::CBotToken(const CBotToken* pSrc) } } +//////////////////////////////////////////////////////////////////////////////// CBotToken::CBotToken(const CBotString& mot, const CBotString& sep, int start, int end) { m_Text = mot; // word (mot) found as token @@ -95,17 +94,20 @@ CBotToken::CBotToken(const char* mot, const char* sep) m_IdKeyWord = -1; } +//////////////////////////////////////////////////////////////////////////////// CBotToken::~CBotToken() { delete m_next; // recursive m_next = nullptr; } +//////////////////////////////////////////////////////////////////////////////// void CBotToken::Free() { m_ListKeyDefine.SetSize(0); } +//////////////////////////////////////////////////////////////////////////////// const CBotToken& CBotToken::operator=(const CBotToken& src) { delete m_next; @@ -123,7 +125,7 @@ const CBotToken& CBotToken::operator=(const CBotToken& src) return *this; } - +//////////////////////////////////////////////////////////////////////////////// int CBotToken::GetType() { if (this == nullptr) return 0; @@ -131,69 +133,66 @@ int CBotToken::GetType() return m_type; } +//////////////////////////////////////////////////////////////////////////////// long CBotToken::GetIdKey() { return m_IdKeyWord; } +//////////////////////////////////////////////////////////////////////////////// CBotToken* CBotToken::GetNext() { if (this == nullptr) return nullptr; return m_next; } +//////////////////////////////////////////////////////////////////////////////// CBotToken* CBotToken::GetPrev() { if (this == nullptr) return nullptr; return m_prev; } -void CBotToken::AddNext(CBotToken* p) -{ - CBotToken* n = new CBotToken(p); - CBotToken* pt = this; - - while ( pt->m_next != nullptr ) pt = pt->m_next; - - pt->m_next = n; - n->m_prev = pt; -} - - -CBotString& CBotToken::GetString() +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotToken::GetString() { return m_Text; } -CBotString& CBotToken::GetSep() +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotToken::GetSep() { return m_Sep; } +//////////////////////////////////////////////////////////////////////////////// void CBotToken::SetString(const char* name) { m_Text = name; } - +//////////////////////////////////////////////////////////////////////////////// int CBotToken::GetStart() { if (this == nullptr) return -1; return m_start; } +//////////////////////////////////////////////////////////////////////////////// int CBotToken::GetEnd() { if (this == nullptr) return -1; return m_end; } +//////////////////////////////////////////////////////////////////////////////// void CBotToken::SetPos(int start, int end) { m_start = start; m_end = end; } +//////////////////////////////////////////////////////////////////////////////// bool CharInList(const char c, const char* list) { int i = 0; @@ -205,6 +204,7 @@ bool CharInList(const char c, const char* list) } } +//////////////////////////////////////////////////////////////////////////////// bool Char2InList(const char c, const char cc, const char* list) { int i = 0; @@ -225,11 +225,7 @@ static char num[] = "0123456789"; // point (single) i static char hexnum[] = "0123456789ABCDEFabcdef"; static char nch[] = "\"\r\n\t"; // forbidden in chains -//static char* duo = "+=-=*=/===!=<=>=++--///**/||&&"; // double operators - -// looking for the next token in a sentence -// do not start with separators -// which are made in the previous token +//////////////////////////////////////////////////////////////////////////////// CBotToken* CBotToken::NextToken(char* &program, int& error, bool first) { CBotString mot; // the word which is found @@ -389,6 +385,7 @@ bis: } } +//////////////////////////////////////////////////////////////////////////////// CBotToken* CBotToken::CompileTokens(const char* program, int& error) { CBotToken *nxt, *prv, *tokenbase; @@ -431,14 +428,13 @@ CBotToken* CBotToken::CompileTokens(const char* program, int& error) return tokenbase; } +//////////////////////////////////////////////////////////////////////////////// void CBotToken::Delete(CBotToken* pToken) { delete pToken; } - -// search if a word is part of the keywords - +//////////////////////////////////////////////////////////////////////////////// int CBotToken::GetKeyWords(const char* w) { int i; @@ -458,6 +454,7 @@ int CBotToken::GetKeyWords(const char* w) return -1; } +//////////////////////////////////////////////////////////////////////////////// bool CBotToken::GetKeyDefNum(const char* w, CBotToken* &token) { int i; @@ -476,12 +473,7 @@ bool CBotToken::GetKeyDefNum(const char* w, CBotToken* &token) return false; } - -/// \todo Fixme Figure out how this should work. - -// recreates the list of keywords and its IDs basing on some resources -// defines of TokenKey.. are in CBotDll.h - +//////////////////////////////////////////////////////////////////////////////// void CBotToken::LoadKeyWords() { CBotString s; @@ -517,6 +509,7 @@ void CBotToken::LoadKeyWords() } } +//////////////////////////////////////////////////////////////////////////////// bool CBotToken::DefineNum(const char* name, long val) { int i; @@ -533,6 +526,7 @@ bool CBotToken::DefineNum(const char* name, long val) return true; } +//////////////////////////////////////////////////////////////////////////////// bool IsOfType(CBotToken* &p, int type1, int type2) { if (p->GetType() == type1 || @@ -543,8 +537,8 @@ bool IsOfType(CBotToken* &p, int type1, int type2) } return false; } -// Same with any number of arguments -// There must be a zero as the last argument + +//////////////////////////////////////////////////////////////////////////////// bool IsOfTypeList(CBotToken* &p, int type1, ...) { int i = type1; diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index 3e0269ec..b82cdfea 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -17,28 +17,264 @@ * along with this program. If not, see http://gnu.org/licenses */ - -// interpreter of the lanuage CBot for game COLOBOT -// writing a program is first transformed into a list of tokens -// before tackling the compiler itself -// for example -// int var = 3 * ( pos.y + x ) -// is decomposed into (each line is a token) -// int -// var -// = -// 3 -// * -// ( -// pos.y -// + -// x -// ) - #pragma once -class CBotToken; +// Modules inlcude +#include "CBotDll.h" +// Local include + +// Global include + +///////////////////////////////////////////////////////////////////////////////////// +// Token management (tokens) + +#define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord) +#define TokenTypNum 2 // number +#define TokenTypString 3 // string +#define TokenTypVar 4 // a variable name +#define TokenTypDef 5 // value according DefineNum + +#define TokenKeyWord 2000 // keywords of the language +#define TokenKeyDeclare 2100 // keywords of declarations (int, float,..) +#define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan) +#define TokenKeyOp 2300 // operators + +#define MAXDEFNUM 1000 // limited number of DefineNum + +/** + * \class CBotToken + * Responsible for token management. A CBot program is a text string. This string + * is first transformed into a list of token. It will only treat the case as an + * error where there is an illegal character in a string. + * For example : + * int var = 3 * ( pos.y + x ) + * is decomposed into (each line is a token) + * int + * var + * = + * 3 + * * + * ( + * pos.y + * + + * x + * ) + */ + +class CBotToken +{ + +public: + + /*! + * \brief CBotToken Default Constructor. + */ + CBotToken(); + CBotToken(const CBotToken* pSrc); + CBotToken(const CBotString& mot, + const CBotString& sep, + int start=0, + int end=0); + CBotToken(const char* mot, const char* sep = nullptr); + + /*! + * \brief ~CBotToken Destructor. Be careful when you delete a CBotToken that + * is in a linked list all the following CBotToken will be deleted too. + */ + ~CBotToken(); + + /*! + * \brief GetType Return the token type or the keyword id. + * \return The token type or the keyword id. + */ + int GetType(); + + /*! + * \brief GetString Return the token string. + * \return The token string if a string has been set. An empty string + * otherwise. + */ + CBotString GetString(); + + /*! + * \brief SetString Set the token string. + * \param [in] name The new string to set. + */ + void SetString(const char* name); + + /*! + * \brief GetSep Return the token separator. + * \return The token separator a separator has been set. An empty separator + * otherwise. + */ + CBotString GetSep(); + + /*! + * \brief GetStart Return the start position of the string token in the + * original CBotProgram. + * \return The start position of the string token or -1 if no string has + * been set. + */ + int GetStart(); + + /*! + * \brief GetEnd Return the end position of the string token in the + * original CBotProgram. + * \return The start position of the string token or -1 if no string has + * been set. + */ + int GetEnd(); + + /*! + * \brief GetNext Gives the next CBotToken in the list. + * \return The next CBotToken if set nullptr otherwise. + */ + CBotToken* GetNext(); + + /*! + * \brief GetPrev Gives the previous CBotToken in the list. + * \return The previous CBotToken if set nullptr otherwise. + */ + CBotToken* GetPrev(); + + /*! + * \brief SetPos Set the token position in the CBot program. + * \param [in] start The start position of the token. + * \param [in] end The end position of the token. + */ + void SetPos(int start, int end); + + /*! + * \brief GetIdKey Get the token id key. + * \return The id key. + */ + long GetIdKey(); + + /*! + * \brief operator = + * \param [in] src The CBotToken to copy. + * \return The CBotToken with the copied value. + */ + const CBotToken& operator=(const CBotToken& src); + + /*! + * \brief CompileTokens This function will transform the entire CBot program + * in CBotToken. If an error occured during the processing an error number + * will be set. Each CBotToken will be linked with the previous one and the + * next one. + * \param [in] p The program string. + * \param [out] error The error code. + * \return The first token of the linked liste. + * \todo Replace the error code by an enum. + */ + static CBotToken* CompileTokens(const char* p, int& error); + + /*! + * \brief NextToken Looking for the next token in the string. The string must + * not start with separators. The separator is part of the previous token. + * \param [in] program The program string. + * \param [out] error The error code. + * \param [in] first True if this is the first call false othewise. + * \return A CBotTOken. + */ + static CBotToken* NextToken(char* &program, int& error, bool first = false); + + /** + * \brief Delete Releases the CBotToken linked list. + * \deprecated This function is deprecated because it only delete a pointer. + * \todo Remove this function. + */ + static void Delete(CBotToken* pToken); + + /*! + * \brief DefineNum This function define a language keyword with an associated + * number. + * \param [in] name The new word to define. + * \param [in] val The number associated with the keyword. + * \return Ture if the number is available false oterhwise. + */ + static bool DefineNum(const char* name, long val); + + /*! + * \brief Free Free the array created with DefineNum. + * \see DefineNum + */ + static void Free(); + +private: + + //! The next token in the linked list + CBotToken* m_next; // following in the list + //! The previous token in the linked list + CBotToken* m_prev; + //! The token type + int m_type; // type of Token + //! The id of the keyword + long m_IdKeyWord; + + //! The token string + CBotString m_Text; + //! The token separator + CBotString m_Sep; + + //! The strat position of the token in the CBotProgram + int m_start; + //! The end position of the token in the CBotProgram + int m_end; + + /*! + * \brief GetKeyWords Check if the word is a keyword. + * \param w The word to compare. + * \return -1 if this is not a keyword the keyword number otherwise. + */ + static int GetKeyWords(const char* w); // is it a keyword? + + /*! + * \brief GetKeyDefNum Check if this is a defined word and set the defined + * word type in a CBotToken. + * \param [in] w The word to compaire. + * \param [out] token The token in which the type will be set. + * \return True if the defined word is found false otherwise. + */ + static bool GetKeyDefNum(const char* w, CBotToken* &token); + + /*! + * \brief LoadKeyWords Loads the list of keywords. The list of keyword is + * CBotString::s_keywordString. This keywords are keywords languages (if, +, + * for, while, case, extern ...) + * \todo Fixme Figure out how this should work. + */ + static void LoadKeyWords(); + + //! List of keywords of the CBot language (if, +, for, while, case, extern ...) + static CBotStringArray m_ListKeyWords; + //! List of id correponding to the keywords of the CBot language + static int m_ListIdKeyWords[200]; + + //! List of CBot language error and list of colobot specific keywords + //! This keywords are defined in : + //! - void CScriptFunctions::Init() + //! - void CBotProgram::Init() + static CBotStringArray m_ListKeyDefine; + //! List of id correponding to the defined words + static long m_ListKeyNums[MAXDEFNUM]; + +}; + +/*! + * \brief IsOfType This function tell if a token is of a specific type. + * \param [in] p The token to compare. + * \param [in] type1 First token type to comapre to the token. + * \param [in] type2 Second token type to comapre to the token. + * \return True if the type of the token match one of a parameter. + */ extern bool IsOfType(CBotToken* &p, int type1, int type2 = -1); -extern bool IsOfTypeList(CBotToken* &p, int type1, ...); +/*! + * \brief IsOfType This function tell if a token is of a specific type. + * \param [in] p The token to compare. + * \param [in] type1 The list of token type to comapre to the token. + * \return True if the type of the token match one of a parameter. + */ +extern bool IsOfTypeList(CBotToken* &p, int type1, ...); diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 8bb8a5cf..dc7707f7 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,15 +1,15 @@ set(SOURCES -CBot.cpp -CBotClass.cpp -CBotFunction.cpp -CBotIf.cpp -CBotProgram.cpp -CBotStack.cpp -CBotString.cpp -CBotToken.cpp -CBotTwoOpExpr.cpp -CBotVar.cpp -CBotWhile.cpp + CBot.cpp + CBotClass.cpp + CBotFunction.cpp + CBotIf.cpp + CBotProgram.cpp + CBotStack.cpp + CBotString.cpp + CBotToken.cpp + CBotTwoOpExpr.cpp + CBotVar.cpp + CBotWhile.cpp ) if(CBOT_STATIC) diff --git a/src/script/script.cpp b/src/script/script.cpp index 9bfb1dac..4ba1f173 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -42,6 +42,7 @@ #include "ui/controls/interface.h" #include "ui/controls/list.h" +#include "CBot/CBotToken.h" const int CBOT_IPF = 100; // CBOT: default number of instructions / frame From 573e1152e339f5d018464a473340980a3d8dbb8b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 18:11:42 +0100 Subject: [PATCH 02/91] Moving CBOT enumerated in a separate file called CBotEnum.h --- src/CBot/CBotDll.h | 33 +----------------------- src/CBot/CBotEnums.h | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 src/CBot/CBotEnums.h diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index dbeaf8f8..deec62cb 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -28,6 +28,7 @@ // Modules inlcude #include "resource.h" +#include "CBotEnums.h" // Local include @@ -59,29 +60,6 @@ class CBotCStack; // stack //////////////////////////////////////////////////////////////////////// // Variables management //////////////////////////////////////////////////////////////////////// - -/** \brief CBotType Defines known types. This types are modeled on Java types. Do not change the order of elements */ -enum CBotType -{ - CBotTypVoid = 0, - CBotTypByte = 1, //n - CBotTypShort = 2, //n - CBotTypChar = 3, //n - CBotTypInt = 4, - CBotTypLong = 5, //n - CBotTypFloat = 6, - CBotTypDouble = 7, //n - CBotTypBoolean = 8, - CBotTypString = 9, - - CBotTypArrayPointer = 10, // array of variables - CBotTypArrayBody = 11, // same but creates an instance - - CBotTypPointer = 12, // pointer to an instance - CBotTypNullPointer = 13, // null pointer is special - CBotTypClass = 15, - CBotTypIntrinsic = 16 // instance of a class intrinsic -}; //n = not implemented yet // for SetUserPtr when deleting an object @@ -462,15 +440,6 @@ public: CBotString& ElementAt(int nIndex); }; -// different modes for GetPosition -enum CBotGet -{ - GetPosExtern = 1, - GetPosNom = 2, - GetPosParam = 3, - GetPosBloc = 4 -}; - //////////////////////////////////////////////////////////////////// // main class managing CBot program // diff --git a/src/CBot/CBotEnums.h b/src/CBot/CBotEnums.h new file mode 100644 index 00000000..524f00c1 --- /dev/null +++ b/src/CBot/CBotEnums.h @@ -0,0 +1,60 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude + +// Local include + +// Global include + +/*! \brief CBotType Defines known types. This types are modeled on Java types. + * Do not change the order of elements + */ +enum CBotType +{ + CBotTypVoid = 0, + CBotTypByte = 1, //n + CBotTypShort = 2, //n + CBotTypChar = 3, //n + CBotTypInt = 4, + CBotTypLong = 5, //n + CBotTypFloat = 6, + CBotTypDouble = 7, //n + CBotTypBoolean = 8, + CBotTypString = 9, + + CBotTypArrayPointer = 10, // array of variables + CBotTypArrayBody = 11, // same but creates an instance + + CBotTypPointer = 12, // pointer to an instance + CBotTypNullPointer = 13, // null pointer is special + CBotTypClass = 15, + CBotTypIntrinsic = 16 // instance of a class intrinsic +}; + +//! \brief CBotGet Different modes for GetPosition. +enum CBotGet +{ + GetPosExtern = 1, + GetPosNom = 2, + GetPosParam = 3, + GetPosBloc = 4 +}; From 1694776ab62658b589df0f89ef4b8192b9b64802 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 18:29:02 +0100 Subject: [PATCH 03/91] Move CBotDo class in its own header and source files. --- src/CBot/CBot.cpp | 6 +- src/CBot/CBot.h | 16 ---- src/CBot/CBotInstr/CBotDo.cpp | 154 ++++++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotDo.h | 73 ++++++++++++++++ src/CBot/CBotWhile.cpp | 134 ----------------------------- src/CBot/CMakeLists.txt | 9 ++ 6 files changed, 241 insertions(+), 151 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotDo.cpp create mode 100644 src/CBot/CBotInstr/CBotDo.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index f4727d33..375c1f39 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -34,9 +34,13 @@ // the error is then on the stack CBotCStack :: Isok () is false - +// Modules inlcude #include "CBot.h" +#include "CBotInstr/CBotDo.h" +// Local include + +// Global include #include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 6f37b8b5..60e1d88c 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -483,22 +483,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotDo : public CBotInstr -{ -private: - CBotInstr* m_Block; // instruction - CBotInstr* m_Condition; // conditions - CBotString m_label; // a label if there is - -public: - CBotDo(); - ~CBotDo(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - class CBotFor : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp new file mode 100644 index 00000000..a24e5dbe --- /dev/null +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -0,0 +1,154 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotDo.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotDo::CBotDo() +{ + m_Condition = + m_Block = nullptr; // nullptr so that delete is not possible further + name = "CBotDo"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotDo::~CBotDo() +{ + delete m_Condition; // frees the condition + delete m_Block; // frees the instruction block +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotDo* inst = new CBotDo(); // creates the object + + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GetString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_DO)) return nullptr; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + + // looking for a statement block after the do + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + + if ( pStk->IsOk() ) + { + if (IsOfType(p, ID_WHILE)) + { + if ( nullptr != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) + { + // the condition exists + if (IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError(TX_ENDOF, p->GetStart()); + } + } + pStk->SetError(TX_WHILE, p->GetStart()); + } + + delete inst; // error, frees up + return pStack->Return(nullptr, pStk); // no object, the error is on the stack +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotDo :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // adds an item to the stack + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GetState() ) // executes the loop + { // there are two possible states (depending on recovery) + case 0: + // evaluates the associated statement block + if ( m_Block != nullptr && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + if (!pile->SetState(1)) return false; // ready for further + + case 1: + // evaluates the condition + if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GetVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + // returns to instruction block to start + if (!pile->SetState(0, 0)) return false; + continue; + } +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack + if ( pile == nullptr ) return; + + switch( pile->GetState() ) + { // there are two possible states (depending on recovery) + case 0: + // restores the assosiated statement's block + if ( m_Block != nullptr ) m_Block->RestoreState(pile, bMain); + return; + + case 1: + // restores the condition + m_Condition->RestoreState(pile, bMain); + return; + } +} diff --git a/src/CBot/CBotInstr/CBotDo.h b/src/CBot/CBotInstr/CBotDo.h new file mode 100644 index 00000000..670a0701 --- /dev/null +++ b/src/CBot/CBotInstr/CBotDo.h @@ -0,0 +1,73 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +class CBotDo : public CBotInstr +{ +public: + + /*! + * \brief CBotDo Default constructor. + */ + CBotDo(); + + /*! + * \brief ~CBotDo Destructor. + */ + ~CBotDo(); + + /*! + * \brief Compile Compile the instruction "do". + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Instruction + CBotInstr* m_Block; + //! Conditions + CBotInstr* m_Condition; + //! A label if there is + CBotString m_label; +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 09cdc2cd..9a653482 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,140 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile the instruction "do" - -CBotDo::CBotDo() -{ - m_Condition = - m_Block = nullptr; // nullptr so that delete is not possible further - name = "CBotDo"; // debug -} - -CBotDo::~CBotDo() -{ - delete m_Condition; // frees the condition - delete m_Block; // frees the instruction block -} - -CBotInstr* CBotDo::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotDo* inst = new CBotDo(); // creates the object - - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GetString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_DO)) return nullptr; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - - // looking for a statement block after the do - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - - if ( pStk->IsOk() ) - { - if (IsOfType(p, ID_WHILE)) - { - if ( nullptr != (inst->m_Condition = CBotCondition::Compile( p, pStk )) ) - { - // the condition exists - if (IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); // return an object to the application - } - pStk->SetError(TX_ENDOF, p->GetStart()); - } - } - pStk->SetError(TX_WHILE, p->GetStart()); - } - - delete inst; // error, frees up - return pStack->Return(nullptr, pStk); // no object, the error is on the stack -} - -// executes instruction "do" - -bool CBotDo :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // adds an item to the stack - // or find in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GetState() ) // executes the loop - { // there are two possible states (depending on recovery) - case 0: - // evaluates the associated statement block - if ( m_Block != nullptr && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(1, m_label)) continue; // if continued, will return to test - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } - - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - if (!pile->SetState(1)) return false; // ready for further - - case 1: - // evaluates the condition - if ( !m_Condition->Execute(pile) ) return false; // interrupted here ? - - // the result of the condition is on the stack - - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GetVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - // returns to instruction block to start - if (!pile->SetState(0, 0)) return false; - continue; - } -} - -void CBotDo :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack - if ( pile == nullptr ) return; - - switch( pile->GetState() ) - { // there are two possible states (depending on recovery) - case 0: - // restores the assosiated statement's block - if ( m_Block != nullptr ) m_Block->RestoreState(pile, bMain); - return; - - case 1: - // restores the condition - m_Condition->RestoreState(pile, bMain); - return; - } -} - - -/////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// // compiles instruction "for" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index dc7707f7..27c283a9 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -10,8 +10,17 @@ set(SOURCES CBotTwoOpExpr.cpp CBotVar.cpp CBotWhile.cpp + CBotInstr/CBotDo.cpp ) +# Includes +set(LOCAL_INCLUDES + ${CMAKE_CURRENT_SOURCE_DIR} +) + +include_directories(${LOCAL_INCLUDES}) + + if(CBOT_STATIC) add_library(CBot STATIC ${SOURCES}) else() From 981f41d8a337097fe45376248098c071bc7b7a3b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 18:44:36 +0100 Subject: [PATCH 04/91] Move CBotFor class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 18 --- src/CBot/CBotInstr/CBotFor.cpp | 208 +++++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotFor.h | 81 +++++++++++++ src/CBot/CBotWhile.cpp | 184 ----------------------------- src/CBot/CMakeLists.txt | 1 + 6 files changed, 291 insertions(+), 202 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotFor.cpp create mode 100644 src/CBot/CBotInstr/CBotFor.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 375c1f39..dad1cccf 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -37,6 +37,7 @@ // Modules inlcude #include "CBot.h" #include "CBotInstr/CBotDo.h" +#include "CBotInstr/CBotFor.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 60e1d88c..37d18673 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -483,24 +483,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotFor : public CBotInstr -{ -private: - CBotInstr* m_Init; // initial intruction - CBotInstr* m_Test; // test condition - CBotInstr* m_Incr; // instruction for increment - CBotInstr* m_Block; // instructions - CBotString m_label; // a label if there is - -public: - CBotFor(); - ~CBotFor(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - class CBotBreak : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp new file mode 100644 index 00000000..f920f0e0 --- /dev/null +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -0,0 +1,208 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotFor.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotFor::CBotFor() +{ + m_Init = + m_Test = + m_Incr = + m_Block = nullptr; // nullptr so that delete is not possible further + name = "CBotFor"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotFor::~CBotFor() +{ + delete m_Init; + delete m_Test; + delete m_Incr; + delete m_Block; // frees the instruction block +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotFor* inst = new CBotFor(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if ( IsOfType( p, TokenTypVar ) && + IsOfType( p, ID_DOTS ) ) + { + inst->m_label = pp->GetString(); // register the name of label + } + + inst->SetToken(p); + if (!IsOfType(p, ID_FOR)) return nullptr; // should never happen + + if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? + { + pStack->SetError(TX_OPENPAR, p->GetStart()); + return nullptr; + } + + CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp + + // compiles instructions for initialization + inst->m_Init = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GetStart()); + delete inst; + return pStack->Return(nullptr, pStk); // no object, the error is on the stack + } + inst->m_Test = CBotBoolExpr::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( !IsOfType(p, ID_SEP)) // lack the semicolon? + { + pStack->SetError(TX_OPENPAR, p->GetStart()); + delete inst; + return pStack->Return(nullptr, pStk); // no object, the error is on the stack + } + inst->m_Incr = CBotListExpression::Compile( p, pStk ); + if ( pStk->IsOk() ) + { + if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? + { + IncLvl(inst->m_label); + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); + DecLvl(); + if ( pStk->IsOk() ) + return pStack->Return(inst, pStk);; + } + pStack->SetError(TX_CLOSEPAR, p->GetStart()); + } + } + } + + delete inst; // error, frees up + return pStack->Return(nullptr, pStk); // no object, the error is on the stack +} + +// execution of instruction "for" +//////////////////////////////////////////////////////////////////////////////// +bool CBotFor :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) + // or find in case of recovery +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + while( true ) switch( pile->GetState() ) // executes the loop + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != nullptr && + !m_Init->Execute(pile) ) return false; // interrupted here ? + if (!pile->SetState(1)) return false; // ready for further + + case 1: + // evaluates the condition + if ( m_Test != nullptr ) // no strings attached? -> True! + { + if (!m_Test->Execute(pile) ) return false; // interrupted here ? + + // the result of the condition is on the stack + + // terminates if an error or if the condition is false + if ( !pile->IsOk() || pile->GetVal() != true ) + { + return pj->Return(pile); // sends the results and releases the stack + } + } + + // la condition est vrai, passe à la suite + if (!pile->SetState(2)) return false; // ready for further + + case 2: + // evaluates the associated statement block + if ( m_Block != nullptr && + !m_Block->Execute(pile) ) + { + if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation + return pj->BreakReturn(pile, m_label); // sends the results and releases the stack + } + + // terminates if there is an error + if ( !pile->IsOk() ) + { + return pj->Return(pile); // sends the results and releases the stack + } + + if (!pile->SetState(3)) return false; // ready for further + + case 3: + // evalutate the incrementation + if ( m_Incr != nullptr && + !m_Incr->Execute(pile) ) return false; // interrupted here ? + + // returns to the test again + if (!pile->SetState(1, 0)) return false; // returns to the test + continue; + } +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) + if ( pile == nullptr ) return; + + switch( pile->GetState() ) + { // there are four possible states (depending on recovery) + case 0: + // initialize + if ( m_Init != nullptr ) m_Init->RestoreState(pile, true); // interrupted here ! + return; + + case 1: + if ( m_Init != nullptr ) m_Init->RestoreState(pile, false); // variables definitions + + // evaluates the condition + if ( m_Test != nullptr ) m_Test->RestoreState(pile, true); // interrupted here ! + return; + + case 2: + if ( m_Init != nullptr ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluates the associated statement block + if ( m_Block != nullptr ) m_Block->RestoreState(pile, true); + return; + + case 3: + if ( m_Init != nullptr ) m_Init->RestoreState(pile, false); // variable definitions + + // evaluate the incrementation + if ( m_Incr != nullptr ) m_Incr->RestoreState(pile, true); // interrupted here ! + return; + } +} diff --git a/src/CBot/CBotInstr/CBotFor.h b/src/CBot/CBotInstr/CBotFor.h new file mode 100644 index 00000000..01edc666 --- /dev/null +++ b/src/CBot/CBotInstr/CBotFor.h @@ -0,0 +1,81 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotFor class + */ +class CBotFor : public CBotInstr +{ +public: + + /*! + * \brief CBotFor + */ + CBotFor(); + + /*! + * \brief CBotFor + */ + ~CBotFor(); + + /*! + * \brief Compile Compiles instruction "for" + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! Initial intruction + CBotInstr* m_Init; + //! Test Condition + CBotInstr* m_Test; + //! instruction for increment + CBotInstr* m_Incr; + //! Instructions + CBotInstr* m_Block; + //! A label if there is + CBotString m_label; +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 9a653482..d58dc546 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,188 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "for" - -CBotFor::CBotFor() -{ - m_Init = - m_Test = - m_Incr = - m_Block = nullptr; // nullptr so that delete is not possible further - name = "CBotFor"; // debug -} - -CBotFor::~CBotFor() -{ - delete m_Init; - delete m_Test; - delete m_Incr; - delete m_Block; // frees the instruction block -} - -CBotInstr* CBotFor::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotFor* inst = new CBotFor(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if ( IsOfType( p, TokenTypVar ) && - IsOfType( p, ID_DOTS ) ) - { - inst->m_label = pp->GetString(); // register the name of label - } - - inst->SetToken(p); - if (!IsOfType(p, ID_FOR)) return nullptr; // should never happen - - if ( !IsOfType(p, ID_OPENPAR)) // missing parenthesis ? - { - pStack->SetError(TX_OPENPAR, p->GetStart()); - return nullptr; - } - - CBotCStack* pStk = pStack->TokenStack(pp, true); // un petit bout de pile svp - - // compiles instructions for initialization - inst->m_Init = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // lack the semicolon? - { - pStack->SetError(TX_OPENPAR, p->GetStart()); - delete inst; - return pStack->Return(nullptr, pStk); // no object, the error is on the stack - } - inst->m_Test = CBotBoolExpr::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( !IsOfType(p, ID_SEP)) // lack the semicolon? - { - pStack->SetError(TX_OPENPAR, p->GetStart()); - delete inst; - return pStack->Return(nullptr, pStk); // no object, the error is on the stack - } - inst->m_Incr = CBotListExpression::Compile( p, pStk ); - if ( pStk->IsOk() ) - { - if ( IsOfType(p, ID_CLOSEPAR)) // missing parenthesis ? - { - IncLvl(inst->m_label); - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk, true ); - DecLvl(); - if ( pStk->IsOk() ) - return pStack->Return(inst, pStk);; - } - pStack->SetError(TX_CLOSEPAR, p->GetStart()); - } - } - } - - delete inst; // error, frees up - return pStack->Return(nullptr, pStk); // no object, the error is on the stack -} - -// execution of instruction "for" - -bool CBotFor :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this, true); // adds an item to the stack (variables locales) - // or find in case of recovery -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - while( true ) switch( pile->GetState() ) // executes the loop - { // there are four possible states (depending on recovery) - case 0: - // initialize - if ( m_Init != nullptr && - !m_Init->Execute(pile) ) return false; // interrupted here ? - if (!pile->SetState(1)) return false; // ready for further - - case 1: - // evaluates the condition - if ( m_Test != nullptr ) // no strings attached? -> True! - { - if (!m_Test->Execute(pile) ) return false; // interrupted here ? - - // the result of the condition is on the stack - - // terminates if an error or if the condition is false - if ( !pile->IsOk() || pile->GetVal() != true ) - { - return pj->Return(pile); // sends the results and releases the stack - } - } - - // la condition est vrai, passe à la suite - if (!pile->SetState(2)) return false; // ready for further - - case 2: - // evaluates the associated statement block - if ( m_Block != nullptr && - !m_Block->Execute(pile) ) - { - if (pile->IfContinue(3, m_label)) continue; // if continued, going on to incrementation - return pj->BreakReturn(pile, m_label); // sends the results and releases the stack - } - - // terminates if there is an error - if ( !pile->IsOk() ) - { - return pj->Return(pile); // sends the results and releases the stack - } - - if (!pile->SetState(3)) return false; // ready for further - - case 3: - // evalutate the incrementation - if ( m_Incr != nullptr && - !m_Incr->Execute(pile) ) return false; // interrupted here ? - - // returns to the test again - if (!pile->SetState(1, 0)) return false; // returns to the test - continue; - } -} - -void CBotFor :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack (variables locales) - if ( pile == nullptr ) return; - - switch( pile->GetState() ) - { // there are four possible states (depending on recovery) - case 0: - // initialize - if ( m_Init != nullptr ) m_Init->RestoreState(pile, true); // interrupted here ! - return; - - case 1: - if ( m_Init != nullptr ) m_Init->RestoreState(pile, false); // variables definitions - - // evaluates the condition - if ( m_Test != nullptr ) m_Test->RestoreState(pile, true); // interrupted here ! - return; - - case 2: - if ( m_Init != nullptr ) m_Init->RestoreState(pile, false); // variable definitions - - // evaluates the associated statement block - if ( m_Block != nullptr ) m_Block->RestoreState(pile, true); - return; - - case 3: - if ( m_Init != nullptr ) m_Init->RestoreState(pile, false); // variable definitions - - // evaluate the incrementation - if ( m_Incr != nullptr ) m_Incr->RestoreState(pile, true); // interrupted here ! - return; - } -} - ////////////////////////////////////////////////////////////////////////////////////// // compiles a list of expressions // is used only in "for" statement @@ -434,8 +252,6 @@ void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) } } -/////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// // compiles instruction "switch" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 27c283a9..34782a01 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES CBotVar.cpp CBotWhile.cpp CBotInstr/CBotDo.cpp + CBotInstr/CBotFor.cpp ) # Includes From dd6f6e29c227a3d5a348bbb7da2189535627c3f8 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 18:54:49 +0100 Subject: [PATCH 05/91] Moving CBotListExpression class in its own header and source files. --- src/CBot/CBot.h | 14 --- src/CBot/CBotInstr/CBotFor.cpp | 1 + src/CBot/CBotInstr/CBotListExpression.cpp | 123 ++++++++++++++++++++++ src/CBot/CBotInstr/CBotListExpression.h | 72 +++++++++++++ src/CBot/CBotWhile.cpp | 95 ----------------- src/CBot/CMakeLists.txt | 1 + 6 files changed, 197 insertions(+), 109 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotListExpression.cpp create mode 100644 src/CBot/CBotInstr/CBotListExpression.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 37d18673..4d701326 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -835,20 +835,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotListExpression : public CBotInstr -{ -private: - CBotInstr* m_Expr; // the first expression to be evaluated - -public: - CBotListExpression(); - ~CBotListExpression(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - class CBotLogicExpr : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index f920f0e0..7dc07d4a 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotFor.h" +#include "CBotListExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp new file mode 100644 index 00000000..66f7eafc --- /dev/null +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -0,0 +1,123 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotListExpression.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +/// Seeks a declaration of variable or expression +static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) +{ + CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? + if ( i== nullptr ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? + if ( i== nullptr ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? + if ( i== nullptr ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? + if ( i== nullptr ) i = CBotExpression::Compile( p, pStack ); // compiles an expression + return i; +} +//////////////////////////////////////////////////////////////////////////////// + + + +//////////////////////////////////////////////////////////////////////////////// +CBotListExpression::CBotListExpression() +{ + m_Expr = nullptr; + name = "CBotListExpression"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotListExpression::~CBotListExpression() +{ + delete m_Expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotListExpression* inst = new CBotListExpression(); + + inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list + if (pStack->IsOk()) + { + while ( IsOfType(p, ID_COMMA) ) // more instructions? + { + CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? + inst->m_Expr->AddNext(i); // added after + if ( !pStack->IsOk() ) + { + delete inst; + return nullptr; // no object, the error is on the stack + } + } + return inst; + } + delete inst; + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotListExpression::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(); // essential + CBotInstr* p = m_Expr; // the first expression + + int state = pile->GetState(); + while (state-->0) p = p->GetNext(); // returns to the interrupted operation + + if ( p != nullptr ) while (true) + { + if ( !p->Execute(pile) ) return false; + p = p->GetNext(); + if ( p == nullptr ) break; + if (!pile->IncState()) return false; // ready for next + } + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + int state = 0x7000; + + if ( bMain ) + { + pile = pj->RestoreStack(); + if ( pile == nullptr ) return; + state = pile->GetState(); + } + + CBotInstr* p = m_Expr; // the first expression + + while (p != nullptr && state-->0) + { + p->RestoreState(pile, false); + p = p->GetNext(); // returns to the interrupted operation + } + + if ( p != nullptr ) + { + p->RestoreState(pile, bMain); + } +} diff --git a/src/CBot/CBotInstr/CBotListExpression.h b/src/CBot/CBotInstr/CBotListExpression.h new file mode 100644 index 00000000..c4e67115 --- /dev/null +++ b/src/CBot/CBotInstr/CBotListExpression.h @@ -0,0 +1,72 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotListExpression class. Compiles a list of expressions is used + * only in "for" statement in incrementing and intitialisation. + */ +class CBotListExpression : public CBotInstr +{ +public: + + /*! + * \brief CBotListExpression + */ + CBotListExpression(); + + /*! + * \brief CBotListExpression + */ + ~CBotListExpression(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param pStack + * \return + */ + bool Execute(CBotStack* &pStack) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! The first expression to be evaluated + CBotInstr* m_Expr; +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index d58dc546..a691c5a2 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,101 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } -////////////////////////////////////////////////////////////////////////////////////// -// compiles a list of expressions -// is used only in "for" statement -// in incrementing and intitialisation - -CBotListExpression::CBotListExpression() -{ - m_Expr = nullptr; - name = "CBotListExpression"; -} - -CBotListExpression::~CBotListExpression() -{ - delete m_Expr; -} - -// seeks a declaration of variable or expression - -static CBotInstr* CompileInstrOrDefVar(CBotToken* &p, CBotCStack* pStack) -{ - CBotInstr* i = CBotInt::Compile( p, pStack, false, true ); // Is this a declaration of an integer? - if ( i== nullptr ) i = CBotFloat::Compile( p, pStack, false, true ); // or a real number? - if ( i== nullptr ) i = CBotBoolean::Compile( p, pStack, false, true ); // or a boolean? - if ( i== nullptr ) i = CBotIString::Compile( p, pStack, false, true ); // ar a string? - if ( i== nullptr ) i = CBotExpression::Compile( p, pStack ); // compiles an expression - return i; -} - -CBotInstr* CBotListExpression::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotListExpression* inst = new CBotListExpression(); - - inst->m_Expr = CompileInstrOrDefVar( p, pStack ); // compile the first expression in a list - if (pStack->IsOk()) - { - while ( IsOfType(p, ID_COMMA) ) // more instructions? - { - CBotInstr* i = CompileInstrOrDefVar( p, pStack ); // Is this a declaration of an integer? - inst->m_Expr->AddNext(i); // added after - if ( !pStack->IsOk() ) - { - delete inst; - return nullptr; // no object, the error is on the stack - } - } - return inst; - } - delete inst; - return nullptr; -} - -bool CBotListExpression::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(); // essential - CBotInstr* p = m_Expr; // the first expression - - int state = pile->GetState(); - while (state-->0) p = p->GetNext(); // returns to the interrupted operation - - if ( p != nullptr ) while (true) - { - if ( !p->Execute(pile) ) return false; - p = p->GetNext(); - if ( p == nullptr ) break; - if (!pile->IncState()) return false; // ready for next - } - return pj->Return(pile); -} - -void CBotListExpression::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - int state = 0x7000; - - if ( bMain ) - { - pile = pj->RestoreStack(); - if ( pile == nullptr ) return; - state = pile->GetState(); - } - - CBotInstr* p = m_Expr; // the first expression - - while (p != nullptr && state-->0) - { - p->RestoreState(pile, false); - p = p->GetNext(); // returns to the interrupted operation - } - - if ( p != nullptr ) - { - p->RestoreState(pile, bMain); - } -} - /////////////////////////////////////////////////////////////////////////// // compiles instruction "switch" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 34782a01..030c4de5 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -12,6 +12,7 @@ set(SOURCES CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp + CBotInstr/CBotListExpression.cpp ) # Includes From 5629bce950470348f293e49250eb54299d80cfb4 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 19:04:49 +0100 Subject: [PATCH 06/91] Moving CBotSwitch class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 17 --- src/CBot/CBotInstr/CBotSwitch.cpp | 214 ++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotSwitch.h | 76 +++++++++++ src/CBot/CBotWhile.cpp | 188 -------------------------- src/CBot/CMakeLists.txt | 1 + 6 files changed, 292 insertions(+), 205 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotSwitch.cpp create mode 100644 src/CBot/CBotInstr/CBotSwitch.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index dad1cccf..97bcc776 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -38,6 +38,7 @@ #include "CBot.h" #include "CBotInstr/CBotDo.h" #include "CBotInstr/CBotFor.h" +#include "CBotInstr/CBotSwitch.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 4d701326..01a874db 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -511,23 +511,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -class CBotSwitch : public CBotInstr -{ -private: - CBotInstr* m_Value; // value to seek - CBotInstr* m_Block; // instructions - -public: - CBotSwitch(); - ~CBotSwitch(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - class CBotCase : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp new file mode 100644 index 00000000..f9510d9d --- /dev/null +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -0,0 +1,214 @@ + + +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotSwitch.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotSwitch::CBotSwitch() +{ + m_Value = + m_Block = nullptr; // nullptr so that delete is not possible further + name = "CBotSwitch"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotSwitch::~CBotSwitch() +{ + delete m_Value; // frees the value + delete m_Block; // frees the instruction block +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotSwitch* inst = new CBotSwitch(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_SWITCH)) return nullptr; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + if ( IsOfType(p, ID_OPENPAR ) ) + { + if ( nullptr != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) + { + if ( pStk->GetType() < CBotTypLong ) + { + if ( IsOfType(p, ID_CLOSEPAR ) ) + { + if ( IsOfType(p, ID_OPBLK ) ) + { + IncLvl(); + + while( !IsOfType( p, ID_CLBLK ) ) + { + if ( p->GetType() == ID_CASE || p->GetType() == ID_DEFAULT) + { + CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp + + CBotInstr* i = CBotCase::Compile( p, pStk2 ); + if (i == nullptr) + { + delete inst; + return pStack->Return(nullptr, pStk2); + } + delete pStk2; + if ( inst->m_Block == nullptr ) inst->m_Block = i; + else inst->m_Block->AddNext(i); + continue; + } + + if ( inst->m_Block == nullptr ) + { + pStk->SetError(TX_NOCASE, p->GetStart()); + delete inst; + return pStack->Return(nullptr, pStk); + } + + CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); + if ( !pStk->IsOk() ) + { + delete inst; + return pStack->Return(nullptr, pStk); + } + inst->m_Block->AddNext(i); + + if ( p == nullptr ) + { + pStk->SetError(TX_CLOSEBLK, -1); + delete inst; + return pStack->Return(nullptr, pStk); + } + } + DecLvl(); + + if ( inst->m_Block == nullptr ) + { + pStk->SetError(TX_NOCASE, p->GetStart()); + delete inst; + return pStack->Return(nullptr, pStk); + } + // the statement block is ok + return pStack->Return(inst, pStk); // return an object to the application + } + pStk->SetError( TX_OPENBLK, p->GetStart() ); + } + pStk->SetError( TX_CLOSEPAR, p->GetStart() ); + } + pStk->SetError( TX_BADTYPE, p->GetStart() ); + } + } + pStk->SetError( TX_OPENPAR, p->GetStart()); + + delete inst; // error, frees up + return pStack->Return(nullptr, pStk); // no object, the error is on the stack +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotSwitch :: Execute(CBotStack* &pj) +{ + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GetState(); + if (state == 0) + { + if ( !m_Value->Execute(pile1) ) return false; + pile1->SetState(state = -1); + } + + if ( pile1->IfStep() ) return false; + + if ( state == -1 ) + { + state = 0; + int val = pile1->GetVal(); // result of the value + + CBotStack* pile2 = pile1->AddStack(); + while ( p != nullptr ) // search for the corresponding case in a list + { + state++; + if ( p->CompCase( pile2, val ) ) break; // found the case + p = p->GetNext(); + } + pile2->Delete(); + + if ( p == nullptr ) return pj->Return(pile1); // completed if nothing + + if ( !pile1->SetState(state) ) return false; + } + + p = m_Block; // returns to the beginning + while (state-->0) p = p->GetNext(); // advance in the list + + while( p != nullptr ) + { + if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); + if ( !pile1->IncState() ) return false; + p = p->GetNext(); + } + return pj->Return(pile1); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == nullptr ) return; + + CBotInstr* p = m_Block; // first expression + + int state = pile1->GetState(); + if (state == 0) + { + m_Value->RestoreState(pile1, bMain); + return; + } + + if ( state == -1 ) + { + return; + } + +// p = m_Block; // returns to the beginning + while ( p != nullptr && state-- > 0 ) + { + p->RestoreState(pile1, false); + p = p->GetNext(); // advance in the list + } + + if( p != nullptr ) + { + p->RestoreState(pile1, true); + return; + } +} diff --git a/src/CBot/CBotInstr/CBotSwitch.h b/src/CBot/CBotInstr/CBotSwitch.h new file mode 100644 index 00000000..c3f5e02c --- /dev/null +++ b/src/CBot/CBotInstr/CBotSwitch.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotSwitch class. Compiles instruction "switch" + */ +class CBotSwitch : public CBotInstr +{ +public: + + /*! + * \brief CBotSwitch + */ + CBotSwitch(); + + /*! + * \brief CBotSwitch + */ + ~CBotSwitch(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Executes instruction "switch". + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! Value to seek + CBotInstr* m_Value; + //! Instructions + CBotInstr* m_Block; + +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index a691c5a2..3f77dbe2 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,194 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "switch" - -CBotSwitch::CBotSwitch() -{ - m_Value = - m_Block = nullptr; // nullptr so that delete is not possible further - name = "CBotSwitch"; // debug -} - -CBotSwitch::~CBotSwitch() -{ - delete m_Value; // frees the value - delete m_Block; // frees the instruction block -} - - -CBotInstr* CBotSwitch::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotSwitch* inst = new CBotSwitch(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_SWITCH)) return nullptr; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - if ( IsOfType(p, ID_OPENPAR ) ) - { - if ( nullptr != (inst->m_Value = CBotExpression::Compile( p, pStk )) ) - { - if ( pStk->GetType() < CBotTypLong ) - { - if ( IsOfType(p, ID_CLOSEPAR ) ) - { - if ( IsOfType(p, ID_OPBLK ) ) - { - IncLvl(); - - while( !IsOfType( p, ID_CLBLK ) ) - { - if ( p->GetType() == ID_CASE || p->GetType() == ID_DEFAULT) - { - CBotCStack* pStk2 = pStk->TokenStack(p); // un petit bout de pile svp - - CBotInstr* i = CBotCase::Compile( p, pStk2 ); - if (i == nullptr) - { - delete inst; - return pStack->Return(nullptr, pStk2); - } - delete pStk2; - if ( inst->m_Block == nullptr ) inst->m_Block = i; - else inst->m_Block->AddNext(i); - continue; - } - - if ( inst->m_Block == nullptr ) - { - pStk->SetError(TX_NOCASE, p->GetStart()); - delete inst; - return pStack->Return(nullptr, pStk); - } - - CBotInstr* i = CBotBlock::CompileBlkOrInst( p, pStk, true ); - if ( !pStk->IsOk() ) - { - delete inst; - return pStack->Return(nullptr, pStk); - } - inst->m_Block->AddNext(i); - - if ( p == nullptr ) - { - pStk->SetError(TX_CLOSEBLK, -1); - delete inst; - return pStack->Return(nullptr, pStk); - } - } - DecLvl(); - - if ( inst->m_Block == nullptr ) - { - pStk->SetError(TX_NOCASE, p->GetStart()); - delete inst; - return pStack->Return(nullptr, pStk); - } - // the statement block is ok - return pStack->Return(inst, pStk); // return an object to the application - } - pStk->SetError( TX_OPENBLK, p->GetStart() ); - } - pStk->SetError( TX_CLOSEPAR, p->GetStart() ); - } - pStk->SetError( TX_BADTYPE, p->GetStart() ); - } - } - pStk->SetError( TX_OPENPAR, p->GetStart()); - - delete inst; // error, frees up - return pStack->Return(nullptr, pStk); // no object, the error is on the stack -} - -// executes instruction "switch" - -bool CBotSwitch :: Execute(CBotStack* &pj) -{ - CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack -// if ( pile1 == EOX ) return true; - - CBotInstr* p = m_Block; // first expression - - int state = pile1->GetState(); - if (state == 0) - { - if ( !m_Value->Execute(pile1) ) return false; - pile1->SetState(state = -1); - } - - if ( pile1->IfStep() ) return false; - - if ( state == -1 ) - { - state = 0; - int val = pile1->GetVal(); // result of the value - - CBotStack* pile2 = pile1->AddStack(); - while ( p != nullptr ) // search for the corresponding case in a list - { - state++; - if ( p->CompCase( pile2, val ) ) break; // found the case - p = p->GetNext(); - } - pile2->Delete(); - - if ( p == nullptr ) return pj->Return(pile1); // completed if nothing - - if ( !pile1->SetState(state) ) return false; - } - - p = m_Block; // returns to the beginning - while (state-->0) p = p->GetNext(); // advance in the list - - while( p != nullptr ) - { - if ( !p->Execute(pile1) ) return pj->BreakReturn(pile1); - if ( !pile1->IncState() ) return false; - p = p->GetNext(); - } - return pj->Return(pile1); -} - -void CBotSwitch :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack - if ( pile1 == nullptr ) return; - - CBotInstr* p = m_Block; // first expression - - int state = pile1->GetState(); - if (state == 0) - { - m_Value->RestoreState(pile1, bMain); - return; - } - - if ( state == -1 ) - { - return; - } - -// p = m_Block; // returns to the beginning - while ( p != nullptr && state-- > 0 ) - { - p->RestoreState(pile1, false); - p = p->GetNext(); // advance in the list - } - - if( p != nullptr ) - { - p->RestoreState(pile1, true); - return; - } -} - -/////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// // compiles instruction "case" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 030c4de5..212a6dc3 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp CBotInstr/CBotListExpression.cpp + CBotInstr/CBotSwitch.cpp ) # Includes From 6d2fbf3ea4107ccdd6f80e8edae110ce3db189fb Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 19:12:03 +0100 Subject: [PATCH 07/91] Moving CBotCase class in its own header and source files. --- src/CBot/CBot.h | 15 ------ src/CBot/CBotInstr/CBotCase.cpp | 88 +++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotCase.h | 83 +++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotSwitch.cpp | 1 + src/CBot/CBotWhile.cpp | 70 ------------------------ src/CBot/CMakeLists.txt | 1 + 6 files changed, 173 insertions(+), 85 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotCase.cpp create mode 100644 src/CBot/CBotInstr/CBotCase.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 01a874db..45260f29 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -511,21 +511,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotCase : public CBotInstr -{ -private: - CBotInstr* m_Value; // value to compare - -public: - CBotCase(); - ~CBotCase(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; - bool CompCase(CBotStack* &pj, int val) override; -}; - class CBotCatch : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotCase.cpp b/src/CBot/CBotInstr/CBotCase.cpp new file mode 100644 index 00000000..ac311039 --- /dev/null +++ b/src/CBot/CBotInstr/CBotCase.cpp @@ -0,0 +1,88 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotCase.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotCase::CBotCase() +{ + m_Value = nullptr; // nullptr so that delete is not possible further + name = "CBotCase"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCase::~CBotCase() +{ + delete m_Value; // frees the value +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCase* inst = new CBotCase(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return nullptr; // should never happen + + if ( pp->GetType() == ID_CASE ) + { + pp = p; + inst->m_Value = CBotExprNum::Compile(p, pStack); + if ( inst->m_Value == nullptr ) + { + pStack->SetError( TX_BADNUM, pp ); + delete inst; + return nullptr; + } + } + if ( !IsOfType( p, ID_DOTS )) + { + pStack->SetError( TX_MISDOTS, p->GetStart() ); + delete inst; + return nullptr; + } + + return inst; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCase::Execute(CBotStack* &pj) +{ + return true; // the "case" statement does nothing! +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCase::RestoreState(CBotStack* &pj, bool bMain) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCase::CompCase(CBotStack* &pile, int val) +{ + if ( m_Value == nullptr ) return true; // "default" case + + while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) + return (pile->GetVal() == val); // compared with the given value +} diff --git a/src/CBot/CBotInstr/CBotCase.h b/src/CBot/CBotInstr/CBotCase.h new file mode 100644 index 00000000..9ba1b86f --- /dev/null +++ b/src/CBot/CBotInstr/CBotCase.h @@ -0,0 +1,83 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotCase class Compiles instruction "case" we are bound to the + * statement block "switch". + */ +class CBotCase : public CBotInstr +{ + +public: + + /*! + * \brief CBotCase + */ + CBotCase(); + + /*! + * \brief ~CBotCase + */ + ~CBotCase(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execution of instruction "case". + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + + /*! + * \brief CompCase Routine to find the entry point of "case" corresponding + * to the value seen. + * \param pj + * \param val + * \return + */ + bool CompCase(CBotStack* &pj, int val) override; + +private: + //! Value to compare. + CBotInstr* m_Value; +}; diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index f9510d9d..c0c79975 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -21,6 +21,7 @@ // Modules inlcude #include "CBotSwitch.h" +#include "CBotCase.h" // Local include diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 3f77dbe2..680dd716 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,76 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } - -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "case" -// we are bound to the statement block "switch" - -CBotCase::CBotCase() -{ - m_Value = nullptr; // nullptr so that delete is not possible further - name = "CBotCase"; // debug -} - -CBotCase::~CBotCase() -{ - delete m_Value; // frees the value -} - - -CBotInstr* CBotCase::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCase* inst = new CBotCase(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_CASE, ID_DEFAULT)) return nullptr; // should never happen - - if ( pp->GetType() == ID_CASE ) - { - pp = p; - inst->m_Value = CBotExprNum::Compile(p, pStack); - if ( inst->m_Value == nullptr ) - { - pStack->SetError( TX_BADNUM, pp ); - delete inst; - return nullptr; - } - } - if ( !IsOfType( p, ID_DOTS )) - { - pStack->SetError( TX_MISDOTS, p->GetStart() ); - delete inst; - return nullptr; - } - - return inst; -} - -// execution of instruction "case" - -bool CBotCase::Execute(CBotStack* &pj) -{ - return true; // the "case" statement does nothing! -} - -void CBotCase::RestoreState(CBotStack* &pj, bool bMain) -{ -} - -// routine to find the entry point of "case" -// corresponding to the value seen - -bool CBotCase::CompCase(CBotStack* &pile, int val) -{ - if ( m_Value == nullptr ) return true; // "default" case - - while (!m_Value->Execute(pile)); // puts the value on the correspondent stack (without interruption) - return (pile->GetVal() == val); // compared with the given value -} - -/////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// // compiles instruction "break" or "continu" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 212a6dc3..ac39e660 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -14,6 +14,7 @@ set(SOURCES CBotInstr/CBotFor.cpp CBotInstr/CBotListExpression.cpp CBotInstr/CBotSwitch.cpp + CBotInstr/CBotCase.cpp ) # Includes From 8da5c675a4e2cdff852bcbfcf1860f4ef0a4ce0c Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 19:20:06 +0100 Subject: [PATCH 08/91] Moving CBotBreak class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 14 ----- src/CBot/CBotInstr/CBotBreak.cpp | 93 ++++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotBreak.h | 75 ++++++++++++++++++++++++++ src/CBot/CBotWhile.cpp | 67 ----------------------- src/CBot/CMakeLists.txt | 1 + 6 files changed, 170 insertions(+), 81 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotBreak.cpp create mode 100644 src/CBot/CBotInstr/CBotBreak.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 97bcc776..9b1083e8 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -39,6 +39,7 @@ #include "CBotInstr/CBotDo.h" #include "CBotInstr/CBotFor.h" #include "CBotInstr/CBotSwitch.h" +#include "CBotInstr/CBotBreak.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 45260f29..cb337af9 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -483,20 +483,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotBreak : public CBotInstr -{ -private: - CBotString m_label; // a label if there is - -public: - CBotBreak(); - ~CBotBreak(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - class CBotReturn : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotBreak.cpp b/src/CBot/CBotInstr/CBotBreak.cpp new file mode 100644 index 00000000..06f53ae2 --- /dev/null +++ b/src/CBot/CBotInstr/CBotBreak.cpp @@ -0,0 +1,93 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotBreak.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotBreak::CBotBreak() +{ + name = "CBotBreak"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotBreak::~CBotBreak() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; // preserves at the ^ token (starting position) + int type = p->GetType(); + + if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return nullptr; // should never happen + + if ( !ChkLvl(CBotString(), type ) ) + { + pStack->SetError(TX_BREAK, pp); + return nullptr; // no object, the error is on the stack + } + + CBotBreak* inst = new CBotBreak(); // creates the object + inst->SetToken(pp); // keeps the operation + + pp = p; + if ( IsOfType( p, TokenTypVar ) ) + { + inst->m_label = pp->GetString(); // register the name of label + if ( !ChkLvl(inst->m_label, type ) ) + { + delete inst; + pStack->SetError(TX_NOLABEL, pp); + return nullptr; // no object, the error is on the stack + } + } + + if (IsOfType(p, ID_SEP)) + { + return inst; // return what it wants + } + delete inst; + + pStack->SetError(TX_ENDOF, p->GetStart()); + return nullptr; // no object, the error is on the stack +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotBreak :: Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + if ( pile->IfStep() ) return false; + + pile->SetBreak(m_token.GetType()==ID_BREAK ? 1 : 2, m_label); + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( bMain ) pj->RestoreStack(this); +} diff --git a/src/CBot/CBotInstr/CBotBreak.h b/src/CBot/CBotInstr/CBotBreak.h new file mode 100644 index 00000000..0ad39408 --- /dev/null +++ b/src/CBot/CBotInstr/CBotBreak.h @@ -0,0 +1,75 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotBreak class Compiles instruction "break" or "continu". + */ +class CBotBreak : public CBotInstr +{ + +public: + + /*! + * \brief CBotBreak + */ + CBotBreak(); + + /*! + * \brief CBotBreak + */ + ~CBotBreak(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execution of statement "break" or "continu" + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + + +private: + //! A label if there is + CBotString m_label; + +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 680dd716..42b024b1 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,73 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "break" or "continu" - -CBotBreak::CBotBreak() -{ - name = "CBotBreak"; // debug -} - -CBotBreak::~CBotBreak() -{ -} - -CBotInstr* CBotBreak::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; // preserves at the ^ token (starting position) - int type = p->GetType(); - - if (!IsOfType(p, ID_BREAK, ID_CONTINUE)) return nullptr; // should never happen - - if ( !ChkLvl(CBotString(), type ) ) - { - pStack->SetError(TX_BREAK, pp); - return nullptr; // no object, the error is on the stack - } - - CBotBreak* inst = new CBotBreak(); // creates the object - inst->SetToken(pp); // keeps the operation - - pp = p; - if ( IsOfType( p, TokenTypVar ) ) - { - inst->m_label = pp->GetString(); // register the name of label - if ( !ChkLvl(inst->m_label, type ) ) - { - delete inst; - pStack->SetError(TX_NOLABEL, pp); - return nullptr; // no object, the error is on the stack - } - } - - if (IsOfType(p, ID_SEP)) - { - return inst; // return what it wants - } - delete inst; - - pStack->SetError(TX_ENDOF, p->GetStart()); - return nullptr; // no object, the error is on the stack -} - -// execution of statement "break" or "continu" - -bool CBotBreak :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - if ( pile->IfStep() ) return false; - - pile->SetBreak(m_token.GetType()==ID_BREAK ? 1 : 2, m_label); - return pj->Return(pile); -} - -void CBotBreak :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( bMain ) pj->RestoreStack(this); -} /////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index ac39e660..07564cb6 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCES CBotInstr/CBotListExpression.cpp CBotInstr/CBotSwitch.cpp CBotInstr/CBotCase.cpp + CBotInstr/CBotBreak.cpp ) # Includes From 179ca18c584db0d63f9e9204ba1fb4d9c837e2f5 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 19:27:58 +0100 Subject: [PATCH 09/91] Moving CBotTry class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 16 --- src/CBot/CBotInstr/CBotTry.cpp | 223 +++++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotTry.h | 76 +++++++++++ src/CBot/CBotWhile.cpp | 206 ------------------------------ src/CBot/CMakeLists.txt | 1 + 6 files changed, 301 insertions(+), 222 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotTry.cpp create mode 100644 src/CBot/CBotInstr/CBotTry.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 9b1083e8..b46a29db 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -40,6 +40,7 @@ #include "CBotInstr/CBotFor.h" #include "CBotInstr/CBotSwitch.h" #include "CBotInstr/CBotBreak.h" +#include "CBotInstr/CBotTry.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index cb337af9..901edad8 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -516,22 +516,6 @@ public: void RestoreCondState(CBotStack* &pj, bool bMain); }; -class CBotTry : public CBotInstr -{ -private: - CBotInstr* m_Block; // instructions - CBotCatch* m_ListCatch; // catches - CBotInstr* m_FinalInst; // final instruction - -public: - CBotTry(); - ~CBotTry(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - class CBotThrow : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotTry.cpp b/src/CBot/CBotInstr/CBotTry.cpp new file mode 100644 index 00000000..c2b2901a --- /dev/null +++ b/src/CBot/CBotInstr/CBotTry.cpp @@ -0,0 +1,223 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotTry.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotTry::CBotTry() +{ + m_ListCatch = nullptr; + m_FinalInst = + m_Block = nullptr; // nullptr so that delete is not possible further + name = "CBotTry"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTry::~CBotTry() +{ + delete m_ListCatch; // frees the list + delete m_Block; // frees the instruction block + delete m_FinalInst; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotTry* inst = new CBotTry(); // creates the object + CBotToken* pp = p; // preserves at the ^ token (starting position) + + inst->SetToken(p); + if (!IsOfType(p, ID_TRY)) return nullptr; // should never happen + + CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp + + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); + CBotCatch** pn = &inst->m_ListCatch; + + while (pStk->IsOk() && p->GetType() == ID_CATCH) + { + CBotCatch* i = CBotCatch::Compile(p, pStk); + *pn = i; + pn = &i->m_next; + } + + if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) + { + inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); + } + + if (pStk->IsOk()) + { + return pStack->Return(inst, pStk); // return an object to the application + } + + delete inst; // error, frees up + return pStack->Return(nullptr, pStk); // no object, the error is on the stack +} + + +//////////////////////////////////////////////////////////////////////////////// +bool CBotTry::Execute(CBotStack* &pj) +{ + int val; + + CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack +// if ( pile1 == EOX ) return true; + + if ( pile1->IfStep() ) return false; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack + CBotStack* pile2 = pile0->AddStack(); + + if ( pile1->GetState() == 0 ) + { + if ( m_Block->Execute(pile1) ) + { + if ( m_FinalInst == nullptr ) return pj->Return(pile1); + pile1->SetState(-2); // passes final + } + + val = pile1->GetError(); + if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? + return false; // does not make the catch + + pile1->IncState(); + pile2->SetState(val); // stores the error number + pile1->SetError(0); // for now there is are more errors! + + if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? + return false; // does not make the catch + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = static_cast(pile1->GetState()); // where were we? + val = pile2->GetState(); // what error? + pile0->SetState(1); // marking the GetRunPos + + if ( val >= 0 && state > 0 ) while ( pc != nullptr ) + { + if ( --state <= 0 ) + { + // request to the catch block if they feel concerned + // demande au bloc catch s'il se sent concerné + if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! + pile1->IncState(); + } + if ( --state <= 0 ) + { + if ( pile2->GetVal() == true ) + { +// pile0->SetState(1); + + if ( !pc->Execute(pile2) ) return false; // performs the operation + if ( m_FinalInst == nullptr ) + return pj->Return(pile2); // ends the try + + pile1->SetState(-2); // passes final + break; + } + pile1->IncState(); + } + pc = pc->m_next; + } + if ( m_FinalInst != nullptr && + pile1->GetState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final + + if (pile1->GetState() <= -1) + { +// pile0->SetState(1); + + if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; + if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception + pile2->SetError(pile1->GetState()==-1 ? val : 0); // gives the initial error + return pj->Return(pile2); + } + + pile1->SetState(0); // returns to the evaluation + pile0->SetState(0); // returns to the evaluation + if ( val != 0 && m_ListCatch == nullptr && m_FinalInst == nullptr ) + return pj->Return(pile2); // ends the try without exception + + pile1->SetError(val); // gives the error + return false; // it's not for us +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotTry::RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + int val; + CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack + if ( pile1 == nullptr ) return; + // or find in case of recovery + CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack + if ( pile0 == nullptr ) return; + + CBotStack* pile2 = pile0->RestoreStack(); + if ( pile2 == nullptr ) return; + + m_Block->RestoreState(pile1, bMain); + if ( pile0->GetState() == 0 ) + { + return; + } + + // there was an interruption + // see what it returns + + CBotCatch* pc = m_ListCatch; + int state = pile1->GetState(); // where were we ? + val = pile2->GetState(); // what error ? + + if ( val >= 0 && state > 0 ) while ( pc != nullptr ) + { + if ( --state <= 0 ) + { + // request to the catch block if they feel concerned + // demande au bloc catch s'il se sent concerné + pc->RestoreCondState(pile2, bMain); // suspend ! + return; + } + if ( --state <= 0 ) + { + if ( pile2->GetVal() == true ) + { + pc->RestoreState(pile2, bMain); // execute the operation + return; + } + } + pc = pc->m_next; + } + + if (pile1->GetState() <= -1) + { + m_FinalInst->RestoreState(pile2, bMain); + return; + } +} diff --git a/src/CBot/CBotInstr/CBotTry.h b/src/CBot/CBotInstr/CBotTry.h new file mode 100644 index 00000000..f12de20f --- /dev/null +++ b/src/CBot/CBotInstr/CBotTry.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotTry class Compiles instruction "try" + */ +class CBotTry : public CBotInstr +{ +public: + + /*! + * \brief CBotTry + */ + CBotTry(); + + /*! + * \brief ~CBotTry + */ + ~CBotTry(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execution of instruction Try manages the return of + * exceptions stops (judgements) by suspension and "finally" + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Instructions + CBotInstr* m_Block; + //! Catches + CBotCatch* m_ListCatch; + //! Final instruction + CBotInstr* m_FinalInst; + +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 42b024b1..c3084be3 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,212 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } - - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "try" - -CBotTry::CBotTry() -{ - m_ListCatch = nullptr; - m_FinalInst = - m_Block = nullptr; // nullptr so that delete is not possible further - name = "CBotTry"; // debug -} - -CBotTry::~CBotTry() -{ - delete m_ListCatch; // frees the list - delete m_Block; // frees the instruction block - delete m_FinalInst; -} - -CBotInstr* CBotTry::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotTry* inst = new CBotTry(); // creates the object - CBotToken* pp = p; // preserves at the ^ token (starting position) - - inst->SetToken(p); - if (!IsOfType(p, ID_TRY)) return nullptr; // should never happen - - CBotCStack* pStk = pStack->TokenStack(pp); // un petit bout de pile svp - - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStk ); - CBotCatch** pn = &inst->m_ListCatch; - - while (pStk->IsOk() && p->GetType() == ID_CATCH) - { - CBotCatch* i = CBotCatch::Compile(p, pStk); - *pn = i; - pn = &i->m_next; - } - - if (pStk->IsOk() && IsOfType( p, ID_FINALLY) ) - { - inst->m_FinalInst = CBotBlock::CompileBlkOrInst( p, pStk ); - } - - if (pStk->IsOk()) - { - return pStack->Return(inst, pStk); // return an object to the application - } - - delete inst; // error, frees up - return pStack->Return(nullptr, pStk); // no object, the error is on the stack -} - -// execution of instruction Try -// manages the return of exceptions -// stops (judgements) by suspension -// and "finally" - -bool CBotTry :: Execute(CBotStack* &pj) -{ - int val; - - CBotStack* pile1 = pj->AddStack(this); // adds an item to the stack -// if ( pile1 == EOX ) return true; - - if ( pile1->IfStep() ) return false; - // or find in case of recovery - CBotStack* pile0 = pj->AddStack2(); // adds an element to the secondary stack - CBotStack* pile2 = pile0->AddStack(); - - if ( pile1->GetState() == 0 ) - { - if ( m_Block->Execute(pile1) ) - { - if ( m_FinalInst == nullptr ) return pj->Return(pile1); - pile1->SetState(-2); // passes final - } - - val = pile1->GetError(); - if ( val == 0 && CBotStack::m_initimer == 0 ) // mode step? - return false; // does not make the catch - - pile1->IncState(); - pile2->SetState(val); // stores the error number - pile1->SetError(0); // for now there is are more errors! - - if ( val == 0 && CBotStack::m_initimer < 0 ) // mode step? - return false; // does not make the catch - } - - // there was an interruption - // see what it returns - - CBotCatch* pc = m_ListCatch; - int state = static_cast(pile1->GetState()); // where were we? - val = pile2->GetState(); // what error? - pile0->SetState(1); // marking the GetRunPos - - if ( val >= 0 && state > 0 ) while ( pc != nullptr ) - { - if ( --state <= 0 ) - { - // request to the catch block if they feel concerned - // demande au bloc catch s'il se sent concerné - if ( !pc->TestCatch(pile2, val) ) return false; // suspend ! - pile1->IncState(); - } - if ( --state <= 0 ) - { - if ( pile2->GetVal() == true ) - { -// pile0->SetState(1); - - if ( !pc->Execute(pile2) ) return false; // performs the operation - if ( m_FinalInst == nullptr ) - return pj->Return(pile2); // ends the try - - pile1->SetState(-2); // passes final - break; - } - pile1->IncState(); - } - pc = pc->m_next; - } - if ( m_FinalInst != nullptr && - pile1->GetState() > 0 && val != 0 ) pile1->SetState(-1);// if stop then made the final - - if (pile1->GetState() <= -1) - { -// pile0->SetState(1); - - if (!m_FinalInst->Execute(pile2) && pile2->IsOk()) return false; - if (!pile2->IsOk()) return pj->Return(pile2); // keep this exception - pile2->SetError(pile1->GetState()==-1 ? val : 0); // gives the initial error - return pj->Return(pile2); - } - - pile1->SetState(0); // returns to the evaluation - pile0->SetState(0); // returns to the evaluation - if ( val != 0 && m_ListCatch == nullptr && m_FinalInst == nullptr ) - return pj->Return(pile2); // ends the try without exception - - pile1->SetError(val); // gives the error - return false; // it's not for us -} - - -void CBotTry :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - int val; - CBotStack* pile1 = pj->RestoreStack(this); // adds an item to the stack - if ( pile1 == nullptr ) return; - // or find in case of recovery - CBotStack* pile0 = pj->AddStack2(); // adds an item to the secondary stack - if ( pile0 == nullptr ) return; - - CBotStack* pile2 = pile0->RestoreStack(); - if ( pile2 == nullptr ) return; - - m_Block->RestoreState(pile1, bMain); - if ( pile0->GetState() == 0 ) - { - return; - } - - // there was an interruption - // see what it returns - - CBotCatch* pc = m_ListCatch; - int state = pile1->GetState(); // where were we ? - val = pile2->GetState(); // what error ? - - if ( val >= 0 && state > 0 ) while ( pc != nullptr ) - { - if ( --state <= 0 ) - { - // request to the catch block if they feel concerned - // demande au bloc catch s'il se sent concerné - pc->RestoreCondState(pile2, bMain); // suspend ! - return; - } - if ( --state <= 0 ) - { - if ( pile2->GetVal() == true ) - { - pc->RestoreState(pile2, bMain); // execute the operation - return; - } - } - pc = pc->m_next; - } - - if (pile1->GetState() <= -1) - { - m_FinalInst->RestoreState(pile2, bMain); - return; - } -} - -/////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// // compiles instruction "catch" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 07564cb6..3d1244d6 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -16,6 +16,7 @@ set(SOURCES CBotInstr/CBotSwitch.cpp CBotInstr/CBotCase.cpp CBotInstr/CBotBreak.cpp + CBotInstr/CBotTry.cpp ) # Includes From 18739d135bac873783980a4acb7c29bd98030ec3 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 19:44:56 +0100 Subject: [PATCH 10/91] Moving CBotCatch class in its own header and source files. --- src/CBot/CBot.h | 19 ------ src/CBot/CBotInstr/CBotCatch.cpp | 107 +++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotCatch.h | 93 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotTry.h | 1 + src/CBot/CBotWhile.cpp | 84 ------------------------ src/CBot/CMakeLists.txt | 1 + 6 files changed, 202 insertions(+), 103 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotCatch.cpp create mode 100644 src/CBot/CBotInstr/CBotCatch.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 901edad8..baebe04c 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -497,25 +497,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotCatch : public CBotInstr -{ -private: - CBotInstr* m_Block; // instructions - CBotInstr* m_Cond; //condition - CBotCatch* m_next; //following catch - friend class CBotTry; - -public: - CBotCatch(); - ~CBotCatch(); - static - CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); - bool TestCatch(CBotStack* &pj, int val); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; - void RestoreCondState(CBotStack* &pj, bool bMain); -}; - class CBotThrow : public CBotInstr { private: diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp new file mode 100644 index 00000000..4c63e029 --- /dev/null +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -0,0 +1,107 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotCatch.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotCatch::CBotCatch() +{ + m_Cond = + m_Block = nullptr; // nullptr so that delete is not possible further + m_next = nullptr; + + name = "CBotCatch"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCatch::~CBotCatch() +{ + delete m_Cond; // frees the list + delete m_Block; // frees the instruction block + delete m_next; // and subsequent +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCatch* inst = new CBotCatch(); // creates the object + pStack->SetStartError(p->GetStart()); + + inst->SetToken(p); + if (!IsOfType(p, ID_CATCH)) return nullptr; // should never happen + + if (IsOfType(p, ID_OPENPAR)) + { + inst->m_Cond = CBotExpression::Compile(p, pStack); + if (( pStack->GetType() < CBotTypLong || + pStack->GetTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); + if ( pStack->IsOk() ) + return inst; // return an object to the application + } + pStack->SetError(TX_CLOSEPAR, p->GetStart()); + } + pStack->SetError(TX_BADTYPE, p->GetStart()); + } + pStack->SetError(TX_OPENPAR, p->GetStart()); + delete inst; // error, frees up + return nullptr; // no object, the error is on the stack +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCatch :: Execute(CBotStack* &pj) +{ + if ( m_Block == nullptr ) return true; + return m_Block->Execute(pj); // executes the associated block +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) +{ + if ( bMain && m_Block != nullptr ) m_Block->RestoreState(pj, bMain); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) +{ + m_Cond->RestoreState(pj, bMain); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCatch :: TestCatch(CBotStack* &pile, int val) +{ + if ( !m_Cond->Execute(pile) ) return false; + + if ( val > 0 || pile->GetType() != CBotTypBoolean ) + { + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypBoolean); + var->SetValInt( pile->GetVal() == val ); + pile->SetVar(var); // calls on the stack + } + + return true; +} diff --git a/src/CBot/CBotInstr/CBotCatch.h b/src/CBot/CBotInstr/CBotCatch.h new file mode 100644 index 00000000..9756c8b8 --- /dev/null +++ b/src/CBot/CBotInstr/CBotCatch.h @@ -0,0 +1,93 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotCatch class. Compiles instruction "catch". + */ +class CBotCatch : public CBotInstr +{ +public: + + /*! + * \brief CBotCatch + */ + CBotCatch(); + + /*! + * \brief CBotCatch + */ + ~CBotCatch(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotCatch* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief TestCatch Routine to see if the catch is to do or not. + * \param pj + * \param val + * \return + */ + bool TestCatch(CBotStack* &pj, int val); + + /*! + * \brief Execute Execution of "catch". + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + + /*! + * \brief RestoreCondState + * \param pj + * \param bMain + */ + void RestoreCondState(CBotStack* &pj, bool bMain); + + +private: + //! Instructions + CBotInstr* m_Block; + //! Condition + CBotInstr* m_Cond; + //! Following catch + CBotCatch* m_next; + + friend class CBotTry; +}; diff --git a/src/CBot/CBotInstr/CBotTry.h b/src/CBot/CBotInstr/CBotTry.h index f12de20f..0a8d2b9a 100644 --- a/src/CBot/CBotInstr/CBotTry.h +++ b/src/CBot/CBotInstr/CBotTry.h @@ -19,6 +19,7 @@ // Modules inlcude #include "CBot.h" +#include "CBotCatch.h" // Local include diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index c3084be3..89647e2a 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -157,90 +157,6 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) } } -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "catch" - -CBotCatch::CBotCatch() -{ - m_Cond = - m_Block = nullptr; // nullptr so that delete is not possible further - m_next = nullptr; - - name = "CBotCatch"; // debug -} - -CBotCatch::~CBotCatch() -{ - delete m_Cond; // frees the list - delete m_Block; // frees the instruction block - delete m_next; // and subsequent -} - -CBotCatch* CBotCatch::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCatch* inst = new CBotCatch(); // creates the object - pStack->SetStartError(p->GetStart()); - - inst->SetToken(p); - if (!IsOfType(p, ID_CATCH)) return nullptr; // should never happen - - if (IsOfType(p, ID_OPENPAR)) - { - inst->m_Cond = CBotExpression::Compile(p, pStack); - if (( pStack->GetType() < CBotTypLong || - pStack->GetTypResult().Eq(CBotTypBoolean) )&& pStack->IsOk() ) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - inst->m_Block = CBotBlock::CompileBlkOrInst( p, pStack ); - if ( pStack->IsOk() ) - return inst; // return an object to the application - } - pStack->SetError(TX_CLOSEPAR, p->GetStart()); - } - pStack->SetError(TX_BADTYPE, p->GetStart()); - } - pStack->SetError(TX_OPENPAR, p->GetStart()); - delete inst; // error, frees up - return nullptr; // no object, the error is on the stack -} - -// execution of "catch" - -bool CBotCatch :: Execute(CBotStack* &pj) -{ - if ( m_Block == nullptr ) return true; - return m_Block->Execute(pj); // executes the associated block -} - -void CBotCatch :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( bMain && m_Block != nullptr ) m_Block->RestoreState(pj, bMain); -} - -void CBotCatch :: RestoreCondState(CBotStack* &pj, bool bMain) -{ - m_Cond->RestoreState(pj, bMain); -} - -// routine to see if the catch is to do or not - -bool CBotCatch :: TestCatch(CBotStack* &pile, int val) -{ - if ( !m_Cond->Execute(pile) ) return false; - - if ( val > 0 || pile->GetType() != CBotTypBoolean ) - { - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypBoolean); - var->SetValInt( pile->GetVal() == val ); - pile->SetVar(var); // calls on the stack - } - - return true; -} - -/////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// // compiles instruction "throw" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 3d1244d6..cd17d7ba 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -17,6 +17,7 @@ set(SOURCES CBotInstr/CBotCase.cpp CBotInstr/CBotBreak.cpp CBotInstr/CBotTry.cpp + CBotInstr/CBotCatch.cpp ) # Includes From a80245b012b8a85e5d01a6eb80d732f03fa2dd43 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 19:53:30 +0100 Subject: [PATCH 11/91] Moving CBotThrow class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 13 ----- src/CBot/CBotInstr/CBotThrow.cpp | 99 ++++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotThrow.h | 73 +++++++++++++++++++++++ src/CBot/CBotWhile.cpp | 75 ------------------------ src/CBot/CMakeLists.txt | 1 + 6 files changed, 174 insertions(+), 88 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotThrow.cpp create mode 100644 src/CBot/CBotInstr/CBotThrow.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index b46a29db..6fda6766 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -41,6 +41,7 @@ #include "CBotInstr/CBotSwitch.h" #include "CBotInstr/CBotBreak.h" #include "CBotInstr/CBotTry.h" +#include "CBotInstr/CBotThrow.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index baebe04c..c331ff4d 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -497,19 +497,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotThrow : public CBotInstr -{ -private: - CBotInstr* m_Value; // the value to send - -public: - CBotThrow(); - ~CBotThrow(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; class CBotIf : public CBotInstr diff --git a/src/CBot/CBotInstr/CBotThrow.cpp b/src/CBot/CBotInstr/CBotThrow.cpp new file mode 100644 index 00000000..14e61f07 --- /dev/null +++ b/src/CBot/CBotInstr/CBotThrow.cpp @@ -0,0 +1,99 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotThrow.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotThrow::CBotThrow() +{ + m_Value = nullptr; // nullptr so that delete is not possible further + + name = "CBotThrow"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotThrow::~CBotThrow() +{ + delete m_Value; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) +{ + pStack->SetStartError(p->GetStart()); + + CBotThrow* inst = new CBotThrow(); // creates the object + inst->SetToken(p); + + CBotToken* pp = p; // preserves at the ^ token (starting position) + + if (!IsOfType(p, ID_THROW)) return nullptr; // should never happen + + inst->m_Value = CBotExpression::Compile( p, pStack ); + + if (pStack->GetType() < CBotTypLong && pStack->IsOk()) + { + return inst; // return an object to the application + } + pStack->SetError(TX_BADTYPE, pp); + + delete inst; // error, frees up + return nullptr; // no object, the error is on the stack +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotThrow::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + if ( pile->GetState() == 0 ) + { + if ( !m_Value->Execute(pile) ) return false; + pile->IncState(); + } + + if ( pile->IfStep() ) return false; + + int val = pile->GetVal(); + if ( val < 0 ) val = TX_BADTHROW; + pile->SetError( val, &m_token ); + return pj->Return( pile ); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotThrow::RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == nullptr ) return; + + if ( pile->GetState() == 0 ) + { + m_Value->RestoreState(pile, bMain); + return; + } +} diff --git a/src/CBot/CBotInstr/CBotThrow.h b/src/CBot/CBotInstr/CBotThrow.h new file mode 100644 index 00000000..997550aa --- /dev/null +++ b/src/CBot/CBotInstr/CBotThrow.h @@ -0,0 +1,73 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotThrow class Compiles instruction "throw". + */ +class CBotThrow : public CBotInstr +{ +public: + + /*! + * \brief CBotThrow + */ + CBotThrow(); + + /*! + * \brief ~CBotThrow + */ + ~CBotThrow(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execution of instruction "throw". + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! The value to send. + CBotInstr* m_Value; + +}; diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotWhile.cpp index 89647e2a..2891c607 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotWhile.cpp @@ -156,78 +156,3 @@ void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) return; } } - -/////////////////////////////////////////////////////////////////////////// -// compiles instruction "throw" - -CBotThrow::CBotThrow() -{ - m_Value = nullptr; // nullptr so that delete is not possible further - - name = "CBotThrow"; // debug -} - -CBotThrow::~CBotThrow() -{ - delete m_Value; -} - -CBotInstr* CBotThrow::Compile(CBotToken* &p, CBotCStack* pStack) -{ - pStack->SetStartError(p->GetStart()); - - CBotThrow* inst = new CBotThrow(); // creates the object - inst->SetToken(p); - - CBotToken* pp = p; // preserves at the ^ token (starting position) - - if (!IsOfType(p, ID_THROW)) return nullptr; // should never happen - - inst->m_Value = CBotExpression::Compile( p, pStack ); - - if (pStack->GetType() < CBotTypLong && pStack->IsOk()) - { - return inst; // return an object to the application - } - pStack->SetError(TX_BADTYPE, pp); - - delete inst; // error, frees up - return nullptr; // no object, the error is on the stack -} - -// execution of instruction "throw" - -bool CBotThrow :: Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - if ( pile->GetState() == 0 ) - { - if ( !m_Value->Execute(pile) ) return false; - pile->IncState(); - } - - if ( pile->IfStep() ) return false; - - int val = pile->GetVal(); - if ( val < 0 ) val = TX_BADTHROW; - pile->SetError( val, &m_token ); - return pj->Return( pile ); -} - -void CBotThrow :: RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == nullptr ) return; - - if ( pile->GetState() == 0 ) - { - m_Value->RestoreState(pile, bMain); - return; - } -} - - diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index cd17d7ba..80b1303b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -18,6 +18,7 @@ set(SOURCES CBotInstr/CBotBreak.cpp CBotInstr/CBotTry.cpp CBotInstr/CBotCatch.cpp + CBotInstr/CBotThrow.cpp ) # Includes From 562752b653cfb06fe90d5590c60cf3ebf7538179 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 8 Nov 2015 20:01:45 +0100 Subject: [PATCH 12/91] Moving CBotWhile class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 14 ----- src/CBot/{ => CBotInstr}/CBotWhile.cpp | 32 ++++------- src/CBot/CBotInstr/CBotWhile.h | 79 ++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 2 +- 5 files changed, 92 insertions(+), 36 deletions(-) rename src/CBot/{ => CBotInstr}/CBotWhile.cpp (86%) create mode 100644 src/CBot/CBotInstr/CBotWhile.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 6fda6766..949ad2d5 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -42,6 +42,7 @@ #include "CBotInstr/CBotBreak.h" #include "CBotInstr/CBotTry.h" #include "CBotInstr/CBotThrow.h" +#include "CBotInstr/CBotWhile.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index c331ff4d..5fb3959c 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -467,21 +467,7 @@ public: bool IsOfClass(CBotString name); }; -class CBotWhile : public CBotInstr -{ -private: - CBotInstr* m_Condition; // condition - CBotInstr* m_Block; // instructions - CBotString m_label; // a label if there is -public: - CBotWhile(); - ~CBotWhile(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; class CBotReturn : public CBotInstr { diff --git a/src/CBot/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp similarity index 86% rename from src/CBot/CBotWhile.cpp rename to src/CBot/CBotInstr/CBotWhile.cpp index 2891c607..92b276db 100644 --- a/src/CBot/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -17,26 +17,14 @@ * along with this program. If not, see http://gnu.org/licenses */ -/////////////////////////////////////////////////////////////////////// -// This file defined the following statements: -// CBotWhile "while (condition) {instructions}" -// CBotDo "do {instructions} while (condition)" -// CBotFor "for (init, condition, incr) {instructions}" -// CBotSwitch "switch (val) {instructions}" -// CBotCase "case val:" -// CBotBreak "break", "break label", "continu", "continu label" -// CBotTry "try {instructions}" -// CBotCatch "catch (condition) {instructions}" or "finally" -// CBotThrow "throw execption" +// Modules inlcude +#include "CBotWhile.h" +// Local include -#include "CBot.h" - -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile an instruction "while" +// Global include +//////////////////////////////////////////////////////////////////////////////// CBotWhile::CBotWhile() { m_Condition = @@ -44,12 +32,14 @@ CBotWhile::CBotWhile() name = "CBotWhile"; // debug } +//////////////////////////////////////////////////////////////////////////////// CBotWhile::~CBotWhile() { delete m_Condition; // frees the condition delete m_Block; // releases the block instruction } +//////////////////////////////////////////////////////////////////////////////// CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) { CBotWhile* inst = new CBotWhile(); // creates the object @@ -88,9 +78,8 @@ CBotInstr* CBotWhile::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(nullptr, pStk); // no object, the error is on the stack } -// executes a "while" instruction - -bool CBotWhile :: Execute(CBotStack* &pj) +//////////////////////////////////////////////////////////////////////////////// +bool CBotWhile::Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); // adds an item to the stack // or find in case of recovery @@ -137,7 +126,8 @@ bool CBotWhile :: Execute(CBotStack* &pj) } } -void CBotWhile :: RestoreState(CBotStack* &pj, bool bMain) +//////////////////////////////////////////////////////////////////////////////// +void CBotWhile::RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; CBotStack* pile = pj->RestoreStack(this); // adds an item to the stack diff --git a/src/CBot/CBotInstr/CBotWhile.h b/src/CBot/CBotInstr/CBotWhile.h new file mode 100644 index 00000000..5d63909a --- /dev/null +++ b/src/CBot/CBotInstr/CBotWhile.h @@ -0,0 +1,79 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotWhile class Compile an instruction "while". + */ +class CBotWhile : public CBotInstr +{ +public: + + /*! + * \brief CBotWhile + */ + CBotWhile(); + + /*! + * \brief ~CBotWhile + */ + ~CBotWhile(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + // executes a "while" instruction + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + + +private: + + //! Condition + CBotInstr* m_Condition; + //! Instructions + CBotInstr* m_Block; + //! A label if there is + CBotString m_label; + +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 80b1303b..af6a1e93 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -9,7 +9,7 @@ set(SOURCES CBotToken.cpp CBotTwoOpExpr.cpp CBotVar.cpp - CBotWhile.cpp + CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp CBotInstr/CBotListExpression.cpp From a0c2c90c9c08e2a4df5e6ca726d0508b868659ed Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 17:24:37 +0100 Subject: [PATCH 13/91] Moving CBotExprAlpha class in its own header and source files. --- src/CBot/CBot.cpp | 58 +-------------------- src/CBot/CBot.h | 16 ------ src/CBot/CBotInstr/CBotExprAlpha.cpp | 77 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprAlpha.h | 67 ++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 146 insertions(+), 73 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprAlpha.cpp create mode 100644 src/CBot/CBotInstr/CBotExprAlpha.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 949ad2d5..8332a1ba 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -43,6 +43,7 @@ #include "CBotInstr/CBotTry.h" #include "CBotInstr/CBotThrow.h" #include "CBotInstr/CBotWhile.h" +#include "CBotInstr/CBotExprAlpha.h" // Local include @@ -2871,63 +2872,6 @@ void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) if (bMain) pj->RestoreStack(this); } -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a token representing a string - -CBotExprAlpha::CBotExprAlpha() -{ - name = "CBotExprAlpha"; -} - -CBotExprAlpha::~CBotExprAlpha() -{ -} - -CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - CBotExprAlpha* inst = new CBotExprAlpha(); - - inst->SetToken(p); - p = p->GetNext(); - - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypString); - pStk->SetVar(var); - - return pStack->Return(inst, pStk); -} - -// execute, returns the corresponding string - -bool CBotExprAlpha::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypString); - - CBotString chaine = m_token.GetString(); - chaine = chaine.Mid(1, chaine.GetLength()-2); // removes the quotes - - var->SetValString(chaine); // value of the number - - pile->SetVar(var); // put on the stack - - return pj->Return(pile); -} - -void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - -////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // compile a token representing true or false diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 5fb3959c..cc1436aa 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -1026,22 +1026,6 @@ public: -// expression representing a string - -class CBotExprAlpha : public CBotInstr -{ -private: - -public: - CBotExprAlpha(); - ~CBotExprAlpha(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprAlpha.cpp b/src/CBot/CBotInstr/CBotExprAlpha.cpp new file mode 100644 index 00000000..c412ac02 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprAlpha.cpp @@ -0,0 +1,77 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprAlpha.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotExprAlpha::CBotExprAlpha() +{ + name = "CBotExprAlpha"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprAlpha::~CBotExprAlpha() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExprAlpha::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + CBotExprAlpha* inst = new CBotExprAlpha(); + + inst->SetToken(p); + p = p->GetNext(); + + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypString); + pStk->SetVar(var); + + return pStack->Return(inst, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprAlpha::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypString); + + CBotString chaine = m_token.GetString(); + chaine = chaine.Mid(1, chaine.GetLength()-2); // removes the quotes + + var->SetValString(chaine); // value of the number + + pile->SetVar(var); // put on the stack + + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprAlpha::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} diff --git a/src/CBot/CBotInstr/CBotExprAlpha.h b/src/CBot/CBotInstr/CBotExprAlpha.h new file mode 100644 index 00000000..3f820607 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprAlpha.h @@ -0,0 +1,67 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotExprAlpha class Expression representing a string. + */ +class CBotExprAlpha : public CBotInstr +{ +public: + + /*! + * \brief CBotExprAlpha + */ + CBotExprAlpha(); + + /*! + * \brief CBotExprAlpha + */ + ~CBotExprAlpha(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execute, returns the corresponding string. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index af6a1e93..60363d30 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -19,6 +19,7 @@ set(SOURCES CBotInstr/CBotTry.cpp CBotInstr/CBotCatch.cpp CBotInstr/CBotThrow.cpp + CBotInstr/CBotExprAlpha.cpp ) # Includes From f8778e8c03b9b98b9c012543fdafdc6e18220ef1 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 17:35:09 +0100 Subject: [PATCH 14/91] Moving CBotExprNum class in its own header and source files. --- src/CBot/CBot.cpp | 188 +------------------------- src/CBot/CBot.h | 19 --- src/CBot/CBotInstr/CBotCase.cpp | 1 + src/CBot/CBotInstr/CBotExprNum.cpp | 210 +++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprNum.h | 78 +++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 291 insertions(+), 206 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprNum.cpp create mode 100644 src/CBot/CBotInstr/CBotExprNum.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 8332a1ba..bc3d5c54 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -44,6 +44,7 @@ #include "CBotInstr/CBotThrow.h" #include "CBotInstr/CBotWhile.h" #include "CBotInstr/CBotExprAlpha.h" +#include "CBotInstr/CBotExprNum.h" // Local include @@ -2686,193 +2687,6 @@ void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) m_next3->RestoreStateVar(pile, bMain); } -////////////////////////////////////////////////////////////////////////////////////////// - -// converts a string into integer -// may be of the form 0xabc123 - -long GetNumInt(const char* p) -{ - long num = 0; - while (*p >= '0' && *p <= '9') - { - num = num * 10 + *p - '0'; - p++; - } - if (*p == 'x' || *p == 'X') - { - while (*++p != 0) - { - if (*p >= '0' && *p <= '9') - { - num = num * 16 + *p - '0'; - continue; - } - if (*p >= 'A' && *p <= 'F') - { - num = num * 16 + *p - 'A' + 10; - continue; - } - if (*p >= 'a' && *p <= 'f') - { - num = num * 16 + *p - 'a' + 10; - continue; - } - break; - } - } - return num; -} - -// converts a string into a float number -extern float GetNumFloat(const char* p) -{ - double num = 0; - double div = 10; - bool bNeg = false; - - if (*p == '-') - { - bNeg = true; - p++; - } - while (*p >= '0' && *p <= '9') - { - num = num * 10. + (*p - '0'); - p++; - } - - if (*p == '.') - { - p++; - while (*p >= '0' && *p <= '9') - { - num = num + (*p - '0') / div; - div = div * 10; - p++; - } - } - - int exp = 0; - if (*p == 'e' || *p == 'E') - { - char neg = 0; - p++; - if (*p == '-' || *p == '+') neg = *p++; - - while (*p >= '0' && *p <= '9') - { - exp = exp * 10 + (*p - '0'); - p++; - } - if (neg == '-') exp = -exp; - } - - while (exp > 0) - { - num *= 10.0; - exp--; - } - - while (exp < 0) - { - num /= 10.0; - exp++; - } - - if (bNeg) num = -num; - return static_cast(num); -} - -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a token representing a number -CBotExprNum::CBotExprNum() -{ - name = "CBotExprNum"; -} - -CBotExprNum::~CBotExprNum() -{ -} - -CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - CBotExprNum* inst = new CBotExprNum(); - - inst->SetToken(p); - CBotString s = p->GetString(); - - inst->m_numtype = CBotTypInt; - if (p->GetType() == TokenTypDef) - { - inst->m_valint = p->GetIdKey(); - } - else - { - if (s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) )) - { - inst->m_numtype = CBotTypFloat; - inst->m_valfloat = GetNumFloat(s); - } - else - { - inst->m_valint = GetNumInt(s); - } - } - - if (pStk->NextToken(p)) - { - CBotVar* var = CBotVar::Create(static_cast(nullptr), inst->m_numtype); - pStk->SetVar(var); - - return pStack->Return(inst, pStk); - } - delete inst; - return pStack->Return(nullptr, pStk); -} - -// execute, returns the corresponding number - -bool CBotExprNum::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var = CBotVar::Create(static_cast(nullptr), m_numtype); - - CBotString nombre ; - if (m_token.GetType() == TokenTypDef) - { - nombre = m_token.GetString(); - } - - switch (m_numtype) - { - case CBotTypShort: - case CBotTypInt: - var->SetValInt(m_valint, nombre); - break; - case CBotTypFloat: - var->SetValFloat(m_valfloat); - break; - } - pile->SetVar(var); // place on the stack - - return pj->Return(pile); // it's ok -} - -void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - - ////////////////////////////////////////////////////////////////////////////////////// // compile a token representing true or false diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index cc1436aa..6fc404af 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -1006,25 +1006,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -// expression representing a number - -class CBotExprNum : public CBotInstr -{ -private: - int m_numtype; // et the type of number - long m_valint; // value for an int - float m_valfloat; // value for a float - -public: - CBotExprNum(); - ~CBotExprNum(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotCase.cpp b/src/CBot/CBotInstr/CBotCase.cpp index ac311039..a2b68c4f 100644 --- a/src/CBot/CBotInstr/CBotCase.cpp +++ b/src/CBot/CBotInstr/CBotCase.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotCase.h" +#include "CBotExprNum.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp new file mode 100644 index 00000000..61cf3eaa --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -0,0 +1,210 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprNum.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +// converts a string into integer +// may be of the form 0xabc123 +long GetNumInt(const char* p) +{ + long num = 0; + while (*p >= '0' && *p <= '9') + { + num = num * 10 + *p - '0'; + p++; + } + if (*p == 'x' || *p == 'X') + { + while (*++p != 0) + { + if (*p >= '0' && *p <= '9') + { + num = num * 16 + *p - '0'; + continue; + } + if (*p >= 'A' && *p <= 'F') + { + num = num * 16 + *p - 'A' + 10; + continue; + } + if (*p >= 'a' && *p <= 'f') + { + num = num * 16 + *p - 'a' + 10; + continue; + } + break; + } + } + return num; +} + +//////////////////////////////////////////////////////////////////////////////// +// converts a string into a float number +extern float GetNumFloat(const char* p) +{ + double num = 0; + double div = 10; + bool bNeg = false; + + if (*p == '-') + { + bNeg = true; + p++; + } + while (*p >= '0' && *p <= '9') + { + num = num * 10. + (*p - '0'); + p++; + } + + if (*p == '.') + { + p++; + while (*p >= '0' && *p <= '9') + { + num = num + (*p - '0') / div; + div = div * 10; + p++; + } + } + + int exp = 0; + if (*p == 'e' || *p == 'E') + { + char neg = 0; + p++; + if (*p == '-' || *p == '+') neg = *p++; + + while (*p >= '0' && *p <= '9') + { + exp = exp * 10 + (*p - '0'); + p++; + } + if (neg == '-') exp = -exp; + } + + while (exp > 0) + { + num *= 10.0; + exp--; + } + + while (exp < 0) + { + num /= 10.0; + exp++; + } + + if (bNeg) num = -num; + return static_cast(num); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprNum::CBotExprNum() +{ + name = "CBotExprNum"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprNum::~CBotExprNum() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExprNum::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + CBotExprNum* inst = new CBotExprNum(); + + inst->SetToken(p); + CBotString s = p->GetString(); + + inst->m_numtype = CBotTypInt; + if (p->GetType() == TokenTypDef) + { + inst->m_valint = p->GetIdKey(); + } + else + { + if (s.Find('.') >= 0 || ( s.Find('x') < 0 && ( s.Find('e') >= 0 || s.Find('E') >= 0 ) )) + { + inst->m_numtype = CBotTypFloat; + inst->m_valfloat = GetNumFloat(s); + } + else + { + inst->m_valint = GetNumInt(s); + } + } + + if (pStk->NextToken(p)) + { + CBotVar* var = CBotVar::Create(static_cast(nullptr), inst->m_numtype); + pStk->SetVar(var); + + return pStack->Return(inst, pStk); + } + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprNum::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var = CBotVar::Create(static_cast(nullptr), m_numtype); + + CBotString nombre ; + if (m_token.GetType() == TokenTypDef) + { + nombre = m_token.GetString(); + } + + switch (m_numtype) + { + case CBotTypShort: + case CBotTypInt: + var->SetValInt(m_valint, nombre); + break; + case CBotTypFloat: + var->SetValFloat(m_valfloat); + break; + } + pile->SetVar(var); // place on the stack + + return pj->Return(pile); // it's ok +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprNum::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} + diff --git a/src/CBot/CBotInstr/CBotExprNum.h b/src/CBot/CBotInstr/CBotExprNum.h new file mode 100644 index 00000000..dc213319 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprNum.h @@ -0,0 +1,78 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotExprNum class Expression representing a number. + */ +class CBotExprNum : public CBotInstr +{ + +public: + + /*! + * \brief CBotExprNum + */ + CBotExprNum(); + + /*! + * \brief ~CBotExprNum + */ + ~CBotExprNum(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execute, returns the corresponding number. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! The type of number. + int m_numtype; + //! Value for an int. + long m_valint; + //! Value for a float. + float m_valfloat; + +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 60363d30..b36e9b08 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -20,6 +20,7 @@ set(SOURCES CBotInstr/CBotCatch.cpp CBotInstr/CBotThrow.cpp CBotInstr/CBotExprAlpha.cpp + CBotInstr/CBotExprNum.cpp ) # Includes From d70527db66ace513da32ed9cda3ed8442c62754d Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 17:42:10 +0100 Subject: [PATCH 15/91] Moving CBotNew class in its own header and source files. --- src/CBot/CBot.cpp | 217 +---------------------------- src/CBot/CBot.h | 18 --- src/CBot/CBotClass.cpp | 1 + src/CBot/CBotInstr/CBotNew.cpp | 241 +++++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotNew.h | 75 ++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 319 insertions(+), 234 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotNew.cpp create mode 100644 src/CBot/CBotInstr/CBotNew.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index bc3d5c54..4c705eb3 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -45,6 +45,7 @@ #include "CBotInstr/CBotWhile.h" #include "CBotInstr/CBotExprAlpha.h" #include "CBotInstr/CBotExprNum.h" +#include "CBotInstr/CBotNew.h" // Local include @@ -3394,222 +3395,6 @@ bool CBotInstrMethode::Execute(CBotStack* &pj) return pj->Return(pile2); // release the entire stack } -/////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////// -// compile an instruction "new" - -CBotNew::CBotNew() -{ - name = "CBotNew"; - m_Parameters = nullptr; - m_nMethodeIdent = 0; -} - -CBotNew::~CBotNew() -{ -} - -CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - if (!IsOfType(p, ID_NEW)) return nullptr; - - // verifies that the token is a class name - if (p->GetType() != TokenTypVar) return nullptr; - - CBotClass* pClass = CBotClass::Find(p); - if (pClass == nullptr) - { - pStack->SetError(TX_BADNEW, p); - return nullptr; - } - - CBotNew* inst = new CBotNew(); - inst->SetToken(pp); - - inst->m_vartoken = p; - p = p->GetNext(); - - // creates the object on the "job" - // with a pointer to the object - CBotVar* pVar = CBotVar::Create("", pClass); - - // do the call of the creator - CBotCStack* pStk = pStack->TokenStack(); - { - // check if there are parameters - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStk, ppVars); - if (!pStk->IsOk()) goto error; - - // constructor exist? - CBotTypResult r = pClass->CompileMethode(pClass->GetName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // release extra stack - int typ = r.GetType(); - - // if there is no constructor, and no parameters either, it's ok - if (typ == TX_UNDEFCALL && inst->m_Parameters == nullptr) typ = 0; - pVar->SetInit(CBotVar::InitType::DEF); // mark the instance as init - - if (typ>20) - { - pStk->SetError(typ, inst->m_vartoken.GetEnd()); - goto error; - } - - // if the constructor does not exist, but there are parameters - if (typ<0 && inst->m_Parameters != nullptr) - { - pStk->SetError(TX_NOCONST, &inst->m_vartoken); - goto error; - } - - // makes pointer to the object on the stack - pStk->SetVar(pVar); - return pStack->Return(inst, pStk); - } -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - -// executes instruction "new" - -bool CBotNew::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); //main stack - - if (pile->IfStep()) return false; - - CBotStack* pile1 = pj->AddStack2(); //secondary stack - - CBotVar* pThis = nullptr; - - CBotToken* pt = &m_vartoken; - CBotClass* pClass = CBotClass::Find(pt); - - // create the variable "this" pointer type to the stack - - if ( pile->GetState()==0) - { - // create an instance of the requested class - // and initialize the pointer to that object - - - pThis = CBotVar::Create("this", pClass); - pThis->SetUniqNum(-2) ; - - pile1->SetVar(pThis); // place on stack1 - pile->IncState(); - } - - // fetch the this pointer if it was interrupted - if ( pThis == nullptr) - { - pThis = pile1->GetVar(); // find the pointer - } - - // is there an assignment or parameters (constructor) - if ( pile->GetState()==1) - { - // evaluates the constructor of the instance - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != nullptr) while ( true) - { - pile2 = pile2->AddStack(); // space on the stack for the result - if (pile2->GetState() == 0) - { - if (!p->Execute(pile2)) return false; // interrupted here? - pile2->SetState(1); - } - ppVars[i++] = pile2->GetVar(); - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - // create a variable for the result - CBotVar* pResult = nullptr; // constructos still void - - if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GetName(), - pThis, ppVars, - pResult, pile2, GetToken())) return false; // interrupt - - pThis->ConstructorSet(); // indicates that the constructor has been called - } - - return pj->Return(pile1); // passes below -} - -void CBotNew::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); //primary stack - if (pile == nullptr) return; - - CBotStack* pile1 = pj->AddStack2(); //secondary stack - - CBotToken* pt = &m_vartoken; - CBotClass* pClass = CBotClass::Find(pt); - - // create the variable "this" pointer type to the object - - if ( pile->GetState()==0) - { - return; - } - - CBotVar* pThis = pile1->GetVar(); // find the pointer - pThis->SetUniqNum(-2); - - // is ther an assignment or parameters (constructor) - if ( pile->GetState()==1) - { - // evaluates the constructor of the instance - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != nullptr) while ( true) - { - pile2 = pile2->RestoreStack(); // space on the stack for the result - if (pile2 == nullptr) return; - - if (pile2->GetState() == 0) - { - p->RestoreState(pile2, bMain); // interrupt here! - return; - } - ppVars[i++] = pile2->GetVar(); - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GetString(), pThis, - ppVars, pile2) ; // interrupt here! - } -} - ///////////////////////////////////////////////////////////// // check if two results are consistent to make an operation diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 6fc404af..76549b0f 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -988,24 +988,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotNew : public CBotInstr -{ -private: - CBotInstr* m_Parameters; // the parameters to be evaluated - long m_nMethodeIdent; -// long m_nThisIdent; - CBotToken m_vartoken; - -public: - CBotNew(); - ~CBotNew(); - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index bebd7692..79030582 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -22,6 +22,7 @@ // #include "CBot.h" +#include "CBotInstr/CBotNew.h" CBotClass* CBotClass::m_ExClass = nullptr; diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp new file mode 100644 index 00000000..d89a8ca7 --- /dev/null +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -0,0 +1,241 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotNew.h" + +// Local include + +// Global include + + + +//////////////////////////////////////////////////////////////////////////////// +CBotNew::CBotNew() +{ + name = "CBotNew"; + m_Parameters = nullptr; + m_nMethodeIdent = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotNew::~CBotNew() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotNew::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + if (!IsOfType(p, ID_NEW)) return nullptr; + + // verifies that the token is a class name + if (p->GetType() != TokenTypVar) return nullptr; + + CBotClass* pClass = CBotClass::Find(p); + if (pClass == nullptr) + { + pStack->SetError(TX_BADNEW, p); + return nullptr; + } + + CBotNew* inst = new CBotNew(); + inst->SetToken(pp); + + inst->m_vartoken = p; + p = p->GetNext(); + + // creates the object on the "job" + // with a pointer to the object + CBotVar* pVar = CBotVar::Create("", pClass); + + // do the call of the creator + CBotCStack* pStk = pStack->TokenStack(); + { + // check if there are parameters + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStk, ppVars); + if (!pStk->IsOk()) goto error; + + // constructor exist? + CBotTypResult r = pClass->CompileMethode(pClass->GetName(), pVar, ppVars, pStk, inst->m_nMethodeIdent); + delete pStk->TokenStack(); // release extra stack + int typ = r.GetType(); + + // if there is no constructor, and no parameters either, it's ok + if (typ == TX_UNDEFCALL && inst->m_Parameters == nullptr) typ = 0; + pVar->SetInit(CBotVar::InitType::DEF); // mark the instance as init + + if (typ>20) + { + pStk->SetError(typ, inst->m_vartoken.GetEnd()); + goto error; + } + + // if the constructor does not exist, but there are parameters + if (typ<0 && inst->m_Parameters != nullptr) + { + pStk->SetError(TX_NOCONST, &inst->m_vartoken); + goto error; + } + + // makes pointer to the object on the stack + pStk->SetVar(pVar); + return pStack->Return(inst, pStk); + } +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotNew::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); //main stack + + if (pile->IfStep()) return false; + + CBotStack* pile1 = pj->AddStack2(); //secondary stack + + CBotVar* pThis = nullptr; + + CBotToken* pt = &m_vartoken; + CBotClass* pClass = CBotClass::Find(pt); + + // create the variable "this" pointer type to the stack + + if ( pile->GetState()==0) + { + // create an instance of the requested class + // and initialize the pointer to that object + + + pThis = CBotVar::Create("this", pClass); + pThis->SetUniqNum(-2) ; + + pile1->SetVar(pThis); // place on stack1 + pile->IncState(); + } + + // fetch the this pointer if it was interrupted + if ( pThis == nullptr) + { + pThis = pile1->GetVar(); // find the pointer + } + + // is there an assignment or parameters (constructor) + if ( pile->GetState()==1) + { + // evaluates the constructor of the instance + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != nullptr) while ( true) + { + pile2 = pile2->AddStack(); // space on the stack for the result + if (pile2->GetState() == 0) + { + if (!p->Execute(pile2)) return false; // interrupted here? + pile2->SetState(1); + } + ppVars[i++] = pile2->GetVar(); + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + // create a variable for the result + CBotVar* pResult = nullptr; // constructos still void + + if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GetName(), + pThis, ppVars, + pResult, pile2, GetToken())) return false; // interrupt + + pThis->ConstructorSet(); // indicates that the constructor has been called + } + + return pj->Return(pile1); // passes below +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotNew::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); //primary stack + if (pile == nullptr) return; + + CBotStack* pile1 = pj->AddStack2(); //secondary stack + + CBotToken* pt = &m_vartoken; + CBotClass* pClass = CBotClass::Find(pt); + + // create the variable "this" pointer type to the object + + if ( pile->GetState()==0) + { + return; + } + + CBotVar* pThis = pile1->GetVar(); // find the pointer + pThis->SetUniqNum(-2); + + // is ther an assignment or parameters (constructor) + if ( pile->GetState()==1) + { + // evaluates the constructor of the instance + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != nullptr) while ( true) + { + pile2 = pile2->RestoreStack(); // space on the stack for the result + if (pile2 == nullptr) return; + + if (pile2->GetState() == 0) + { + p->RestoreState(pile2, bMain); // interrupt here! + return; + } + ppVars[i++] = pile2->GetVar(); + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + pClass->RestoreMethode(m_nMethodeIdent, m_vartoken.GetString(), pThis, + ppVars, pile2) ; // interrupt here! + } +} diff --git a/src/CBot/CBotInstr/CBotNew.h b/src/CBot/CBotInstr/CBotNew.h new file mode 100644 index 00000000..77e2b2c6 --- /dev/null +++ b/src/CBot/CBotInstr/CBotNew.h @@ -0,0 +1,75 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotNew class Compile an instruction "new". + */ +class CBotNew : public CBotInstr +{ +public: + + /*! + * \brief CBotNew + */ + CBotNew(); + + /*! + * \brief ~CBotNew + */ + ~CBotNew(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Executes instruction "new". + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! The parameters to be evaluated + CBotInstr* m_Parameters; + long m_nMethodeIdent; + CBotToken m_vartoken; + +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index b36e9b08..6b2aeab5 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -21,6 +21,7 @@ set(SOURCES CBotInstr/CBotThrow.cpp CBotInstr/CBotExprAlpha.cpp CBotInstr/CBotExprNum.cpp + CBotInstr/CBotNew.cpp ) # Includes From 85756b4da15386af93c1d69c450018bb9767e978 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 17:47:44 +0100 Subject: [PATCH 16/91] Moving CBotExprNan class in its own header and source files. --- src/CBot/CBot.cpp | 33 +---------------- src/CBot/CBot.h | 12 ------ src/CBot/CBotInstr/CBotExprNan.cpp | 55 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprNan.h | 59 ++++++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 116 insertions(+), 44 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprNan.cpp create mode 100644 src/CBot/CBotInstr/CBotExprNan.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 4c705eb3..b69af068 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -46,6 +46,7 @@ #include "CBotInstr/CBotExprAlpha.h" #include "CBotInstr/CBotExprNum.h" #include "CBotInstr/CBotNew.h" +#include "CBotInstr/CBotExprNan.h" // Local include @@ -2773,38 +2774,6 @@ void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) if (bMain) pj->RestoreStack(this); } -////////////////////////////////////////////////////////////////////////////////////////// - -// management of the operand "nan" - -CBotExprNan::CBotExprNan() -{ - name = "CBotExprNan"; -} - -CBotExprNan::~CBotExprNan() -{ -} - -// executes, returns null pointer - -bool CBotExprNan::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypInt); - - var->SetInit(CBotVar::InitType::IS_NAN); // nan - pile->SetVar(var); // put on the stack - return pj->Return(pile); // forward below -} - -void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - ////////////////////////////////////////////////////////////////////////////////////// // compile a variable name // check that it is known on the stack diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 76549b0f..1e0f91e1 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -976,18 +976,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotExprNan : public CBotInstr -{ -private: - -public: - CBotExprNan(); - ~CBotExprNan(); - - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprNan.cpp b/src/CBot/CBotInstr/CBotExprNan.cpp new file mode 100644 index 00000000..3eebb74a --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprNan.cpp @@ -0,0 +1,55 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprNan.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotExprNan::CBotExprNan() +{ + name = "CBotExprNan"; +} +//////////////////////////////////////////////////////////////////////////////// +CBotExprNan::~CBotExprNan() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprNan::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypInt); + + var->SetInit(CBotVar::InitType::IS_NAN); // nan + pile->SetVar(var); // put on the stack + return pj->Return(pile); // forward below +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprNan::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} diff --git a/src/CBot/CBotInstr/CBotExprNan.h b/src/CBot/CBotInstr/CBotExprNan.h new file mode 100644 index 00000000..0d6bbf6f --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprNan.h @@ -0,0 +1,59 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotExprNan class Management of the operand "nan". + */ +class CBotExprNan : public CBotInstr +{ +public: + + /*! + * \brief CBotExprNan + */ + CBotExprNan(); + + /*! + * \brief ~CBotExprNan + */ + ~CBotExprNan(); + + /*! + * \brief Execute Executes, returns null pointer. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 6b2aeab5..c45dd1c4 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -22,6 +22,7 @@ set(SOURCES CBotInstr/CBotExprAlpha.cpp CBotInstr/CBotExprNum.cpp CBotInstr/CBotNew.cpp + CBotInstr/CBotExprNan.cpp ) # Includes From 27a2c0b55aade64c57e59e8cc280ab91ce089a7d Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 17:52:41 +0100 Subject: [PATCH 17/91] Moving CBotExprNull class in its own header and source files. --- src/CBot/CBot.cpp | 33 +--------------- src/CBot/CBot.h | 14 ------- src/CBot/CBotInstr/CBotExprNull.cpp | 56 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprNull.h | 60 +++++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 118 insertions(+), 46 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprNull.cpp create mode 100644 src/CBot/CBotInstr/CBotExprNull.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index b69af068..25efe2bd 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -47,6 +47,7 @@ #include "CBotInstr/CBotExprNum.h" #include "CBotInstr/CBotNew.h" #include "CBotInstr/CBotExprNan.h" +#include "CBotInstr/CBotExprNull.h" // Local include @@ -2742,38 +2743,6 @@ void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) if (bMain) pj->RestoreStack(this); } -////////////////////////////////////////////////////////////////////////////////////////// - -// management of the operand "null" - -CBotExprNull::CBotExprNull() -{ - name = "CBotExprNull"; -} - -CBotExprNull::~CBotExprNull() -{ -} - -// executes, returns an empty pointer - -bool CBotExprNull::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypNullPointer); - - var->SetInit(CBotVar::InitType::DEF); // null pointer valid - pile->SetVar(var); // place on the stack - return pj->Return(pile); // forwards below -} - -void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - ////////////////////////////////////////////////////////////////////////////////////// // compile a variable name // check that it is known on the stack diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 1e0f91e1..e860c4a6 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -963,20 +963,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -class CBotExprNull : public CBotInstr -{ -private: - -public: - CBotExprNull(); - ~CBotExprNull(); - - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprNull.cpp b/src/CBot/CBotInstr/CBotExprNull.cpp new file mode 100644 index 00000000..0324d50b --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprNull.cpp @@ -0,0 +1,56 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprNull.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotExprNull::CBotExprNull() +{ + name = "CBotExprNull"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprNull::~CBotExprNull() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprNull::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypNullPointer); + + var->SetInit(CBotVar::InitType::DEF); // null pointer valid + pile->SetVar(var); // place on the stack + return pj->Return(pile); // forwards below +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprNull::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} diff --git a/src/CBot/CBotInstr/CBotExprNull.h b/src/CBot/CBotInstr/CBotExprNull.h new file mode 100644 index 00000000..187cb261 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprNull.h @@ -0,0 +1,60 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotExprNull class Management of the operand "null". + */ +class CBotExprNull : public CBotInstr +{ +public: + + /*! + * \brief CBotExprNull + */ + CBotExprNull(); + + /*! + * \brief ~CBotExprNull + */ + ~CBotExprNull(); + + /*! + * \brief Execute Executes, returns an empty pointer. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index c45dd1c4..5701d39b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -23,6 +23,7 @@ set(SOURCES CBotInstr/CBotExprNum.cpp CBotInstr/CBotNew.cpp CBotInstr/CBotExprNan.cpp + CBotInstr/CBotExprNull.cpp ) # Includes From 2f52520421b3972d9d5bf14ad5caa1b9fcf6894f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 17:59:50 +0100 Subject: [PATCH 18/91] Moving CBotExprBool class in its own header and source files. --- src/CBot/CBot.cpp | 54 +------------------- src/CBot/CBot.h | 15 ------ src/CBot/CBotInstr/CBotExprBool.cpp | 79 +++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprBool.h | 68 +++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 149 insertions(+), 68 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprBool.cpp create mode 100644 src/CBot/CBotInstr/CBotExprBool.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 25efe2bd..df2db443 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -48,6 +48,7 @@ #include "CBotInstr/CBotNew.h" #include "CBotInstr/CBotExprNan.h" #include "CBotInstr/CBotExprNull.h" +#include "CBotInstr/CBotExprBool.h" // Local include @@ -2690,59 +2691,6 @@ void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) m_next3->RestoreStateVar(pile, bMain); } -////////////////////////////////////////////////////////////////////////////////////// -// compile a token representing true or false - -CBotExprBool::CBotExprBool() -{ - name = "CBotExprBool"; -} - -CBotExprBool::~CBotExprBool() -{ -} - -CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - CBotExprBool* inst = nullptr; - - if ( p->GetType() == ID_TRUE || - p->GetType() == ID_FALSE ) - { - inst = new CBotExprBool(); - inst->SetToken(p); // stores the operation false or true - p = p->GetNext(); - - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypBoolean); - pStk->SetVar(var); - } - - return pStack->Return(inst, pStk); -} - -// executes, returns true or false - -bool CBotExprBool::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypBoolean); - - if (GetTokenType() == ID_TRUE) var->SetValInt(1); - else var->SetValInt(0); - - pile->SetVar(var); // put on the stack - return pj->Return(pile); // forwards below -} - -void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) pj->RestoreStack(this); -} - ////////////////////////////////////////////////////////////////////////////////////// // compile a variable name // check that it is known on the stack diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index e860c4a6..eebdb95b 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -948,21 +948,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -class CBotExprBool : public CBotInstr -{ -private: - -public: - CBotExprBool(); - ~CBotExprBool(); - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprBool.cpp b/src/CBot/CBotInstr/CBotExprBool.cpp new file mode 100644 index 00000000..17c09e55 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprBool.cpp @@ -0,0 +1,79 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprBool.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotExprBool::CBotExprBool() +{ + name = "CBotExprBool"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprBool::~CBotExprBool() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExprBool::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + CBotExprBool* inst = nullptr; + + if ( p->GetType() == ID_TRUE || + p->GetType() == ID_FALSE ) + { + inst = new CBotExprBool(); + inst->SetToken(p); // stores the operation false or true + p = p->GetNext(); + + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypBoolean); + pStk->SetVar(var); + } + + return pStack->Return(inst, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprBool::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var = CBotVar::Create(static_cast(nullptr), CBotTypBoolean); + + if (GetTokenType() == ID_TRUE) var->SetValInt(1); + else var->SetValInt(0); + + pile->SetVar(var); // put on the stack + return pj->Return(pile); // forwards below +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprBool::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) pj->RestoreStack(this); +} diff --git a/src/CBot/CBotInstr/CBotExprBool.h b/src/CBot/CBotInstr/CBotExprBool.h new file mode 100644 index 00000000..a2609e26 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprBool.h @@ -0,0 +1,68 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotExprBool class Compile a token representing true or false. + */ +class CBotExprBool : public CBotInstr +{ +public: + + /*! + * \brief CBotExprBool + */ + CBotExprBool(); + + /*! + * \brief ~CBotExprBool + */ + ~CBotExprBool(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Executes, returns true or false. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 5701d39b..104e2b1e 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -24,6 +24,7 @@ set(SOURCES CBotInstr/CBotNew.cpp CBotInstr/CBotExprNan.cpp CBotInstr/CBotExprNull.cpp + CBotInstr/CBotExprBool.cpp ) # Includes From a878b0d2527ebc74abdf6c6910c3108039985d27 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 18:07:11 +0100 Subject: [PATCH 19/91] Moving CBotLeftExprVar class in its own header and source files. --- src/CBot/CBot.cpp | 63 +------------------ src/CBot/CBot.h | 18 ------ src/CBot/CBotClass.cpp | 2 +- src/CBot/CBotInstr/CBotLeftExprVar.cpp | 83 ++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotLeftExprVar.h | 75 +++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 161 insertions(+), 81 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotLeftExprVar.cpp create mode 100644 src/CBot/CBotInstr/CBotLeftExprVar.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index df2db443..eff35498 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -49,6 +49,7 @@ #include "CBotInstr/CBotExprNan.h" #include "CBotInstr/CBotExprNull.h" #include "CBotInstr/CBotExprBool.h" +#include "CBotInstr/CBotLeftExprVar.h" // Local include @@ -498,68 +499,6 @@ void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) if (p != nullptr) p->RestoreState(pile, true); } -////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// compilation of an element to the left of an assignment - -CBotLeftExprVar::CBotLeftExprVar() -{ - name = "CBotLeftExprVar"; - m_typevar = -1; - m_nIdent = 0; -} - -CBotLeftExprVar::~CBotLeftExprVar() -{ -} - -CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) -{ - // verifies that the token is a variable name - if (p->GetType() != TokenTypVar) - { - pStack->SetError( TX_NOVAR, p->GetStart()); - return nullptr; - } - - CBotLeftExprVar* inst = new CBotLeftExprVar(); - inst->SetToken(p); - p = p->GetNext(); - - return inst; -} - -// creates a variable and assigns the result to the stack -bool CBotLeftExprVar::Execute(CBotStack* &pj) -{ - CBotVar* var1; - CBotVar* var2; - - var1 = CBotVar::Create(m_token.GetString(), m_typevar); - var1->SetUniqNum(m_nIdent); // with the unique identifier - pj->AddVar(var1); // place it on the stack - - var2 = pj->GetVar(); // result on the stack - if (var2) var1->SetVal(var2); // do the assignment - - return true; -} - -void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotVar* var1; - - var1 = pj->FindVar(m_token.GetString()); - if (var1 == nullptr) assert(0); - - var1->SetUniqNum(m_nIdent); // with the unique identifier -} - -////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // defining an array of any type // int a[12]; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index eebdb95b..00e766e7 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -930,24 +930,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -class CBotLeftExprVar : public CBotInstr -{ -private: -public: - CBotTypResult - m_typevar; // type of variable declared - long m_nIdent; // unique identifier for that variable - -public: - CBotLeftExprVar(); - ~CBotLeftExprVar(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 79030582..b7aeb937 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -23,7 +23,7 @@ #include "CBot.h" #include "CBotInstr/CBotNew.h" - +#include "CBotInstr/CBotLeftExprVar.h" CBotClass* CBotClass::m_ExClass = nullptr; diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.cpp b/src/CBot/CBotInstr/CBotLeftExprVar.cpp new file mode 100644 index 00000000..43ed496d --- /dev/null +++ b/src/CBot/CBotInstr/CBotLeftExprVar.cpp @@ -0,0 +1,83 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotLeftExprVar.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotLeftExprVar::CBotLeftExprVar() +{ + name = "CBotLeftExprVar"; + m_typevar = -1; + m_nIdent = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotLeftExprVar::~CBotLeftExprVar() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotLeftExprVar::Compile(CBotToken* &p, CBotCStack* pStack) +{ + // verifies that the token is a variable name + if (p->GetType() != TokenTypVar) + { + pStack->SetError( TX_NOVAR, p->GetStart()); + return nullptr; + } + + CBotLeftExprVar* inst = new CBotLeftExprVar(); + inst->SetToken(p); + p = p->GetNext(); + + return inst; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotLeftExprVar::Execute(CBotStack* &pj) +{ + CBotVar* var1; + CBotVar* var2; + + var1 = CBotVar::Create(m_token.GetString(), m_typevar); + var1->SetUniqNum(m_nIdent); // with the unique identifier + pj->AddVar(var1); // place it on the stack + + var2 = pj->GetVar(); // result on the stack + if (var2) var1->SetVal(var2); // do the assignment + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotLeftExprVar::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotVar* var1; + + var1 = pj->FindVar(m_token.GetString()); + if (var1 == nullptr) assert(0); + + var1->SetUniqNum(m_nIdent); // with the unique identifier +} diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.h b/src/CBot/CBotInstr/CBotLeftExprVar.h new file mode 100644 index 00000000..a264dbae --- /dev/null +++ b/src/CBot/CBotInstr/CBotLeftExprVar.h @@ -0,0 +1,75 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotLeftExprVar class Compilation of an element to the left of an assignment. + */ +class CBotLeftExprVar : public CBotInstr +{ +public: + + /*! + * \brief CBotLeftExprVar + */ + CBotLeftExprVar(); + + /*! + * \brief ~CBotLeftExprVar + */ + ~CBotLeftExprVar(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Creates a variable and assigns the result to the stack. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +public: + + //! Type of variable declared. + CBotTypResult m_typevar; + //! Unique identifier for that variable. + long m_nIdent; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 104e2b1e..5f2a0208 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCES CBotInstr/CBotExprNan.cpp CBotInstr/CBotExprNull.cpp CBotInstr/CBotExprBool.cpp + CBotInstr/CBotLeftExprVar.cpp ) # Includes From 605b1b244aeaedbc219ac0bd313de865a0555013 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 18:16:49 +0100 Subject: [PATCH 20/91] Moving CBotPreIncExpr class in its own header and source files. --- src/CBot/CBot.cpp | 66 +------------------ src/CBot/CBot.h | 15 ----- src/CBot/CBotInstr/CBotPreIncExpr.cpp | 92 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotPreIncExpr.h | 65 +++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 159 insertions(+), 80 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotPreIncExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotPreIncExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index eff35498..f6c11ca8 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -50,6 +50,7 @@ #include "CBotInstr/CBotExprNull.h" #include "CBotInstr/CBotExprBool.h" #include "CBotInstr/CBotLeftExprVar.h" +#include "CBotInstr/CBotPreIncExpr.h" // Local include @@ -2001,17 +2002,6 @@ CBotPostIncExpr::~CBotPostIncExpr() delete m_Instr; } -CBotPreIncExpr::CBotPreIncExpr() -{ - m_Instr = nullptr; - name = "CBotPreIncExpr"; -} - -CBotPreIncExpr::~CBotPreIncExpr() -{ - delete m_Instr; -} - bool CBotPostIncExpr::Execute(CBotStack* &pj) { CBotStack* pile1 = pj->AddStack(this); @@ -2056,60 +2046,6 @@ void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) if (pile1 != nullptr) pile1->RestoreStack(this); } -bool CBotPreIncExpr::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->IfStep()) return false; - - CBotVar* var1; - - if (pile->GetState() == 0) - { - CBotStack* pile2 = pile; - // retrieves the variable fields and indexes according - // pile2 is modified on return - if (!(static_cast(m_Instr))->ExecuteVar(var1, pile2, nullptr, true)) return false; - - if (var1->IsNAN()) - { - pile->SetError(TX_OPNAN, &m_token); - return pj->Return(pile); // operation performed - } - - if (!var1->IsDefined()) - { - pile->SetError(TX_NOTINIT, &m_token); - return pj->Return(pile); // operation performed - } - - if (GetTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); // ((CBotVarInt*)var1)->m_val - - pile->IncState(); - } - - if (!m_Instr->Execute(pile)) return false; - return pj->Return(pile); // operation performed -} - - -void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - if (pile->GetState() == 0) - { - return; - } - - m_Instr->RestoreState(pile, bMain); -} - - ////////////////////////////////////////////////////////////////////////////////////// // compile an unary expression // + diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 00e766e7..814183b9 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -915,21 +915,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotPreIncExpr : public CBotInstr -{ -private: - CBotInstr* m_Instr; - friend class CBotParExpr; - -public: - CBotPreIncExpr(); - ~CBotPreIncExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.cpp b/src/CBot/CBotInstr/CBotPreIncExpr.cpp new file mode 100644 index 00000000..700bbffa --- /dev/null +++ b/src/CBot/CBotInstr/CBotPreIncExpr.cpp @@ -0,0 +1,92 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotPreIncExpr.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotPreIncExpr::CBotPreIncExpr() +{ + m_Instr = nullptr; + name = "CBotPreIncExpr"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotPreIncExpr::~CBotPreIncExpr() +{ + delete m_Instr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotPreIncExpr::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->IfStep()) return false; + + CBotVar* var1; + + if (pile->GetState() == 0) + { + CBotStack* pile2 = pile; + // retrieves the variable fields and indexes according + // pile2 is modified on return + if (!(static_cast(m_Instr))->ExecuteVar(var1, pile2, nullptr, true)) return false; + + if (var1->IsNAN()) + { + pile->SetError(TX_OPNAN, &m_token); + return pj->Return(pile); // operation performed + } + + if (!var1->IsDefined()) + { + pile->SetError(TX_NOTINIT, &m_token); + return pj->Return(pile); // operation performed + } + + if (GetTokenType() == ID_INC) var1->Inc(); + else var1->Dec(); // ((CBotVarInt*)var1)->m_val + + pile->IncState(); + } + + if (!m_Instr->Execute(pile)) return false; + return pj->Return(pile); // operation performed +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotPreIncExpr::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + if (pile->GetState() == 0) + { + return; + } + + m_Instr->RestoreState(pile, bMain); +} diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.h b/src/CBot/CBotInstr/CBotPreIncExpr.h new file mode 100644 index 00000000..ede694d4 --- /dev/null +++ b/src/CBot/CBotInstr/CBotPreIncExpr.h @@ -0,0 +1,65 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotPreIncExpr class Management of pre increment. There is no + * routine Compiles, the object is created directly. Compiles in CBotParExpr + */ +class CBotPreIncExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotPreIncExpr + */ + CBotPreIncExpr(); + + /*! + * \brief ~CBotPreIncExpr + */ + ~CBotPreIncExpr(); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + CBotInstr* m_Instr; + friend class CBotParExpr; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 5f2a0208..b7c63490 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -26,6 +26,7 @@ set(SOURCES CBotInstr/CBotExprNull.cpp CBotInstr/CBotExprBool.cpp CBotInstr/CBotLeftExprVar.cpp + CBotInstr/CBotPreIncExpr.cpp ) # Includes From 5343c15d603096493189812b6d645440dc34e02d Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 18:22:04 +0100 Subject: [PATCH 21/91] Moving CBotPostIncExpr class in its own header and source files. --- src/CBot/CBot.cpp | 63 +------------------ src/CBot/CBot.h | 15 ----- src/CBot/CBotInstr/CBotPostIncExpr.cpp | 85 ++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotPostIncExpr.h | 69 +++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 156 insertions(+), 77 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotPostIncExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotPostIncExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index f6c11ca8..b7c8287b 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -51,6 +51,7 @@ #include "CBotInstr/CBotExprBool.h" #include "CBotInstr/CBotLeftExprVar.h" #include "CBotInstr/CBotPreIncExpr.h" +#include "CBotInstr/CBotPostIncExpr.h" // Local include @@ -1983,68 +1984,6 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(nullptr, pStk); } -////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// Management of pre-and post increment / decrement -// There is no routine Compiles, the object is created directly -// Compiles in CBotParExpr :: - - -CBotPostIncExpr::CBotPostIncExpr() -{ - m_Instr = nullptr; - name = "CBotPostIncExpr"; -} - -CBotPostIncExpr::~CBotPostIncExpr() -{ - delete m_Instr; -} - -bool CBotPostIncExpr::Execute(CBotStack* &pj) -{ - CBotStack* pile1 = pj->AddStack(this); - CBotStack* pile2 = pile1; - - CBotVar* var1 = nullptr; - - // retrieves the variable fields and indexes according - if (!(static_cast(m_Instr))->ExecuteVar(var1, pile2, nullptr, true)) return false; - - pile1->SetState(1); - pile1->SetCopyVar(var1); // places the result (before incrementation); - - CBotStack* pile3 = pile2->AddStack(this); - if (pile3->IfStep()) return false; - - if (var1->IsNAN()) - { - pile1->SetError(TX_OPNAN, &m_token); - } - - if (!var1->IsDefined()) - { - pile1->SetError(TX_NOTINIT, &m_token); - } - - if (GetTokenType() == ID_INC) var1->Inc(); - else var1->Dec(); - - return pj->Return(pile1); // operation done, result on pile2 -} - -void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile1 = pj->RestoreStack(this); - if (pile1 == nullptr) return; - - (static_cast(m_Instr))->RestoreStateVar(pile1, bMain); - - if (pile1 != nullptr) pile1->RestoreStack(this); -} ////////////////////////////////////////////////////////////////////////////////////// // compile an unary expression diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 814183b9..6b27e563 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -900,21 +900,6 @@ public: void RestoreStateVar(CBotStack* &pj, bool bMain) override; }; -class CBotPostIncExpr : public CBotInstr -{ -private: - CBotInstr* m_Instr; - friend class CBotParExpr; - -public: - CBotPostIncExpr(); - ~CBotPostIncExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.cpp b/src/CBot/CBotInstr/CBotPostIncExpr.cpp new file mode 100644 index 00000000..a0b24fc0 --- /dev/null +++ b/src/CBot/CBotInstr/CBotPostIncExpr.cpp @@ -0,0 +1,85 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotPostIncExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotPostIncExpr::CBotPostIncExpr() +{ + m_Instr = nullptr; + name = "CBotPostIncExpr"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotPostIncExpr::~CBotPostIncExpr() +{ + delete m_Instr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotPostIncExpr::Execute(CBotStack* &pj) +{ + CBotStack* pile1 = pj->AddStack(this); + CBotStack* pile2 = pile1; + + CBotVar* var1 = nullptr; + + // retrieves the variable fields and indexes according + if (!(static_cast(m_Instr))->ExecuteVar(var1, pile2, nullptr, true)) return false; + + pile1->SetState(1); + pile1->SetCopyVar(var1); // places the result (before incrementation); + + CBotStack* pile3 = pile2->AddStack(this); + if (pile3->IfStep()) return false; + + if (var1->IsNAN()) + { + pile1->SetError(TX_OPNAN, &m_token); + } + + if (!var1->IsDefined()) + { + pile1->SetError(TX_NOTINIT, &m_token); + } + + if (GetTokenType() == ID_INC) var1->Inc(); + else var1->Dec(); + + return pj->Return(pile1); // operation done, result on pile2 +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotPostIncExpr::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile1 = pj->RestoreStack(this); + if (pile1 == nullptr) return; + + (static_cast(m_Instr))->RestoreStateVar(pile1, bMain); + + if (pile1 != nullptr) pile1->RestoreStack(this); +} diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.h b/src/CBot/CBotInstr/CBotPostIncExpr.h new file mode 100644 index 00000000..5a3240c8 --- /dev/null +++ b/src/CBot/CBotInstr/CBotPostIncExpr.h @@ -0,0 +1,69 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +////////////////////////////////////////////////////////////////////////////////////// +// Management of pre-and post increment / decrement +// There is no routine Compiles, the object is created directly +// Compiles in CBotParExpr :: + +/*! + * \brief The CBotPostIncExpr class + */ +class CBotPostIncExpr : public CBotInstr +{ + +public: + + /*! + * \brief CBotPostIncExpr + */ + CBotPostIncExpr(); + + /*! + * \brief ~CBotPostIncExpr + */ + ~CBotPostIncExpr(); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + CBotInstr* m_Instr; + friend class CBotParExpr; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index b7c63490..158478b9 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -27,6 +27,7 @@ set(SOURCES CBotInstr/CBotExprBool.cpp CBotInstr/CBotLeftExprVar.cpp CBotInstr/CBotPreIncExpr.cpp + CBotInstr/CBotPostIncExpr.cpp ) # Includes From d708be50e736722c53abb257cf779d35ed2cb7f3 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 18:31:31 +0100 Subject: [PATCH 22/91] Moving CBotExprVar class in its own header and source files. --- src/CBot/CBot.cpp | 282 +---------------------- src/CBot/CBot.h | 23 -- src/CBot/CBotInstr/CBotExprVar.cpp | 301 +++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprVar.h | 101 +++++++++ src/CBot/CBotInstr/CBotPostIncExpr.cpp | 1 + src/CBot/CBotInstr/CBotPreIncExpr.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 7 files changed, 406 insertions(+), 304 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprVar.cpp create mode 100644 src/CBot/CBotInstr/CBotExprVar.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index b7c8287b..9dc07208 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -52,6 +52,7 @@ #include "CBotInstr/CBotLeftExprVar.h" #include "CBotInstr/CBotPreIncExpr.h" #include "CBotInstr/CBotPostIncExpr.h" +#include "CBotInstr/CBotExprVar.h" // Local include @@ -2505,287 +2506,6 @@ void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) m_next3->RestoreStateVar(pile, bMain); } -////////////////////////////////////////////////////////////////////////////////////// -// compile a variable name -// check that it is known on the stack -// and it has been initialized - -CBotExprVar::CBotExprVar() -{ - name = "CBotExprVar"; - m_nIdent = 0; -} - -CBotExprVar::~CBotExprVar() -{ -} - -CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) -{ -// CBotToken* pDebut = p; - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GetStart()); - - // is it a variable? - if (p->GetType() == TokenTypVar) - { - CBotInstr* inst = new CBotExprVar(); // create the object - - inst->SetToken(p); - - CBotVar* var; - - if (nullptr != (var = pStk->FindVar(p))) // seek if known variable - { - int ident = var->GetUniqNum(); - (static_cast(inst))->m_nIdent = ident; // identifies variable by its number - - if (ident > 0 && ident < 9000) - { - if ( var->IsPrivate(privat) && - !pStk->GetBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, p); - goto err; - } - - // This is an element of the current class - // ads the equivalent of this. before - CBotToken token("this"); - inst->SetToken(&token); - (static_cast(inst))->m_nIdent = -2; // identificator for this - - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(p); // keeps the name of the token - i->SetUniqNum(ident); - inst->AddNext3(i); // added after - } - - p = p->GetNext(); // next token - - while (true) - { - if (var->GetType() == CBotTypArrayPointer) - { - if (IsOfType( p, ID_OPBRK )) // check if there is an aindex - { - CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); // compile the formula - inst->AddNext3(i); // add to the chain - - var = (static_cast(var))->GetItem(0,true); // gets the component [0] - - if (i->m_expr == nullptr) - { - pStk->SetError(TX_BADINDEX, p->GetStart()); - goto err; - } - if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto err; - } - continue; - } - } - if (var->GetType(1) == CBotTypPointer) // for classes - { - if (IsOfType(p, ID_DOT)) - { - CBotToken* pp = p; - - if (p->GetType() == TokenTypVar) // must be a name - { - if (p->GetNext()->GetType() == ID_OPENPAR) // a method call? - { - CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); - if (!pStk->IsOk()) goto err; - inst->AddNext3(i); // added after - return pStack->Return(inst, pStk); - } - else - { - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(pp); // keeps the name of the token - inst->AddNext3(i); // add after - var = var->GetItem(p->GetString()); // get item correspondent - if (var != nullptr) - { - i->SetUniqNum(var->GetUniqNum()); - if ( var->IsPrivate() && - !pStk->GetBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, pp); - goto err; - } - } - } - - - if (var != nullptr) - { - p = p->GetNext(); // skips the name - continue; - } - pStk->SetError(TX_NOITEM, p); - goto err; - } - pStk->SetError(TX_DOT, p->GetStart()); - goto err; - } - } - - break; - } - - pStk->SetCopyVar(var); // place the copy of the variable on the stack (for type) - if (pStk->IsOk()) return pStack->Return(inst, pStk); - } - pStk->SetError(TX_UNDEFVAR, p); -err: - delete inst; - return pStack->Return(nullptr, pStk); - } - - return pStack->Return(nullptr, pStk); -} - -CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(pp->GetStart()); - - // is it a variable ? - if (pp->GetType() == TokenTypVar) - { - CBotToken pthis("this"); - CBotVar* var = pStk->FindVar(pthis); - if (var == nullptr) return pStack->Return(nullptr, pStk); - - CBotInstr* inst = new CBotExprVar(); - - // this is an element of the current class - // adds the equivalent of this. before - - inst->SetToken(&pthis); - (static_cast(inst))->m_nIdent = -2; // ident for this - - CBotToken* pp = p; - - if (pp->GetType() == TokenTypVar) - { - if (pp->GetNext()->GetType() == ID_OPENPAR) // a method call? - { - CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); - if (pStk->IsOk()) - { - inst->AddNext3(i); // add after - p = pp; // previous instruction - return pStack->Return(inst, pStk); - } - pStk->SetError(0,0); // the error is not adressed here - } - } - delete inst; - } - return pStack->Return(nullptr, pStk); -} - - -// execute, making the value of a variable - -bool CBotExprVar::Execute(CBotStack* &pj) -{ - CBotVar* pVar = nullptr; - CBotStack* pile = pj->AddStack(this); - - CBotStack* pile1 = pile; - - if (pile1->GetState() == 0) - { - if (!ExecuteVar(pVar, pile, nullptr, true)) return false; // Get the variable fields and indexes according - - if (pVar) pile1->SetCopyVar(pVar); // place a copy on the stack - else - { - return pj->Return(pile1); - } - pile1->IncState(); - } - - pVar = pile1->GetVar(); - - if (pVar == nullptr) - { - return pj->Return(pile1); - } - - if (pVar->IsUndefined()) - { - CBotToken* pt = &m_token; - while (pt->GetNext() != nullptr) pt = pt->GetNext(); - pile1->SetError(TX_NOTINIT, pt); - return pj->Return(pile1); - } - return pj->Return(pile1); // operation completed -} - -void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - CBotStack* pile1 = pile; - - if (pile1->GetState() == 0) - { - RestoreStateVar(pile, bMain); // retrieves the variable fields and indexes according - return; - } -} - -// fetch a variable at runtime - -bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) -{ - CBotStack* pile = pj; - pj = pj->AddStack(this); - - if (bStep && m_nIdent>0 && pj->IfStep()) return false; - - pVar = pj->FindVar(m_nIdent, true); // tries with the variable update if necessary - if (pVar == nullptr) - { -#ifdef _DEBUG - assert(0); -#endif - pj->SetError(1, &m_token); - return false; - } - if ( m_next3 != nullptr && - !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) - return false; // field of an instance, table, methode - - return pile->ReturnKeep(pj); // does not put on stack but get the result if a method was called -} - - -// fetch variable at runtime - -void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) -{ - pj = pj->RestoreStack(this); - if (pj == nullptr) return; - - if (m_next3 != nullptr) - m_next3->RestoreStateVar(pj, bMain); -} - ////////////////////////////////////////////////////////////////////////////////////////// // compile a list of parameters diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 6b27e563..ffd2273b 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -877,29 +877,6 @@ public: void RestoreStateVar(CBotStack* &pj, bool bMain) override; }; -// expression for the variable name - -class CBotExprVar : public CBotInstr -{ -private: - long m_nIdent; - friend class CBotPostIncExpr; - friend class CBotPreIncExpr; - -public: - CBotExprVar(); - ~CBotExprVar(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); - static - CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); - - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); - void RestoreStateVar(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp new file mode 100644 index 00000000..5e0c5683 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -0,0 +1,301 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprVar.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotExprVar::CBotExprVar() +{ + name = "CBotExprVar"; + m_nIdent = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprVar::~CBotExprVar() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) +{ +// CBotToken* pDebut = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GetStart()); + + // is it a variable? + if (p->GetType() == TokenTypVar) + { + CBotInstr* inst = new CBotExprVar(); // create the object + + inst->SetToken(p); + + CBotVar* var; + + if (nullptr != (var = pStk->FindVar(p))) // seek if known variable + { + int ident = var->GetUniqNum(); + (static_cast(inst))->m_nIdent = ident; // identifies variable by its number + + if (ident > 0 && ident < 9000) + { + if ( var->IsPrivate(privat) && + !pStk->GetBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, p); + goto err; + } + + // This is an element of the current class + // ads the equivalent of this. before + CBotToken token("this"); + inst->SetToken(&token); + (static_cast(inst))->m_nIdent = -2; // identificator for this + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(p); // keeps the name of the token + i->SetUniqNum(ident); + inst->AddNext3(i); // added after + } + + p = p->GetNext(); // next token + + while (true) + { + if (var->GetType() == CBotTypArrayPointer) + { + if (IsOfType( p, ID_OPBRK )) // check if there is an aindex + { + CBotIndexExpr* i = new CBotIndexExpr(); + i->m_expr = CBotExpression::Compile(p, pStk); // compile the formula + inst->AddNext3(i); // add to the chain + + var = (static_cast(var))->GetItem(0,true); // gets the component [0] + + if (i->m_expr == nullptr) + { + pStk->SetError(TX_BADINDEX, p->GetStart()); + goto err; + } + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto err; + } + continue; + } + } + if (var->GetType(1) == CBotTypPointer) // for classes + { + if (IsOfType(p, ID_DOT)) + { + CBotToken* pp = p; + + if (p->GetType() == TokenTypVar) // must be a name + { + if (p->GetNext()->GetType() == ID_OPENPAR) // a method call? + { + CBotInstr* i = CBotInstrMethode::Compile(p, pStk, var); + if (!pStk->IsOk()) goto err; + inst->AddNext3(i); // added after + return pStack->Return(inst, pStk); + } + else + { + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(pp); // keeps the name of the token + inst->AddNext3(i); // add after + var = var->GetItem(p->GetString()); // get item correspondent + if (var != nullptr) + { + i->SetUniqNum(var->GetUniqNum()); + if ( var->IsPrivate() && + !pStk->GetBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, pp); + goto err; + } + } + } + + + if (var != nullptr) + { + p = p->GetNext(); // skips the name + continue; + } + pStk->SetError(TX_NOITEM, p); + goto err; + } + pStk->SetError(TX_DOT, p->GetStart()); + goto err; + } + } + + break; + } + + pStk->SetCopyVar(var); // place the copy of the variable on the stack (for type) + if (pStk->IsOk()) return pStack->Return(inst, pStk); + } + pStk->SetError(TX_UNDEFVAR, p); +err: + delete inst; + return pStack->Return(nullptr, pStk); + } + + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExprVar::CompileMethode(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(pp->GetStart()); + + // is it a variable ? + if (pp->GetType() == TokenTypVar) + { + CBotToken pthis("this"); + CBotVar* var = pStk->FindVar(pthis); + if (var == nullptr) return pStack->Return(nullptr, pStk); + + CBotInstr* inst = new CBotExprVar(); + + // this is an element of the current class + // adds the equivalent of this. before + + inst->SetToken(&pthis); + (static_cast(inst))->m_nIdent = -2; // ident for this + + CBotToken* pp = p; + + if (pp->GetType() == TokenTypVar) + { + if (pp->GetNext()->GetType() == ID_OPENPAR) // a method call? + { + CBotInstr* i = CBotInstrMethode::Compile(pp, pStk, var); + if (pStk->IsOk()) + { + inst->AddNext3(i); // add after + p = pp; // previous instruction + return pStack->Return(inst, pStk); + } + pStk->SetError(0,0); // the error is not adressed here + } + } + delete inst; + } + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprVar::Execute(CBotStack* &pj) +{ + CBotVar* pVar = nullptr; + CBotStack* pile = pj->AddStack(this); + + CBotStack* pile1 = pile; + + if (pile1->GetState() == 0) + { + if (!ExecuteVar(pVar, pile, nullptr, true)) return false; // Get the variable fields and indexes according + + if (pVar) pile1->SetCopyVar(pVar); // place a copy on the stack + else + { + return pj->Return(pile1); + } + pile1->IncState(); + } + + pVar = pile1->GetVar(); + + if (pVar == nullptr) + { + return pj->Return(pile1); + } + + if (pVar->IsUndefined()) + { + CBotToken* pt = &m_token; + while (pt->GetNext() != nullptr) pt = pt->GetNext(); + pile1->SetError(TX_NOTINIT, pt); + return pj->Return(pile1); + } + return pj->Return(pile1); // operation completed +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprVar::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + CBotStack* pile1 = pile; + + if (pile1->GetState() == 0) + { + RestoreStateVar(pile, bMain); // retrieves the variable fields and indexes according + return; + } +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprVar::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep) +{ + CBotStack* pile = pj; + pj = pj->AddStack(this); + + if (bStep && m_nIdent>0 && pj->IfStep()) return false; + + pVar = pj->FindVar(m_nIdent, true); // tries with the variable update if necessary + if (pVar == nullptr) + { +#ifdef _DEBUG + assert(0); +#endif + pj->SetError(1, &m_token); + return false; + } + if ( m_next3 != nullptr && + !m_next3->ExecuteVar(pVar, pj, &m_token, bStep, false) ) + return false; // field of an instance, table, methode + + return pile->ReturnKeep(pj); // does not put on stack but get the result if a method was called +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprVar::RestoreStateVar(CBotStack* &pj, bool bMain) +{ + pj = pj->RestoreStack(this); + if (pj == nullptr) return; + + if (m_next3 != nullptr) + m_next3->RestoreStateVar(pj, bMain); +} diff --git a/src/CBot/CBotInstr/CBotExprVar.h b/src/CBot/CBotInstr/CBotExprVar.h new file mode 100644 index 00000000..46017e51 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprVar.h @@ -0,0 +1,101 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotExprVar class Expression for the variable name. Compile a + * variable name check that it is known on the stack and it has been initialized. + */ +class CBotExprVar : public CBotInstr +{ +public: + + /*! + * \brief CBotExprVar + */ + CBotExprVar(); + + /*! + * \brief ~CBotExprVar + */ + ~CBotExprVar(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param privat + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); + + /*! + * \brief CompileMethode + * \param p + * \param pStack + * \return + */ + static CBotInstr* CompileMethode(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execute, making the value of a variable. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + + /*! + * \brief ExecuteVar Fetch a variable at runtime. + * \param pVar + * \param pile + * \param prevToken + * \param bStep + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + + /*! + * \brief RestoreStateVar Fetch variable at runtime. + * \param pj + * \param bMain + */ + void RestoreStateVar(CBotStack* &pj, bool bMain) override; + +private: + long m_nIdent; + friend class CBotPostIncExpr; + friend class CBotPreIncExpr; + +}; diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.cpp b/src/CBot/CBotInstr/CBotPostIncExpr.cpp index a0b24fc0..cb146ca4 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPostIncExpr.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotPostIncExpr.h" +#include "CBotExprVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.cpp b/src/CBot/CBotInstr/CBotPreIncExpr.cpp index 700bbffa..073e0d4a 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPreIncExpr.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotPreIncExpr.h" +#include "CBotExprVar.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 158478b9..2908c5e2 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -28,6 +28,7 @@ set(SOURCES CBotInstr/CBotLeftExprVar.cpp CBotInstr/CBotPreIncExpr.cpp CBotInstr/CBotPostIncExpr.cpp + CBotInstr/CBotExprVar.cpp ) # Includes From 631621fb7e20ebfa759af0f89cb4a36b2ed54055 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 18:39:04 +0100 Subject: [PATCH 23/91] Moving CBotInstrMethode class in its own header and source files. --- src/CBot/CBot.cpp | 250 ---------------------- src/CBot/CBot.h | 26 --- src/CBot/CBotInstr/CBotExprVar.cpp | 1 + src/CBot/CBotInstr/CBotInstrMethode.cpp | 273 ++++++++++++++++++++++++ src/CBot/CBotInstr/CBotInstrMethode.h | 92 ++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 367 insertions(+), 276 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotInstrMethode.cpp create mode 100644 src/CBot/CBotInstr/CBotInstrMethode.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 9dc07208..b60bb7df 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -2565,256 +2565,6 @@ CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) return ret; } -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile a method call - -CBotInstrMethode::CBotInstrMethode() -{ - m_Parameters = nullptr; - m_MethodeIdent = 0; - name = "CBotInstrMethode"; -} - -CBotInstrMethode::~CBotInstrMethode() -{ - delete m_Parameters; -} - -CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* var) -{ - CBotInstrMethode* inst = new CBotInstrMethode(); - inst->SetToken(p); // corresponding token - - if (nullptr != var) - { - CBotToken* pp = p; - p = p->GetNext(); - - if (p->GetType() == ID_OPENPAR) - { - inst->m_NomMethod = pp->GetString(); - - // compiles the list of parameters - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStack, ppVars); - - if (pStack->IsOk()) - { - CBotClass* pClass = var->GetClass(); // pointer to the class - inst->m_ClassName = pClass->GetName(); // name of the class - CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, - pStack, inst->m_MethodeIdent); - delete pStack->TokenStack(); // release parameters on the stack - inst->m_typRes = r; - - if (inst->m_typRes.GetType() > 20) - { - pStack->SetError(inst->m_typRes.GetType(), pp); - delete inst; - return nullptr; - } - // put the result on the stack to have something - if (inst->m_typRes.GetType() > 0) - { - CBotVar* pResult = CBotVar::Create("", inst->m_typRes); - if (inst->m_typRes.Eq(CBotTypClass)) - { - pResult->SetClass(inst->m_typRes.GetClass()); - } - pStack->SetVar(pResult); - } - return inst; - } - delete inst; - return nullptr; - } - } - pStack->SetError(1234, p); - delete inst; - return nullptr; -} - -// execute the method call - -bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) -{ - CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // a place for the copy of This - - if (pVar->GetPointer() == nullptr) - { - pj->SetError(TX_NULLPT, prevToken); - } - - if (pile1->IfStep()) return false; - - CBotStack* pile2 = pile1->AddStack(); // for the next parameters - - if ( pile1->GetState() == 0) - { - CBotVar* pThis = CBotVar::Create(pVar); - pThis->Copy(pVar); - // this value should be taken before the evaluation parameters - // Test.Action (Test = Other); - // action must act on the value before test = Other! - - pThis->SetName("this"); - pThis->SetUniqNum(-2); - pile1->AddVar(pThis); - pile1->IncState(); - } - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != nullptr) while ( true) - { - if (pile2->GetState() == 0) - { - if (!p->Execute(pile2)) return false; // interrupted here? - if (!pile2->SetState(1)) return false; // special mark to recognize parameters - } - ppVars[i++] = pile2->GetVar(); // construct the list of pointers - pile2 = pile2->AddStack(); // space on the stack for the result - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pThis = pile1->FindVar(-2); - CBotVar* pResult = nullptr; - if (m_typRes.GetType() > 0) pResult = CBotVar::Create("", m_typRes); - if (m_typRes.Eq(CBotTypClass)) - { - pResult->SetClass(m_typRes.GetClass()); - } - CBotVar* pRes = pResult; - - if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, - pResult, pile2, GetToken())) return false; - if (pRes != pResult) delete pRes; - - pVar = nullptr; // does not return value for this - return pj->Return(pile2); // release the entire stack -} - -void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - if (!bMain) return; - - CBotVar* ppVars[1000]; - CBotStack* pile1 = pile->RestoreStack(this); // place for the copy of This - if (pile1 == nullptr) return; - - CBotStack* pile2 = pile1->RestoreStack(); // and for the parameters coming - if (pile2 == nullptr) return; - - CBotVar* pThis = pile1->FindVar("this"); - pThis->SetUniqNum(-2); - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - - if (p != nullptr) while ( true) - { - if (pile2->GetState() == 0) - { - p->RestoreState(pile2, true); // interrupted here! - return; - } - ppVars[i++] = pile2->GetVar(); // construct the list of pointers - pile2 = pile2->RestoreStack(); - if (pile2 == nullptr) return; - - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - CBotClass* pClass = CBotClass::Find(m_ClassName); -// CBotVar* pResult = nullptr; - -// CBotVar* pRes = pResult; - - pClass->RestoreMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, pile2); -} - - -bool CBotInstrMethode::Execute(CBotStack* &pj) -{ - CBotVar* ppVars[1000]; - CBotStack* pile1 = pj->AddStack(this, true); // place for the copy of This - - if (pile1->IfStep()) return false; - - CBotStack* pile2 = pile1->AddStack(); // and for the parameters coming - - if ( pile1->GetState() == 0) - { - CBotVar* pThis = pile1->CopyVar(m_token); - // this value should be taken before the evaluation parameters - // Test.Action (Test = Other); - // Action must act on the value before test = Other! - pThis->SetName("this"); - pile1->AddVar(pThis); - pile1->IncState(); - } - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluate the parameters - // and places the values on the stack - // to be interrupted at any time - if (p != nullptr) while ( true) - { - if (pile2->GetState() == 0) - { - if (!p->Execute(pile2)) return false; // interrupted here? - if (!pile2->SetState(1)) return false; // special mark to recognize parameters - } - ppVars[i++] = pile2->GetVar(); // construct the list of pointers - pile2 = pile2->AddStack(); // space on the stack for the results - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - CBotClass* pClass = CBotClass::Find(m_ClassName); - CBotVar* pThis = pile1->FindVar("this"); - CBotVar* pResult = nullptr; - if (m_typRes.GetType()>0) pResult = CBotVar::Create("", m_typRes); - if (m_typRes.Eq(CBotTypClass)) - { - pResult->SetClass(m_typRes.GetClass()); - } - CBotVar* pRes = pResult; - - if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, - pThis, ppVars, - pResult, pile2, GetToken())) return false; // interupted - - // set the new value of this in place of the old variable - CBotVar* old = pile1->FindVar(m_token); - old->Copy(pThis, false); - - if (pRes != pResult) delete pRes; - - return pj->Return(pile2); // release the entire stack -} - ///////////////////////////////////////////////////////////// // check if two results are consistent to make an operation diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index ffd2273b..41d961f4 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -851,32 +851,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -// a call of method - -class CBotInstrMethode : public CBotInstr -{ -private: - CBotInstr* m_Parameters; // the parameters to be evaluated -// int m_typeRes; // type of the result -// CBotString m_RetClassName; // class of the result - CBotTypResult - m_typRes; // complete type of the result - - CBotString m_NomMethod; // name of the method - long m_MethodeIdent; // identifier of the method -// long m_nThisIdent; // identifier for "this" - CBotString m_ClassName; // name of the class - -public: - CBotInstrMethode(); - ~CBotInstrMethode(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); - bool Execute(CBotStack* &pj) override; - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) override; - void RestoreStateVar(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 5e0c5683..57b2b431 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotExprVar.h" +#include "CBotInstrMethode.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp new file mode 100644 index 00000000..e35c0f25 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -0,0 +1,273 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotInstrMethode.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstrMethode::CBotInstrMethode() +{ + m_Parameters = nullptr; + m_MethodeIdent = 0; + name = "CBotInstrMethode"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstrMethode::~CBotInstrMethode() +{ + delete m_Parameters; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstrMethode::Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* var) +{ + CBotInstrMethode* inst = new CBotInstrMethode(); + inst->SetToken(p); // corresponding token + + if (nullptr != var) + { + CBotToken* pp = p; + p = p->GetNext(); + + if (p->GetType() == ID_OPENPAR) + { + inst->m_NomMethod = pp->GetString(); + + // compiles the list of parameters + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStack, ppVars); + + if (pStack->IsOk()) + { + CBotClass* pClass = var->GetClass(); // pointer to the class + inst->m_ClassName = pClass->GetName(); // name of the class + CBotTypResult r = pClass->CompileMethode(inst->m_NomMethod, var, ppVars, + pStack, inst->m_MethodeIdent); + delete pStack->TokenStack(); // release parameters on the stack + inst->m_typRes = r; + + if (inst->m_typRes.GetType() > 20) + { + pStack->SetError(inst->m_typRes.GetType(), pp); + delete inst; + return nullptr; + } + // put the result on the stack to have something + if (inst->m_typRes.GetType() > 0) + { + CBotVar* pResult = CBotVar::Create("", inst->m_typRes); + if (inst->m_typRes.Eq(CBotTypClass)) + { + pResult->SetClass(inst->m_typRes.GetClass()); + } + pStack->SetVar(pResult); + } + return inst; + } + delete inst; + return nullptr; + } + } + pStack->SetError(1234, p); + delete inst; + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstrMethode::ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) +{ + CBotVar* ppVars[1000]; + CBotStack* pile1 = pj->AddStack(this, true); // a place for the copy of This + + if (pVar->GetPointer() == nullptr) + { + pj->SetError(TX_NULLPT, prevToken); + } + + if (pile1->IfStep()) return false; + + CBotStack* pile2 = pile1->AddStack(); // for the next parameters + + if ( pile1->GetState() == 0) + { + CBotVar* pThis = CBotVar::Create(pVar); + pThis->Copy(pVar); + // this value should be taken before the evaluation parameters + // Test.Action (Test = Other); + // action must act on the value before test = Other! + + pThis->SetName("this"); + pThis->SetUniqNum(-2); + pile1->AddVar(pThis); + pile1->IncState(); + } + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != nullptr) while ( true) + { + if (pile2->GetState() == 0) + { + if (!p->Execute(pile2)) return false; // interrupted here? + if (!pile2->SetState(1)) return false; // special mark to recognize parameters + } + ppVars[i++] = pile2->GetVar(); // construct the list of pointers + pile2 = pile2->AddStack(); // space on the stack for the result + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pThis = pile1->FindVar(-2); + CBotVar* pResult = nullptr; + if (m_typRes.GetType() > 0) pResult = CBotVar::Create("", m_typRes); + if (m_typRes.Eq(CBotTypClass)) + { + pResult->SetClass(m_typRes.GetClass()); + } + CBotVar* pRes = pResult; + + if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, + pResult, pile2, GetToken())) return false; + if (pRes != pResult) delete pRes; + + pVar = nullptr; // does not return value for this + return pj->Return(pile2); // release the entire stack +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstrMethode::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + if (!bMain) return; + + CBotVar* ppVars[1000]; + CBotStack* pile1 = pile->RestoreStack(this); // place for the copy of This + if (pile1 == nullptr) return; + + CBotStack* pile2 = pile1->RestoreStack(); // and for the parameters coming + if (pile2 == nullptr) return; + + CBotVar* pThis = pile1->FindVar("this"); + pThis->SetUniqNum(-2); + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + + if (p != nullptr) while ( true) + { + if (pile2->GetState() == 0) + { + p->RestoreState(pile2, true); // interrupted here! + return; + } + ppVars[i++] = pile2->GetVar(); // construct the list of pointers + pile2 = pile2->RestoreStack(); + if (pile2 == nullptr) return; + + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + CBotClass* pClass = CBotClass::Find(m_ClassName); +// CBotVar* pResult = nullptr; + +// CBotVar* pRes = pResult; + + pClass->RestoreMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, pile2); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstrMethode::Execute(CBotStack* &pj) +{ + CBotVar* ppVars[1000]; + CBotStack* pile1 = pj->AddStack(this, true); // place for the copy of This + + if (pile1->IfStep()) return false; + + CBotStack* pile2 = pile1->AddStack(); // and for the parameters coming + + if ( pile1->GetState() == 0) + { + CBotVar* pThis = pile1->CopyVar(m_token); + // this value should be taken before the evaluation parameters + // Test.Action (Test = Other); + // Action must act on the value before test = Other! + pThis->SetName("this"); + pile1->AddVar(pThis); + pile1->IncState(); + } + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluate the parameters + // and places the values on the stack + // to be interrupted at any time + if (p != nullptr) while ( true) + { + if (pile2->GetState() == 0) + { + if (!p->Execute(pile2)) return false; // interrupted here? + if (!pile2->SetState(1)) return false; // special mark to recognize parameters + } + ppVars[i++] = pile2->GetVar(); // construct the list of pointers + pile2 = pile2->AddStack(); // space on the stack for the results + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + CBotClass* pClass = CBotClass::Find(m_ClassName); + CBotVar* pThis = pile1->FindVar("this"); + CBotVar* pResult = nullptr; + if (m_typRes.GetType()>0) pResult = CBotVar::Create("", m_typRes); + if (m_typRes.Eq(CBotTypClass)) + { + pResult->SetClass(m_typRes.GetClass()); + } + CBotVar* pRes = pResult; + + if ( !pClass->ExecuteMethode(m_MethodeIdent, m_NomMethod, + pThis, ppVars, + pResult, pile2, GetToken())) return false; // interupted + + // set the new value of this in place of the old variable + CBotVar* old = pile1->FindVar(m_token); + old->Copy(pThis, false); + + if (pRes != pResult) delete pRes; + + return pj->Return(pile2); // release the entire stack +} diff --git a/src/CBot/CBotInstr/CBotInstrMethode.h b/src/CBot/CBotInstr/CBotInstrMethode.h new file mode 100644 index 00000000..ca4ad90b --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstrMethode.h @@ -0,0 +1,92 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotInstrMethode class A call of method. Compile a method call. + */ +class CBotInstrMethode : public CBotInstr +{ +public: + + /*! + * \brief CBotInstrMethode + */ + CBotInstrMethode(); + + /*! + * \brief ~CBotInstrMethode + */ + ~CBotInstrMethode(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param pVar + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotVar* pVar); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief ExecuteVar Execute the method call. + * \param pVar + * \param pj + * \param prevToken + * \param bStep + * \param bExtend + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pj, CBotToken* prevToken, bool bStep, bool bExtend) override; + + /*! + * \brief RestoreStateVar + * \param pj + * \param bMain + */ + void RestoreStateVar(CBotStack* &pj, bool bMain) override; + +private: + //! The parameters to be evaluated. + CBotInstr *m_Parameters; + //! Complete type of the result. + CBotTypResult m_typRes; + //! Name of the method. + CBotString m_NomMethod; + //! Identifier of the method. + long m_MethodeIdent; + //! Name of the class. + CBotString m_ClassName; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 2908c5e2..bca48c36 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -29,6 +29,7 @@ set(SOURCES CBotInstr/CBotPreIncExpr.cpp CBotInstr/CBotPostIncExpr.cpp CBotInstr/CBotExprVar.cpp + CBotInstr/CBotInstrMethode.cpp ) # Includes From 0216359445f44395183282cf12ace51d06716515 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 19:14:45 +0100 Subject: [PATCH 24/91] Moving CBotInstrCall class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 18 --- src/CBot/CBotFunction.cpp | 173 ----------------------- src/CBot/CBotInstr/CBotInstrCall.cpp | 201 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotInstrCall.h | 76 ++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 279 insertions(+), 191 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotInstrCall.cpp create mode 100644 src/CBot/CBotInstr/CBotInstrCall.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index b60bb7df..8eb59b8e 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -53,6 +53,7 @@ #include "CBotInstr/CBotPreIncExpr.h" #include "CBotInstr/CBotPostIncExpr.h" #include "CBotInstr/CBotExprVar.h" +#include "CBotInstr/CBotInstrCall.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 41d961f4..809617e5 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -832,24 +832,6 @@ public: }; -class CBotInstrCall : public CBotInstr -{ -private: - CBotInstr* m_Parameters; // the parameters to be evaluated -// int m_typeRes; // type of the result -// CBotString m_RetClassName; // class of the result - CBotTypResult - m_typRes; // complete type of the result - long m_nFuncIdent; // id of a function - -public: - CBotInstrCall(); - ~CBotInstrCall(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index ff92ba48..226add20 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -1186,179 +1186,6 @@ void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) } } -//////////////////////////////////////////////////////////////////////////////// -// Calls of these functions - -CBotInstrCall::CBotInstrCall() -{ - m_Parameters = nullptr; - m_nFuncIdent = 0; - name = "CBotInstrCall"; -} - -CBotInstrCall::~CBotInstrCall() -{ - delete m_Parameters; -} - -CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotVar* ppVars[1000]; - - int i = 0; - - CBotToken* pp = p; - p = p->GetNext(); - - pStack->SetStartError(p->GetStart()); - CBotCStack* pile = pStack; - - if ( IsOfType(p, ID_OPENPAR) ) - { - int start, end; - CBotInstrCall* inst = new CBotInstrCall(); - inst->SetToken(pp); - - // compile la list of parameters - if (!IsOfType(p, ID_CLOSEPAR)) while (true) - { - start = p->GetStart(); - pile = pile->TokenStack(); // keeps the results on the stack - - CBotInstr* param = CBotExpression::Compile(p, pile); - end = p->GetStart(); - if ( inst->m_Parameters == nullptr ) inst->m_Parameters = param; - else inst->m_Parameters->AddNext(param); // constructs the list - - if ( !pile->IsOk() ) - { - delete inst; - return pStack->Return(nullptr, pile); - } - - if ( param != nullptr ) - { - if ( pile->GetTypResult().Eq(99) ) - { - delete pStack->TokenStack(); - pStack->SetError(TX_VOID, p->GetStart()); - delete inst; - return nullptr; - } - ppVars[i] = pile->GetVar(); - ppVars[i]->GetToken()->SetPos(start, end); - i++; - - if (IsOfType(p, ID_COMMA)) continue; // skips the comma - if (IsOfType(p, ID_CLOSEPAR)) break; - } - - pStack->SetError(TX_CLOSEPAR, p->GetStart()); - delete pStack->TokenStack(); - delete inst; - return nullptr; - } - ppVars[i] = nullptr; - - // the routine is known? -// CBotClass* pClass = nullptr; - inst->m_typRes = pStack->CompileCall(pp, ppVars, inst->m_nFuncIdent); - if ( inst->m_typRes.GetType() >= 20 ) - { -// if (pVar2!=nullptr) pp = pVar2->RetToken(); - pStack->SetError( inst->m_typRes.GetType(), pp ); - delete pStack->TokenStack(); - delete inst; - return nullptr; - } - - delete pStack->TokenStack(); - if ( inst->m_typRes.GetType() > 0 ) - { - CBotVar* pRes = CBotVar::Create("", inst->m_typRes); - pStack->SetVar(pRes); // for knowing the type of the result - } - else pStack->SetVar(nullptr); // routine returns void - - return inst; - } - p = pp; - delete pStack->TokenStack(); - return nullptr; -} - -bool CBotInstrCall::Execute(CBotStack* &pj) -{ - CBotVar* ppVars[1000]; - CBotStack* pile = pj->AddStack(this); - if ( pile->StackOver() ) return pj->Return( pile ); - -// CBotStack* pile1 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluates parameters - // and places the values ​​on the stack - // for allow of interruption at any time - if ( p != nullptr) while ( true ) - { - pile = pile->AddStack(); // place on the stack for the results - if ( pile->GetState() == 0 ) - { - if (!p->Execute(pile)) return false; // interrupted here? - pile->SetState(1); // mark as special for reknowed parameters \TODO marque spéciale pour reconnaîre parameters - } - ppVars[i++] = pile->GetVar(); - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - CBotStack* pile2 = pile->AddStack(); - if ( pile2->IfStep() ) return false; - - if ( !pile2->ExecuteCall(m_nFuncIdent, GetToken(), ppVars, m_typRes)) return false; // interrupt - - return pj->Return(pile2); // release the entire stack -} - -void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == nullptr ) return; - -// CBotStack* pile1 = pile; - - int i = 0; - CBotVar* ppVars[1000]; - CBotInstr* p = m_Parameters; - // evaluate parameters - // and place the values on the stack - // for allow of interruption at any time - if ( p != nullptr) while ( true ) - { - pile = pile->RestoreStack(); // place on the stack for the results - if ( pile == nullptr ) return; - if ( pile->GetState() == 0 ) - { - p->RestoreState(pile, bMain); // interrupt here! - return; - } - ppVars[i++] = pile->GetVar(); // constructs the list of parameters - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - CBotStack* pile2 = pile->RestoreStack(); - if ( pile2 == nullptr ) return; - - pile2->RestoreCall(m_nFuncIdent, GetToken(), ppVars); -} - ////////////////////////////////////////////////////////////////////////////// // statement of user classes diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp new file mode 100644 index 00000000..66a48e34 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstrCall.cpp @@ -0,0 +1,201 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotInstrCall.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstrCall::CBotInstrCall() +{ + m_Parameters = nullptr; + m_nFuncIdent = 0; + name = "CBotInstrCall"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstrCall::~CBotInstrCall() +{ + delete m_Parameters; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstrCall::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotVar* ppVars[1000]; + + int i = 0; + + CBotToken* pp = p; + p = p->GetNext(); + + pStack->SetStartError(p->GetStart()); + CBotCStack* pile = pStack; + + if ( IsOfType(p, ID_OPENPAR) ) + { + int start, end; + CBotInstrCall* inst = new CBotInstrCall(); + inst->SetToken(pp); + + // compile la list of parameters + if (!IsOfType(p, ID_CLOSEPAR)) while (true) + { + start = p->GetStart(); + pile = pile->TokenStack(); // keeps the results on the stack + + CBotInstr* param = CBotExpression::Compile(p, pile); + end = p->GetStart(); + if ( inst->m_Parameters == nullptr ) inst->m_Parameters = param; + else inst->m_Parameters->AddNext(param); // constructs the list + + if ( !pile->IsOk() ) + { + delete inst; + return pStack->Return(nullptr, pile); + } + + if ( param != nullptr ) + { + if ( pile->GetTypResult().Eq(99) ) + { + delete pStack->TokenStack(); + pStack->SetError(TX_VOID, p->GetStart()); + delete inst; + return nullptr; + } + ppVars[i] = pile->GetVar(); + ppVars[i]->GetToken()->SetPos(start, end); + i++; + + if (IsOfType(p, ID_COMMA)) continue; // skips the comma + if (IsOfType(p, ID_CLOSEPAR)) break; + } + + pStack->SetError(TX_CLOSEPAR, p->GetStart()); + delete pStack->TokenStack(); + delete inst; + return nullptr; + } + ppVars[i] = nullptr; + + // the routine is known? +// CBotClass* pClass = nullptr; + inst->m_typRes = pStack->CompileCall(pp, ppVars, inst->m_nFuncIdent); + if ( inst->m_typRes.GetType() >= 20 ) + { +// if (pVar2!=nullptr) pp = pVar2->RetToken(); + pStack->SetError( inst->m_typRes.GetType(), pp ); + delete pStack->TokenStack(); + delete inst; + return nullptr; + } + + delete pStack->TokenStack(); + if ( inst->m_typRes.GetType() > 0 ) + { + CBotVar* pRes = CBotVar::Create("", inst->m_typRes); + pStack->SetVar(pRes); // for knowing the type of the result + } + else pStack->SetVar(nullptr); // routine returns void + + return inst; + } + p = pp; + delete pStack->TokenStack(); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstrCall::Execute(CBotStack* &pj) +{ + CBotVar* ppVars[1000]; + CBotStack* pile = pj->AddStack(this); + if ( pile->StackOver() ) return pj->Return( pile ); + +// CBotStack* pile1 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates parameters + // and places the values ​​on the stack + // for allow of interruption at any time + if ( p != nullptr) while ( true ) + { + pile = pile->AddStack(); // place on the stack for the results + if ( pile->GetState() == 0 ) + { + if (!p->Execute(pile)) return false; // interrupted here? + pile->SetState(1); // mark as special for reknowed parameters \TODO marque spéciale pour reconnaîre parameters + } + ppVars[i++] = pile->GetVar(); + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + CBotStack* pile2 = pile->AddStack(); + if ( pile2->IfStep() ) return false; + + if ( !pile2->ExecuteCall(m_nFuncIdent, GetToken(), ppVars, m_typRes)) return false; // interrupt + + return pj->Return(pile2); // release the entire stack +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstrCall::RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == nullptr ) return; + +// CBotStack* pile1 = pile; + + int i = 0; + CBotVar* ppVars[1000]; + CBotInstr* p = m_Parameters; + // evaluate parameters + // and place the values on the stack + // for allow of interruption at any time + if ( p != nullptr) while ( true ) + { + pile = pile->RestoreStack(); // place on the stack for the results + if ( pile == nullptr ) return; + if ( pile->GetState() == 0 ) + { + p->RestoreState(pile, bMain); // interrupt here! + return; + } + ppVars[i++] = pile->GetVar(); // constructs the list of parameters + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + CBotStack* pile2 = pile->RestoreStack(); + if ( pile2 == nullptr ) return; + + pile2->RestoreCall(m_nFuncIdent, GetToken(), ppVars); +} diff --git a/src/CBot/CBotInstr/CBotInstrCall.h b/src/CBot/CBotInstr/CBotInstrCall.h new file mode 100644 index 00000000..7e194f8a --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstrCall.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotInstrCall class Calls of these functions. + */ +class CBotInstrCall : public CBotInstr +{ +public: + + /*! + * \brief CBotInstrCall + */ + CBotInstrCall(); + + /*! + * \brief ~CBotInstrCall + */ + ~CBotInstrCall(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! The parameters to be evaluated. + CBotInstr* m_Parameters; + //! Complete type of the result. + CBotTypResult m_typRes; + //! Id of a function. + long m_nFuncIdent; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index bca48c36..0ba14397 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -30,6 +30,7 @@ set(SOURCES CBotInstr/CBotPostIncExpr.cpp CBotInstr/CBotExprVar.cpp CBotInstr/CBotInstrMethode.cpp + CBotInstr/CBotInstrCall.cpp ) # Includes From 840da007a9e0075e77dc53187b45bee7bd5ee50c Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 19:25:12 +0100 Subject: [PATCH 25/91] Moving CBotListInstr class in its own header and source files. --- src/CBot/CBot.cpp | 96 +--------------------- src/CBot/CBot.h | 18 ----- src/CBot/CBotInstr/CBotListInstr.cpp | 117 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotListInstr.h | 74 +++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 193 insertions(+), 113 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotListInstr.cpp create mode 100644 src/CBot/CBotInstr/CBotListInstr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 8eb59b8e..98e04d9c 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -54,6 +54,7 @@ #include "CBotInstr/CBotPostIncExpr.h" #include "CBotInstr/CBotExprVar.h" #include "CBotInstr/CBotInstrCall.h" +#include "CBotInstr/CBotListInstr.h" // Local include @@ -408,101 +409,6 @@ CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool b // where the variable x is known only in the block following the if -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a list of instructions separated by semicolons - -CBotListInstr::CBotListInstr() -{ - m_Instr = nullptr; - name = "CBotListInstr"; -} - -CBotListInstr::~CBotListInstr() -{ - delete m_Instr; -} - -CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - CBotCStack* pStk = pStack->TokenStack(p, bLocal); // variables are local - - CBotListInstr* inst = new CBotListInstr(); - - while (true) - { - if (p == nullptr) break; - - if (IsOfType(p, ID_SEP)) continue; // empty statement ignored - if (p->GetType() == ID_CLBLK) break; - - if (IsOfType(p, 0)) - { - pStack->SetError(TX_CLOSEBLK, p->GetStart()); - delete inst; - return pStack->Return(nullptr, pStk); - } - - CBotInstr* i = CBotBlock::CompileBlkOrInst(p, pStk); // compiles next - - if (!pStk->IsOk()) - { - delete inst; - return pStack->Return(nullptr, pStk); - } - - if (inst->m_Instr == nullptr) inst->m_Instr = i; - else inst->m_Instr->AddNext(i); // added a result - } - return pStack->Return(inst, pStk); -} - -// executes a set of instructions - -bool CBotListInstr::Execute(CBotStack* &pj) -{ - - CBotStack* pile = pj->AddStack(this, true); //needed for SetState() - if (pile->StackOver() ) return pj->Return( pile); - - - CBotInstr* p = m_Instr; // the first expression - - int state = pile->GetState(); - while (state-->0) p = p->GetNext(); // returns to the interrupted operation - - if (p != nullptr) while (true) - { - if (!p->Execute(pile)) return false; - p = p->GetNext(); - if (p == nullptr) break; - (void)pile->IncState(); // ready for next - } - - return pj->Return(pile); -} - -void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - CBotInstr* p = m_Instr; // the first expression - - int state = pile->GetState(); - while ( p != nullptr && state-- > 0) - { - p->RestoreState(pile, false); - p = p->GetNext(); // returns to the interrupted operation - } - - if (p != nullptr) p->RestoreState(pile, true); -} - ////////////////////////////////////////////////////////////////////////////////////// // defining an array of any type // int a[12]; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 809617e5..326a79ec 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -815,24 +815,6 @@ private: CBotBlock(const CBotBlock &) = delete; }; - -// the content of a block of instructions ... ; ... ; ... ; ... ; -class CBotListInstr : public CBotInstr -{ -private: - CBotInstr* m_Instr; // instructions to do - -public: - CBotListInstr(); - ~CBotListInstr(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp new file mode 100644 index 00000000..3d1d9c57 --- /dev/null +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -0,0 +1,117 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotListInstr.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotListInstr::CBotListInstr() +{ + m_Instr = nullptr; + name = "CBotListInstr"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotListInstr::~CBotListInstr() +{ + delete m_Instr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotListInstr::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + CBotCStack* pStk = pStack->TokenStack(p, bLocal); // variables are local + + CBotListInstr* inst = new CBotListInstr(); + + while (true) + { + if (p == nullptr) break; + + if (IsOfType(p, ID_SEP)) continue; // empty statement ignored + if (p->GetType() == ID_CLBLK) break; + + if (IsOfType(p, 0)) + { + pStack->SetError(TX_CLOSEBLK, p->GetStart()); + delete inst; + return pStack->Return(nullptr, pStk); + } + + CBotInstr* i = CBotBlock::CompileBlkOrInst(p, pStk); // compiles next + + if (!pStk->IsOk()) + { + delete inst; + return pStack->Return(nullptr, pStk); + } + + if (inst->m_Instr == nullptr) inst->m_Instr = i; + else inst->m_Instr->AddNext(i); // added a result + } + return pStack->Return(inst, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotListInstr::Execute(CBotStack* &pj) +{ + + CBotStack* pile = pj->AddStack(this, true); //needed for SetState() + if (pile->StackOver() ) return pj->Return( pile); + + + CBotInstr* p = m_Instr; // the first expression + + int state = pile->GetState(); + while (state-->0) p = p->GetNext(); // returns to the interrupted operation + + if (p != nullptr) while (true) + { + if (!p->Execute(pile)) return false; + p = p->GetNext(); + if (p == nullptr) break; + (void)pile->IncState(); // ready for next + } + + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotListInstr::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + CBotInstr* p = m_Instr; // the first expression + + int state = pile->GetState(); + while ( p != nullptr && state-- > 0) + { + p->RestoreState(pile, false); + p = p->GetNext(); // returns to the interrupted operation + } + + if (p != nullptr) p->RestoreState(pile, true); +} diff --git a/src/CBot/CBotInstr/CBotListInstr.h b/src/CBot/CBotInstr/CBotListInstr.h new file mode 100644 index 00000000..763c3a88 --- /dev/null +++ b/src/CBot/CBotInstr/CBotListInstr.h @@ -0,0 +1,74 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotListInstr class Compiles a list of instructions separated by + * semicolons eg : ... ; ... ; ... ; ... ; + */ +class CBotListInstr : public CBotInstr +{ +public: + + /*! + * \brief CBotListInstr + */ + CBotListInstr(); + + /*! + * \brief ~CBotListInstr + */ + ~CBotListInstr(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param bLocal + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + + /*! + * \brief Execute Executes a set of instructions. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Instructions to do. + CBotInstr* m_Instr; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 0ba14397..30e717be 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -31,6 +31,7 @@ set(SOURCES CBotInstr/CBotExprVar.cpp CBotInstr/CBotInstrMethode.cpp CBotInstr/CBotInstrCall.cpp + CBotInstr/CBotListInstr.cpp ) # Includes From 8eff62a78cf9ee1c45e226112844118e073773f1 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 19:53:00 +0100 Subject: [PATCH 26/91] Moving CBotBlock class in its own header and source files. --- src/CBot/CBot.cpp | 52 --------------------- src/CBot/CBot.h | 16 ------- src/CBot/CBotFunction.cpp | 2 + src/CBot/CBotIf.cpp | 2 + src/CBot/CBotInstr/CBotBlock.cpp | 69 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotBlock.h | 64 ++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotCatch.cpp | 1 + src/CBot/CBotInstr/CBotDo.cpp | 1 + src/CBot/CBotInstr/CBotFor.cpp | 1 + src/CBot/CBotInstr/CBotListInstr.cpp | 1 + src/CBot/CBotInstr/CBotSwitch.cpp | 1 + src/CBot/CBotInstr/CBotTry.cpp | 1 + src/CBot/CBotInstr/CBotWhile.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 14 files changed, 145 insertions(+), 68 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotBlock.cpp create mode 100644 src/CBot/CBotInstr/CBotBlock.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 98e04d9c..042e4c74 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -357,58 +357,6 @@ bool CBotInstr::CompCase(CBotStack* &pj, int val) return false; } -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a statement block " { i ; i ; } " - -// this class have no constructor because there is never an instance of this -// class -// the object returned by Compile is usually of type CBotListInstr - - -CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - pStack->SetStartError(p->GetStart()); - - if (IsOfType(p, ID_OPBLK)) - { - CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); - - if (IsOfType(p, ID_CLBLK)) - { - return inst; - } - - pStack->SetError(TX_CLOSEBLK, p->GetStart()); // missing parenthesis - delete inst; - return nullptr; - } - - pStack->SetError(TX_OPENBLK, p->GetStart()); - return nullptr; -} - -CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) -{ - // is this a new block - if (p->GetType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); - - // otherwise, look for a single statement instead - - // to handle the case with local definition instruction (*) - CBotCStack* pStk = pStack->TokenStack(p, bLocal); - - return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction - pStk); -} - -// (*) is the case in the following statement -// if (1 == 1) int x = 0; -// where the variable x is known only in the block following the if - - ////////////////////////////////////////////////////////////////////////////////////// // defining an array of any type // int a[12]; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 326a79ec..32cdb695 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -799,22 +799,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - - - -// an instruction block { .... } -class CBotBlock : public CBotInstr -{ -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); - static - CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); -private: - CBotBlock() = delete; - CBotBlock(const CBotBlock &) = delete; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 226add20..6ebad172 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -23,6 +23,8 @@ #include "CBot.h" +#include "CBotInstr/CBotBlock.h" + #include diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotIf.cpp index 15770af4..781ec12c 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotIf.cpp @@ -22,6 +22,8 @@ #include "CBot.h" +#include "CBotInstr/CBotBlock.h" + // various constructors / destructors CBotIf::CBotIf() { diff --git a/src/CBot/CBotInstr/CBotBlock.cpp b/src/CBot/CBotInstr/CBotBlock.cpp new file mode 100644 index 00000000..ed87b53b --- /dev/null +++ b/src/CBot/CBotInstr/CBotBlock.cpp @@ -0,0 +1,69 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotBlock.h" +#include "CBotListInstr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBlock::Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + pStack->SetStartError(p->GetStart()); + + if (IsOfType(p, ID_OPBLK)) + { + CBotInstr* inst = CBotListInstr::Compile(p, pStack, bLocal); + + if (IsOfType(p, ID_CLBLK)) + { + return inst; + } + + pStack->SetError(TX_CLOSEBLK, p->GetStart()); // missing parenthesis + delete inst; + return nullptr; + } + + pStack->SetError(TX_OPENBLK, p->GetStart()); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBlock::CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal) +{ + // is this a new block + if (p->GetType() == ID_OPBLK) return CBotBlock::Compile(p, pStack); + + // otherwise, look for a single statement instead + + // to handle the case with local definition instruction (*) + CBotCStack* pStk = pStack->TokenStack(p, bLocal); + + return pStack->Return( CBotInstr::Compile(p, pStk), // a single instruction + pStk); +} + +// (*) is the case in the following statement +// if (1 == 1) int x = 0; +// where the variable x is known only in the block following the if diff --git a/src/CBot/CBotInstr/CBotBlock.h b/src/CBot/CBotInstr/CBotBlock.h new file mode 100644 index 00000000..f413e756 --- /dev/null +++ b/src/CBot/CBotInstr/CBotBlock.h @@ -0,0 +1,64 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotBlock class An instruction block { .... }. + */ +class CBotBlock : public CBotInstr +{ +public: + + /*! + * \brief Compile Compiles a statement block " { i ; i ; } " + * \param p + * \param pStack + * \param bLocal + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool bLocal = true); + + /*! + * \brief CompileBlkOrInst + * \param p + * \param pStack + * \param bLocal + * \return + */ + static CBotInstr* CompileBlkOrInst(CBotToken* &p, CBotCStack* pStack, bool bLocal = false); + +private: + + /*! + * \brief CBotBlock This class have no constructor because there is never an + * instance of this class the object returned by Compile is usually of type + * CBotListInstr + */ + CBotBlock() = delete; + CBotBlock(const CBotBlock &) = delete; +}; diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index 4c63e029..e4a5571e 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotCatch.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp index a24e5dbe..94c281d4 100644 --- a/src/CBot/CBotInstr/CBotDo.cpp +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotDo.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index 7dc07d4a..32caa3c5 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotFor.h" #include "CBotListExpression.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp index 3d1d9c57..b2fcf49c 100644 --- a/src/CBot/CBotInstr/CBotListInstr.cpp +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotListInstr.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index c0c79975..70fd8033 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -22,6 +22,7 @@ // Modules inlcude #include "CBotSwitch.h" #include "CBotCase.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTry.cpp b/src/CBot/CBotInstr/CBotTry.cpp index c2b2901a..89b689ea 100644 --- a/src/CBot/CBotInstr/CBotTry.cpp +++ b/src/CBot/CBotInstr/CBotTry.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotTry.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp index 92b276db..340cc239 100644 --- a/src/CBot/CBotInstr/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotWhile.h" +#include "CBotBlock.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 30e717be..ac6a0c09 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -32,6 +32,7 @@ set(SOURCES CBotInstr/CBotInstrMethode.cpp CBotInstr/CBotInstrCall.cpp CBotInstr/CBotListInstr.cpp + CBotInstr/CBotBlock.cpp ) # Includes From f6cc7d2c9c91baf08bd53b9616590f18737fc814 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 20:15:42 +0100 Subject: [PATCH 27/91] Moving CBotExprUnaire class in its own header and source files. --- src/CBot/CBot.cpp | 100 +--------------------- src/CBot/CBot.h | 14 --- src/CBot/CBotInstr/CBotExprUnaire.cpp | 118 ++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotExprUnaire.h | 73 ++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 5 files changed, 193 insertions(+), 113 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExprUnaire.cpp create mode 100644 src/CBot/CBotInstr/CBotExprUnaire.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 042e4c74..de9f730b 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -55,6 +55,7 @@ #include "CBotInstr/CBotExprVar.h" #include "CBotInstr/CBotInstrCall.h" #include "CBotInstr/CBotListInstr.h" +#include "CBotInstr/CBotExprUnaire.h" // Local include @@ -1840,105 +1841,6 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(nullptr, pStk); } - -////////////////////////////////////////////////////////////////////////////////////// -// compile an unary expression -// + -// - -// not -// ! -// ~ - -CBotExprUnaire::CBotExprUnaire() -{ - m_Expr = nullptr; - name = "CBotExprUnaire"; -} - -CBotExprUnaire::~CBotExprUnaire() -{ - delete m_Expr; -} - -CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) -{ - int op = p->GetType(); - CBotToken* pp = p; - if (!IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 )) return nullptr; - - CBotCStack* pStk = pStack->TokenStack(pp); - - CBotExprUnaire* inst = new CBotExprUnaire(); - inst->SetToken(pp); - - if (nullptr != (inst->m_Expr = CBotParExpr::Compile( p, pStk ))) - { - if (op == ID_ADD && pStk->GetType() < CBotTypBoolean) // only with the number - return pStack->Return(inst, pStk); - if (op == ID_SUB && pStk->GetType() < CBotTypBoolean) // only with the numer - return pStack->Return(inst, pStk); - if (op == ID_NOT && pStk->GetType() < CBotTypFloat) // only with an integer - return pStack->Return(inst, pStk); - if (op == ID_LOG_NOT && pStk->GetTypResult().Eq(CBotTypBoolean))// only with boolean - return pStack->Return(inst, pStk); - if (op == ID_TXT_NOT && pStk->GetTypResult().Eq(CBotTypBoolean))// only with boolean - return pStack->Return(inst, pStk); - - pStk->SetError(TX_BADTYPE, &inst->m_token); - } - delete inst; - return pStack->Return(nullptr, pStk); -} - -// executes unary expression - -bool CBotExprUnaire::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if (pile->GetState() == 0) - { - if (!m_Expr->Execute(pile)) return false; // interrupted ? - pile->IncState(); - } - - CBotStack* pile2 = pile->AddStack(); - if (pile2->IfStep()) return false; - - CBotVar* var = pile->GetVar(); // get the result on the stack - - switch (GetTokenType()) - { - case ID_ADD: - break; - case ID_SUB: - var->Neg(); // change the sign - break; - case ID_NOT: - case ID_LOG_NOT: - case ID_TXT_NOT: - var->Not(); - break; - } - return pj->Return(pile); // forwards below -} - -void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) -{ - if (!bMain) return; - - CBotStack* pile = pj->RestoreStack(this); - if ( pile == nullptr) return; - - if (pile->GetState() == 0) - { - m_Expr->RestoreState(pile, bMain); // interrupted here! - return; - } -} - -////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // index management for arrays // array [ expression ] diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 32cdb695..6ae6885f 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -769,20 +769,6 @@ public: CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); }; -// unary expression -class CBotExprUnaire : public CBotInstr -{ -private: - CBotInstr* m_Expr; // expression to be evaluated -public: - CBotExprUnaire(); - ~CBotExprUnaire(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - // all operations with two operands class CBotTwoOpExpr : public CBotInstr diff --git a/src/CBot/CBotInstr/CBotExprUnaire.cpp b/src/CBot/CBotInstr/CBotExprUnaire.cpp new file mode 100644 index 00000000..1d63f1dd --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprUnaire.cpp @@ -0,0 +1,118 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExprUnaire.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotExprUnaire::CBotExprUnaire() +{ + m_Expr = nullptr; + name = "CBotExprUnaire"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExprUnaire::~CBotExprUnaire() +{ + delete m_Expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExprUnaire::Compile(CBotToken* &p, CBotCStack* pStack) +{ + int op = p->GetType(); + CBotToken* pp = p; + if (!IsOfTypeList( p, ID_ADD, ID_SUB, ID_LOG_NOT, ID_TXT_NOT, ID_NOT, 0 )) return nullptr; + + CBotCStack* pStk = pStack->TokenStack(pp); + + CBotExprUnaire* inst = new CBotExprUnaire(); + inst->SetToken(pp); + + if (nullptr != (inst->m_Expr = CBotParExpr::Compile( p, pStk ))) + { + if (op == ID_ADD && pStk->GetType() < CBotTypBoolean) // only with the number + return pStack->Return(inst, pStk); + if (op == ID_SUB && pStk->GetType() < CBotTypBoolean) // only with the numer + return pStack->Return(inst, pStk); + if (op == ID_NOT && pStk->GetType() < CBotTypFloat) // only with an integer + return pStack->Return(inst, pStk); + if (op == ID_LOG_NOT && pStk->GetTypResult().Eq(CBotTypBoolean))// only with boolean + return pStack->Return(inst, pStk); + if (op == ID_TXT_NOT && pStk->GetTypResult().Eq(CBotTypBoolean))// only with boolean + return pStack->Return(inst, pStk); + + pStk->SetError(TX_BADTYPE, &inst->m_token); + } + delete inst; + return pStack->Return(nullptr, pStk); +} + +// executes unary expression +//////////////////////////////////////////////////////////////////////////////// +bool CBotExprUnaire::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if (pile->GetState() == 0) + { + if (!m_Expr->Execute(pile)) return false; // interrupted ? + pile->IncState(); + } + + CBotStack* pile2 = pile->AddStack(); + if (pile2->IfStep()) return false; + + CBotVar* var = pile->GetVar(); // get the result on the stack + + switch (GetTokenType()) + { + case ID_ADD: + break; + case ID_SUB: + var->Neg(); // change the sign + break; + case ID_NOT: + case ID_LOG_NOT: + case ID_TXT_NOT: + var->Not(); + break; + } + return pj->Return(pile); // forwards below +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExprUnaire::RestoreState(CBotStack* &pj, bool bMain) +{ + if (!bMain) return; + + CBotStack* pile = pj->RestoreStack(this); + if ( pile == nullptr) return; + + if (pile->GetState() == 0) + { + m_Expr->RestoreState(pile, bMain); // interrupted here! + return; + } +} diff --git a/src/CBot/CBotInstr/CBotExprUnaire.h b/src/CBot/CBotInstr/CBotExprUnaire.h new file mode 100644 index 00000000..25d21251 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExprUnaire.h @@ -0,0 +1,73 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotExprUnaire class Unary expression. Compile an unary expression + * eg : (+, * -, not, !, ~) + */ +class CBotExprUnaire : public CBotInstr +{ +public: + + /*! + * \brief CBotExprUnaire + */ + CBotExprUnaire(); + + /*! + * \brief ~CBotExprUnaire + */ + ~CBotExprUnaire(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param pStack + * \return + */ + bool Execute(CBotStack* &pStack) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Expression to be evaluated. + CBotInstr* m_Expr; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index ac6a0c09..4cff8efd 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -33,6 +33,7 @@ set(SOURCES CBotInstr/CBotInstrCall.cpp CBotInstr/CBotListInstr.cpp CBotInstr/CBotBlock.cpp + CBotInstr/CBotExprUnaire.cpp ) # Includes From 75f5126dddb81f6fba5c8ada95ade7bb12e80e9f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 20:36:47 +0100 Subject: [PATCH 28/91] Moving CBotParExpr class in its own header and source files. --- src/CBot/CBot.cpp | 168 ----------------------- src/CBot/CBot.h | 14 -- src/CBot/CBotInstr/CBotExprUnaire.cpp | 1 + src/CBot/CBotInstr/CBotParExpr.cpp | 188 ++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotParExpr.h | 62 +++++++++ src/CBot/CBotTwoOpExpr.cpp | 2 + src/CBot/CMakeLists.txt | 1 + 7 files changed, 254 insertions(+), 182 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotParExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotParExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index de9f730b..93642bcb 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -1673,174 +1673,6 @@ CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) return nullptr; } -////////////////////////////////////////////////////////////////////////////////////////// - - -////////////////////////////////////////////////////////////////////////////////////// -// compile either: -// instruction in parentheses (...) -// a unary expression (negative, not) -// variable name -// variables pre and post-incremented or decremented -// a given number DefineNum -// a constant -// procedure call -// new statement -// -// this class has no constructor, because there is never an instance of this class -// the object returned by Compile is the class corresponding to the instruction - - -CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GetStart()); - - // is it an expression in parentheses? - if (IsOfType(p, ID_OPENPAR)) - { - CBotInstr* inst = CBotExpression::Compile(p, pStk); - - if (nullptr != inst) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - return pStack->Return(inst, pStk); - } - pStk->SetError(TX_CLOSEPAR, p->GetStart()); - } - delete inst; - return pStack->Return(nullptr, pStk); - } - - // is this a unary operation? - CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); - if (inst != nullptr || !pStk->IsOk()) - return pStack->Return(inst, pStk); - - // is it a variable name? - if (p->GetType() == TokenTypVar) - { - // this may be a method call without the "this." before - inst = CBotExprVar::CompileMethode(p, pStk); - if (inst != nullptr) return pStack->Return(inst, pStk); - - - // is it a procedure call? - inst = CBotInstrCall::Compile(p, pStk); - if (inst != nullptr || !pStk->IsOk()) - return pStack->Return(inst, pStk); - - - CBotToken* pvar = p; - // no, it an "ordinaty" variable - inst = CBotExprVar::Compile(p, pStk); - - CBotToken* pp = p; - // post incremented or decremented? - if (IsOfType(p, ID_INC, ID_DEC)) - { - if (pStk->GetType() >= CBotTypBoolean) - { - pStk->SetError(TX_BADTYPE, pp); - delete inst; - return pStack->Return(nullptr, pStk); - } - - // recompile the variable for read-only - delete inst; - p = pvar; - inst = CBotExprVar::Compile(p, pStk, PR_READ); - p = p->GetNext(); - - CBotPostIncExpr* i = new CBotPostIncExpr(); - i->SetToken(pp); - i->m_Instr = inst; // associated statement - return pStack->Return(i, pStk); - } - return pStack->Return(inst, pStk); - } - - // pre increpemted or pre decremented? - CBotToken* pp = p; - if (IsOfType(p, ID_INC, ID_DEC)) - { - CBotPreIncExpr* i = new CBotPreIncExpr(); - i->SetToken(pp); - - if (p->GetType() == TokenTypVar) - { - if (nullptr != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) - { - if (pStk->GetType() >= CBotTypBoolean) - { - pStk->SetError(TX_BADTYPE, pp); - delete inst; - return pStack->Return(nullptr, pStk); - } - return pStack->Return(i, pStk); - } - delete i; - return pStack->Return(nullptr, pStk); - } - } - - // is it a number or DefineNum? - if (p->GetType() == TokenTypNum || - p->GetType() == TokenTypDef ) - { - CBotInstr* inst = CBotExprNum::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is this a chaine? - if (p->GetType() == TokenTypString) - { - CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is a "true" or "false" - if (p->GetType() == ID_TRUE || - p->GetType() == ID_FALSE ) - { - CBotInstr* inst = CBotExprBool::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is an object to be created with new - if (p->GetType() == ID_NEW) - { - CBotInstr* inst = CBotNew::Compile(p, pStk); - return pStack->Return(inst, pStk); - } - - // is a null pointer - if (IsOfType(p, ID_NULL)) - { - CBotInstr* inst = new CBotExprNull (); - inst->SetToken(pp); - CBotVar* var = CBotVar::Create("", CBotTypNullPointer); - pStk->SetVar(var); - return pStack->Return(inst, pStk); - } - - // is a number nan - if (IsOfType(p, ID_NAN)) - { - CBotInstr* inst = new CBotExprNan (); - inst->SetToken(pp); - CBotVar* var = CBotVar::Create("", CBotTypInt); - var->SetInit(CBotVar::InitType::IS_NAN); - pStk->SetVar(var); - return pStack->Return(inst, pStk); - } - - - return pStack->Return(nullptr, pStk); -} - ////////////////////////////////////////////////////////////////////////////////////// // index management for arrays // array [ expression ] diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 6ae6885f..7fc2a71e 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -755,20 +755,6 @@ public: CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); }; - - -// possibly an expression in parentheses ( ... ) -// there is never an instance of this class -// being the object returned inside the parenthesis -class CBotParExpr : public CBotInstr -{ -private: - -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); -}; - // all operations with two operands class CBotTwoOpExpr : public CBotInstr diff --git a/src/CBot/CBotInstr/CBotExprUnaire.cpp b/src/CBot/CBotInstr/CBotExprUnaire.cpp index 1d63f1dd..e1a18e7f 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.cpp +++ b/src/CBot/CBotInstr/CBotExprUnaire.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotExprUnaire.h" +#include "CBotParExpr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp new file mode 100644 index 00000000..1e9e53ec --- /dev/null +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -0,0 +1,188 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotParExpr.h" +#include "CBotExprUnaire.h" +#include "CBotExprVar.h" +#include "CBotInstrCall.h" +#include "CBotPostIncExpr.h" +#include "CBotPreIncExpr.h" +#include "CBotExprNum.h" +#include "CBotExprAlpha.h" +#include "CBotExprBool.h" +#include "CBotNew.h" +#include "CBotExprNull.h" +#include "CBotExprNan.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GetStart()); + + // is it an expression in parentheses? + if (IsOfType(p, ID_OPENPAR)) + { + CBotInstr* inst = CBotExpression::Compile(p, pStk); + + if (nullptr != inst) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + return pStack->Return(inst, pStk); + } + pStk->SetError(TX_CLOSEPAR, p->GetStart()); + } + delete inst; + return pStack->Return(nullptr, pStk); + } + + // is this a unary operation? + CBotInstr* inst = CBotExprUnaire::Compile(p, pStk); + if (inst != nullptr || !pStk->IsOk()) + return pStack->Return(inst, pStk); + + // is it a variable name? + if (p->GetType() == TokenTypVar) + { + // this may be a method call without the "this." before + inst = CBotExprVar::CompileMethode(p, pStk); + if (inst != nullptr) return pStack->Return(inst, pStk); + + + // is it a procedure call? + inst = CBotInstrCall::Compile(p, pStk); + if (inst != nullptr || !pStk->IsOk()) + return pStack->Return(inst, pStk); + + + CBotToken* pvar = p; + // no, it an "ordinaty" variable + inst = CBotExprVar::Compile(p, pStk); + + CBotToken* pp = p; + // post incremented or decremented? + if (IsOfType(p, ID_INC, ID_DEC)) + { + if (pStk->GetType() >= CBotTypBoolean) + { + pStk->SetError(TX_BADTYPE, pp); + delete inst; + return pStack->Return(nullptr, pStk); + } + + // recompile the variable for read-only + delete inst; + p = pvar; + inst = CBotExprVar::Compile(p, pStk, PR_READ); + p = p->GetNext(); + + CBotPostIncExpr* i = new CBotPostIncExpr(); + i->SetToken(pp); + i->m_Instr = inst; // associated statement + return pStack->Return(i, pStk); + } + return pStack->Return(inst, pStk); + } + + // pre increpemted or pre decremented? + CBotToken* pp = p; + if (IsOfType(p, ID_INC, ID_DEC)) + { + CBotPreIncExpr* i = new CBotPreIncExpr(); + i->SetToken(pp); + + if (p->GetType() == TokenTypVar) + { + if (nullptr != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) + { + if (pStk->GetType() >= CBotTypBoolean) + { + pStk->SetError(TX_BADTYPE, pp); + delete inst; + return pStack->Return(nullptr, pStk); + } + return pStack->Return(i, pStk); + } + delete i; + return pStack->Return(nullptr, pStk); + } + } + + // is it a number or DefineNum? + if (p->GetType() == TokenTypNum || + p->GetType() == TokenTypDef ) + { + CBotInstr* inst = CBotExprNum::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is this a chaine? + if (p->GetType() == TokenTypString) + { + CBotInstr* inst = CBotExprAlpha::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is a "true" or "false" + if (p->GetType() == ID_TRUE || + p->GetType() == ID_FALSE ) + { + CBotInstr* inst = CBotExprBool::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is an object to be created with new + if (p->GetType() == ID_NEW) + { + CBotInstr* inst = CBotNew::Compile(p, pStk); + return pStack->Return(inst, pStk); + } + + // is a null pointer + if (IsOfType(p, ID_NULL)) + { + CBotInstr* inst = new CBotExprNull (); + inst->SetToken(pp); + CBotVar* var = CBotVar::Create("", CBotTypNullPointer); + pStk->SetVar(var); + return pStack->Return(inst, pStk); + } + + // is a number nan + if (IsOfType(p, ID_NAN)) + { + CBotInstr* inst = new CBotExprNan (); + inst->SetToken(pp); + CBotVar* var = CBotVar::Create("", CBotTypInt); + var->SetInit(CBotVar::InitType::IS_NAN); + pStk->SetVar(var); + return pStack->Return(inst, pStk); + } + + + return pStack->Return(nullptr, pStk); +} diff --git a/src/CBot/CBotInstr/CBotParExpr.h b/src/CBot/CBotInstr/CBotParExpr.h new file mode 100644 index 00000000..de0df458 --- /dev/null +++ b/src/CBot/CBotInstr/CBotParExpr.h @@ -0,0 +1,62 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +// possibly an expression in parentheses ( ... ) +// there is never an instance of this class +// being the object returned inside the parenthesis +//////////////////////////////////////////////////////////////////////////////// +// compile either: +// instruction in parentheses (...) +// a unary expression (negative, not) +// variable name +// variables pre and post-incremented or decremented +// a given number DefineNum +// a constant +// procedure call +// new statement +// +// this class has no constructor, because there is never an instance of this class +// the object returned by Compile is the class corresponding to the instruction +//////////////////////////////////////////////////////////////////////////////// + +/*! + * \brief The CBotParExpr class + */ +class CBotParExpr : public CBotInstr +{ +public: + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); +}; diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotTwoOpExpr.cpp index bc3ebdfa..9f4cb2c5 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotTwoOpExpr.cpp @@ -23,6 +23,8 @@ #include "CBot.h" +#include "CBotInstr/CBotParExpr.h" + #include namespace diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 4cff8efd..dc6f7461 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -34,6 +34,7 @@ set(SOURCES CBotInstr/CBotListInstr.cpp CBotInstr/CBotBlock.cpp CBotInstr/CBotExprUnaire.cpp + CBotInstr/CBotParExpr.cpp ) # Includes From 4cd7a7a03111c365eba2f9d5cb460986a0165f80 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 20:44:38 +0100 Subject: [PATCH 29/91] Moving CBotBoolExpr class in its own header and source files. --- src/CBot/CBot.cpp | 30 +----------------- src/CBot/CBot.h | 8 ----- src/CBot/CBotInstr/CBotBoolExpr.cpp | 47 +++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotBoolExpr.h | 47 +++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotFor.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 6 files changed, 97 insertions(+), 37 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotBoolExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotBoolExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 93642bcb..93eec5f6 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -56,6 +56,7 @@ #include "CBotInstr/CBotInstrCall.h" #include "CBotInstr/CBotListInstr.h" #include "CBotInstr/CBotExprUnaire.h" +#include "CBotInstr/CBotBoolExpr.h" // Local include @@ -1644,35 +1645,6 @@ CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) return nullptr; } - -////////////////////////////////////////////////////////////////////////////////////// -// compile a statement such as "(condition)" -// the condition must be Boolean -// -// this class has no constructor, because there is never an instance of this -// class -// the object returned by Compile is usually type CBotExpression -// - -CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - pStack->SetStartError(p->GetStart()); - - CBotInstr* inst = CBotTwoOpExpr::Compile(p, pStack); - - if (nullptr != inst) - { - if (pStack->GetTypResult().Eq(CBotTypBoolean)) - { - return inst; - } - pStack->SetError(TX_NOTBOOL, p->GetStart()); // is not a boolean - } - - delete inst; - return nullptr; -} - ////////////////////////////////////////////////////////////////////////////////////// // index management for arrays // array [ expression ] diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 7fc2a71e..1ad497e4 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -746,14 +746,6 @@ public: -class CBotBoolExpr : public CBotInstr -{ -private: - -public: - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); -}; // all operations with two operands diff --git a/src/CBot/CBotInstr/CBotBoolExpr.cpp b/src/CBot/CBotInstr/CBotBoolExpr.cpp new file mode 100644 index 00000000..4fd4d7b6 --- /dev/null +++ b/src/CBot/CBotInstr/CBotBoolExpr.cpp @@ -0,0 +1,47 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotBoolExpr.h" + +// Local include + +// Global include + + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBoolExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + pStack->SetStartError(p->GetStart()); + + CBotInstr* inst = CBotTwoOpExpr::Compile(p, pStack); + + if (nullptr != inst) + { + if (pStack->GetTypResult().Eq(CBotTypBoolean)) + { + return inst; + } + pStack->SetError(TX_NOTBOOL, p->GetStart()); // is not a boolean + } + + delete inst; + return nullptr; +} diff --git a/src/CBot/CBotInstr/CBotBoolExpr.h b/src/CBot/CBotInstr/CBotBoolExpr.h new file mode 100644 index 00000000..df7354ee --- /dev/null +++ b/src/CBot/CBotInstr/CBotBoolExpr.h @@ -0,0 +1,47 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotBoolExpr class Compile a statement such as "(condition)" + * the condition must be Boolean. This class has no constructor, because there + * is never an instance of this class the object returned by Compile is usually + * type CBotExpression + */ +class CBotBoolExpr : public CBotInstr +{ +public: + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); +}; diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index 32caa3c5..d83650cb 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -21,6 +21,7 @@ #include "CBotFor.h" #include "CBotListExpression.h" #include "CBotBlock.h" +#include "CBotBoolExpr.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index dc6f7461..697dd019 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -35,6 +35,7 @@ set(SOURCES CBotInstr/CBotBlock.cpp CBotInstr/CBotExprUnaire.cpp CBotInstr/CBotParExpr.cpp + CBotInstr/CBotBoolExpr.cpp ) # Includes From 8ee0b7df5687f03a5224172f5108fa4002324a7f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 21:03:16 +0100 Subject: [PATCH 30/91] Moving CBotLogicExpr class in its own header and source files. --- src/CBot/CBot.h | 19 ------ src/CBot/CBotInstr/CBotLogicExpr.cpp | 92 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotLogicExpr.h | 70 +++++++++++++++++++++ src/CBot/CBotTwoOpExpr.cpp | 65 +------------------- src/CBot/CMakeLists.txt | 1 + 5 files changed, 164 insertions(+), 83 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotLogicExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotLogicExpr.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 1ad497e4..976789cf 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -727,25 +727,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotLogicExpr : public CBotInstr -{ -private: - CBotInstr* m_condition; // test to evaluate - CBotInstr* m_op1; // left element - CBotInstr* m_op2; // right element - friend class CBotTwoOpExpr; - -public: - CBotLogicExpr(); - ~CBotLogicExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - - // all operations with two operands diff --git a/src/CBot/CBotInstr/CBotLogicExpr.cpp b/src/CBot/CBotInstr/CBotLogicExpr.cpp new file mode 100644 index 00000000..bfa6af1a --- /dev/null +++ b/src/CBot/CBotInstr/CBotLogicExpr.cpp @@ -0,0 +1,92 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotLogicExpr.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotLogicExpr::CBotLogicExpr() +{ + m_condition = + m_op1 = + m_op2 = nullptr; // nullptr to be able to delete without other + name = "CBotLogicExpr"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotLogicExpr::~CBotLogicExpr() +{ + delete m_condition; + delete m_op1; + delete m_op2; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotLogicExpr::Execute(CBotStack* &pStack) +{ + CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack + // or return in case of recovery +// if ( pStk1 == EOX ) return true; + + if ( pStk1->GetState() == 0 ) + { + if ( !m_condition->Execute(pStk1) ) return false; + if (!pStk1->SetState(1)) return false; + } + + if ( pStk1->GetVal() == true ) + { + if ( !m_op1->Execute(pStk1) ) return false; + } + else + { + if ( !m_op2->Execute(pStk1) ) return false; + } + + return pStack->Return(pStk1); // transmits the result +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) +{ + if ( !bMain ) return; + + CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack + if ( pStk1 == nullptr ) return; + + if ( pStk1->GetState() == 0 ) + { + m_condition->RestoreState(pStk1, bMain); + return; + } + + if ( pStk1->GetVal() == true ) + { + m_op1->RestoreState(pStk1, bMain); + } + else + { + m_op2->RestoreState(pStk1, bMain); + } +} + diff --git a/src/CBot/CBotInstr/CBotLogicExpr.h b/src/CBot/CBotInstr/CBotLogicExpr.h new file mode 100644 index 00000000..2bffc243 --- /dev/null +++ b/src/CBot/CBotInstr/CBotLogicExpr.h @@ -0,0 +1,70 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotLogicExpr class + */ +class CBotLogicExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotLogicExpr + */ + CBotLogicExpr(); + + /*! + * \brief ~CBotLogicExpr + */ + ~CBotLogicExpr(); + + /*! + * \brief Execute + * \param pStack + * \return + */ + bool Execute(CBotStack* &pStack) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! Test to evaluate + CBotInstr* m_condition; + //! Left element + CBotInstr* m_op1; + //! Right element + CBotInstr* m_op2; + friend class CBotTwoOpExpr; +}; diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotTwoOpExpr.cpp index 9f4cb2c5..d9173d60 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotTwoOpExpr.cpp @@ -24,6 +24,7 @@ #include "CBot.h" #include "CBotInstr/CBotParExpr.h" +#include "CBotInstr/CBotLogicExpr.h" #include @@ -50,22 +51,6 @@ CBotTwoOpExpr::~CBotTwoOpExpr() delete m_rightop; } -CBotLogicExpr::CBotLogicExpr() -{ - m_condition = - m_op1 = - m_op2 = nullptr; // nullptr to be able to delete without other - name = "CBotLogicExpr"; // debug -} - -CBotLogicExpr::~CBotLogicExpr() -{ - delete m_condition; - delete m_op1; - delete m_op2; -} - - // type of operands accepted by operations #define ENTIER ((1<AddStack(this); // adds an item to the stack - // or return in case of recovery -// if ( pStk1 == EOX ) return true; - - if ( pStk1->GetState() == 0 ) - { - if ( !m_condition->Execute(pStk1) ) return false; - if (!pStk1->SetState(1)) return false; - } - - if ( pStk1->GetVal() == true ) - { - if ( !m_op1->Execute(pStk1) ) return false; - } - else - { - if ( !m_op2->Execute(pStk1) ) return false; - } - - return pStack->Return(pStk1); // transmits the result -} - -void CBotLogicExpr::RestoreState(CBotStack* &pStack, bool bMain) -{ - if ( !bMain ) return; - - CBotStack* pStk1 = pStack->RestoreStack(this); // adds an item to the stack - if ( pStk1 == nullptr ) return; - - if ( pStk1->GetState() == 0 ) - { - m_condition->RestoreState(pStk1, bMain); - return; - } - - if ( pStk1->GetVal() == true ) - { - m_op1->RestoreState(pStk1, bMain); - } - else - { - m_op2->RestoreState(pStk1, bMain); - } -} - #if 0 void t() { diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 697dd019..150724a9 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -36,6 +36,7 @@ set(SOURCES CBotInstr/CBotExprUnaire.cpp CBotInstr/CBotParExpr.cpp CBotInstr/CBotBoolExpr.cpp + CBotInstr/CBotLogicExpr.cpp ) # Includes From d44df45d26e1ae02206ba2d32a4e2fc078ce44b0 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 21:27:56 +0100 Subject: [PATCH 31/91] Moving CBotTwoOpExpr class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 17 ----- src/CBot/CBotClass.cpp | 1 + src/CBot/CBotFunction.cpp | 1 + src/CBot/CBotInstr/CBotBoolExpr.cpp | 1 + src/CBot/{ => CBotInstr}/CBotTwoOpExpr.cpp | 55 +++++---------- src/CBot/CBotInstr/CBotTwoOpExpr.h | 78 ++++++++++++++++++++++ src/CBot/CMakeLists.txt | 2 +- 8 files changed, 99 insertions(+), 57 deletions(-) rename src/CBot/{ => CBotInstr}/CBotTwoOpExpr.cpp (96%) create mode 100644 src/CBot/CBotInstr/CBotTwoOpExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 93eec5f6..78bf8ca0 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -57,6 +57,7 @@ #include "CBotInstr/CBotListInstr.h" #include "CBotInstr/CBotExprUnaire.h" #include "CBotInstr/CBotBoolExpr.h" +#include "CBotInstr/CBotTwoOpExpr.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 976789cf..31edd2a9 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -727,23 +727,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -// all operations with two operands - -class CBotTwoOpExpr : public CBotInstr -{ -private: - CBotInstr* m_leftop; // left element - CBotInstr* m_rightop; // right element -public: - CBotTwoOpExpr(); - ~CBotTwoOpExpr(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = nullptr); - bool Execute(CBotStack* &pStack) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index b7aeb937..11f93a1f 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -24,6 +24,7 @@ #include "CBot.h" #include "CBotInstr/CBotNew.h" #include "CBotInstr/CBotLeftExprVar.h" +#include "CBotInstr/CBotTwoOpExpr.h" CBotClass* CBotClass::m_ExClass = nullptr; diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 6ebad172..838c8de1 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -24,6 +24,7 @@ #include "CBot.h" #include "CBotInstr/CBotBlock.h" +#include "CBotInstr/CBotTwoOpExpr.h" #include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.cpp b/src/CBot/CBotInstr/CBotBoolExpr.cpp index 4fd4d7b6..2815c8ed 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.cpp +++ b/src/CBot/CBotInstr/CBotBoolExpr.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotBoolExpr.h" +#include "CBotTwoOpExpr.h" // Local include diff --git a/src/CBot/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp similarity index 96% rename from src/CBot/CBotTwoOpExpr.cpp rename to src/CBot/CBotInstr/CBotTwoOpExpr.cpp index d9173d60..b381ca46 100644 --- a/src/CBot/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -17,27 +17,18 @@ * along with this program. If not, see http://gnu.org/licenses */ -/////////////////////////////////////////////////// -// expression of type Opérande1 + Opérande2 -// Opérande1 > Opérande2 - +// Modules inlcude +#include "CBotTwoOpExpr.h" +#include "CBotParExpr.h" +#include "CBotLogicExpr.h" #include "CBot.h" -#include "CBotInstr/CBotParExpr.h" -#include "CBotInstr/CBotLogicExpr.h" +// Local include +// Global include #include -namespace -{ -bool VarIsNAN(const CBotVar* var) -{ - return var->GetInit() > CBotVar::InitType::DEF; -} -} - -// various constructors - +//////////////////////////////////////////////////////////////////////////////// CBotTwoOpExpr::CBotTwoOpExpr() { m_leftop = @@ -45,6 +36,7 @@ CBotTwoOpExpr::CBotTwoOpExpr() name = "CBotTwoOpExpr"; // debug } +//////////////////////////////////////////////////////////////////////////////// CBotTwoOpExpr::~CBotTwoOpExpr() { delete m_leftop; @@ -116,8 +108,7 @@ bool TypeOk( int type, int test ) } } -// compiles a instruction of type A op B - +//////////////////////////////////////////////////////////////////////////////// CBotInstr* CBotTwoOpExpr::Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations) { int typemasque; @@ -281,6 +272,11 @@ CBotInstr* CBotTwoOpExpr::Compile(CBotToken* &p, CBotCStack* pStack, int* pOpera } +bool VarIsNAN(const CBotVar* var) +{ + return var->GetInit() > CBotVar::InitType::DEF; +} + bool IsNan(CBotVar* left, CBotVar* right, int* err = nullptr) { if ( VarIsNAN(left) || VarIsNAN(right) ) @@ -291,9 +287,7 @@ bool IsNan(CBotVar* left, CBotVar* right, int* err = nullptr) return false; } - -// performes the operation on two operands - +//////////////////////////////////////////////////////////////////////////////// bool CBotTwoOpExpr::Execute(CBotStack* &pStack) { CBotStack* pStk1 = pStack->AddStack(this); // adds an item to the stack @@ -477,6 +471,7 @@ bool CBotTwoOpExpr::Execute(CBotStack* &pStack) return pStack->Return(pStk2); // transmits the result } +//////////////////////////////////////////////////////////////////////////////// void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, bool bMain) { if ( !bMain ) return; @@ -501,21 +496,3 @@ void CBotTwoOpExpr::RestoreState(CBotStack* &pStack, bool bMain) return; } } - -#if 0 -void t() -{ - int x,y; - 1>0 ? x = 0 : y = 0; -} -#endif - -#if 0 -void t(bool t) -{ - int x; - x = 1 + t ? 1 : 3 + 4 * 2 ; - t ? 0 : "test"; -} -#endif - diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.h b/src/CBot/CBotInstr/CBotTwoOpExpr.h new file mode 100644 index 00000000..1bcc2f8f --- /dev/null +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.h @@ -0,0 +1,78 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotTwoOpExpr class All operations with two operands. + * eg : + * - Opérande1 + Opérande2 + * - Opérande1 > Opérande2 + */ +class CBotTwoOpExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotTwoOpExpr + */ + CBotTwoOpExpr(); + + /*! + * \brief ~CBotTwoOpExpr + */ + ~CBotTwoOpExpr(); + + /*! + * \brief Compile Compiles a instruction of type A op B. + * \param p + * \param pStack + * \param pOperations + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int* pOperations = nullptr); + + /*! + * \brief Execute Performes the operation on two operands. + * \param pStack + * \return + */ + bool Execute(CBotStack* &pStack) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Left element + CBotInstr* m_leftop; + //! Right element + CBotInstr* m_rightop; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 150724a9..ba8f1a2b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -7,7 +7,6 @@ set(SOURCES CBotStack.cpp CBotString.cpp CBotToken.cpp - CBotTwoOpExpr.cpp CBotVar.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp @@ -37,6 +36,7 @@ set(SOURCES CBotInstr/CBotParExpr.cpp CBotInstr/CBotBoolExpr.cpp CBotInstr/CBotLogicExpr.cpp + CBotInstr/CBotTwoOpExpr.cpp ) # Includes From 3c1296b4b931b7338e014c5dbf84f958896e96b1 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 21:43:56 +0100 Subject: [PATCH 32/91] Moving CBotExpression class in its own header and source files. --- src/CBot/CBot.cpp | 271 +------------------- src/CBot/CBot.h | 19 -- src/CBot/CBotFunction.cpp | 1 + src/CBot/CBotInstr/CBotCatch.cpp | 1 + src/CBot/CBotInstr/CBotExprVar.cpp | 1 + src/CBot/CBotInstr/CBotExpression.cpp | 290 ++++++++++++++++++++++ src/CBot/CBotInstr/CBotExpression.h | 85 +++++++ src/CBot/CBotInstr/CBotInstrCall.cpp | 1 + src/CBot/CBotInstr/CBotListExpression.cpp | 1 + src/CBot/CBotInstr/CBotParExpr.cpp | 1 + src/CBot/CBotInstr/CBotSwitch.cpp | 1 + src/CBot/CBotInstr/CBotThrow.cpp | 1 + src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 14 files changed, 386 insertions(+), 289 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotExpression.cpp create mode 100644 src/CBot/CBotInstr/CBotExpression.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 78bf8ca0..c2b392b8 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -58,6 +58,7 @@ #include "CBotInstr/CBotExprUnaire.h" #include "CBotInstr/CBotBoolExpr.h" #include "CBotInstr/CBotTwoOpExpr.h" +#include "CBotInstr/CBotExpression.h" // Local include @@ -1345,276 +1346,6 @@ void CBotIString::RestoreState(CBotStack* &pj, bool bMain) m_next2b->RestoreState(pile, bMain); } -////////////////////////////////////////////////////////////////////////////////////////// - - - -////////////////////////////////////////////////////////////////////////////////////// -// compiles a statement such as " x = 123 " ou " z * 5 + 4 " -// with or without assignment - -CBotExpression::CBotExpression() -{ - m_leftop = nullptr; - m_rightop = nullptr; - name = "CBotExpression"; -} - -CBotExpression::~CBotExpression() -{ - delete m_leftop; - delete m_rightop; -} - -CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - - CBotExpression* inst = new CBotExpression(); - - inst->m_leftop = CBotLeftExpr::Compile(p, pStack); - - inst->SetToken(p); - int OpType = p->GetType(); - - if ( pStack->IsOk() && - IsOfTypeList(p, ID_ASS, ID_ASSADD, ID_ASSSUB, ID_ASSMUL, ID_ASSDIV, ID_ASSMODULO, - ID_ASSAND, ID_ASSXOR, ID_ASSOR, - ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) - { - if (inst->m_leftop == nullptr) - { - pStack->SetError(TX_BADLEFT, p->GetEnd()); - delete inst; - return nullptr; - } - - inst->m_rightop = CBotExpression::Compile(p, pStack); - if (inst->m_rightop == nullptr) - { - delete inst; - return nullptr; - } - - CBotTypResult type1 = pStack->GetTypResult(); - - // get the variable assigned to mark - CBotVar* var = nullptr; - inst->m_leftop->ExecuteVar(var, pStack); - if (var == nullptr) - { - delete inst; - return nullptr; - } - - if (OpType != ID_ASS && !var->IsDefined()) - { - pStack->SetError(TX_NOTINIT, pp); - delete inst; - return nullptr; - } - - CBotTypResult type2 = var->GetTypResult(); - - // what types are acceptable? - switch (OpType) - { - case ID_ASS: - // if (type2 == CBotTypClass) type2 = -1; - if ((type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer)) || - (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) - { -/* CBotClass* c1 = type1.GetClass(); - CBotClass* c2 = type2.GetClass(); - if (!c1->IsChildOf(c2)) type2.SetType(-1); -//- if (!type1.Eq(CBotTypClass)) var->SetPointer(pStack->GetVar()->GetPointer());*/ - var->SetInit(CBotVar::InitType::IS_POINTER); - } - else - var->SetInit(CBotVar::InitType::DEF); - - break; - case ID_ASSADD: - if (type2.Eq(CBotTypBoolean) || - type2.Eq(CBotTypPointer) ) type2 = -1; // numbers and strings - break; - case ID_ASSSUB: - case ID_ASSMUL: - case ID_ASSDIV: - case ID_ASSMODULO: - if (type2.GetType() >= CBotTypBoolean) type2 = -1; // numbers only - break; - } - - if (!TypeCompatible(type1, type2, OpType)) - { - pStack->SetError(TX_BADTYPE, &inst->m_token); - delete inst; - return nullptr; - } - - return inst; // compatible type? - } - - delete inst; - int start, end, error = pStack->GetError(start, end); - - p = pp; // returns to the top - pStack->SetError(0,0); // forget the error - - CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // tries without assignment - if (i != nullptr && error == TX_PRIVATE && p->GetType() == ID_ASS) - pStack->ResetError(error, start, end); - return i; -} - -// executes an expression with assignment - -bool CBotExpression::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - -// CBotToken* pToken = m_leftop->GetToken(); - - CBotVar* pVar = nullptr; - - CBotStack* pile1 = pile; - - CBotVar::InitType initKind = CBotVar::InitType::DEF; - CBotVar* result = nullptr; - - // must be done before any indexes (stack can be changed) - if (!m_leftop->ExecuteVar(pVar, pile, nullptr, false)) return false; // variable before accessing the value on the right - - if ( pile1->GetState()==0) - { - pile1->SetCopyVar(pVar); // keeps the copy on the stack (if interrupted) - pile1->IncState(); - } - - CBotStack* pile2 = pile->AddStack(); - - if ( pile2->GetState()==0) - { - if (m_rightop && !m_rightop->Execute(pile2)) return false; // initial value // interrupted? - pile2->IncState(); - } - - if (pile1->GetState() == 1) - { - if (m_token.GetType() != ID_ASS) - { - pVar = pile1->GetVar(); // recovers if interrupted - initKind = pVar->GetInit(); - if (initKind == CBotVar::InitType::IS_NAN) - { - pile2->SetError(TX_OPNAN, m_leftop->GetToken()); - return pj->Return(pile2); - } - result = CBotVar::Create("", pVar->GetTypResult(2)); - } - - switch (m_token.GetType()) - { - case ID_ASS: - break; - case ID_ASSADD: - result->Add(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSSUB: - result->Sub(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSMUL: - result->Mul(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSDIV: - if (initKind != CBotVar::InitType::UNDEF && - result->Div(pile1->GetVar(), pile2->GetVar())) - pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); - break; - case ID_ASSMODULO: - if (initKind != CBotVar::InitType::UNDEF && - result->Modulo(pile1->GetVar(), pile2->GetVar())) - pile2->SetError(TX_DIVZERO, &m_token); - pile2->SetVar(result); - break; - case ID_ASSAND: - result->And(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSXOR: - result->XOr(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSOR: - result->Or(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSSL: - result->SL(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSSR: - result->SR(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - case ID_ASSASR: - result->ASR(pile1->GetVar(), pile2->GetVar()); - pile2->SetVar(result); - break; - default: - assert(0); - } - if (initKind == CBotVar::InitType::UNDEF) - pile2->SetError(TX_NOTINIT, m_leftop->GetToken()); - - pile1->IncState(); - } - - if (!m_leftop->Execute( pile2, pile1 )) - return false; - - return pj->Return(pile2); -} - - -void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) - { -// CBotToken* pToken = m_leftop->GetToken(); -// CBotVar* pVar = nullptr; - - CBotStack* pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - CBotStack* pile1 = pile; - - if ( pile1->GetState()==0) - { - m_leftop->RestoreStateVar(pile, true); - return; - } - - m_leftop->RestoreStateVar(pile, false); - - CBotStack* pile2 = pile->RestoreStack(); - if (pile2 == nullptr) return; - - if ( pile2->GetState()==0) - { - if (m_rightop) m_rightop->RestoreState(pile2, bMain); - return; - } - } -} - -////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // compile a statement such as "(condition)" diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 31edd2a9..1709ba11 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -708,25 +708,6 @@ public: void RestoreStateVar(CBotStack* &pj, bool bMain) override; }; -// expressions like -// x = a; -// x * y + 3; - -class CBotExpression : public CBotInstr -{ -private: - CBotLeftExpr* m_leftop; // left operand - CBotInstr* m_rightop; // right operant - -public: - CBotExpression(); - ~CBotExpression(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 838c8de1..b672f32a 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -25,6 +25,7 @@ #include "CBotInstr/CBotBlock.h" #include "CBotInstr/CBotTwoOpExpr.h" +#include "CBotInstr/CBotExpression.h" #include diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index e4a5571e..e4d9bb4c 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotCatch.h" #include "CBotBlock.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 57b2b431..0101fcba 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotExprVar.h" #include "CBotInstrMethode.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp new file mode 100644 index 00000000..cb47103b --- /dev/null +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -0,0 +1,290 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotExpression.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include +#include + + +////////////////////////////////////////////////////////////////////////////////////// +CBotExpression::CBotExpression() +{ + m_leftop = nullptr; + m_rightop = nullptr; + name = "CBotExpression"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotExpression::~CBotExpression() +{ + delete m_leftop; + delete m_rightop; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotExpression::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + + CBotExpression* inst = new CBotExpression(); + + inst->m_leftop = CBotLeftExpr::Compile(p, pStack); + + inst->SetToken(p); + int OpType = p->GetType(); + + if ( pStack->IsOk() && + IsOfTypeList(p, ID_ASS, ID_ASSADD, ID_ASSSUB, ID_ASSMUL, ID_ASSDIV, ID_ASSMODULO, + ID_ASSAND, ID_ASSXOR, ID_ASSOR, + ID_ASSSL , ID_ASSSR, ID_ASSASR, 0 )) + { + if (inst->m_leftop == nullptr) + { + pStack->SetError(TX_BADLEFT, p->GetEnd()); + delete inst; + return nullptr; + } + + inst->m_rightop = CBotExpression::Compile(p, pStack); + if (inst->m_rightop == nullptr) + { + delete inst; + return nullptr; + } + + CBotTypResult type1 = pStack->GetTypResult(); + + // get the variable assigned to mark + CBotVar* var = nullptr; + inst->m_leftop->ExecuteVar(var, pStack); + if (var == nullptr) + { + delete inst; + return nullptr; + } + + if (OpType != ID_ASS && !var->IsDefined()) + { + pStack->SetError(TX_NOTINIT, pp); + delete inst; + return nullptr; + } + + CBotTypResult type2 = var->GetTypResult(); + + // what types are acceptable? + switch (OpType) + { + case ID_ASS: + // if (type2 == CBotTypClass) type2 = -1; + if ((type1.Eq(CBotTypPointer) && type2.Eq(CBotTypPointer)) || + (type1.Eq(CBotTypClass) && type2.Eq(CBotTypClass) ) ) + { +/* CBotClass* c1 = type1.GetClass(); + CBotClass* c2 = type2.GetClass(); + if (!c1->IsChildOf(c2)) type2.SetType(-1); +//- if (!type1.Eq(CBotTypClass)) var->SetPointer(pStack->GetVar()->GetPointer());*/ + var->SetInit(CBotVar::InitType::IS_POINTER); + } + else + var->SetInit(CBotVar::InitType::DEF); + + break; + case ID_ASSADD: + if (type2.Eq(CBotTypBoolean) || + type2.Eq(CBotTypPointer) ) type2 = -1; // numbers and strings + break; + case ID_ASSSUB: + case ID_ASSMUL: + case ID_ASSDIV: + case ID_ASSMODULO: + if (type2.GetType() >= CBotTypBoolean) type2 = -1; // numbers only + break; + } + + if (!TypeCompatible(type1, type2, OpType)) + { + pStack->SetError(TX_BADTYPE, &inst->m_token); + delete inst; + return nullptr; + } + + return inst; // compatible type? + } + + delete inst; + int start, end, error = pStack->GetError(start, end); + + p = pp; // returns to the top + pStack->SetError(0,0); // forget the error + + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStack); // tries without assignment + if (i != nullptr && error == TX_PRIVATE && p->GetType() == ID_ASS) + pStack->ResetError(error, start, end); + return i; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotExpression::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + +// CBotToken* pToken = m_leftop->GetToken(); + + CBotVar* pVar = nullptr; + + CBotStack* pile1 = pile; + + CBotVar::InitType initKind = CBotVar::InitType::DEF; + CBotVar* result = nullptr; + + // must be done before any indexes (stack can be changed) + if (!m_leftop->ExecuteVar(pVar, pile, nullptr, false)) return false; // variable before accessing the value on the right + + if ( pile1->GetState()==0) + { + pile1->SetCopyVar(pVar); // keeps the copy on the stack (if interrupted) + pile1->IncState(); + } + + CBotStack* pile2 = pile->AddStack(); + + if ( pile2->GetState()==0) + { + if (m_rightop && !m_rightop->Execute(pile2)) return false; // initial value // interrupted? + pile2->IncState(); + } + + if (pile1->GetState() == 1) + { + if (m_token.GetType() != ID_ASS) + { + pVar = pile1->GetVar(); // recovers if interrupted + initKind = pVar->GetInit(); + if (initKind == CBotVar::InitType::IS_NAN) + { + pile2->SetError(TX_OPNAN, m_leftop->GetToken()); + return pj->Return(pile2); + } + result = CBotVar::Create("", pVar->GetTypResult(2)); + } + + switch (m_token.GetType()) + { + case ID_ASS: + break; + case ID_ASSADD: + result->Add(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSSUB: + result->Sub(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSMUL: + result->Mul(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSDIV: + if (initKind != CBotVar::InitType::UNDEF && + result->Div(pile1->GetVar(), pile2->GetVar())) + pile2->SetError(TX_DIVZERO, &m_token); + pile2->SetVar(result); + break; + case ID_ASSMODULO: + if (initKind != CBotVar::InitType::UNDEF && + result->Modulo(pile1->GetVar(), pile2->GetVar())) + pile2->SetError(TX_DIVZERO, &m_token); + pile2->SetVar(result); + break; + case ID_ASSAND: + result->And(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSXOR: + result->XOr(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSOR: + result->Or(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSSL: + result->SL(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSSR: + result->SR(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + case ID_ASSASR: + result->ASR(pile1->GetVar(), pile2->GetVar()); + pile2->SetVar(result); + break; + default: + assert(0); + } + if (initKind == CBotVar::InitType::UNDEF) + pile2->SetError(TX_NOTINIT, m_leftop->GetToken()); + + pile1->IncState(); + } + + if (!m_leftop->Execute( pile2, pile1 )) + return false; + + return pj->Return(pile2); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotExpression::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) + { +// CBotToken* pToken = m_leftop->GetToken(); +// CBotVar* pVar = nullptr; + + CBotStack* pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + CBotStack* pile1 = pile; + + if ( pile1->GetState()==0) + { + m_leftop->RestoreStateVar(pile, true); + return; + } + + m_leftop->RestoreStateVar(pile, false); + + CBotStack* pile2 = pile->RestoreStack(); + if (pile2 == nullptr) return; + + if ( pile2->GetState()==0) + { + if (m_rightop) m_rightop->RestoreState(pile2, bMain); + return; + } + } +} diff --git a/src/CBot/CBotInstr/CBotExpression.h b/src/CBot/CBotInstr/CBotExpression.h new file mode 100644 index 00000000..b47e9e24 --- /dev/null +++ b/src/CBot/CBotInstr/CBotExpression.h @@ -0,0 +1,85 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotExpression class Compiles a statement with or without + * assignment. + * eg : + * - x = a; + * - x * y + 3; + * - x = 123 + * - z * 5 + 4 + */ + + +// compiles a statement such as " " ou " z * 5 + 4 " +// + +class CBotExpression : public CBotInstr +{ +public: + + /*! + * \brief CBotExpression + */ + CBotExpression(); + + /*! + * \brief ~CBotExpression + */ + ~CBotExpression(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Executes an expression with assignment. + * \param pStack + * \return + */ + bool Execute(CBotStack* &pStack) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Left operand + CBotLeftExpr* m_leftop; + //! Right operand + CBotInstr* m_rightop; +}; diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp index 66a48e34..615f041a 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.cpp +++ b/src/CBot/CBotInstr/CBotInstrCall.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotInstrCall.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index 66f7eafc..c631e762 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotListExpression.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index 1e9e53ec..5d2839e8 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -30,6 +30,7 @@ #include "CBotNew.h" #include "CBotExprNull.h" #include "CBotExprNan.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index 70fd8033..45558dc1 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -23,6 +23,7 @@ #include "CBotSwitch.h" #include "CBotCase.h" #include "CBotBlock.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotThrow.cpp b/src/CBot/CBotInstr/CBotThrow.cpp index 14e61f07..a4a1831e 100644 --- a/src/CBot/CBotInstr/CBotThrow.cpp +++ b/src/CBot/CBotInstr/CBotThrow.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotThrow.h" +#include "CBotExpression.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index b381ca46..275730a6 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -21,6 +21,7 @@ #include "CBotTwoOpExpr.h" #include "CBotParExpr.h" #include "CBotLogicExpr.h" +#include "CBotExpression.h" #include "CBot.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index ba8f1a2b..ce4f6d75 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -37,6 +37,7 @@ set(SOURCES CBotInstr/CBotBoolExpr.cpp CBotInstr/CBotLogicExpr.cpp CBotInstr/CBotTwoOpExpr.cpp + CBotInstr/CBotExpression.cpp ) # Includes From d0cfdfb998d15818d9e5ef31869cd836434e7dfe Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 21:55:02 +0100 Subject: [PATCH 33/91] Moving CBotIndexExpr class in its own header and source files. --- src/CBot/CBot.cpp | 98 +---------------------- src/CBot/CBot.h | 17 ---- src/CBot/CBotInstr/CBotExprVar.cpp | 1 + src/CBot/CBotInstr/CBotIndexExpr.cpp | 115 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotIndexExpr.h | 81 +++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 199 insertions(+), 114 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotIndexExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotIndexExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index c2b392b8..d0a1239e 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -59,6 +59,7 @@ #include "CBotInstr/CBotBoolExpr.h" #include "CBotInstr/CBotTwoOpExpr.h" #include "CBotInstr/CBotExpression.h" +#include "CBotInstr/CBotIndexExpr.h" // Local include @@ -1377,103 +1378,6 @@ CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) return nullptr; } -////////////////////////////////////////////////////////////////////////////////////// -// index management for arrays -// array [ expression ] - - -CBotIndexExpr::CBotIndexExpr() -{ - m_expr = nullptr; - name = "CBotIndexExpr"; -} - -CBotIndexExpr::~CBotIndexExpr() -{ - delete m_expr; -} - -// finds a field from the instance at compile time - -bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - if (pVar->GetType(1) != CBotTypArrayPointer) - assert(0); - - pVar = (static_cast(pVar))->GetItem(0, false); // at compile time makes the element [0] - if (pVar == nullptr) - { - pile->SetError(TX_OUTARRAY, m_token.GetEnd()); - return false; - } - if (m_next3 != nullptr) return m_next3->ExecuteVar(pVar, pile); - return true; -} - -// attention, changes the pointer to the stack intentionally -// place the index calculated on the additional stack - - -bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) -{ - CBotStack* pj = pile; - - if (pVar->GetType(1) != CBotTypArrayPointer) - assert(0); - - pile = pile->AddStack(); - - if (pile->GetState() == 0) - { - if (!m_expr->Execute(pile)) return false; - pile->IncState(); - } - // handles array - - CBotVar* p = pile->GetVar(); // result on the stack - - if (p == nullptr || p->GetType() > CBotTypDouble) - { - pile->SetError(TX_BADINDEX, prevToken); - return pj->Return(pile); - } - - int n = p->GetValInt(); // position in the table - - pVar = (static_cast(pVar))->GetItem(n, bExtend); - if (pVar == nullptr) - { - pile->SetError(TX_OUTARRAY, prevToken); - return pj->Return(pile); - } - - pVar->Maj(pile->GetPUser(), true); - - if ( m_next3 != nullptr && - !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; - - // does not release the stack - // to avoid recalculation of the index twice where appropriate - return true; -} - -void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - pile = pile->RestoreStack(); - if (pile == nullptr) return; - - if (bMain && pile->GetState() == 0) - { - m_expr->RestoreState(pile, true); - return; - } - - if (m_next3) - m_next3->RestoreStateVar(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // field management in an instance (dot operator) // toto.x diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 1709ba11..995e5c6d 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -689,24 +689,7 @@ public: void RestoreStateVar(CBotStack* &pj, bool bMain) override; }; -// management of indices of the tables -class CBotIndexExpr : public CBotInstr -{ -private: - CBotInstr* m_expr; // expression for calculating the index - friend class CBotLeftExpr; - friend class CBotExprVar; - -public: - CBotIndexExpr(); - ~CBotIndexExpr(); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) override; - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) override; - void RestoreStateVar(CBotStack* &pj, bool bMain) override; -}; #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 0101fcba..93ffeecf 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -21,6 +21,7 @@ #include "CBotExprVar.h" #include "CBotInstrMethode.h" #include "CBotExpression.h" +#include "CBotIndexExpr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.cpp b/src/CBot/CBotInstr/CBotIndexExpr.cpp new file mode 100644 index 00000000..2d3d6d2a --- /dev/null +++ b/src/CBot/CBotInstr/CBotIndexExpr.cpp @@ -0,0 +1,115 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotIndexExpr.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotIndexExpr::CBotIndexExpr() +{ + m_expr = nullptr; + name = "CBotIndexExpr"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotIndexExpr::~CBotIndexExpr() +{ + delete m_expr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + if (pVar->GetType(1) != CBotTypArrayPointer) + assert(0); + + pVar = (static_cast(pVar))->GetItem(0, false); // at compile time makes the element [0] + if (pVar == nullptr) + { + pile->SetError(TX_OUTARRAY, m_token.GetEnd()); + return false; + } + if (m_next3 != nullptr) return m_next3->ExecuteVar(pVar, pile); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotIndexExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) +{ + CBotStack* pj = pile; + + if (pVar->GetType(1) != CBotTypArrayPointer) + assert(0); + + pile = pile->AddStack(); + + if (pile->GetState() == 0) + { + if (!m_expr->Execute(pile)) return false; + pile->IncState(); + } + // handles array + + CBotVar* p = pile->GetVar(); // result on the stack + + if (p == nullptr || p->GetType() > CBotTypDouble) + { + pile->SetError(TX_BADINDEX, prevToken); + return pj->Return(pile); + } + + int n = p->GetValInt(); // position in the table + + pVar = (static_cast(pVar))->GetItem(n, bExtend); + if (pVar == nullptr) + { + pile->SetError(TX_OUTARRAY, prevToken); + return pj->Return(pile); + } + + pVar->Maj(pile->GetPUser(), true); + + if ( m_next3 != nullptr && + !m_next3->ExecuteVar(pVar, pile, prevToken, bStep, bExtend) ) return false; + + // does not release the stack + // to avoid recalculation of the index twice where appropriate + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotIndexExpr::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + pile = pile->RestoreStack(); + if (pile == nullptr) return; + + if (bMain && pile->GetState() == 0) + { + m_expr->RestoreState(pile, true); + return; + } + + if (m_next3) + m_next3->RestoreStateVar(pile, bMain); +} diff --git a/src/CBot/CBotInstr/CBotIndexExpr.h b/src/CBot/CBotInstr/CBotIndexExpr.h new file mode 100644 index 00000000..730a4de4 --- /dev/null +++ b/src/CBot/CBotInstr/CBotIndexExpr.h @@ -0,0 +1,81 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotIndexExpr class Index management for arrays + * eg : + * - array [ expression ] + */ +class CBotIndexExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotIndexExpr + */ + CBotIndexExpr(); + + /*! + * \brief ~CBotIndexExpr + */ + ~CBotIndexExpr(); + + /*! + * \brief ExecuteVar Finds a field from the instance at compile time. + * \param pVar + * \param pile + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) override; + + /*! + * \brief ExecuteVar Warning, changes the pointer to the stack intentionally + * place the index calculated on the additional stack. + * \param pVar + * \param pile + * \param prevToken + * \param bStep + * \param bExtend + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) override; + + /*! + * \brief RestoreStateVar + * \param pj + * \param bMain + */ + void RestoreStateVar(CBotStack* &pj, bool bMain) override; + + +private: + //! Expression for calculating the index. + CBotInstr* m_expr; + friend class CBotLeftExpr; + friend class CBotExprVar; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index ce4f6d75..bb4d3256 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -38,6 +38,7 @@ set(SOURCES CBotInstr/CBotLogicExpr.cpp CBotInstr/CBotTwoOpExpr.cpp CBotInstr/CBotExpression.cpp + CBotInstr/CBotIndexExpr.cpp ) # Includes From 4b10358df767aa28a49a4b17b84da4ac98694604 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 22:02:17 +0100 Subject: [PATCH 34/91] Moving CBotFieldExpr class in its own header and source files. --- src/CBot/CBot.cpp | 103 +--------------------- src/CBot/CBot.h | 21 ----- src/CBot/CBotInstr/CBotExprVar.cpp | 1 + src/CBot/CBotInstr/CBotFieldExpr.cpp | 124 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotFieldExpr.h | 83 ++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 210 insertions(+), 123 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotFieldExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotFieldExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index d0a1239e..7511afa4 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -60,6 +60,7 @@ #include "CBotInstr/CBotTwoOpExpr.h" #include "CBotInstr/CBotExpression.h" #include "CBotInstr/CBotIndexExpr.h" +#include "CBotInstr/CBotFieldExpr.h" // Local include @@ -1378,108 +1379,6 @@ CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) return nullptr; } -////////////////////////////////////////////////////////////////////////////////////// -// field management in an instance (dot operator) -// toto.x - - -CBotFieldExpr::CBotFieldExpr() -{ - name = "CBotFieldExpr"; - m_nIdent = 0; -} - -CBotFieldExpr::~CBotFieldExpr() -{ -} - -void CBotFieldExpr::SetUniqNum(int num) -{ - m_nIdent = num; -} - - -// find a field from the instance at compile - -bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - if (pVar->GetType(1) != CBotTypPointer) - assert(0); - - pVar = pVar->GetItemRef(m_nIdent); - if (pVar == nullptr) - { - pile->SetError(TX_NOITEM, &m_token); - return false; - } - - if ( m_next3 != nullptr && - !m_next3->ExecuteVar(pVar, pile) ) return false; - - return true; -} - -bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) -{ - CBotStack* pj = pile; - pile = pile->AddStack(this); // changes in output stack - if (pile == EOX) return true; - - - if (pVar->GetType(1) != CBotTypPointer) - assert(0); - - CBotVarClass* pItem = pVar->GetPointer(); - if (pItem == nullptr) - { - pile->SetError(TX_NULLPT, prevToken); - return pj->Return(pile); - } - if (pItem->GetUserPtr() == OBJECTDELETED) - { - pile->SetError(TX_DELETEDPT, prevToken); - return pj->Return(pile); - } - - if (bStep && pile->IfStep()) return false; - - pVar = pVar->GetItemRef(m_nIdent); - if (pVar == nullptr) - { - pile->SetError(TX_NOITEM, &m_token); - return pj->Return(pile); - } - - if (pVar->IsStatic()) - { - // for a static variable, takes it in the class itself - CBotClass* pClass = pItem->GetClass(); - pVar = pClass->GetItem(m_token.GetString()); - } - - // request the update of the element, if applicable - pVar->Maj(pile->GetPUser(), true); - - if ( m_next3 != nullptr && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; - - // does not release the stack - // to maintain the state SetState () corresponding to step - - return true; -} - -void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) -{ - pj = pj->RestoreStack(this); - if (pj == nullptr) return; - - if (m_next3 != nullptr) - m_next3->RestoreStateVar(pj, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // compile a left operand for an assignment CBotLeftExpr::CBotLeftExpr() diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 995e5c6d..a0beb27a 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -670,27 +670,6 @@ public: }; -// management of the fields of an instance - -class CBotFieldExpr : public CBotInstr -{ -private: - friend class CBotExpression; - int m_nIdent; - -public: - CBotFieldExpr(); - ~CBotFieldExpr(); - void SetUniqNum(int num); -// static -// CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) override; - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) override; - void RestoreStateVar(CBotStack* &pj, bool bMain) override; -}; - - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 93ffeecf..3c03da91 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -22,6 +22,7 @@ #include "CBotInstrMethode.h" #include "CBotExpression.h" #include "CBotIndexExpr.h" +#include "CBotFieldExpr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.cpp b/src/CBot/CBotInstr/CBotFieldExpr.cpp new file mode 100644 index 00000000..6453bc3c --- /dev/null +++ b/src/CBot/CBotInstr/CBotFieldExpr.cpp @@ -0,0 +1,124 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotFieldExpr.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotFieldExpr::CBotFieldExpr() +{ + name = "CBotFieldExpr"; + m_nIdent = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotFieldExpr::~CBotFieldExpr() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotFieldExpr::SetUniqNum(int num) +{ + m_nIdent = num; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + if (pVar->GetType(1) != CBotTypPointer) + assert(0); + + pVar = pVar->GetItemRef(m_nIdent); + if (pVar == nullptr) + { + pile->SetError(TX_NOITEM, &m_token); + return false; + } + + if ( m_next3 != nullptr && + !m_next3->ExecuteVar(pVar, pile) ) return false; + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) +{ + CBotStack* pj = pile; + pile = pile->AddStack(this); // changes in output stack + if (pile == EOX) return true; + + + if (pVar->GetType(1) != CBotTypPointer) + assert(0); + + CBotVarClass* pItem = pVar->GetPointer(); + if (pItem == nullptr) + { + pile->SetError(TX_NULLPT, prevToken); + return pj->Return(pile); + } + if (pItem->GetUserPtr() == OBJECTDELETED) + { + pile->SetError(TX_DELETEDPT, prevToken); + return pj->Return(pile); + } + + if (bStep && pile->IfStep()) return false; + + pVar = pVar->GetItemRef(m_nIdent); + if (pVar == nullptr) + { + pile->SetError(TX_NOITEM, &m_token); + return pj->Return(pile); + } + + if (pVar->IsStatic()) + { + // for a static variable, takes it in the class itself + CBotClass* pClass = pItem->GetClass(); + pVar = pClass->GetItem(m_token.GetString()); + } + + // request the update of the element, if applicable + pVar->Maj(pile->GetPUser(), true); + + if ( m_next3 != nullptr && + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, bExtend) ) return false; + + // does not release the stack + // to maintain the state SetState () corresponding to step + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotFieldExpr::RestoreStateVar(CBotStack* &pj, bool bMain) +{ + pj = pj->RestoreStack(this); + if (pj == nullptr) return; + + if (m_next3 != nullptr) + m_next3->RestoreStateVar(pj, bMain); +} diff --git a/src/CBot/CBotInstr/CBotFieldExpr.h b/src/CBot/CBotInstr/CBotFieldExpr.h new file mode 100644 index 00000000..13f9fc37 --- /dev/null +++ b/src/CBot/CBotInstr/CBotFieldExpr.h @@ -0,0 +1,83 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotFieldExpr class Management of the fields of an instance + * (dot operator) eg : toto.x + */ +class CBotFieldExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotFieldExpr + */ + CBotFieldExpr(); + + /*! + * \brief ~CBotFieldExpr + */ + ~CBotFieldExpr(); + + /*! + * \brief SetUniqNum + * \param num + */ + void SetUniqNum(int num); + + /*! + * \brief ExecuteVar Find a field from the instance at compile. + * \param pVar + * \param pile + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) override; + + /*! + * \brief ExecuteVar + * \param pVar + * \param pile + * \param prevToken + * \param bStep + * \param bExtend + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) override; + + /*! + * \brief RestoreStateVar + * \param pj + * \param bMain + */ + void RestoreStateVar(CBotStack* &pj, bool bMain) override; + +private: + friend class CBotExpression; + int m_nIdent; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index bb4d3256..29380c32 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -39,6 +39,7 @@ set(SOURCES CBotInstr/CBotTwoOpExpr.cpp CBotInstr/CBotExpression.cpp CBotInstr/CBotIndexExpr.cpp + CBotInstr/CBotFieldExpr.cpp ) # Includes From d89fd629a265c3c3637e16600fee4cd23bde27f3 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 22:16:38 +0100 Subject: [PATCH 35/91] Moving CBotLeftExpr class in its own header and source files. --- src/CBot/CBot.cpp | 223 ------------------------- src/CBot/CBot.h | 22 --- src/CBot/CBotInstr/CBotExpression.h | 1 + src/CBot/CBotInstr/CBotLeftExpr.cpp | 242 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotLeftExpr.h | 101 ++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 345 insertions(+), 245 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotLeftExpr.cpp create mode 100644 src/CBot/CBotInstr/CBotLeftExpr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 7511afa4..c4ac2874 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -1379,229 +1379,6 @@ CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) return nullptr; } -////////////////////////////////////////////////////////////////////////////////////// -// compile a left operand for an assignment -CBotLeftExpr::CBotLeftExpr() -{ - name = "CBotLeftExpr"; - m_nIdent = 0; -} - -CBotLeftExpr::~CBotLeftExpr() -{ -} - -// compiles an expression for a left-operand (left of an assignment) -// this can be -// toto -// toto[ 3 ] -// toto.x -// toto.pos.x -// toto[2].pos.x -// toto[1].pos[2].x -// toto[1][2][3] - -CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotCStack* pStk = pStack->TokenStack(); - - pStk->SetStartError(p->GetStart()); - - // is it a variable name? - if (p->GetType() == TokenTypVar) - { - CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object - - inst->SetToken(p); - - CBotVar* var; - - if (nullptr != (var = pStk->FindVar(p))) // seek if known variable - { - inst->m_nIdent = var->GetUniqNum(); - if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) - { - if ( var->IsPrivate(PR_READ) && - !pStk->GetBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, p); - goto err; - } - // this is an element of the current class - // adds the equivalent of this. before - CBotToken pthis("this"); - inst->SetToken(&pthis); - inst->m_nIdent = -2; // indent for this - - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(p); // keeps the name of the token - inst->AddNext3(i); // add after - - var = pStk->FindVar(pthis); - var = var->GetItem(p->GetString()); - i->SetUniqNum(var->GetUniqNum()); - } - p = p->GetNext(); // next token - - while (true) - { - if (var->GetType() == CBotTypArrayPointer) - { - if (IsOfType( p, ID_OPBRK )) - { - CBotIndexExpr* i = new CBotIndexExpr(); - i->m_expr = CBotExpression::Compile(p, pStk); - inst->AddNext3(i); // add to the chain - - var = (static_cast(var))->GetItem(0,true); // gets the component [0] - - if (i->m_expr == nullptr) - { - pStk->SetError(TX_BADINDEX, p->GetStart()); - goto err; - } - - if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto err; - } - continue; - } - } - - if (var->GetType(1) == CBotTypPointer) // for classes - { - if (IsOfType(p, ID_DOT)) - { - CBotToken* pp = p; - - CBotFieldExpr* i = new CBotFieldExpr(); // new element - i->SetToken(pp); // keeps the name of the token - inst->AddNext3(i); // adds after - - if (p->GetType() == TokenTypVar) // must be a name - { - var = var->GetItem(p->GetString()); // get item correspondent - if (var != nullptr) - { - if ( var->IsPrivate(PR_READ) && - !pStk->GetBotCall()->m_bCompileClass) - { - pStk->SetError(TX_PRIVATE, pp); - goto err; - } - - i->SetUniqNum(var->GetUniqNum()); - p = p->GetNext(); // skips the name - continue; - } - pStk->SetError(TX_NOITEM, p); - } - pStk->SetError(TX_DOT, p->GetStart()); - goto err; - } - } - break; - } - - - if (pStk->IsOk()) return static_cast (pStack->Return(inst, pStk)); - } - pStk->SetError(TX_UNDEFVAR, p); -err: - delete inst; - return static_cast ( pStack->Return(nullptr, pStk)); - } - - return static_cast ( pStack->Return(nullptr, pStk)); -} - -// runs, is a variable and assigns the result to the stack -bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) -{ - CBotStack* pile = pj->AddStack(); - - CBotVar* var1 = nullptr; - CBotVar* var2 = nullptr; - // fetch a variable (not copy) - if (!ExecuteVar(var1, array, nullptr, false)) return false; - - if (pile->IfStep()) return false; - - if (var1) - { - var2 = pj->GetVar(); // result on the input stack - if (var2) - { - CBotTypResult t1 = var1->GetTypResult(); - CBotTypResult t2 = var2->GetTypResult(); - if (t2.Eq(CBotTypPointer)) - { - CBotClass* c1 = t1.GetClass(); - CBotClass* c2 = t2.GetClass(); - if ( !c2->IsChildOf(c1)) - { - CBotToken* pt = &m_token; - pile->SetError(TX_BADTYPE, pt); - return pj->Return(pile); // operation performed - } - } - var1->SetVal(var2); // do assignment - } - pile->SetCopyVar(var1); // replace the stack with the copy of the variable - // (for name) - } - - return pj->Return(pile); // operation performed -} - -// fetch a variable during compilation - -bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - pVar = pile->FindVar(m_token); - if (pVar == nullptr) return false; - - if ( m_next3 != nullptr && - !m_next3->ExecuteVar(pVar, pile) ) return false; - - return true; -} - -// fetch the variable at runtume - -bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) -{ - pile = pile->AddStack(this); - - pVar = pile->FindVar(m_nIdent); - if (pVar == nullptr) - { -#ifdef _DEBUG - assert(0); -#endif - pile->SetError(2, &m_token); - return false; - } - - if (bStep && m_next3 == nullptr && pile->IfStep()) return false; - - if ( m_next3 != nullptr && - !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; - - return true; -} - -void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - pile = pile->RestoreStack(this); - if (pile == nullptr) return; - - if (m_next3 != nullptr) - m_next3->RestoreStateVar(pile, bMain); -} - ////////////////////////////////////////////////////////////////////////////////////////// // compile a list of parameters diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index a0beb27a..481335ec 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -648,28 +648,6 @@ public: CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); }; - -// left operand -// accept the expressions that be to the left of assignment - -class CBotLeftExpr : public CBotInstr -{ -private: - long m_nIdent; - -public: - CBotLeftExpr(); - ~CBotLeftExpr(); - static - CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pStack, CBotStack* array); - - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) override; - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); - void RestoreStateVar(CBotStack* &pile, bool bMain) override; -}; - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotExpression.h b/src/CBot/CBotInstr/CBotExpression.h index b47e9e24..d25fd39c 100644 --- a/src/CBot/CBotInstr/CBotExpression.h +++ b/src/CBot/CBotInstr/CBotExpression.h @@ -21,6 +21,7 @@ // Modules inlcude #include "CBot.h" +#include "CBotLeftExpr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp new file mode 100644 index 00000000..66e0dce5 --- /dev/null +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -0,0 +1,242 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotLeftExpr.h" +#include "CBotFieldExpr.h" +#include "CBotIndexExpr.h" +#include "CBotExpression.h" + +// Local include + +// Global include + + +////////////////////////////////////////////////////////////////////////////////////// +CBotLeftExpr::CBotLeftExpr() +{ + name = "CBotLeftExpr"; + m_nIdent = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotLeftExpr::~CBotLeftExpr() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotCStack* pStk = pStack->TokenStack(); + + pStk->SetStartError(p->GetStart()); + + // is it a variable name? + if (p->GetType() == TokenTypVar) + { + CBotLeftExpr* inst = new CBotLeftExpr(); // creates the object + + inst->SetToken(p); + + CBotVar* var; + + if (nullptr != (var = pStk->FindVar(p))) // seek if known variable + { + inst->m_nIdent = var->GetUniqNum(); + if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) + { + if ( var->IsPrivate(PR_READ) && + !pStk->GetBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, p); + goto err; + } + // this is an element of the current class + // adds the equivalent of this. before + CBotToken pthis("this"); + inst->SetToken(&pthis); + inst->m_nIdent = -2; // indent for this + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(p); // keeps the name of the token + inst->AddNext3(i); // add after + + var = pStk->FindVar(pthis); + var = var->GetItem(p->GetString()); + i->SetUniqNum(var->GetUniqNum()); + } + p = p->GetNext(); // next token + + while (true) + { + if (var->GetType() == CBotTypArrayPointer) + { + if (IsOfType( p, ID_OPBRK )) + { + CBotIndexExpr* i = new CBotIndexExpr(); + i->m_expr = CBotExpression::Compile(p, pStk); + inst->AddNext3(i); // add to the chain + + var = (static_cast(var))->GetItem(0,true); // gets the component [0] + + if (i->m_expr == nullptr) + { + pStk->SetError(TX_BADINDEX, p->GetStart()); + goto err; + } + + if (!pStk->IsOk() || !IsOfType( p, ID_CLBRK )) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto err; + } + continue; + } + } + + if (var->GetType(1) == CBotTypPointer) // for classes + { + if (IsOfType(p, ID_DOT)) + { + CBotToken* pp = p; + + CBotFieldExpr* i = new CBotFieldExpr(); // new element + i->SetToken(pp); // keeps the name of the token + inst->AddNext3(i); // adds after + + if (p->GetType() == TokenTypVar) // must be a name + { + var = var->GetItem(p->GetString()); // get item correspondent + if (var != nullptr) + { + if ( var->IsPrivate(PR_READ) && + !pStk->GetBotCall()->m_bCompileClass) + { + pStk->SetError(TX_PRIVATE, pp); + goto err; + } + + i->SetUniqNum(var->GetUniqNum()); + p = p->GetNext(); // skips the name + continue; + } + pStk->SetError(TX_NOITEM, p); + } + pStk->SetError(TX_DOT, p->GetStart()); + goto err; + } + } + break; + } + + + if (pStk->IsOk()) return static_cast (pStack->Return(inst, pStk)); + } + pStk->SetError(TX_UNDEFVAR, p); +err: + delete inst; + return static_cast ( pStack->Return(nullptr, pStk)); + } + + return static_cast ( pStack->Return(nullptr, pStk)); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotLeftExpr::Execute(CBotStack* &pj, CBotStack* array) +{ + CBotStack* pile = pj->AddStack(); + + CBotVar* var1 = nullptr; + CBotVar* var2 = nullptr; + // fetch a variable (not copy) + if (!ExecuteVar(var1, array, nullptr, false)) return false; + + if (pile->IfStep()) return false; + + if (var1) + { + var2 = pj->GetVar(); // result on the input stack + if (var2) + { + CBotTypResult t1 = var1->GetTypResult(); + CBotTypResult t2 = var2->GetTypResult(); + if (t2.Eq(CBotTypPointer)) + { + CBotClass* c1 = t1.GetClass(); + CBotClass* c2 = t2.GetClass(); + if ( !c2->IsChildOf(c1)) + { + CBotToken* pt = &m_token; + pile->SetError(TX_BADTYPE, pt); + return pj->Return(pile); // operation performed + } + } + var1->SetVal(var2); // do assignment + } + pile->SetCopyVar(var1); // replace the stack with the copy of the variable + // (for name) + } + + return pj->Return(pile); // operation performed +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + pVar = pile->FindVar(m_token); + if (pVar == nullptr) return false; + + if ( m_next3 != nullptr && + !m_next3->ExecuteVar(pVar, pile) ) return false; + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotLeftExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep) +{ + pile = pile->AddStack(this); + + pVar = pile->FindVar(m_nIdent); + if (pVar == nullptr) + { +#ifdef _DEBUG + assert(0); +#endif + pile->SetError(2, &m_token); + return false; + } + + if (bStep && m_next3 == nullptr && pile->IfStep()) return false; + + if ( m_next3 != nullptr && + !m_next3->ExecuteVar(pVar, pile, &m_token, bStep, true) ) return false; + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotLeftExpr::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + pile = pile->RestoreStack(this); + if (pile == nullptr) return; + + if (m_next3 != nullptr) + m_next3->RestoreStateVar(pile, bMain); +} diff --git a/src/CBot/CBotInstr/CBotLeftExpr.h b/src/CBot/CBotInstr/CBotLeftExpr.h new file mode 100644 index 00000000..f0dba0b4 --- /dev/null +++ b/src/CBot/CBotInstr/CBotLeftExpr.h @@ -0,0 +1,101 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotLeftExpr class Accept the expressions that be to the left of + * assignment. + */ +class CBotLeftExpr : public CBotInstr +{ +public: + + /*! + * \brief CBotLeftExpr + */ + CBotLeftExpr(); + + /*! + * \brief ~CBotLeftExpr + */ + ~CBotLeftExpr(); + + /*! + * \brief Compile Compiles an expression for a left-operand + * (left of an assignment). + * eg : + * - toto + * - toto[ 3 ] + * - toto.x + * - toto.pos.x + * - toto[2].pos.x + * - toto[1].pos[2].x + * - toto[1][2][3] + * + * \param p + * \param pStack + * \return + */ + static CBotLeftExpr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Runs, is a variable and assigns the result to the stack. + * \param pStack + * \param array + * \return + */ + bool Execute(CBotStack* &pStack, CBotStack* array); + + /*! + * \brief ExecuteVar Fetch a variable during compilation. + * \param pVar + * \param pile + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) override; + + /*! + * \brief ExecuteVar Fetch the variable at runtume. + * \param pVar + * \param pile + * \param prevToken + * \param bStep + * \return + */ + bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep); + + /*! + * \brief RestoreStateVar + * \param pile + * \param bMain + */ + void RestoreStateVar(CBotStack* &pile, bool bMain) override; + +private: + long m_nIdent; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 29380c32..028c4d8f 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -40,6 +40,7 @@ set(SOURCES CBotInstr/CBotExpression.cpp CBotInstr/CBotIndexExpr.cpp CBotInstr/CBotFieldExpr.cpp + CBotInstr/CBotLeftExpr.cpp ) # Includes From dc8a6b32739c47ccb51571f7c587a21cdb2137b8 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 22:24:37 +0100 Subject: [PATCH 36/91] Moving CBotCondition class in its own header and source files. --- src/CBot/CBot.cpp | 31 ----------------- src/CBot/CBot.h | 10 ------ src/CBot/CBotIf.cpp | 1 + src/CBot/CBotInstr/CBotCondition.cpp | 50 ++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotCondition.h | 46 +++++++++++++++++++++++++ src/CBot/CBotInstr/CBotDo.cpp | 1 + src/CBot/CBotInstr/CBotWhile.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 8 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotCondition.cpp create mode 100644 src/CBot/CBotInstr/CBotCondition.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index c4ac2874..7ad12b3f 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -1348,37 +1348,6 @@ void CBotIString::RestoreState(CBotStack* &pj, bool bMain) m_next2b->RestoreState(pile, bMain); } - -////////////////////////////////////////////////////////////////////////////////////// -// compile a statement such as "(condition)" -// the condition must be Boolean - -// this class has no constructor, because there is never an instance of this class -// the object returned by Compile is usually type CBotExpression - - -CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) -{ - pStack->SetStartError(p->GetStart()); - if (IsOfType(p, ID_OPENPAR)) - { - CBotInstr* inst = CBotBoolExpr::Compile(p, pStack); - if (nullptr != inst) - { - if (IsOfType(p, ID_CLOSEPAR)) - { - return inst; - } - pStack->SetError(TX_CLOSEPAR, p->GetStart()); // missing parenthesis - } - delete inst; - } - - pStack->SetError(TX_OPENPAR, p->GetStart()); // missing parenthesis - - return nullptr; -} - ////////////////////////////////////////////////////////////////////////////////////////// // compile a list of parameters diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 481335ec..1b038cb2 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -638,16 +638,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -class CBotCondition : public CBotInstr -{ -private: - -public: - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotIf.cpp index 781ec12c..551ecb18 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotIf.cpp @@ -23,6 +23,7 @@ #include "CBot.h" #include "CBotInstr/CBotBlock.h" +#include "CBotInstr/CBotCondition.h" // various constructors / destructors CBotIf::CBotIf() diff --git a/src/CBot/CBotInstr/CBotCondition.cpp b/src/CBot/CBotInstr/CBotCondition.cpp new file mode 100644 index 00000000..d87c451b --- /dev/null +++ b/src/CBot/CBotInstr/CBotCondition.cpp @@ -0,0 +1,50 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotCondition.h" +#include "CBotBoolExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotCondition::Compile(CBotToken* &p, CBotCStack* pStack) +{ + pStack->SetStartError(p->GetStart()); + if (IsOfType(p, ID_OPENPAR)) + { + CBotInstr* inst = CBotBoolExpr::Compile(p, pStack); + if (nullptr != inst) + { + if (IsOfType(p, ID_CLOSEPAR)) + { + return inst; + } + pStack->SetError(TX_CLOSEPAR, p->GetStart()); // missing parenthesis + } + delete inst; + } + + pStack->SetError(TX_OPENPAR, p->GetStart()); // missing parenthesis + + return nullptr; +} diff --git a/src/CBot/CBotInstr/CBotCondition.h b/src/CBot/CBotInstr/CBotCondition.h new file mode 100644 index 00000000..08996db1 --- /dev/null +++ b/src/CBot/CBotInstr/CBotCondition.h @@ -0,0 +1,46 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotCondition class This class has no constructor, because there + * is never an instance of this class the object returned by Compile is usually + * type CBotExpression + */ +class CBotCondition : public CBotInstr +{ +public: + + /*! + * \brief Compile Compile a statement such as "(condition)" the condition + * must be Boolean + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); +}; diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp index 94c281d4..3bf3aefd 100644 --- a/src/CBot/CBotInstr/CBotDo.cpp +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotDo.h" #include "CBotBlock.h" +#include "CBotCondition.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp index 340cc239..c24084b6 100644 --- a/src/CBot/CBotInstr/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotWhile.h" #include "CBotBlock.h" +#include "CBotCondition.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 028c4d8f..e94bf82b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -41,6 +41,7 @@ set(SOURCES CBotInstr/CBotIndexExpr.cpp CBotInstr/CBotFieldExpr.cpp CBotInstr/CBotLeftExpr.cpp + CBotInstr/CBotCondition.cpp ) # Includes From bbf2e48802082ff0fbd336b2f5a5eeb4200f818c Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 22:36:18 +0100 Subject: [PATCH 37/91] Moving CBotClassInst class in its own header and source files. --- src/CBot/CBot.cpp | 2 + src/CBot/CBot.h | 20 -- src/CBot/CBotClass.cpp | 411 ------------------------- src/CBot/CBotInstr/CBotClassInst.cpp | 430 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotClassInst.h | 85 ++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 518 insertions(+), 431 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotClassInst.cpp create mode 100644 src/CBot/CBotInstr/CBotClassInst.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 7ad12b3f..9e4716ec 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -61,6 +61,8 @@ #include "CBotInstr/CBotExpression.h" #include "CBotInstr/CBotIndexExpr.h" #include "CBotInstr/CBotFieldExpr.h" +#include "CBotInstr/CBotClassInst.h" + // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 1b038cb2..a045dc37 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -617,26 +617,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -// definition of an element of any class - -class CBotClassInst : public CBotInstr -{ -private: - CBotInstr* m_var; // variable to initialise - CBotClass* m_pClass; // reference to the class - CBotInstr* m_Parameters; // parameters to be evaluated for the contructor - CBotInstr* m_expr; // a value to put, if there is - bool m_hasParams; // has it parameters? - long m_nMethodeIdent; - -public: - CBotClassInst(); - ~CBotClassInst(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = nullptr); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 11f93a1f..f98eaaaa 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -463,417 +463,6 @@ bool CBotClass::RestoreStaticState(FILE* pf) return true; } - -///////////////////////////////////////////////////////////////////// - -CBotClassInst::CBotClassInst() -{ - m_next = nullptr; - m_var = nullptr; - m_Parameters = nullptr; - m_expr = nullptr; - m_hasParams = false; - m_nMethodeIdent = 0; - name = "CBotClassInst"; -} - -CBotClassInst::~CBotClassInst() -{ - delete m_var; -// delete m_next; // done by the destructor of the base class ~CBotInstr() -} - -// definition of pointer (s) to an object -// style -// CPoint A, B ; - -CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) -{ - // seeks the corresponding classes - if ( pClass == nullptr ) - { - pStack->SetStartError(p->GetStart()); - pClass = CBotClass::Find(p); - if ( pClass == nullptr ) - { - // not found? is bizare - pStack->SetError(TX_NOCLASS, p); - return nullptr; - } - p = p->GetNext(); - } - - bool bIntrinsic = pClass->IsIntrinsic(); - CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); - CBotClassInst* inst = static_cast(CompileArray(p, pStack, type)); - if ( inst != nullptr || !pStack->IsOk() ) return inst; - - CBotCStack* pStk = pStack->TokenStack(); - - inst = new CBotClassInst(); - /// TODO Need to be revised and fixed after adding unit tests - CBotToken token(pClass->GetName(), CBotString(), p->GetStart(), p->GetEnd()); - inst->SetToken(&token); - CBotToken* vartoken = p; - - if ( nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) - { - (static_cast(inst->m_var))->m_typevar = type; - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable - { - pStk->SetStartError(vartoken->GetStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GetEnd()); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) // with any clues? - { - delete inst; // is not type CBotInt - p = vartoken; // returns to the variable name - - // compiles declaration an array - - inst = static_cast(CBotInstArray::Compile( p, pStk, type )); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto error; - } - goto suite; // no assignment, variable already created - } - - - CBotVar* var; - var = CBotVar::Create(vartoken->GetString(), type); // creates the instance -// var->SetClass(pClass); - var->SetUniqNum( - (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); - // its attribute a unique number - pStack->AddVar(var); // placed on the stack - - // look if there are parameters - inst->m_hasParams = (p->GetType() == ID_OPENPAR); - - CBotVar* ppVars[1000]; - inst->m_Parameters = CompileParams(p, pStk, ppVars); - if ( !pStk->IsOk() ) goto error; - - // if there are parameters, is the equivalent to the stament "new" - // CPoint A ( 0, 0 ) is equivalent to - // CPoint A = new CPoint( 0, 0 ) - -// if ( nullptr != inst->m_Parameters ) - if ( inst->m_hasParams ) - { - // the constructor is there? -// CBotString noname; - CBotTypResult r = pClass->CompileMethode(pClass->GetName(), var, ppVars, pStk, inst->m_nMethodeIdent); - delete pStk->TokenStack(); // releases the supplement stack - int typ = r.GetType(); - - if (typ == TX_UNDEFCALL) - { - // si le constructeur n'existe pas - if (inst->m_Parameters != nullptr) // with parameters - { - pStk->SetError(TX_NOCONST, vartoken); - goto error; - } - typ = 0; - } - - if (typ>20) - { - pStk->SetError(typ, vartoken->GetEnd()); - goto error; - } - - } - - if (IsOfType(p, ID_ASS)) // with a assignment? - { - if (inst->m_hasParams) - { - pStk->SetError(TX_ENDOF, p->GetStart()); - goto error; - } - - if ( nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) - { - goto error; - } - CBotClass* result = pStk->GetClass(); - if ( !pStk->GetTypResult(1).Eq(CBotTypNullPointer) && - ( !pStk->GetTypResult(1).Eq(CBotTypPointer) || - ( result != nullptr && !pClass->IsChildOf(result) ))) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - } -// if ( !bIntrinsic ) var->SetPointer(pStk->GetVar()->GetPointer()); - if ( !bIntrinsic ) - { - // does not use the result on the stack, to impose the class - CBotVar* pvar = CBotVar::Create("", pClass); - var->SetPointer( pvar ); // variable already declared instance pointer - delete pvar; // removes the second pointer - } - var->SetInit(CBotVar::InitType::DEF); // marks the pointer as init - } - else if (inst->m_hasParams) - { - // creates the object on the "job" (\TODO "tas") - // with a pointer to the object - if ( !bIntrinsic ) - { - CBotVar* pvar = CBotVar::Create("", pClass); - var->SetPointer( pvar ); // variable already declared instance pointer - delete pvar; // removes the second pointer - } - var->SetInit(CBotVar::InitType::IS_POINTER); // marks the pointer as init - } -suite: - if (IsOfType(p, ID_COMMA)) // several chained definitions - { - if ( nullptr != ( inst->m_next = CBotClassInst::Compile(p, pStk, pClass) )) // compiles the following - { - return pStack->Return(inst, pStk); - } - } - - if (IsOfType(p, ID_SEP)) // complete instruction - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GetStart()); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - -// declaration of the instance of a class, for example: -// CPoint A, B; - -bool CBotClassInst::Execute(CBotStack* &pj) -{ - CBotVar* pThis = nullptr; - - CBotStack* pile = pj->AddStack(this);//essential for SetState() -// if ( pile == EOX ) return true; - - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - - bool bIntrincic = pClass->IsIntrinsic(); - - // creates the variable of type pointer to the object - - if ( pile->GetState()==0) - { - CBotString name = m_var->m_token.GetString(); - if ( bIntrincic ) - { - pThis = CBotVar::Create(name, CBotTypResult( CBotTypIntrinsic, pClass )); - } - else - { - pThis = CBotVar::Create(name, CBotTypResult( CBotTypPointer, pClass )); - } - - pThis->SetUniqNum((static_cast(m_var))->m_nIdent); // its attribute as unique number - pile->AddVar(pThis); // place on the stack - pile->IncState(); - } - - if ( pThis == nullptr ) pThis = pile->FindVar((static_cast(m_var))->m_nIdent); - - if ( pile->GetState()<3) - { - // ss there an assignment or parameters (contructor) - -// CBotVarClass* pInstance = nullptr; - - if ( m_expr != nullptr ) - { - // evaluates the expression for the assignment - if (!m_expr->Execute(pile)) return false; - - if ( bIntrincic ) - { - CBotVar* pv = pile->GetVar(); - if ( pv == nullptr || pv->GetPointer() == nullptr ) - { - pile->SetError(TX_NULLPT, &m_token); - return pj->Return(pile); - } - pThis->Copy(pile->GetVar(), false); - } - else - { - CBotVarClass* pInstance; - pInstance = (static_cast(pile->GetVar()))->GetPointer(); // value for the assignment - pThis->SetPointer(pInstance); - } - pThis->SetInit(CBotVar::InitType::DEF); - } - - else if ( m_hasParams ) - { - // evaluates the constructor of an instance - - if ( !bIntrincic && pile->GetState() == 1) - { - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - - // creates an instance of the requested class - - CBotVarClass* pInstance; - pInstance = static_cast(CBotVar::Create("", pClass)); - pThis->SetPointer(pInstance); - delete pInstance; - - pile->IncState(); - } - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluates the parameters - // and places the values ​​on the stack - // to (can) be interrupted (broken) at any time - - if ( p != nullptr) while ( true ) - { - pile2 = pile2->AddStack(); // place on the stack for the results - if ( pile2->GetState() == 0 ) - { - if (!p->Execute(pile2)) return false; // interrupted here? - pile2->SetState(1); - } - ppVars[i++] = pile2->GetVar(); - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - // creates a variable for the result - CBotVar* pResult = nullptr; // constructor still void - - if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GetName(), - pThis, ppVars, - pResult, pile2, GetToken())) return false; // interrupt - - pThis->SetInit(CBotVar::InitType::DEF); - pThis->ConstructorSet(); // indicates that the constructor has been called - pile->Return(pile2); // releases a piece of stack - -// pInstance = pThis->GetPointer(); - - } - -// if ( !bIntrincic ) pThis->SetPointer(pInstance); // a pointer to the instance - - pile->SetState(3); // finished this part - } - - if ( pile->IfStep() ) return false; - - if ( m_next2b != nullptr && - !m_next2b->Execute(pile)) return false; // other (s) definition (s) - - return pj->Return( pile ); // transmits below (further) -} - - - -void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotVar* pThis = nullptr; - - CBotStack* pile = pj; - if ( bMain ) pile = pj->RestoreStack(this); - if ( pile == nullptr ) return; - - // creates the variable of type pointer to the object - { - CBotString name = m_var->m_token.GetString(); - pThis = pile->FindVar(name); - pThis->SetUniqNum((static_cast(m_var))->m_nIdent); // its attribute a unique number - } - - CBotToken* pt = &m_token; - CBotClass* pClass = CBotClass::Find(pt); - bool bIntrincic = pClass->IsIntrinsic(); - - if ( bMain && pile->GetState()<3) - { - // is there an assignment or parameters (constructor) - -// CBotVarClass* pInstance = nullptr; - - if ( m_expr != nullptr ) - { - // evaluates the expression for the assignment - m_expr->RestoreState(pile, bMain); - return; - } - - else if ( m_hasParams ) - { - // evaluates the constructor of an instance - - if ( !bIntrincic && pile->GetState() == 1) - { - return; - } - - CBotVar* ppVars[1000]; - CBotStack* pile2 = pile; - - int i = 0; - - CBotInstr* p = m_Parameters; - // evaluates the parameters - // and the values an the stack - // for the ability to be interrupted at any time (\TODO pour pouvoir être interrompu n'importe quand) - - if ( p != nullptr) while ( true ) - { - pile2 = pile2->RestoreStack(); // place on the stack for the results - if ( pile2 == nullptr ) return; - - if ( pile2->GetState() == 0 ) - { - p->RestoreState(pile2, bMain); // interrupted here? - return; - } - ppVars[i++] = pile2->GetVar(); - p = p->GetNext(); - if ( p == nullptr) break; - } - ppVars[i] = nullptr; - - // creates a variable for the result -// CBotVar* pResult = nullptr; // constructor still void - - pClass->RestoreMethode(m_nMethodeIdent, pClass->GetName(), pThis, ppVars, pile2); - return; - } - } - - if ( m_next2b != nullptr ) - m_next2b->RestoreState(pile, bMain); // other(s) definition(s) -} - - // test if a procedure name is already defined somewhere bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp new file mode 100644 index 00000000..cf835df3 --- /dev/null +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -0,0 +1,430 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotClassInst.h" +#include "CBotLeftExprVar.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotClassInst::CBotClassInst() +{ + m_next = nullptr; + m_var = nullptr; + m_Parameters = nullptr; + m_expr = nullptr; + m_hasParams = false; + m_nMethodeIdent = 0; + name = "CBotClassInst"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotClassInst::~CBotClassInst() +{ + delete m_var; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotClassInst::Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) +{ + // seeks the corresponding classes + if ( pClass == nullptr ) + { + pStack->SetStartError(p->GetStart()); + pClass = CBotClass::Find(p); + if ( pClass == nullptr ) + { + // not found? is bizare + pStack->SetError(TX_NOCLASS, p); + return nullptr; + } + p = p->GetNext(); + } + + bool bIntrinsic = pClass->IsIntrinsic(); + CBotTypResult type = CBotTypResult( bIntrinsic ? CBotTypIntrinsic : CBotTypPointer, pClass ); + CBotClassInst* inst = static_cast(CompileArray(p, pStack, type)); + if ( inst != nullptr || !pStack->IsOk() ) return inst; + + CBotCStack* pStk = pStack->TokenStack(); + + inst = new CBotClassInst(); + /// TODO Need to be revised and fixed after adding unit tests + CBotToken token(pClass->GetName(), CBotString(), p->GetStart(), p->GetEnd()); + inst->SetToken(&token); + CBotToken* vartoken = p; + + if ( nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk )) ) + { + (static_cast(inst->m_var))->m_typevar = type; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetStartError(vartoken->GetStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GetEnd()); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) // with any clues? + { + delete inst; // is not type CBotInt + p = vartoken; // returns to the variable name + + // compiles declaration an array + + inst = static_cast(CBotInstArray::Compile( p, pStk, type )); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + + CBotVar* var; + var = CBotVar::Create(vartoken->GetString(), type); // creates the instance +// var->SetClass(pClass); + var->SetUniqNum( + (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); + // its attribute a unique number + pStack->AddVar(var); // placed on the stack + + // look if there are parameters + inst->m_hasParams = (p->GetType() == ID_OPENPAR); + + CBotVar* ppVars[1000]; + inst->m_Parameters = CompileParams(p, pStk, ppVars); + if ( !pStk->IsOk() ) goto error; + + // if there are parameters, is the equivalent to the stament "new" + // CPoint A ( 0, 0 ) is equivalent to + // CPoint A = new CPoint( 0, 0 ) + +// if ( nullptr != inst->m_Parameters ) + if ( inst->m_hasParams ) + { + // the constructor is there? +// CBotString noname; + CBotTypResult r = pClass->CompileMethode(pClass->GetName(), var, ppVars, pStk, inst->m_nMethodeIdent); + delete pStk->TokenStack(); // releases the supplement stack + int typ = r.GetType(); + + if (typ == TX_UNDEFCALL) + { + // si le constructeur n'existe pas + if (inst->m_Parameters != nullptr) // with parameters + { + pStk->SetError(TX_NOCONST, vartoken); + goto error; + } + typ = 0; + } + + if (typ>20) + { + pStk->SetError(typ, vartoken->GetEnd()); + goto error; + } + + } + + if (IsOfType(p, ID_ASS)) // with a assignment? + { + if (inst->m_hasParams) + { + pStk->SetError(TX_ENDOF, p->GetStart()); + goto error; + } + + if ( nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk )) ) + { + goto error; + } + CBotClass* result = pStk->GetClass(); + if ( !pStk->GetTypResult(1).Eq(CBotTypNullPointer) && + ( !pStk->GetTypResult(1).Eq(CBotTypPointer) || + ( result != nullptr && !pClass->IsChildOf(result) ))) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + } +// if ( !bIntrinsic ) var->SetPointer(pStk->GetVar()->GetPointer()); + if ( !bIntrinsic ) + { + // does not use the result on the stack, to impose the class + CBotVar* pvar = CBotVar::Create("", pClass); + var->SetPointer( pvar ); // variable already declared instance pointer + delete pvar; // removes the second pointer + } + var->SetInit(CBotVar::InitType::DEF); // marks the pointer as init + } + else if (inst->m_hasParams) + { + // creates the object on the "job" (\TODO "tas") + // with a pointer to the object + if ( !bIntrinsic ) + { + CBotVar* pvar = CBotVar::Create("", pClass); + var->SetPointer( pvar ); // variable already declared instance pointer + delete pvar; // removes the second pointer + } + var->SetInit(CBotVar::InitType::IS_POINTER); // marks the pointer as init + } +suite: + if (IsOfType(p, ID_COMMA)) // several chained definitions + { + if ( nullptr != ( inst->m_next = CBotClassInst::Compile(p, pStk, pClass) )) // compiles the following + { + return pStack->Return(inst, pStk); + } + } + + if (IsOfType(p, ID_SEP)) // complete instruction + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GetStart()); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotClassInst::Execute(CBotStack* &pj) +{ + CBotVar* pThis = nullptr; + + CBotStack* pile = pj->AddStack(this);//essential for SetState() +// if ( pile == EOX ) return true; + + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + + bool bIntrincic = pClass->IsIntrinsic(); + + // creates the variable of type pointer to the object + + if ( pile->GetState()==0) + { + CBotString name = m_var->m_token.GetString(); + if ( bIntrincic ) + { + pThis = CBotVar::Create(name, CBotTypResult( CBotTypIntrinsic, pClass )); + } + else + { + pThis = CBotVar::Create(name, CBotTypResult( CBotTypPointer, pClass )); + } + + pThis->SetUniqNum((static_cast(m_var))->m_nIdent); // its attribute as unique number + pile->AddVar(pThis); // place on the stack + pile->IncState(); + } + + if ( pThis == nullptr ) pThis = pile->FindVar((static_cast(m_var))->m_nIdent); + + if ( pile->GetState()<3) + { + // ss there an assignment or parameters (contructor) + +// CBotVarClass* pInstance = nullptr; + + if ( m_expr != nullptr ) + { + // evaluates the expression for the assignment + if (!m_expr->Execute(pile)) return false; + + if ( bIntrincic ) + { + CBotVar* pv = pile->GetVar(); + if ( pv == nullptr || pv->GetPointer() == nullptr ) + { + pile->SetError(TX_NULLPT, &m_token); + return pj->Return(pile); + } + pThis->Copy(pile->GetVar(), false); + } + else + { + CBotVarClass* pInstance; + pInstance = (static_cast(pile->GetVar()))->GetPointer(); // value for the assignment + pThis->SetPointer(pInstance); + } + pThis->SetInit(CBotVar::InitType::DEF); + } + + else if ( m_hasParams ) + { + // evaluates the constructor of an instance + + if ( !bIntrincic && pile->GetState() == 1) + { + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + + // creates an instance of the requested class + + CBotVarClass* pInstance; + pInstance = static_cast(CBotVar::Create("", pClass)); + pThis->SetPointer(pInstance); + delete pInstance; + + pile->IncState(); + } + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates the parameters + // and places the values ​​on the stack + // to (can) be interrupted (broken) at any time + + if ( p != nullptr) while ( true ) + { + pile2 = pile2->AddStack(); // place on the stack for the results + if ( pile2->GetState() == 0 ) + { + if (!p->Execute(pile2)) return false; // interrupted here? + pile2->SetState(1); + } + ppVars[i++] = pile2->GetVar(); + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + // creates a variable for the result + CBotVar* pResult = nullptr; // constructor still void + + if ( !pClass->ExecuteMethode(m_nMethodeIdent, pClass->GetName(), + pThis, ppVars, + pResult, pile2, GetToken())) return false; // interrupt + + pThis->SetInit(CBotVar::InitType::DEF); + pThis->ConstructorSet(); // indicates that the constructor has been called + pile->Return(pile2); // releases a piece of stack + +// pInstance = pThis->GetPointer(); + + } + +// if ( !bIntrincic ) pThis->SetPointer(pInstance); // a pointer to the instance + + pile->SetState(3); // finished this part + } + + if ( pile->IfStep() ) return false; + + if ( m_next2b != nullptr && + !m_next2b->Execute(pile)) return false; // other (s) definition (s) + + return pj->Return( pile ); // transmits below (further) +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotClassInst::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotVar* pThis = nullptr; + + CBotStack* pile = pj; + if ( bMain ) pile = pj->RestoreStack(this); + if ( pile == nullptr ) return; + + // creates the variable of type pointer to the object + { + CBotString name = m_var->m_token.GetString(); + pThis = pile->FindVar(name); + pThis->SetUniqNum((static_cast(m_var))->m_nIdent); // its attribute a unique number + } + + CBotToken* pt = &m_token; + CBotClass* pClass = CBotClass::Find(pt); + bool bIntrincic = pClass->IsIntrinsic(); + + if ( bMain && pile->GetState()<3) + { + // is there an assignment or parameters (constructor) + +// CBotVarClass* pInstance = nullptr; + + if ( m_expr != nullptr ) + { + // evaluates the expression for the assignment + m_expr->RestoreState(pile, bMain); + return; + } + + else if ( m_hasParams ) + { + // evaluates the constructor of an instance + + if ( !bIntrincic && pile->GetState() == 1) + { + return; + } + + CBotVar* ppVars[1000]; + CBotStack* pile2 = pile; + + int i = 0; + + CBotInstr* p = m_Parameters; + // evaluates the parameters + // and the values an the stack + // for the ability to be interrupted at any time (\TODO pour pouvoir être interrompu n'importe quand) + + if ( p != nullptr) while ( true ) + { + pile2 = pile2->RestoreStack(); // place on the stack for the results + if ( pile2 == nullptr ) return; + + if ( pile2->GetState() == 0 ) + { + p->RestoreState(pile2, bMain); // interrupted here? + return; + } + ppVars[i++] = pile2->GetVar(); + p = p->GetNext(); + if ( p == nullptr) break; + } + ppVars[i] = nullptr; + + // creates a variable for the result +// CBotVar* pResult = nullptr; // constructor still void + + pClass->RestoreMethode(m_nMethodeIdent, pClass->GetName(), pThis, ppVars, pile2); + return; + } + } + + if ( m_next2b != nullptr ) + m_next2b->RestoreState(pile, bMain); // other(s) definition(s) +} diff --git a/src/CBot/CBotInstr/CBotClassInst.h b/src/CBot/CBotInstr/CBotClassInst.h new file mode 100644 index 00000000..370a46cd --- /dev/null +++ b/src/CBot/CBotInstr/CBotClassInst.h @@ -0,0 +1,85 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotClassInst class Definition of an element of any class. + */ +class CBotClassInst : public CBotInstr +{ + +public: + + /*! + * \brief CBotClassInst + */ + CBotClassInst(); + + /*! + * \brief ~CBotClassInst + */ + ~CBotClassInst(); + + /*! + * \brief Compile Definition of pointer (s) to an object style CPoint A, B ; + * \param p + * \param pStack + * \param pClass + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass = nullptr); + + /*! + * \brief Execute Declaration of the instance of a class, for example: + * CPoint A, B; + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! Variable to initialise. + CBotInstr* m_var; + //! Reference to the class. + CBotClass* m_pClass; + //! Parameters to be evaluated for the contructor. + CBotInstr* m_Parameters; + //! A value to put, if there is. + CBotInstr* m_expr; + //! Has it parameters. + bool m_hasParams; + long m_nMethodeIdent; + +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index e94bf82b..7142723d 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -42,6 +42,7 @@ set(SOURCES CBotInstr/CBotFieldExpr.cpp CBotInstr/CBotLeftExpr.cpp CBotInstr/CBotCondition.cpp + CBotInstr/CBotClassInst.cpp ) # Includes From 13b82b7e8e60afc6d69e35efe4c90a8a665535e9 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 22:45:27 +0100 Subject: [PATCH 38/91] Moving CBotIString class in its own header and source files. --- src/CBot/CBot.cpp | 133 +----------------- src/CBot/CBot.h | 17 --- src/CBot/CBotInstr/CBotIString.cpp | 156 ++++++++++++++++++++++ src/CBot/CBotInstr/CBotIString.h | 76 +++++++++++ src/CBot/CBotInstr/CBotListExpression.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 6 files changed, 235 insertions(+), 149 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotIString.cpp create mode 100644 src/CBot/CBotInstr/CBotIString.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 9e4716ec..072aa28f 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -62,6 +62,7 @@ #include "CBotInstr/CBotIndexExpr.h" #include "CBotInstr/CBotFieldExpr.h" #include "CBotInstr/CBotClassInst.h" +#include "CBotInstr/CBotIString.h" // Local include @@ -1220,138 +1221,6 @@ void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// define a string variable -// int a, b = "salut"; - -CBotIString::CBotIString() -{ - m_var = - m_expr = nullptr; - name = "CBotIString"; -} - -CBotIString::~CBotIString() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? nullptr : p; - - if (!cont && !IsOfType(p, ID_STRING)) return nullptr; - - CBotIString* inst = static_cast(CompileArray(p, pStack, CBotTypString)); - if (inst != nullptr || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotIString(); - - inst->m_expr = nullptr; - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - - if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - (static_cast(inst->m_var))->m_typevar = CBotTypString; - if (pStk->CheckVarLocal(vartoken)) - { - pStk->SetStartError(vartoken->GetStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GetEnd()); - goto error; - } - - if (IsOfType(p, ID_ASS)) - { - if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } -/* if (!pStk->GetTypResult().Eq(CBotTypString)) // type compatible ? - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - }*/ - } - - CBotVar* var = CBotVar::Create(vartoken, CBotTypString); - var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); - var->SetUniqNum( - (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); - - if (IsOfType(p, ID_COMMA)) - { - if (nullptr != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip))) - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GetStart()); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - -// executes the definition of the string variable - -bool CBotIString::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if ( pile->GetState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; - m_var->Execute(pile); - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; - - return pj->Return(pile); -} - -void CBotIString::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - if ( pile->GetState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) - m_next2b->RestoreState(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - // compile a list of parameters CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index a045dc37..9c1a8e21 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -600,23 +600,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -// definition of an element string - -class CBotIString : public CBotInstr -{ -private: - CBotInstr* m_var; // variable to initialise - CBotInstr* m_expr; // a value to put, if there is - -public: - CBotIString(); - ~CBotIString(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotIString.cpp b/src/CBot/CBotInstr/CBotIString.cpp new file mode 100644 index 00000000..b0e8a6a3 --- /dev/null +++ b/src/CBot/CBotInstr/CBotIString.cpp @@ -0,0 +1,156 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotIString.h" +#include "CBotLeftExprVar.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotIString::CBotIString() +{ + m_var = + m_expr = nullptr; + name = "CBotIString"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotIString::~CBotIString() +{ + delete m_var; + delete m_expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotIString::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? nullptr : p; + + if (!cont && !IsOfType(p, ID_STRING)) return nullptr; + + CBotIString* inst = static_cast(CompileArray(p, pStack, CBotTypString)); + if (inst != nullptr || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotIString(); + + inst->m_expr = nullptr; + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + + if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + (static_cast(inst->m_var))->m_typevar = CBotTypString; + if (pStk->CheckVarLocal(vartoken)) + { + pStk->SetStartError(vartoken->GetStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GetEnd()); + goto error; + } + + if (IsOfType(p, ID_ASS)) + { + if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } +/* if (!pStk->GetTypResult().Eq(CBotTypString)) // type compatible ? + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + }*/ + } + + CBotVar* var = CBotVar::Create(vartoken, CBotTypString); + var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); + var->SetUniqNum( + (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); + + if (IsOfType(p, ID_COMMA)) + { + if (nullptr != ( inst->m_next2b = CBotIString::Compile(p, pStk, true, noskip))) + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GetStart()); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotIString::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if ( pile->GetState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; + + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotIString::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + if ( pile->GetState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) + m_next2b->RestoreState(pile, bMain); +} diff --git a/src/CBot/CBotInstr/CBotIString.h b/src/CBot/CBotInstr/CBotIString.h new file mode 100644 index 00000000..1d91ea26 --- /dev/null +++ b/src/CBot/CBotInstr/CBotIString.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotIString class Define a string variable eg : int a, b = "salut"; + */ +class CBotIString : public CBotInstr +{ +public: + + /*! + * \brief CBotIString + */ + CBotIString(); + + /*! + * \brief ~CBotIString + */ + ~CBotIString(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param cont + * \param noskip + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + + /*! + * \brief Execute Executes the definition of the string variable. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Variable to initialise. + CBotInstr* m_var; + //! A value to put, if there is. + CBotInstr* m_expr; +}; diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index c631e762..285c6161 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -20,6 +20,7 @@ // Modules inlcude #include "CBotListExpression.h" #include "CBotExpression.h" +#include "CBotIString.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 7142723d..a0b76fcd 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -43,6 +43,7 @@ set(SOURCES CBotInstr/CBotLeftExpr.cpp CBotInstr/CBotCondition.cpp CBotInstr/CBotClassInst.cpp + CBotInstr/CBotIString.cpp ) # Includes From ff72d1a77f2cfeffce46fbe1d5e73bd4aa32c72b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 22:54:10 +0100 Subject: [PATCH 39/91] Moving CBotFloat class in its own header and source files. --- src/CBot/CBot.cpp | 147 +------------------ src/CBot/CBot.h | 19 --- src/CBot/CBotInstr/CBotFloat.cpp | 170 ++++++++++++++++++++++ src/CBot/CBotInstr/CBotFloat.h | 76 ++++++++++ src/CBot/CBotInstr/CBotListExpression.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 6 files changed, 249 insertions(+), 165 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotFloat.cpp create mode 100644 src/CBot/CBotInstr/CBotFloat.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 072aa28f..5cb8cace 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -63,6 +63,7 @@ #include "CBotInstr/CBotFieldExpr.h" #include "CBotInstr/CBotClassInst.h" #include "CBotInstr/CBotIString.h" +#include "CBotInstr/CBotFloat.h" // Local include @@ -1075,152 +1076,6 @@ void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// definition of a real/float variable -// int a, b = 12.4; - -CBotFloat::CBotFloat() -{ - m_var = - m_expr = nullptr; - name = "CBotFloat"; -} - -CBotFloat::~CBotFloat() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? nullptr : p; - - if (!cont && !IsOfType(p, ID_FLOAT)) return nullptr; - - CBotFloat* inst = static_cast(CompileArray(p, pStack, CBotTypFloat)); - if (inst != nullptr || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotFloat(); - - inst->m_expr = nullptr; - - CBotToken* vartoken = p; - CBotVar* var = nullptr; - inst->SetToken(vartoken); - - if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - (static_cast(inst->m_var))->m_typevar = CBotTypFloat; - if (pStk->CheckVarLocal(vartoken)) // redefinition of a variable - { - pStk->SetStartError(vartoken->GetStart()); - pStk->SetError(TX_REDEFVAR, vartoken->GetEnd()); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) - { - delete inst; - p = vartoken; - inst = static_cast(CBotInstArray::Compile(p, pStk, CBotTypFloat)); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto error; - } - goto suite; // no assignment, variable already created - } - - if (IsOfType(p, ID_ASS)) - { - if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - if (pStk->GetType() >= CBotTypBoolean) - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - } - } - - var = CBotVar::Create(vartoken, CBotTypFloat); - var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); - var->SetUniqNum( - (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); -suite: - if (IsOfType(p, ID_COMMA)) - { - if (nullptr != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip))) - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GetStart()); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - -// executes the definition of a real variable - -bool CBotFloat::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); - - if ( pile->GetState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; - m_var->Execute(pile); - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; - - return pj->Return(pile); -} - -void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - if ( pile->GetState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) - m_next2b->RestoreState(pile, bMain); -} - -////////////////////////////////////////////////////////////////////////////////////////// - // compile a list of parameters CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 9c1a8e21..8ae8e36d 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -582,25 +582,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -// definition of a real number - -class CBotFloat : public CBotInstr -{ -private: - CBotInstr* m_var; // variable to initialise - CBotInstr* m_expr; // a value to put, if there is - -public: - CBotFloat(); - ~CBotFloat(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotFloat.cpp b/src/CBot/CBotInstr/CBotFloat.cpp new file mode 100644 index 00000000..96c730be --- /dev/null +++ b/src/CBot/CBotInstr/CBotFloat.cpp @@ -0,0 +1,170 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotFloat.h" +#include "CBotLeftExprVar.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotFloat::CBotFloat() +{ + m_var = + m_expr = nullptr; + name = "CBotFloat"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotFloat::~CBotFloat() +{ + delete m_var; + delete m_expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotFloat::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? nullptr : p; + + if (!cont && !IsOfType(p, ID_FLOAT)) return nullptr; + + CBotFloat* inst = static_cast(CompileArray(p, pStack, CBotTypFloat)); + if (inst != nullptr || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotFloat(); + + inst->m_expr = nullptr; + + CBotToken* vartoken = p; + CBotVar* var = nullptr; + inst->SetToken(vartoken); + + if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + (static_cast(inst->m_var))->m_typevar = CBotTypFloat; + if (pStk->CheckVarLocal(vartoken)) // redefinition of a variable + { + pStk->SetStartError(vartoken->GetStart()); + pStk->SetError(TX_REDEFVAR, vartoken->GetEnd()); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) + { + delete inst; + p = vartoken; + inst = static_cast(CBotInstArray::Compile(p, pStk, CBotTypFloat)); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + if (IsOfType(p, ID_ASS)) + { + if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + if (pStk->GetType() >= CBotTypBoolean) + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + } + } + + var = CBotVar::Create(vartoken, CBotTypFloat); + var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); + var->SetUniqNum( + (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); +suite: + if (IsOfType(p, ID_COMMA)) + { + if (nullptr != ( inst->m_next2b = CBotFloat::Compile(p, pStk, true, noskip))) + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GetStart()); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotFloat::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); + + if ( pile->GetState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; + + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotFloat::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + if ( pile->GetState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) + m_next2b->RestoreState(pile, bMain); +} diff --git a/src/CBot/CBotInstr/CBotFloat.h b/src/CBot/CBotInstr/CBotFloat.h new file mode 100644 index 00000000..6223071c --- /dev/null +++ b/src/CBot/CBotInstr/CBotFloat.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotFloat class Definition of a real/float variable int a, b = 12.4; + */ +class CBotFloat : public CBotInstr +{ +public: + + /*! + * \brief CBotFloat + */ + CBotFloat(); + + /*! + * \brief ~CBotFloat + */ + ~CBotFloat(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param cont + * \param noskip + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + + /*! + * \brief Execute Executes the definition of a real variable. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Variable to initialise. + CBotInstr* m_var; + //! A value to put, if there is. + CBotInstr* m_expr; +}; diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index 285c6161..0d2ec838 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -21,6 +21,7 @@ #include "CBotListExpression.h" #include "CBotExpression.h" #include "CBotIString.h" +#include "CBotFloat.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index a0b76fcd..60045335 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -44,6 +44,7 @@ set(SOURCES CBotInstr/CBotCondition.cpp CBotInstr/CBotClassInst.cpp CBotInstr/CBotIString.cpp + CBotInstr/CBotFloat.cpp ) # Includes From 17cbae8e688a551ba225f9d646ec8137cacd9ad0 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Wed, 11 Nov 2015 23:03:32 +0100 Subject: [PATCH 40/91] Moving CBotBoolean class in its own header and source files. --- src/CBot/CBot.cpp | 149 +------------------ src/CBot/CBot.h | 17 --- src/CBot/CBotInstr/CBotBoolean.cpp | 172 ++++++++++++++++++++++ src/CBot/CBotInstr/CBotBoolean.h | 76 ++++++++++ src/CBot/CBotInstr/CBotListExpression.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 6 files changed, 251 insertions(+), 165 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotBoolean.cpp create mode 100644 src/CBot/CBotInstr/CBotBoolean.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 5cb8cace..d08e01e7 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -64,6 +64,7 @@ #include "CBotInstr/CBotClassInst.h" #include "CBotInstr/CBotIString.h" #include "CBotInstr/CBotFloat.h" +#include "CBotInstr/CBotBoolean.h" // Local include @@ -928,154 +929,6 @@ void CBotInt::RestoreState(CBotStack* &pj, bool bMain) ////////////////////////////////////////////////////////////////////////////////////////// - -////////////////////////////////////////////////////////////////////////////////////// -// defining a boolean variable -// int a, b = false; - -CBotBoolean::CBotBoolean() -{ - m_var = - m_expr = nullptr; - name = "CBotBoolean"; -} - -CBotBoolean::~CBotBoolean() -{ - delete m_var; - delete m_expr; -} - -CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? nullptr : p; - - if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return nullptr; - - CBotBoolean* inst = static_cast(CompileArray(p, pStack, CBotTypBoolean)); - if (inst != nullptr || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotBoolean(); - - inst->m_expr = nullptr; - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - CBotVar* var = nullptr; - - if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - (static_cast(inst->m_var))->m_typevar = CBotTypBoolean; - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) - { - delete inst; // type is not CBotInt - p = vartoken; // resutns to the variable name - - // compiles an array declaration - - inst = static_cast(CBotInstArray::Compile(p, pStk, CBotTypBoolean)); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto error; - } - goto suite; // no assignment, variable already created - } - - if (IsOfType(p, ID_ASS)) - { - if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - if (!pStk->GetTypResult().Eq(CBotTypBoolean)) - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - } - } - - var = CBotVar::Create(vartoken, CBotTypBoolean);// create the variable (evaluated after the assignment) - var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); - var->SetUniqNum( - (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); -suite: - if (IsOfType(p, ID_COMMA)) - { - if (nullptr != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip))) - { - return pStack->Return(inst, pStk); - } - } - - if (noskip || IsOfType(p, ID_SEP)) - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GetStart()); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - -// executes a boolean variable definition - -bool CBotBoolean::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this);//essential for SetState() - - if ( pile->GetState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; - m_var->Execute(pile); - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; - - return pj->Return(pile); -} - -void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - if ( pile->GetState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // initial value interrupted? - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) - m_next2b->RestoreState(pile, bMain); // other(s) definition(s) -} - -////////////////////////////////////////////////////////////////////////////////////////// - // compile a list of parameters CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 8ae8e36d..cc1cb2d8 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -565,23 +565,6 @@ class CBotEmpty : public CBotInstr void RestoreState(CBotStack* &pj, bool bMain) override; }; -// defininition of a boolean - -class CBotBoolean : public CBotInstr -{ -private: - CBotInstr* m_var; // variable to initialise - CBotInstr* m_expr; // a value to put, if there is - -public: - CBotBoolean(); - ~CBotBoolean(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotBoolean.cpp b/src/CBot/CBotInstr/CBotBoolean.cpp new file mode 100644 index 00000000..9e74fffe --- /dev/null +++ b/src/CBot/CBotInstr/CBotBoolean.cpp @@ -0,0 +1,172 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotBoolean.h" +#include "CBotLeftExprVar.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotBoolean::CBotBoolean() +{ + m_var = + m_expr = nullptr; + name = "CBotBoolean"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotBoolean::~CBotBoolean() +{ + delete m_var; + delete m_expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotBoolean::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? nullptr : p; + + if (!cont && !IsOfType(p, ID_BOOLEAN, ID_BOOL)) return nullptr; + + CBotBoolean* inst = static_cast(CompileArray(p, pStack, CBotTypBoolean)); + if (inst != nullptr || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotBoolean(); + + inst->m_expr = nullptr; + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + CBotVar* var = nullptr; + + if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + (static_cast(inst->m_var))->m_typevar = CBotTypBoolean; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) + { + delete inst; // type is not CBotInt + p = vartoken; // resutns to the variable name + + // compiles an array declaration + + inst = static_cast(CBotInstArray::Compile(p, pStk, CBotTypBoolean)); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto error; + } + goto suite; // no assignment, variable already created + } + + if (IsOfType(p, ID_ASS)) + { + if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + if (!pStk->GetTypResult().Eq(CBotTypBoolean)) + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + } + } + + var = CBotVar::Create(vartoken, CBotTypBoolean);// create the variable (evaluated after the assignment) + var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); + var->SetUniqNum( + (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); +suite: + if (IsOfType(p, ID_COMMA)) + { + if (nullptr != ( inst->m_next2b = CBotBoolean::Compile(p, pStk, true, noskip))) + { + return pStack->Return(inst, pStk); + } + } + + if (noskip || IsOfType(p, ID_SEP)) + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GetStart()); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotBoolean::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this);//essential for SetState() + + if ( pile->GetState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; + m_var->Execute(pile); + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; + + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotBoolean::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + if ( pile->GetState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // initial value interrupted? + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) + m_next2b->RestoreState(pile, bMain); // other(s) definition(s) +} diff --git a/src/CBot/CBotInstr/CBotBoolean.h b/src/CBot/CBotInstr/CBotBoolean.h new file mode 100644 index 00000000..57674200 --- /dev/null +++ b/src/CBot/CBotInstr/CBotBoolean.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotBoolean class Defining a boolean variable int a, b = false; + */ +class CBotBoolean : public CBotInstr +{ +public: + + /*! + * \brief CBotBoolean + */ + CBotBoolean(); + + /*! + * \brief ~CBotBoolean + */ + ~CBotBoolean(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param cont + * \param noskip + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip=false); + + /*! + * \brief Execute Executes a boolean variable definition. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Variable to initialise. + CBotInstr* m_var; + //! A value to put, if there is. + CBotInstr* m_expr; +}; diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index 0d2ec838..a6a58e5e 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -22,6 +22,7 @@ #include "CBotExpression.h" #include "CBotIString.h" #include "CBotFloat.h" +#include "CBotBoolean.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 60045335..a001e1d2 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -45,6 +45,7 @@ set(SOURCES CBotInstr/CBotClassInst.cpp CBotInstr/CBotIString.cpp CBotInstr/CBotFloat.cpp + CBotInstr/CBotBoolean.cpp ) # Includes From 14961dbc5779fed099c9e5e0ef626c8447e58010 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 11:40:51 +0100 Subject: [PATCH 41/91] Moving CBotEmpty class in its own header and source files. --- src/CBot/CBot.cpp | 14 +--------- src/CBot/CBot.h | 7 ----- src/CBot/CBotFunction.cpp | 1 + src/CBot/CBotInstr/CBotEmpty.cpp | 40 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotEmpty.h | 47 ++++++++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotEmpty.cpp create mode 100644 src/CBot/CBotInstr/CBotEmpty.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index d08e01e7..e9d41719 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -65,6 +65,7 @@ #include "CBotInstr/CBotIString.h" #include "CBotInstr/CBotFloat.h" #include "CBotInstr/CBotBoolean.h" +#include "CBotInstr/CBotEmpty.h" // Local include @@ -565,19 +566,6 @@ void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) if (m_next2b ) m_next2b->RestoreState( pile1, bMain); } -// special case for empty indexes -bool CBotEmpty :: Execute(CBotStack* &pj) -{ - CBotVar* pVar = CBotVar::Create("", CBotTypInt); - pVar->SetValInt(-1); - pj->SetVar(pVar); - return true; -} - -void CBotEmpty :: RestoreState(CBotStack* &pj, bool bMain) -{ -} - ////////////////////////////////////////////////////////////////////////////////////// // defining a list table initialization // int [ ] a [ ] = (( 1, 2, 3 ) , ( 3, 2, 1 )) ; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index cc1cb2d8..5197bd86 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -558,13 +558,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -class CBotEmpty : public CBotInstr -{ - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index b672f32a..f298f102 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -26,6 +26,7 @@ #include "CBotInstr/CBotBlock.h" #include "CBotInstr/CBotTwoOpExpr.h" #include "CBotInstr/CBotExpression.h" +#include "CBotInstr/CBotEmpty.h" #include diff --git a/src/CBot/CBotInstr/CBotEmpty.cpp b/src/CBot/CBotInstr/CBotEmpty.cpp new file mode 100644 index 00000000..4f3cb5ae --- /dev/null +++ b/src/CBot/CBotInstr/CBotEmpty.cpp @@ -0,0 +1,40 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotEmpty.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +bool CBotEmpty::Execute(CBotStack* &pj) +{ + CBotVar* pVar = CBotVar::Create("", CBotTypInt); + pVar->SetValInt(-1); + pj->SetVar(pVar); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotEmpty::RestoreState(CBotStack* &pj, bool bMain) +{ +} diff --git a/src/CBot/CBotInstr/CBotEmpty.h b/src/CBot/CBotInstr/CBotEmpty.h new file mode 100644 index 00000000..04f9fafd --- /dev/null +++ b/src/CBot/CBotInstr/CBotEmpty.h @@ -0,0 +1,47 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotEmpty class + */ +class CBotEmpty : public CBotInstr +{ + /*! + * \brief Execute Special case for empty indexes. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index a001e1d2..bc2a19eb 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -46,6 +46,7 @@ set(SOURCES CBotInstr/CBotIString.cpp CBotInstr/CBotFloat.cpp CBotInstr/CBotBoolean.cpp + CBotInstr/CBotEmpty.cpp ) # Includes From c0e2201c70756a22d590a8678fb3487c8a4b85b9 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 11:51:15 +0100 Subject: [PATCH 42/91] Moving CBotReturn class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 18 ----- src/CBot/CBotFunction.cpp | 83 ---------------------- src/CBot/CBotInstr/CBotReturn.cpp | 110 ++++++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotReturn.h | 72 +++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 184 insertions(+), 101 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotReturn.cpp create mode 100644 src/CBot/CBotInstr/CBotReturn.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index e9d41719..4d6a14bd 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -66,6 +66,7 @@ #include "CBotInstr/CBotFloat.h" #include "CBotInstr/CBotBoolean.h" #include "CBotInstr/CBotEmpty.h" +#include "CBotInstr/CBotReturn.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 5197bd86..9707b607 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -467,24 +467,6 @@ public: bool IsOfClass(CBotString name); }; - - -class CBotReturn : public CBotInstr -{ -private: - CBotInstr* m_Instr; // paramter of return - -public: - CBotReturn(); - ~CBotReturn(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - - class CBotIf : public CBotInstr { private: diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index f298f102..090f28d4 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -1108,89 +1108,6 @@ CBotString CBotDefParam::GetParamString() return param; } - - -////////////////////////////////////////////////////////////////////////// -// return parameters - -CBotReturn::CBotReturn() -{ - m_Instr = nullptr; - name = "CBotReturn"; // debug -} - -CBotReturn::~CBotReturn() -{ - delete m_Instr; -} - -CBotInstr* CBotReturn::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - - if (!IsOfType(p, ID_RETURN)) return nullptr; // should never happen - - CBotReturn* inst = new CBotReturn(); // creates the object - inst->SetToken( pp ); - - CBotTypResult type = pStack->GetRetType(); - - if ( type.GetType() == 0 ) // returned void ? - { - if ( IsOfType( p, ID_SEP ) ) return inst; - pStack->SetError( TX_BADTYPE, pp ); - return nullptr; - } - - inst->m_Instr = CBotExpression::Compile(p, pStack); - if ( pStack->IsOk() ) - { - CBotTypResult retType = pStack->GetTypResult(2); - if (TypeCompatible(retType, type, ID_ASS)) - { - if ( IsOfType( p, ID_SEP ) ) - return inst; - - pStack->SetError(TX_ENDOF, p->GetStart()); - } - pStack->SetError(TX_BADTYPE, p->GetStart()); - } - - delete inst; - return nullptr; // no object, the error is on the stack -} - -bool CBotReturn::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); -// if ( pile == EOX ) return true; - - if ( pile->GetState() == 0 ) - { - if ( m_Instr != nullptr && !m_Instr->Execute(pile) ) return false; // evaluate the result - // the result is on the stack - pile->IncState(); - } - - if ( pile->IfStep() ) return false; - - pile->SetBreak(3, CBotString()); - return pj->Return(pile); -} - -void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) -{ - if ( !bMain ) return; - CBotStack* pile = pj->RestoreStack(this); - if ( pile == nullptr ) return; - - if ( pile->GetState() == 0 ) - { - if ( m_Instr != nullptr ) m_Instr->RestoreState(pile, bMain); // evaluate the result - return; - } -} - ////////////////////////////////////////////////////////////////////////////// // statement of user classes diff --git a/src/CBot/CBotInstr/CBotReturn.cpp b/src/CBot/CBotInstr/CBotReturn.cpp new file mode 100644 index 00000000..d64326c9 --- /dev/null +++ b/src/CBot/CBotInstr/CBotReturn.cpp @@ -0,0 +1,110 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotReturn.h" +#include "CBotExpression.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotReturn::CBotReturn() +{ + m_Instr = nullptr; + name = "CBotReturn"; // debug +} + +//////////////////////////////////////////////////////////////////////////////// +CBotReturn::~CBotReturn() +{ + delete m_Instr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotReturn::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + + if (!IsOfType(p, ID_RETURN)) return nullptr; // should never happen + + CBotReturn* inst = new CBotReturn(); // creates the object + inst->SetToken( pp ); + + CBotTypResult type = pStack->GetRetType(); + + if ( type.GetType() == 0 ) // returned void ? + { + if ( IsOfType( p, ID_SEP ) ) return inst; + pStack->SetError( TX_BADTYPE, pp ); + return nullptr; + } + + inst->m_Instr = CBotExpression::Compile(p, pStack); + if ( pStack->IsOk() ) + { + CBotTypResult retType = pStack->GetTypResult(2); + if (TypeCompatible(retType, type, ID_ASS)) + { + if ( IsOfType( p, ID_SEP ) ) + return inst; + + pStack->SetError(TX_ENDOF, p->GetStart()); + } + pStack->SetError(TX_BADTYPE, p->GetStart()); + } + + delete inst; + return nullptr; // no object, the error is on the stack +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotReturn::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); +// if ( pile == EOX ) return true; + + if ( pile->GetState() == 0 ) + { + if ( m_Instr != nullptr && !m_Instr->Execute(pile) ) return false; // evaluate the result + // the result is on the stack + pile->IncState(); + } + + if ( pile->IfStep() ) return false; + + pile->SetBreak(3, CBotString()); + return pj->Return(pile); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotReturn::RestoreState(CBotStack* &pj, bool bMain) +{ + if ( !bMain ) return; + CBotStack* pile = pj->RestoreStack(this); + if ( pile == nullptr ) return; + + if ( pile->GetState() == 0 ) + { + if ( m_Instr != nullptr ) m_Instr->RestoreState(pile, bMain); // evaluate the result + return; + } +} diff --git a/src/CBot/CBotInstr/CBotReturn.h b/src/CBot/CBotInstr/CBotReturn.h new file mode 100644 index 00000000..978c6c10 --- /dev/null +++ b/src/CBot/CBotInstr/CBotReturn.h @@ -0,0 +1,72 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + +/*! + * \brief The CBotReturn class. Return parameters + */ +class CBotReturn : public CBotInstr +{ +public: + + /*! + * \brief CBotReturn + */ + CBotReturn(); + + /*! + * \brief ~CBotReturn + */ + ~CBotReturn(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Parameter of return + CBotInstr *m_Instr; + +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index bc2a19eb..2d25fca4 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -47,6 +47,7 @@ set(SOURCES CBotInstr/CBotFloat.cpp CBotInstr/CBotBoolean.cpp CBotInstr/CBotEmpty.cpp + CBotInstr/CBotReturn.cpp ) # Includes From 8c04d7fc652728753f154c33a2e35d03d2419e1a Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 11:59:32 +0100 Subject: [PATCH 43/91] Moving CBotIf class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 17 ------- src/CBot/{ => CBotInstr}/CBotIf.cpp | 25 +++++----- src/CBot/CBotInstr/CBotIf.h | 77 +++++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 2 +- 5 files changed, 91 insertions(+), 31 deletions(-) rename src/CBot/{ => CBotInstr}/CBotIf.cpp (92%) create mode 100644 src/CBot/CBotInstr/CBotIf.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 4d6a14bd..03c6aabb 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -67,6 +67,7 @@ #include "CBotInstr/CBotBoolean.h" #include "CBotInstr/CBotEmpty.h" #include "CBotInstr/CBotReturn.h" +#include "CBotInstr/CBotIf.h" // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 9707b607..d1804741 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -467,23 +467,6 @@ public: bool IsOfClass(CBotString name); }; -class CBotIf : public CBotInstr -{ -private: - CBotInstr* m_Condition; // condition - CBotInstr* m_Block; // instructions - CBotInstr* m_BlockElse; // instructions - -public: - CBotIf(); - ~CBotIf(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - - // definition of an integer class CBotInt : public CBotInstr diff --git a/src/CBot/CBotIf.cpp b/src/CBot/CBotInstr/CBotIf.cpp similarity index 92% rename from src/CBot/CBotIf.cpp rename to src/CBot/CBotInstr/CBotIf.cpp index 551ecb18..2ae5acf1 100644 --- a/src/CBot/CBotIf.cpp +++ b/src/CBot/CBotInstr/CBotIf.cpp @@ -17,15 +17,17 @@ * along with this program. If not, see http://gnu.org/licenses */ -/////////////////////////////////////////////////////////////////////// -// instruction if (condition) operation1 else operation2; - -#include "CBot.h" - +// Modules inlcude +#include "CBotIf.h" #include "CBotInstr/CBotBlock.h" #include "CBotInstr/CBotCondition.h" -// various constructors / destructors +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// CBotIf::CBotIf() { m_Condition = @@ -34,6 +36,7 @@ CBotIf::CBotIf() name = "CBotIf"; // debug } +//////////////////////////////////////////////////////////////////////////////// CBotIf::~CBotIf() { delete m_Condition; // frees the condition @@ -41,9 +44,7 @@ CBotIf::~CBotIf() delete m_BlockElse; // frees the block of instruction2 } -// compilation (static routine) -// called when the token "if" has been found - +//////////////////////////////////////////////////////////////////////////////// CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) { CBotToken* pp = p; // preserves at the ^ token (starting instruction) @@ -89,9 +90,7 @@ CBotInstr* CBotIf::Compile(CBotToken* &p, CBotCStack* pStack) return pStack->Return(nullptr, pStk); } - -// execution of the instruction - +//////////////////////////////////////////////////////////////////////////////// bool CBotIf :: Execute(CBotStack* &pj) { CBotStack* pile = pj->AddStack(this); // adds an item to the stack @@ -134,7 +133,7 @@ bool CBotIf :: Execute(CBotStack* &pj) return pj->Return(pile); } - +//////////////////////////////////////////////////////////////////////////////// void CBotIf :: RestoreState(CBotStack* &pj, bool bMain) { if ( !bMain ) return; diff --git a/src/CBot/CBotInstr/CBotIf.h b/src/CBot/CBotInstr/CBotIf.h new file mode 100644 index 00000000..6ea6ee9c --- /dev/null +++ b/src/CBot/CBotInstr/CBotIf.h @@ -0,0 +1,77 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotIf class Instruction if (condition) operation1 else operation2; + */ +class CBotIf : public CBotInstr +{ +public: + + /*! + * \brief CBotIf + */ + CBotIf(); + + /*! + * \brief ~CBotIf + */ + ~CBotIf(); + + /*! + * \brief Compile Compilation (static routine) called when the token "if" + * has been found + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute Execution of the instruction. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! Condition + CBotInstr* m_Condition; + //! Instruction + CBotInstr* m_Block; + //! Instruction + CBotInstr* m_BlockElse; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 2d25fca4..8469f468 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -2,7 +2,6 @@ set(SOURCES CBot.cpp CBotClass.cpp CBotFunction.cpp - CBotIf.cpp CBotProgram.cpp CBotStack.cpp CBotString.cpp @@ -48,6 +47,7 @@ set(SOURCES CBotInstr/CBotBoolean.cpp CBotInstr/CBotEmpty.cpp CBotInstr/CBotReturn.cpp + CBotInstr/CBotIf.cpp ) # Includes From ede0d03026292a8e30373f37eb250aa5bf459a33 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 12:07:17 +0100 Subject: [PATCH 44/91] Moving CBotListArray class in its own header and source files. --- src/CBot/CBot.cpp | 154 +---------------------- src/CBot/CBot.h | 18 --- src/CBot/CBotFunction.cpp | 1 + src/CBot/CBotInstr/CBotListArray.cpp | 175 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotListArray.h | 76 ++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 254 insertions(+), 171 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotListArray.cpp create mode 100644 src/CBot/CBotInstr/CBotListArray.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 03c6aabb..bb40150f 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -68,6 +68,7 @@ #include "CBotInstr/CBotEmpty.h" #include "CBotInstr/CBotReturn.h" #include "CBotInstr/CBotIf.h" +#include "CBotInstr/CBotListArray.h" // Local include @@ -568,159 +569,6 @@ void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) if (m_next2b ) m_next2b->RestoreState( pile1, bMain); } -////////////////////////////////////////////////////////////////////////////////////// -// defining a list table initialization -// int [ ] a [ ] = (( 1, 2, 3 ) , ( 3, 2, 1 )) ; - - -CBotListArray::CBotListArray() -{ - m_expr = nullptr; - name = "CBotListArray"; -} - -CBotListArray::~CBotListArray() -{ - delete m_expr; -} - - -CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) -{ - CBotCStack* pStk = pStack->TokenStack(p); - - CBotToken* pp = p; - - if (IsOfType( p, ID_NULL )) - { - CBotInstr* inst = new CBotExprNull (); - inst->SetToken(pp); - return pStack->Return(inst, pStk); // ok with empty element - } - - CBotListArray* inst = new CBotListArray(); - - if (IsOfType( p, ID_OPENPAR )) - { - // each element takes the one after the other - if (type.Eq( CBotTypArrayPointer )) - { - type = type.GetTypElem(); - - pStk->SetStartError(p->GetStart()); - if (nullptr == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) )) - { - goto error; - } - - while (IsOfType( p, ID_COMMA )) // other elements? - { - pStk->SetStartError(p->GetStart()); - - CBotInstr* i = CBotListArray::Compile(p, pStk, type); - if (nullptr == i) - { - goto error; - } - - inst->m_expr->AddNext3(i); - } - } - else - { - pStk->SetStartError(p->GetStart()); - if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - CBotVar* pv = pStk->GetVar(); // result of the expression - - if (pv == nullptr || !TypesCompatibles( type, pv->GetTypResult())) // compatible type? - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - } - - while (IsOfType( p, ID_COMMA )) // other elements? - { - pStk->SetStartError(p->GetStart()); - - CBotInstr* i = CBotTwoOpExpr::Compile(p, pStk) ; - if (nullptr == i) - { - goto error; - } - - CBotVar* pv = pStk->GetVar(); // result of the expression - - if (pv == nullptr || !TypesCompatibles( type, pv->GetTypResult())) // compatible type? - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - } - inst->m_expr->AddNext3(i); - } - } - - if (!IsOfType(p, ID_CLOSEPAR) ) - { - pStk->SetError(TX_CLOSEPAR, p->GetStart()); - goto error; - } - - return pStack->Return(inst, pStk); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - - -// executes the definition of an array - -bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) -{ - CBotStack* pile1 = pj->AddStack(); - CBotVar* pVar2; - - CBotInstr* p = m_expr; - - int n = 0; - - for (; p != nullptr ; n++, p = p->GetNext3()) - { - if (pile1->GetState() > n) continue; - - pVar2 = pVar->GetItem(n, true); - - if (!p->Execute(pile1, pVar2)) return false; // evaluate expression - - pile1->IncState(); - } - - return pj->Return(pile1); -} - -void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) -{ - if (bMain) - { - CBotStack* pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - CBotInstr* p = m_expr; - - int state = pile->GetState(); - - while(state-- > 0) p = p->GetNext3() ; - - p->RestoreState(pile, bMain); // size calculation //interrupted! - } -} - -////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////////////////////////////////////////////////////////////// // definition of an integer variable // int a, b = 12; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index d1804741..e663e8ec 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -505,24 +505,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; - -// definition of a assignment list for a table -// int [ ] a [ ] = ( ( 1, 2, 3 ) , ( 3, 2, 1 ) ) ; - -class CBotListArray : public CBotInstr -{ -private: - CBotInstr* m_expr; // an expression for an element - // others are linked with CBotInstr :: m_next3; -public: - CBotListArray(); - ~CBotListArray(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - bool Execute(CBotStack* &pj, CBotVar* pVar) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 090f28d4..88bfabd8 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -27,6 +27,7 @@ #include "CBotInstr/CBotTwoOpExpr.h" #include "CBotInstr/CBotExpression.h" #include "CBotInstr/CBotEmpty.h" +#include "CBotInstr/CBotListArray.h" #include diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp new file mode 100644 index 00000000..8ab32079 --- /dev/null +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -0,0 +1,175 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotListArray.h" + +#include "CBotExprNull.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotListArray::CBotListArray() +{ + m_expr = nullptr; + name = "CBotListArray"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotListArray::~CBotListArray() +{ + delete m_expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotListArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) +{ + CBotCStack* pStk = pStack->TokenStack(p); + + CBotToken* pp = p; + + if (IsOfType( p, ID_NULL )) + { + CBotInstr* inst = new CBotExprNull (); + inst->SetToken(pp); + return pStack->Return(inst, pStk); // ok with empty element + } + + CBotListArray* inst = new CBotListArray(); + + if (IsOfType( p, ID_OPENPAR )) + { + // each element takes the one after the other + if (type.Eq( CBotTypArrayPointer )) + { + type = type.GetTypElem(); + + pStk->SetStartError(p->GetStart()); + if (nullptr == ( inst->m_expr = CBotListArray::Compile( p, pStk, type ) )) + { + goto error; + } + + while (IsOfType( p, ID_COMMA )) // other elements? + { + pStk->SetStartError(p->GetStart()); + + CBotInstr* i = CBotListArray::Compile(p, pStk, type); + if (nullptr == i) + { + goto error; + } + + inst->m_expr->AddNext3(i); + } + } + else + { + pStk->SetStartError(p->GetStart()); + if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + CBotVar* pv = pStk->GetVar(); // result of the expression + + if (pv == nullptr || !TypesCompatibles( type, pv->GetTypResult())) // compatible type? + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + } + + while (IsOfType( p, ID_COMMA )) // other elements? + { + pStk->SetStartError(p->GetStart()); + + CBotInstr* i = CBotTwoOpExpr::Compile(p, pStk) ; + if (nullptr == i) + { + goto error; + } + + CBotVar* pv = pStk->GetVar(); // result of the expression + + if (pv == nullptr || !TypesCompatibles( type, pv->GetTypResult())) // compatible type? + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + } + inst->m_expr->AddNext3(i); + } + } + + if (!IsOfType(p, ID_CLOSEPAR) ) + { + pStk->SetError(TX_CLOSEPAR, p->GetStart()); + goto error; + } + + return pStack->Return(inst, pStk); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotListArray::Execute(CBotStack* &pj, CBotVar* pVar) +{ + CBotStack* pile1 = pj->AddStack(); + CBotVar* pVar2; + + CBotInstr* p = m_expr; + + int n = 0; + + for (; p != nullptr ; n++, p = p->GetNext3()) + { + if (pile1->GetState() > n) continue; + + pVar2 = pVar->GetItem(n, true); + + if (!p->Execute(pile1, pVar2)) return false; // evaluate expression + + pile1->IncState(); + } + + return pj->Return(pile1); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotListArray::RestoreState(CBotStack* &pj, bool bMain) +{ + if (bMain) + { + CBotStack* pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + CBotInstr* p = m_expr; + + int state = pile->GetState(); + + while(state-- > 0) p = p->GetNext3() ; + + p->RestoreState(pile, bMain); // size calculation //interrupted! + } +} diff --git a/src/CBot/CBotInstr/CBotListArray.h b/src/CBot/CBotInstr/CBotListArray.h new file mode 100644 index 00000000..921273d1 --- /dev/null +++ b/src/CBot/CBotInstr/CBotListArray.h @@ -0,0 +1,76 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotListArray class Definition of a assignment list for a table + * int [ ] a [ ] = ( ( 1, 2, 3 ) , ( 3, 2, 1 ) ) ; + */ +class CBotListArray : public CBotInstr +{ + +public: + + /*! + * \brief CBotListArray + */ + CBotListArray(); + + /*! + * \brief ~CBotListArray + */ + ~CBotListArray(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param type + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); + + /*! + * \brief Execute Executes the definition of an array. + * \param pj + * \param pVar + * \return + */ + bool Execute(CBotStack* &pj, CBotVar* pVar) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + //! An expression for an element others are linked with CBotInstr :: m_next3; + CBotInstr* m_expr; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 8469f468..64a97f67 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -48,6 +48,7 @@ set(SOURCES CBotInstr/CBotEmpty.cpp CBotInstr/CBotReturn.cpp CBotInstr/CBotIf.cpp + CBotInstr/CBotListArray.cpp ) # Includes From b01e2180d89c0a1b5cd3bc3ce7a24b6ac45f54a9 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 12:18:34 +0100 Subject: [PATCH 45/91] Moving CBotInstArray class in its own header and source files. --- src/CBot/CBot.cpp | 198 +---------------------- src/CBot/CBot.h | 20 --- src/CBot/CBotInstr/CBotBoolean.cpp | 1 + src/CBot/CBotInstr/CBotClassInst.cpp | 1 + src/CBot/CBotInstr/CBotFloat.cpp | 1 + src/CBot/CBotInstr/CBotInstArray.cpp | 224 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotInstArray.h | 82 ++++++++++ src/CBot/CMakeLists.txt | 1 + 8 files changed, 311 insertions(+), 217 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotInstArray.cpp create mode 100644 src/CBot/CBotInstr/CBotInstArray.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index bb40150f..84d73981 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -69,6 +69,7 @@ #include "CBotInstr/CBotReturn.h" #include "CBotInstr/CBotIf.h" #include "CBotInstr/CBotListArray.h" +#include "CBotInstr/CBotInstArray.h" // Local include @@ -372,203 +373,6 @@ bool CBotInstr::CompCase(CBotStack* &pj, int val) return false; } -////////////////////////////////////////////////////////////////////////////////////// -// defining an array of any type -// int a[12]; -// point x[]; - -CBotInstArray::CBotInstArray() -{ - m_var = nullptr; - m_listass = nullptr; - name = "CBotInstArray"; -} - -CBotInstArray::~CBotInstArray() -{ - delete m_var; - delete m_listass; -} - - -CBotInstr* CBotInstArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) -{ - CBotCStack* pStk = pStack->TokenStack(p); - - CBotInstArray* inst = new CBotInstArray(); - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - - // determinse the expression is valid for the item on the left side - if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable? - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - CBotInstr* i; - while (IsOfType(p, ID_OPBRK)) - { - if (p->GetType() != ID_CLBRK) - i = CBotExpression::Compile(p, pStk); // expression for the value - else - i = new CBotEmpty(); // if no special formula - - inst->AddNext3b(i); // construct a list - type = CBotTypResult(CBotTypArrayPointer, type); - - if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK )) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto error; - } - } - - CBotVar* var = CBotVar::Create(vartoken, type); // create an instance - inst->m_typevar = type; - - var->SetUniqNum( - (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); // place it on the stack - - if (IsOfType(p, ID_ASS)) // with an assignment - { - inst->m_listass = CBotListArray::Compile(p, pStk, type.GetTypElem()); - } - - if (pStk->IsOk()) return pStack->Return(inst, pStk); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - - -// executes the definition of an array - -bool CBotInstArray::Execute(CBotStack* &pj) -{ - CBotStack* pile1 = pj->AddStack(this); - - CBotStack* pile = pile1; - - if (pile1->GetState() == 0) - { - // seek the maximum dimension of the table - CBotInstr* p = GetNext3b(); // the different formulas - int nb = 0; - - while (p != nullptr) - { - pile = pile->AddStack(); // little room to work - nb++; - if (pile->GetState() == 0) - { - if (!p->Execute(pile)) return false; // size calculation //interrupted? - pile->IncState(); - } - p = p->GetNext3b(); - } - - p = GetNext3b(); - pile = pile1; // returns to the stack - int n = 0; - int max[100]; - - while (p != nullptr) - { - pile = pile->AddStack(); - CBotVar* v = pile->GetVar(); // result - max[n] = v->GetValInt(); // value - if (max[n]>MAXARRAYSIZE) - { - pile->SetError(TX_OUTARRAY, &m_token); - return pj->Return (pile); - } - n++; - p = p->GetNext3b(); - } - while (n<100) max[n++] = 0; - - m_typevar.SetArray(max); // store the limitations - - // create simply a nullptr pointer - CBotVar* var = CBotVar::Create(m_var->GetToken(), m_typevar); - var->SetPointer(nullptr); - var->SetUniqNum((static_cast(m_var))->m_nIdent); - pj->AddVar(var); - -#if STACKMEM - pile1->AddStack()->Delete(); -#else - delete pile1->AddStack(); // need more indices -#endif - pile1->IncState(); - } - - if (pile1->GetState() == 1) - { - if (m_listass != nullptr) // there is the assignment for this table - { - CBotVar* pVar = pj->FindVar((static_cast(m_var))->m_nIdent); - - if (!m_listass->Execute(pile1, pVar)) return false; - } - pile1->IncState(); - } - - if (pile1->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile1 )) return false; - - return pj->Return(pile1); -} - -void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile1 = pj; - - CBotVar* var = pj->FindVar(m_var->GetToken()->GetString()); - if (var != nullptr) var->SetUniqNum((static_cast(m_var))->m_nIdent); - - if (bMain) - { - pile1 = pj->RestoreStack(this); - CBotStack* pile = pile1; - if (pile == nullptr) return; - - if (pile1->GetState() == 0) - { - // seek the maximum dimension of the table - CBotInstr* p = GetNext3b(); - - while (p != nullptr) - { - pile = pile->RestoreStack(); - if (pile == nullptr) return; - if (pile->GetState() == 0) - { - p->RestoreState(pile, bMain); - return; - } - p = p->GetNext3b(); - } - } - if (pile1->GetState() == 1 && m_listass != nullptr) - { - m_listass->RestoreState(pile1, bMain); - } - - } - - if (m_next2b ) m_next2b->RestoreState( pile1, bMain); -} - ////////////////////////////////////////////////////////////////////////////////////// // definition of an integer variable // int a, b = 12; diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index e663e8ec..4ccd6a90 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -485,26 +485,6 @@ public: void RestoreState(CBotStack* &pj, bool bMain) override; }; -// definition of an array - -class CBotInstArray : public CBotInstr -{ -private: - CBotInstr* m_var; // the variables to initialize - CBotInstr* m_listass; // list of assignments for array - CBotTypResult - m_typevar; // type of elements -// CBotString m_ClassName; - -public: - CBotInstArray(); - ~CBotInstArray(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotBoolean.cpp b/src/CBot/CBotInstr/CBotBoolean.cpp index 9e74fffe..e2d2ad2d 100644 --- a/src/CBot/CBotInstr/CBotBoolean.cpp +++ b/src/CBot/CBotInstr/CBotBoolean.cpp @@ -21,6 +21,7 @@ #include "CBotBoolean.h" #include "CBotLeftExprVar.h" #include "CBotTwoOpExpr.h" +#include "CBotInstArray.h" // Local include diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index cf835df3..dfbd47b2 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -21,6 +21,7 @@ #include "CBotClassInst.h" #include "CBotLeftExprVar.h" #include "CBotTwoOpExpr.h" +#include "CBotInstArray.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFloat.cpp b/src/CBot/CBotInstr/CBotFloat.cpp index 96c730be..acc29ff9 100644 --- a/src/CBot/CBotInstr/CBotFloat.cpp +++ b/src/CBot/CBotInstr/CBotFloat.cpp @@ -21,6 +21,7 @@ #include "CBotFloat.h" #include "CBotLeftExprVar.h" #include "CBotTwoOpExpr.h" +#include "CBotInstArray.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstArray.cpp b/src/CBot/CBotInstr/CBotInstArray.cpp new file mode 100644 index 00000000..b97e2b54 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstArray.cpp @@ -0,0 +1,224 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotInstArray.h" + +#include "CBotLeftExprVar.h" +#include "CBotExpression.h" +#include "CBotListArray.h" +#include "CBotEmpty.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInstArray::CBotInstArray() +{ + m_var = nullptr; + m_listass = nullptr; + name = "CBotInstArray"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstArray::~CBotInstArray() +{ + delete m_var; + delete m_listass; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstArray::Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type) +{ + CBotCStack* pStk = pStack->TokenStack(p); + + CBotInstArray* inst = new CBotInstArray(); + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + + // determinse the expression is valid for the item on the left side + if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable? + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + CBotInstr* i; + while (IsOfType(p, ID_OPBRK)) + { + if (p->GetType() != ID_CLBRK) + i = CBotExpression::Compile(p, pStk); // expression for the value + else + i = new CBotEmpty(); // if no special formula + + inst->AddNext3b(i); // construct a list + type = CBotTypResult(CBotTypArrayPointer, type); + + if (!pStk->IsOk() || !IsOfType(p, ID_CLBRK )) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto error; + } + } + + CBotVar* var = CBotVar::Create(vartoken, type); // create an instance + inst->m_typevar = type; + + var->SetUniqNum( + (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); // place it on the stack + + if (IsOfType(p, ID_ASS)) // with an assignment + { + inst->m_listass = CBotListArray::Compile(p, pStk, type.GetTypElem()); + } + + if (pStk->IsOk()) return pStack->Return(inst, pStk); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstArray::Execute(CBotStack* &pj) +{ + CBotStack* pile1 = pj->AddStack(this); + + CBotStack* pile = pile1; + + if (pile1->GetState() == 0) + { + // seek the maximum dimension of the table + CBotInstr* p = GetNext3b(); // the different formulas + int nb = 0; + + while (p != nullptr) + { + pile = pile->AddStack(); // little room to work + nb++; + if (pile->GetState() == 0) + { + if (!p->Execute(pile)) return false; // size calculation //interrupted? + pile->IncState(); + } + p = p->GetNext3b(); + } + + p = GetNext3b(); + pile = pile1; // returns to the stack + int n = 0; + int max[100]; + + while (p != nullptr) + { + pile = pile->AddStack(); + CBotVar* v = pile->GetVar(); // result + max[n] = v->GetValInt(); // value + if (max[n]>MAXARRAYSIZE) + { + pile->SetError(TX_OUTARRAY, &m_token); + return pj->Return (pile); + } + n++; + p = p->GetNext3b(); + } + while (n<100) max[n++] = 0; + + m_typevar.SetArray(max); // store the limitations + + // create simply a nullptr pointer + CBotVar* var = CBotVar::Create(m_var->GetToken(), m_typevar); + var->SetPointer(nullptr); + var->SetUniqNum((static_cast(m_var))->m_nIdent); + pj->AddVar(var); + +#if STACKMEM + pile1->AddStack()->Delete(); +#else + delete pile1->AddStack(); // need more indices +#endif + pile1->IncState(); + } + + if (pile1->GetState() == 1) + { + if (m_listass != nullptr) // there is the assignment for this table + { + CBotVar* pVar = pj->FindVar((static_cast(m_var))->m_nIdent); + + if (!m_listass->Execute(pile1, pVar)) return false; + } + pile1->IncState(); + } + + if (pile1->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile1 )) return false; + + return pj->Return(pile1); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstArray::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile1 = pj; + + CBotVar* var = pj->FindVar(m_var->GetToken()->GetString()); + if (var != nullptr) var->SetUniqNum((static_cast(m_var))->m_nIdent); + + if (bMain) + { + pile1 = pj->RestoreStack(this); + CBotStack* pile = pile1; + if (pile == nullptr) return; + + if (pile1->GetState() == 0) + { + // seek the maximum dimension of the table + CBotInstr* p = GetNext3b(); + + while (p != nullptr) + { + pile = pile->RestoreStack(); + if (pile == nullptr) return; + if (pile->GetState() == 0) + { + p->RestoreState(pile, bMain); + return; + } + p = p->GetNext3b(); + } + } + if (pile1->GetState() == 1 && m_listass != nullptr) + { + m_listass->RestoreState(pile1, bMain); + } + + } + + if (m_next2b ) m_next2b->RestoreState( pile1, bMain); +} diff --git a/src/CBot/CBotInstr/CBotInstArray.h b/src/CBot/CBotInstr/CBotInstArray.h new file mode 100644 index 00000000..ca2cbe07 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstArray.h @@ -0,0 +1,82 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotInstArray class Definition of an array. + * Defining an array of any type + * int a[12]; + * point x[]; + */ +class CBotInstArray : public CBotInstr +{ +public: + + /*! + * \brief CBotInstArray + */ + CBotInstArray(); + + /*! + * \brief ~CBotInstArray + */ + ~CBotInstArray(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param type + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, CBotTypResult type); + + /*! + * \brief Execute Executes the definition of an array. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! The variables to initialize. + CBotInstr* m_var; + //! List of assignments for array. + CBotInstr* m_listass; + //! Type of elements. + CBotTypResult m_typevar; + +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 64a97f67..baf7b8a1 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -49,6 +49,7 @@ set(SOURCES CBotInstr/CBotReturn.cpp CBotInstr/CBotIf.cpp CBotInstr/CBotListArray.cpp + CBotInstr/CBotInstArray.cpp ) # Includes From 0373692ea115309523b397af1dc0f9a4631781a2 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 12:27:59 +0100 Subject: [PATCH 46/91] Moving CBotInt class in its own header and source files. --- src/CBot/CBot.cpp | 157 +----------------- src/CBot/CBot.h | 18 --- src/CBot/CBotInstr/CBotInt.cpp | 185 ++++++++++++++++++++++ src/CBot/CBotInstr/CBotInt.h | 78 +++++++++ src/CBot/CBotInstr/CBotListExpression.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 6 files changed, 266 insertions(+), 174 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotInt.cpp create mode 100644 src/CBot/CBotInstr/CBotInt.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 84d73981..a7b8cc40 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -70,6 +70,7 @@ #include "CBotInstr/CBotIf.h" #include "CBotInstr/CBotListArray.h" #include "CBotInstr/CBotInstArray.h" +#include "CBotInstr/CBotInt.h" // Local include @@ -373,24 +374,6 @@ bool CBotInstr::CompCase(CBotStack* &pj, int val) return false; } -////////////////////////////////////////////////////////////////////////////////////// -// definition of an integer variable -// int a, b = 12; - -CBotInt::CBotInt() -{ - m_next = nullptr; // for multiple definitions - m_var = - m_expr = nullptr; - name = "CBotInt"; -} - -CBotInt::~CBotInt() -{ - delete m_var; - delete m_expr; -} - CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) { if (IsOfType(p, ID_OPBRK)) @@ -431,144 +414,6 @@ CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypRes return nullptr; } -CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) -{ - CBotToken* pp = cont ? nullptr : p; // no repetition of the token "int" - - if (!cont && !IsOfType(p, ID_INT)) return nullptr; - - CBotInt* inst = static_cast(CompileArray(p, pStack, CBotTypInt)); - if (inst != nullptr || !pStack->IsOk()) return inst; - - CBotCStack* pStk = pStack->TokenStack(pp); - - inst = new CBotInt(); - - inst->m_expr = nullptr; - - CBotToken* vartoken = p; - inst->SetToken(vartoken); - - // determines the expression is valid for the item on the left side - if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) - { - (static_cast(inst->m_var))->m_typevar = CBotTypInt; - if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable - { - pStk->SetError(TX_REDEFVAR, vartoken); - goto error; - } - - if (IsOfType(p, ID_OPBRK)) - { - delete inst; // type is not CBotInt - p = vartoken; // returns the variable name - - // compiles an array declaration - - CBotInstr* inst2 = CBotInstArray::Compile(p, pStk, CBotTypInt); - - if (!pStk->IsOk() ) - { - pStk->SetError(TX_CLBRK, p->GetStart()); - goto error; - } - - if (IsOfType(p, ID_COMMA)) // several definition chained - { - if (nullptr != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile the next one - { - return pStack->Return(inst2, pStk); - } - } - inst = static_cast(inst2); - goto suite; // no assignment, variable already created - } - - if (IsOfType(p, ID_ASS)) // with an assignment? - { - if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) - { - goto error; - } - if (pStk->GetType() >= CBotTypBoolean) // compatible type ? - { - pStk->SetError(TX_BADTYPE, p->GetStart()); - goto error; - } - } - - { - CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// create the variable (evaluated after the assignment) - var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); // if initialized with assignment - var->SetUniqNum( //set it with a unique number - (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); - pStack->AddVar(var); // place it on the stack - } - - if (IsOfType(p, ID_COMMA)) // chained several definitions - { - if (nullptr != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile next one - { - return pStack->Return(inst, pStk); - } - } -suite: - if (noskip || IsOfType(p, ID_SEP)) // instruction is completed - { - return pStack->Return(inst, pStk); - } - - pStk->SetError(TX_ENDOF, p->GetStart()); - } - -error: - delete inst; - return pStack->Return(nullptr, pStk); -} - -// execute the definition of the integer variable - -bool CBotInt::Execute(CBotStack* &pj) -{ - CBotStack* pile = pj->AddStack(this); // essential for SetState() - - if ( pile->GetState()==0) - { - if (m_expr && !m_expr->Execute(pile)) return false; // initial value // interrupted? - m_var->Execute(pile); // creates and assign the result - - if (!pile->SetState(1)) return false; - } - - if (pile->IfStep()) return false; - - if ( m_next2b && - !m_next2b->Execute(pile)) return false; // other(s) definition(s) - - return pj->Return(pile); // forward below -} - -void CBotInt::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotStack* pile = pj; - if (bMain) - { - pile = pj->RestoreStack(this); - if (pile == nullptr) return; - - if ( pile->GetState()==0) - { - if (m_expr) m_expr->RestoreState(pile, bMain); // initial value // interrupted? - return; - } - } - - m_var->RestoreState(pile, bMain); - - if (m_next2b) m_next2b->RestoreState(pile, bMain); // other(s) definition(s) -} - ////////////////////////////////////////////////////////////////////////////////////////// // compile a list of parameters diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 4ccd6a90..db9c1098 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -467,24 +467,6 @@ public: bool IsOfClass(CBotString name); }; -// definition of an integer - -class CBotInt : public CBotInstr -{ -private: - CBotInstr* m_var; // the variable to initialize - CBotInstr* m_expr; // a value to put, if there is -/// CBotInstr* m_next; // several definitions chained - -public: - CBotInt(); - ~CBotInt(); - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); - bool Execute(CBotStack* &pj) override; - void RestoreState(CBotStack* &pj, bool bMain) override; -}; - #define MAX(a,b) ((a>b) ? a : b) diff --git a/src/CBot/CBotInstr/CBotInt.cpp b/src/CBot/CBotInstr/CBotInt.cpp new file mode 100644 index 00000000..ed4dbdd4 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInt.cpp @@ -0,0 +1,185 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotInt.h" + +#include "CBotLeftExprVar.h" +#include "CBotInstArray.h" +#include "CBotTwoOpExpr.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotInt::CBotInt() +{ + m_next = nullptr; // for multiple definitions + m_var = + m_expr = nullptr; + name = "CBotInt"; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInt::~CBotInt() +{ + delete m_var; + delete m_expr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInt::Compile(CBotToken* &p, CBotCStack* pStack, bool cont, bool noskip) +{ + CBotToken* pp = cont ? nullptr : p; // no repetition of the token "int" + + if (!cont && !IsOfType(p, ID_INT)) return nullptr; + + CBotInt* inst = static_cast(CompileArray(p, pStack, CBotTypInt)); + if (inst != nullptr || !pStack->IsOk()) return inst; + + CBotCStack* pStk = pStack->TokenStack(pp); + + inst = new CBotInt(); + + inst->m_expr = nullptr; + + CBotToken* vartoken = p; + inst->SetToken(vartoken); + + // determines the expression is valid for the item on the left side + if (nullptr != (inst->m_var = CBotLeftExprVar::Compile( p, pStk ))) + { + (static_cast(inst->m_var))->m_typevar = CBotTypInt; + if (pStk->CheckVarLocal(vartoken)) // redefinition of the variable + { + pStk->SetError(TX_REDEFVAR, vartoken); + goto error; + } + + if (IsOfType(p, ID_OPBRK)) + { + delete inst; // type is not CBotInt + p = vartoken; // returns the variable name + + // compiles an array declaration + + CBotInstr* inst2 = CBotInstArray::Compile(p, pStk, CBotTypInt); + + if (!pStk->IsOk() ) + { + pStk->SetError(TX_CLBRK, p->GetStart()); + goto error; + } + + if (IsOfType(p, ID_COMMA)) // several definition chained + { + if (nullptr != ( inst2->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile the next one + { + return pStack->Return(inst2, pStk); + } + } + inst = static_cast(inst2); + goto suite; // no assignment, variable already created + } + + if (IsOfType(p, ID_ASS)) // with an assignment? + { + if (nullptr == ( inst->m_expr = CBotTwoOpExpr::Compile( p, pStk ))) + { + goto error; + } + if (pStk->GetType() >= CBotTypBoolean) // compatible type ? + { + pStk->SetError(TX_BADTYPE, p->GetStart()); + goto error; + } + } + + { + CBotVar* var = CBotVar::Create(vartoken, CBotTypInt);// create the variable (evaluated after the assignment) + var->SetInit(inst->m_expr != nullptr ? CBotVar::InitType::DEF : CBotVar::InitType::UNDEF); // if initialized with assignment + var->SetUniqNum( //set it with a unique number + (static_cast(inst->m_var))->m_nIdent = CBotVar::NextUniqNum()); + pStack->AddVar(var); // place it on the stack + } + + if (IsOfType(p, ID_COMMA)) // chained several definitions + { + if (nullptr != ( inst->m_next2b = CBotInt::Compile(p, pStk, true, noskip))) // compile next one + { + return pStack->Return(inst, pStk); + } + } +suite: + if (noskip || IsOfType(p, ID_SEP)) // instruction is completed + { + return pStack->Return(inst, pStk); + } + + pStk->SetError(TX_ENDOF, p->GetStart()); + } + +error: + delete inst; + return pStack->Return(nullptr, pStk); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInt::Execute(CBotStack* &pj) +{ + CBotStack* pile = pj->AddStack(this); // essential for SetState() + + if ( pile->GetState()==0) + { + if (m_expr && !m_expr->Execute(pile)) return false; // initial value // interrupted? + m_var->Execute(pile); // creates and assign the result + + if (!pile->SetState(1)) return false; + } + + if (pile->IfStep()) return false; + + if ( m_next2b && + !m_next2b->Execute(pile)) return false; // other(s) definition(s) + + return pj->Return(pile); // forward below +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInt::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotStack* pile = pj; + if (bMain) + { + pile = pj->RestoreStack(this); + if (pile == nullptr) return; + + if ( pile->GetState()==0) + { + if (m_expr) m_expr->RestoreState(pile, bMain); // initial value // interrupted? + return; + } + } + + m_var->RestoreState(pile, bMain); + + if (m_next2b) m_next2b->RestoreState(pile, bMain); // other(s) definition(s) +} diff --git a/src/CBot/CBotInstr/CBotInt.h b/src/CBot/CBotInstr/CBotInt.h new file mode 100644 index 00000000..702f5290 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInt.h @@ -0,0 +1,78 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotInt class Definition of an integer variable + * int a, b = 12; + */ +class CBotInt : public CBotInstr +{ +public: + + /*! + * \brief CBotInt + */ + CBotInt(); + + /*! + * \brief ~CBotInt + */ + ~CBotInt(); + + /*! + * \brief Compile + * \param p + * \param pStack + * \param cont + * \param noskip + * \return + */ + static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, bool cont = false, bool noskip = false); + + /*! + * \brief Execute Execute the definition of the integer variable. + * \param pj + * \return + */ + bool Execute(CBotStack* &pj) override; + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain) override; + +private: + + //! The variable to initialize. + CBotInstr* m_var; + //! A value to put, if there is. + CBotInstr* m_expr; +}; diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index a6a58e5e..1c45ec1b 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -23,6 +23,7 @@ #include "CBotIString.h" #include "CBotFloat.h" #include "CBotBoolean.h" +#include "CBotInt.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index baf7b8a1..484d023c 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -50,6 +50,7 @@ set(SOURCES CBotInstr/CBotIf.cpp CBotInstr/CBotListArray.cpp CBotInstr/CBotInstArray.cpp + CBotInstr/CBotInt.cpp ) # Includes From e54d8f1ebc6baa2d2eedd3e5a4fe9954554c8d8b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sat, 14 Nov 2015 12:56:16 +0100 Subject: [PATCH 47/91] Moving CBotStack class in its own header and source files. --- src/CBot/CBot.cpp | 2 + src/CBot/CBot.h | 247 -------------------- src/CBot/CBotFunction.cpp | 2 + src/CBot/CBotInstr/CBotBoolean.cpp | 2 + src/CBot/CBotInstr/CBotBreak.cpp | 2 + src/CBot/CBotInstr/CBotCase.cpp | 2 + src/CBot/CBotInstr/CBotCatch.cpp | 2 + src/CBot/CBotInstr/CBotClassInst.cpp | 2 + src/CBot/CBotInstr/CBotDo.cpp | 2 + src/CBot/CBotInstr/CBotEmpty.cpp | 2 + src/CBot/CBotInstr/CBotExprAlpha.cpp | 2 + src/CBot/CBotInstr/CBotExprBool.cpp | 2 + src/CBot/CBotInstr/CBotExprNan.cpp | 2 + src/CBot/CBotInstr/CBotExprNull.cpp | 2 + src/CBot/CBotInstr/CBotExprNum.cpp | 2 + src/CBot/CBotInstr/CBotExprUnaire.cpp | 2 + src/CBot/CBotInstr/CBotExprVar.cpp | 2 + src/CBot/CBotInstr/CBotExpression.cpp | 2 + src/CBot/CBotInstr/CBotFieldExpr.cpp | 2 + src/CBot/CBotInstr/CBotFloat.cpp | 2 + src/CBot/CBotInstr/CBotFor.cpp | 2 + src/CBot/CBotInstr/CBotIString.cpp | 2 + src/CBot/CBotInstr/CBotIf.cpp | 2 + src/CBot/CBotInstr/CBotIndexExpr.cpp | 2 + src/CBot/CBotInstr/CBotInstArray.cpp | 2 + src/CBot/CBotInstr/CBotInstrCall.cpp | 2 + src/CBot/CBotInstr/CBotInstrMethode.cpp | 2 + src/CBot/CBotInstr/CBotInt.cpp | 2 + src/CBot/CBotInstr/CBotLeftExpr.cpp | 2 + src/CBot/CBotInstr/CBotLeftExprVar.cpp | 2 + src/CBot/CBotInstr/CBotListArray.cpp | 2 + src/CBot/CBotInstr/CBotListExpression.cpp | 2 + src/CBot/CBotInstr/CBotListInstr.cpp | 2 + src/CBot/CBotInstr/CBotLogicExpr.cpp | 2 + src/CBot/CBotInstr/CBotNew.cpp | 2 + src/CBot/CBotInstr/CBotParExpr.h | 2 + src/CBot/CBotInstr/CBotPostIncExpr.cpp | 2 + src/CBot/CBotInstr/CBotPreIncExpr.cpp | 2 + src/CBot/CBotInstr/CBotReturn.cpp | 2 + src/CBot/CBotInstr/CBotSwitch.cpp | 2 + src/CBot/CBotInstr/CBotThrow.cpp | 2 + src/CBot/CBotInstr/CBotTry.cpp | 2 + src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 3 +- src/CBot/CBotInstr/CBotWhile.cpp | 2 + src/CBot/CBotProgram.cpp | 3 + src/CBot/CBotStack.cpp | 145 ++++++++---- src/CBot/CBotStack.h | 270 ++++++++++++++++++++++ src/CBot/CBotVar.cpp | 2 + 48 files changed, 463 insertions(+), 291 deletions(-) create mode 100644 src/CBot/CBotStack.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index a7b8cc40..30f22461 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -72,6 +72,8 @@ #include "CBotInstr/CBotInstArray.h" #include "CBotInstr/CBotInt.h" +#include "CBotStack.h" + // Local include diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index db9c1098..15c870e4 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -53,254 +53,7 @@ class CBotDefParam; // paramerer list of a function -//////////////////////////////////////////////////////////////////////// -// Management of the execution stack -//////////////////////////////////////////////////////////////////////// -// actually, externally, the only thing it can do -// is to create an instance of a stack -// to use for routine CBotProgram :: Execute (CBotStack) - - -/**\class CBotStack - * \brief Management of the execution stack. - * \brief Actually the only thing it can do is to create an instance of a stack - * \brief to use for routine CBotProgram :: Execute(CBotStack)*/ -class CBotStack -{ -public: -#if STACKMEM - /** - * \brief FirstStack Allocate first stack - * \return pointer to created stack - */ - static CBotStack * FirstStack(); - - /** \brief Delete Remove current stack */ - void Delete(); -#endif - - /** - * \brief CBotStack Constructor of the stack - * \param ppapa Not used. - */ - CBotStack(CBotStack* ppapa); - - - /** \brief ~CBotStack Destructor */ - ~CBotStack(); - - /** - * \brief StackOver Check if end of stack is reached - * \return true if end of stack - */ - bool StackOver(); - - /** - * \brief GetError Get error number of the stack - * \param [out] start beginning of the stack - * \param [out] end end of stack - * \return error number - */ - int GetError(int& start, int& end); - - /** - * \brief GetError Get error number - * \return eror number - */ - int GetError();// rend le numéro d'erreur retourné - - /** - * \brief Reset Reset error at and set user - * \param [in] pUser User of stack - */ - void Reset(void* pUser); - - /** - * \brief SetType Determines the type. - * \param type Type of instruction on the stack. - */ - void SetType(CBotTypResult& type); - - /** - * \brief GetType Get the type of value on the stack. - * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). - * \return Type number. - */ - int GetType(int mode = 0); - - /** - * \brief Getes the type of complete value on the stack. - * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). - * \return Type of an element. - */ - CBotTypResult GetTypResult(int mode = 0); - - /** - * \brief Adds a local variable. - * \param [in] p Variable to be added. - */ - void AddVar(CBotVar* p); - - /** - * \brief Fetch a variable by its token. - * \brief This may be a composite variable - * \param [in] pToken Token upon which search is performed - * \param [in] bUpdate Not used. Probably need to be removed - * \param [in] bModif Not used. Probably need to be removed - * \return Found variable - */ - CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false, - bool bModif = false); - - /** - * \brief Fetch a variable by its token. - * \brief This may be a composite variable - * \param [in] pToken Token upon which search is performed - * \param [in] bUpdate Not used. Probably need to be removed - * \param [in] bModif Not used. Probably need to be removed - * \return Found variable - */ - CBotVar* FindVar(CBotToken& pToken, bool bUpdate = false, - bool bModif = false); - - /** - * \brief Fetch variable by its name - * \param [in] name Name of variable to find - * \return Found variable - */ - CBotVar* FindVar(const char* name); - - /** - * \brief Fetch a variable on the stack according to its identification number - * \brief This is faster than comparing names - * \param [in] ident Identifier of a variable - * \param [in] bUpdate Not used. Probably need to be removed - * \param [in] bModif Not used. Probably need to be removed - * \return Found variable - */ - CBotVar* FindVar(long ident, bool bUpdate = false, - bool bModif = false); - - /** - * \brief Find variable by its token and returns a copy of it. - * \param Token Token upon which search is performed - * \param bUpdate Not used. - * \return Found variable, nullptr if not found - */ - CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); - - - CBotStack* AddStack(CBotInstr* instr = nullptr, bool bBlock = false); // extends the stack - CBotStack* AddStackEOX(CBotCall* instr = nullptr, bool bBlock = false); // extends the stack - CBotStack* RestoreStack(CBotInstr* instr = nullptr); - CBotStack* RestoreStackEOX(CBotCall* instr = nullptr); - - CBotStack* AddStack2(bool bBlock = false); // extends the stack - bool Return(CBotStack* pFils); // transmits the result over - bool ReturnKeep(CBotStack* pFils); // transmits the result without reducing the stack - bool BreakReturn(CBotStack* pfils, const char* name = nullptr); - // in case of eventual break - bool IfContinue(int state, const char* name); - // or "continue" - - bool IsOk(); - - bool SetState(int n, int lim = -10); // select a state - int GetState(); // in what state am I? - bool IncState(int lim = -10); // passes to the next state - bool IfStep(); // do step by step - bool Execute(); - - void SetVar( CBotVar* var ); - void SetCopyVar( CBotVar* var ); - CBotVar* GetVar(); - CBotVar* GetCopyVar(); - CBotVar* GetPtVar(); - bool GetRetVar(bool bRet); - long GetVal(); - - void SetError(int n, CBotToken* token = nullptr); - void SetPosError(CBotToken* token); - void ResetError(int n, int start, int end); - void SetBreak(int val, const char* name); - - void SetBotCall(CBotProgram* p); - CBotProgram* GetBotCall(bool bFirst = false); - void* GetPUser(); - bool GetBlock(); - - - bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); - void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); - - bool SaveState(FILE* pf); - bool RestoreState(FILE* pf, CBotStack* &pStack); - - static - void SetTimer(int n); - - void GetRunPos(const char* &FunctionName, int &start, int &end); - CBotVar* GetStackVars(const char* &FunctionName, int level); - - int m_temp; - -private: - CBotStack* m_next; - CBotStack* m_next2; - CBotStack* m_prev; - friend class CBotInstArray; - -#ifdef _DEBUG - int m_index; -#endif - int m_state; - int m_step; - static int m_error; - static int m_start; - static int m_end; - static - CBotVar* m_retvar; // result of a return - - CBotVar* m_var; // result of the operations - CBotVar* m_listVar; // variables declared at this level - - bool m_bBlock; // is part of a block (variables are local to this block) - bool m_bOver; // stack limits? -// bool m_bDontDelete; // special, not to destroy the variable during delete - CBotProgram* m_prog; // user-defined functions - - static - int m_initimer; - static - int m_timer; - static - CBotString m_labelBreak; - static - void* m_pUser; - - CBotInstr* m_instr; // the corresponding instruction - bool m_bFunc; // an input of a function? - CBotCall* m_call; // recovery point in a extern call - friend class CBotTry; -}; - -// inline routinees must be declared in file.h - -inline bool CBotStack::IsOk() -{ - return (m_error == 0); -} - -inline int CBotStack::GetState() -{ - return m_state; -} - -inline int CBotStack::GetError() -{ - return m_error; -} //////////////////////////////////////////////////////////////////////// // Management of the stack of compilation diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 88bfabd8..64b85fdc 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -29,6 +29,8 @@ #include "CBotInstr/CBotEmpty.h" #include "CBotInstr/CBotListArray.h" +#include "CBotStack.h" + #include diff --git a/src/CBot/CBotInstr/CBotBoolean.cpp b/src/CBot/CBotInstr/CBotBoolean.cpp index e2d2ad2d..6fda2dbf 100644 --- a/src/CBot/CBotInstr/CBotBoolean.cpp +++ b/src/CBot/CBotInstr/CBotBoolean.cpp @@ -23,6 +23,8 @@ #include "CBotTwoOpExpr.h" #include "CBotInstArray.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBreak.cpp b/src/CBot/CBotInstr/CBotBreak.cpp index 06f53ae2..5879f0f8 100644 --- a/src/CBot/CBotInstr/CBotBreak.cpp +++ b/src/CBot/CBotInstr/CBotBreak.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotBreak.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCase.cpp b/src/CBot/CBotInstr/CBotCase.cpp index a2b68c4f..f188f8ed 100644 --- a/src/CBot/CBotInstr/CBotCase.cpp +++ b/src/CBot/CBotInstr/CBotCase.cpp @@ -21,6 +21,8 @@ #include "CBotCase.h" #include "CBotExprNum.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index e4d9bb4c..77b5294d 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -22,6 +22,8 @@ #include "CBotBlock.h" #include "CBotExpression.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index dfbd47b2..e02328da 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -23,6 +23,8 @@ #include "CBotTwoOpExpr.h" #include "CBotInstArray.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp index 3bf3aefd..9cfeddb3 100644 --- a/src/CBot/CBotInstr/CBotDo.cpp +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -22,6 +22,8 @@ #include "CBotBlock.h" #include "CBotCondition.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotEmpty.cpp b/src/CBot/CBotInstr/CBotEmpty.cpp index 4f3cb5ae..0f0ad7ce 100644 --- a/src/CBot/CBotInstr/CBotEmpty.cpp +++ b/src/CBot/CBotInstr/CBotEmpty.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotEmpty.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.cpp b/src/CBot/CBotInstr/CBotExprAlpha.cpp index c412ac02..d0edb1d2 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.cpp +++ b/src/CBot/CBotInstr/CBotExprAlpha.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotExprAlpha.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprBool.cpp b/src/CBot/CBotInstr/CBotExprBool.cpp index 17c09e55..3a252676 100644 --- a/src/CBot/CBotInstr/CBotExprBool.cpp +++ b/src/CBot/CBotInstr/CBotExprBool.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotExprBool.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNan.cpp b/src/CBot/CBotInstr/CBotExprNan.cpp index 3eebb74a..7335219b 100644 --- a/src/CBot/CBotInstr/CBotExprNan.cpp +++ b/src/CBot/CBotInstr/CBotExprNan.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotExprNan.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNull.cpp b/src/CBot/CBotInstr/CBotExprNull.cpp index 0324d50b..d80ef035 100644 --- a/src/CBot/CBotInstr/CBotExprNull.cpp +++ b/src/CBot/CBotInstr/CBotExprNull.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotExprNull.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp index 61cf3eaa..24ba0ac7 100644 --- a/src/CBot/CBotInstr/CBotExprNum.cpp +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotExprNum.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprUnaire.cpp b/src/CBot/CBotInstr/CBotExprUnaire.cpp index e1a18e7f..2b7c0d6e 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.cpp +++ b/src/CBot/CBotInstr/CBotExprUnaire.cpp @@ -21,6 +21,8 @@ #include "CBotExprUnaire.h" #include "CBotParExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 3c03da91..0e531fae 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -24,6 +24,8 @@ #include "CBotIndexExpr.h" #include "CBotFieldExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp index cb47103b..3637049b 100644 --- a/src/CBot/CBotInstr/CBotExpression.cpp +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -21,6 +21,8 @@ #include "CBotExpression.h" #include "CBotTwoOpExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.cpp b/src/CBot/CBotInstr/CBotFieldExpr.cpp index 6453bc3c..ae8e583d 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.cpp +++ b/src/CBot/CBotInstr/CBotFieldExpr.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotFieldExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFloat.cpp b/src/CBot/CBotInstr/CBotFloat.cpp index acc29ff9..70a2c97b 100644 --- a/src/CBot/CBotInstr/CBotFloat.cpp +++ b/src/CBot/CBotInstr/CBotFloat.cpp @@ -23,6 +23,8 @@ #include "CBotTwoOpExpr.h" #include "CBotInstArray.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index d83650cb..7cbca510 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -23,6 +23,8 @@ #include "CBotBlock.h" #include "CBotBoolExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIString.cpp b/src/CBot/CBotInstr/CBotIString.cpp index b0e8a6a3..91e1a925 100644 --- a/src/CBot/CBotInstr/CBotIString.cpp +++ b/src/CBot/CBotInstr/CBotIString.cpp @@ -22,6 +22,8 @@ #include "CBotLeftExprVar.h" #include "CBotTwoOpExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIf.cpp b/src/CBot/CBotInstr/CBotIf.cpp index 2ae5acf1..b0cdbc88 100644 --- a/src/CBot/CBotInstr/CBotIf.cpp +++ b/src/CBot/CBotInstr/CBotIf.cpp @@ -22,6 +22,8 @@ #include "CBotInstr/CBotBlock.h" #include "CBotInstr/CBotCondition.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.cpp b/src/CBot/CBotInstr/CBotIndexExpr.cpp index 2d3d6d2a..f4882c00 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.cpp +++ b/src/CBot/CBotInstr/CBotIndexExpr.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotIndexExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstArray.cpp b/src/CBot/CBotInstr/CBotInstArray.cpp index b97e2b54..e1327a58 100644 --- a/src/CBot/CBotInstr/CBotInstArray.cpp +++ b/src/CBot/CBotInstr/CBotInstArray.cpp @@ -25,6 +25,8 @@ #include "CBotListArray.h" #include "CBotEmpty.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp index 615f041a..cec9810b 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.cpp +++ b/src/CBot/CBotInstr/CBotInstrCall.cpp @@ -21,6 +21,8 @@ #include "CBotInstrCall.h" #include "CBotExpression.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index e35c0f25..1ec99067 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotInstrMethode.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInt.cpp b/src/CBot/CBotInstr/CBotInt.cpp index ed4dbdd4..f94d53c5 100644 --- a/src/CBot/CBotInstr/CBotInt.cpp +++ b/src/CBot/CBotInstr/CBotInt.cpp @@ -24,6 +24,8 @@ #include "CBotInstArray.h" #include "CBotTwoOpExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp index 66e0dce5..84c7b735 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.cpp +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -23,6 +23,8 @@ #include "CBotIndexExpr.h" #include "CBotExpression.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.cpp b/src/CBot/CBotInstr/CBotLeftExprVar.cpp index 43ed496d..ff0bbf15 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.cpp +++ b/src/CBot/CBotInstr/CBotLeftExprVar.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotLeftExprVar.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp index 8ab32079..dc6e2b29 100644 --- a/src/CBot/CBotInstr/CBotListArray.cpp +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -23,6 +23,8 @@ #include "CBotExprNull.h" #include "CBotTwoOpExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index 1c45ec1b..afa5f877 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -25,6 +25,8 @@ #include "CBotBoolean.h" #include "CBotInt.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp index b2fcf49c..fde2103c 100644 --- a/src/CBot/CBotInstr/CBotListInstr.cpp +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -21,6 +21,8 @@ #include "CBotListInstr.h" #include "CBotBlock.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLogicExpr.cpp b/src/CBot/CBotInstr/CBotLogicExpr.cpp index bfa6af1a..d332d18f 100644 --- a/src/CBot/CBotInstr/CBotLogicExpr.cpp +++ b/src/CBot/CBotInstr/CBotLogicExpr.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotLogicExpr.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp index d89a8ca7..fef5c074 100644 --- a/src/CBot/CBotInstr/CBotNew.cpp +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotNew.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotParExpr.h b/src/CBot/CBotInstr/CBotParExpr.h index de0df458..60c09933 100644 --- a/src/CBot/CBotInstr/CBotParExpr.h +++ b/src/CBot/CBotInstr/CBotParExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.cpp b/src/CBot/CBotInstr/CBotPostIncExpr.cpp index cb146ca4..f7fbddab 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPostIncExpr.cpp @@ -21,6 +21,8 @@ #include "CBotPostIncExpr.h" #include "CBotExprVar.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.cpp b/src/CBot/CBotInstr/CBotPreIncExpr.cpp index 073e0d4a..5fe3d365 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPreIncExpr.cpp @@ -21,6 +21,8 @@ #include "CBotPreIncExpr.h" #include "CBotExprVar.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotReturn.cpp b/src/CBot/CBotInstr/CBotReturn.cpp index d64326c9..3837b26c 100644 --- a/src/CBot/CBotInstr/CBotReturn.cpp +++ b/src/CBot/CBotInstr/CBotReturn.cpp @@ -21,6 +21,8 @@ #include "CBotReturn.h" #include "CBotExpression.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index 45558dc1..0a527d2f 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -25,6 +25,8 @@ #include "CBotBlock.h" #include "CBotExpression.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotThrow.cpp b/src/CBot/CBotInstr/CBotThrow.cpp index a4a1831e..c02f2214 100644 --- a/src/CBot/CBotInstr/CBotThrow.cpp +++ b/src/CBot/CBotInstr/CBotThrow.cpp @@ -21,6 +21,8 @@ #include "CBotThrow.h" #include "CBotExpression.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotTry.cpp b/src/CBot/CBotInstr/CBotTry.cpp index 89b689ea..2ac2ae64 100644 --- a/src/CBot/CBotInstr/CBotTry.cpp +++ b/src/CBot/CBotInstr/CBotTry.cpp @@ -21,6 +21,8 @@ #include "CBotTry.h" #include "CBotBlock.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index 275730a6..816efefb 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -22,7 +22,8 @@ #include "CBotParExpr.h" #include "CBotLogicExpr.h" #include "CBotExpression.h" -#include "CBot.h" + +#include "CBotStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp index c24084b6..bfc2061f 100644 --- a/src/CBot/CBotInstr/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -22,6 +22,8 @@ #include "CBotBlock.h" #include "CBotCondition.h" +#include "CBotStack.h" + // Local include // Global include diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index cda190ce..f1862c1e 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -21,6 +21,9 @@ // database management of CBoT program #include "CBot.h" + +#include "CBotStack.h" + #include CBotProgram::CBotProgram() diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index bbc01d8b..b1715e15 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -17,11 +17,12 @@ * along with this program. If not, see http://gnu.org/licenses */ -//Management of the stack +// Modules inlcude +#include "CBotStack.h" +// Local include -#include "CBot.h" - +// Global include #include #include #include @@ -29,10 +30,6 @@ #define ITIMER 100 -//////////////////////////////////////////////////////////////////////////// -// management of a execution of a stack -//////////////////////////////////////////////////////////////////////////// - int CBotStack::m_initimer = ITIMER; int CBotStack::m_timer = 0; CBotVar* CBotStack::m_retvar = nullptr; @@ -44,6 +41,7 @@ void* CBotStack::m_pUser = nullptr; #if STACKMEM +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::FirstStack() { CBotStack* p; @@ -82,17 +80,20 @@ CBotStack* CBotStack::FirstStack() return p; } +//////////////////////////////////////////////////////////////////////////////// CBotStack::CBotStack(CBotStack* ppapa) { // constructor must exist or the destructor is never called! assert(0); } +//////////////////////////////////////////////////////////////////////////////// CBotStack::~CBotStack() { assert(0); // use Delete () instead } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::Delete() { if ( this == nullptr || this == EOX ) return; @@ -129,8 +130,8 @@ void CBotStack::Delete() free( this ); } - // routine improved +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) { if (m_next != nullptr) @@ -163,6 +164,7 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) return p; } +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) { if (m_next != nullptr) @@ -180,6 +182,7 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) return p; } +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::AddStack2(bool bBlock) { if (m_next2 != nullptr) @@ -203,11 +206,13 @@ CBotStack* CBotStack::AddStack2(bool bBlock) return p; } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::GetBlock() { return m_bBlock; } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::Return(CBotStack* pfils) { if ( pfils == this ) return true; // special @@ -222,6 +227,7 @@ bool CBotStack::Return(CBotStack* pfils) return (m_error == 0); // interrupted if error } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::ReturnKeep(CBotStack* pfils) { if ( pfils == this ) return true; // special @@ -233,6 +239,7 @@ bool CBotStack::ReturnKeep(CBotStack* pfils) return (m_error == 0); // interrupted if error } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::StackOver() { if (!m_bOver) return false; @@ -242,6 +249,7 @@ bool CBotStack::StackOver() #else +//////////////////////////////////////////////////////////////////////////////// CBotStack::CBotStack(CBotStack* ppapa) { m_next = nullptr; @@ -265,6 +273,7 @@ CBotStack::CBotStack(CBotStack* ppapa) m_bFunc = false; } +//////////////////////////////////////////////////////////////////////////////// // destructor CBotStack::~CBotStack() { @@ -277,6 +286,7 @@ CBotStack::~CBotStack() if ( !m_bDontDelete ) delete m_listVar; } +//////////////////////////////////////////////////////////////////////////////// // \TODO routine has/to optimize CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) { @@ -293,6 +303,7 @@ CBotStack* CBotStack::AddStack(CBotInstr* instr, bool bBlock) return p; } +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) { if (m_next != nullptr) @@ -314,6 +325,7 @@ CBotStack* CBotStack::AddStackEOX(CBotCall* instr, bool bBlock) return p; } +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::AddStack2(bool bBlock) { if (m_next2 != nullptr) @@ -331,6 +343,7 @@ CBotStack* CBotStack::AddStack2(bool bBlock) return p; } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::Return(CBotStack* pfils) { if ( pfils == this ) return true; // special @@ -345,6 +358,7 @@ bool CBotStack::Return(CBotStack* pfils) return (m_error == 0); // interrupted if error } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::StackOver() { return false; // no overflow check in this version @@ -352,6 +366,7 @@ bool CBotStack::StackOver() #endif +//////////////////////////////////////////////////////////////////////////////// void CBotStack::Reset(void* pUser) { m_timer = m_initimer; // resets the timer @@ -362,9 +377,7 @@ void CBotStack::Reset(void* pUser) m_pUser = pUser; } - - - +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::RestoreStack(CBotInstr* instr) { if (m_next != nullptr) @@ -376,6 +389,7 @@ CBotStack* CBotStack::RestoreStack(CBotInstr* instr) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) { CBotStack* p = RestoreStack(); @@ -383,8 +397,7 @@ CBotStack* CBotStack::RestoreStackEOX(CBotCall* instr) return p; } - - +//////////////////////////////////////////////////////////////////////////////// // routine for execution step by step bool CBotStack::IfStep() { @@ -392,7 +405,7 @@ bool CBotStack::IfStep() return true; } - +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::BreakReturn(CBotStack* pfils, const char* name) { if ( m_error>=0 ) return false; // normal output @@ -406,6 +419,7 @@ bool CBotStack::BreakReturn(CBotStack* pfils, const char* name) return Return(pfils); } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::IfContinue(int state, const char* name) { if ( m_error != -2 ) return false; @@ -420,6 +434,7 @@ bool CBotStack::IfContinue(int state, const char* name) return true; } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetBreak(int val, const char* name) { m_error = -val; // reacts as an Exception @@ -432,7 +447,7 @@ void CBotStack::SetBreak(int val, const char* name) } // gives on the stack value calculated by the last CBotReturn - +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::GetRetVar(bool bRet) { if (m_error == -3) @@ -446,6 +461,7 @@ bool CBotStack::GetRetVar(bool bRet) return bRet; // interrupted by something other than return } +//////////////////////////////////////////////////////////////////////////////// int CBotStack::GetError(int& start, int& end) { start = m_start; @@ -453,26 +469,28 @@ int CBotStack::GetError(int& start, int& end) return m_error; } - +//////////////////////////////////////////////////////////////////////////////// int CBotStack::GetType(int mode) { if (m_var == nullptr) return -1; return m_var->GetType(mode); } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotStack::GetTypResult(int mode) { if (m_var == nullptr) return -1; return m_var->GetTypResult(mode); } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetType(CBotTypResult& type) { if (m_var == nullptr) return; m_var->SetType( type ); } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif) { CBotStack* p = this; @@ -497,6 +515,7 @@ CBotVar* CBotStack::FindVar(CBotToken* &pToken, bool bUpdate, bool bModif) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::FindVar(const char* name) { CBotStack* p = this; @@ -516,6 +535,7 @@ CBotVar* CBotStack::FindVar(const char* name) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif) { CBotStack* p = this; @@ -538,14 +558,14 @@ CBotVar* CBotStack::FindVar(long ident, bool bUpdate, bool bModif) return nullptr; } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::FindVar(CBotToken& pToken, bool bUpdate, bool bModif) { CBotToken* pt = &pToken; return FindVar(pt, bUpdate, bModif); } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) { CBotVar* pVar = FindVar( Token, bUpdate ); @@ -557,7 +577,7 @@ CBotVar* CBotStack::CopyVar(CBotToken& Token, bool bUpdate) return pCopy; } - +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::SetState(int n, int limite) { m_state = n; @@ -566,6 +586,7 @@ bool CBotStack::SetState(int n, int limite) return ( m_timer > limite ); // interrupted if timer pass } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::IncState(int limite) { m_state++; @@ -574,7 +595,7 @@ bool CBotStack::IncState(int limite) return ( m_timer > limite ); // interrupted if timer pass } - +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetError(int n, CBotToken* token) { if ( n!= 0 && m_error != 0) return; // does not change existing error @@ -586,6 +607,7 @@ void CBotStack::SetError(int n, CBotToken* token) } } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::ResetError(int n, int start, int end) { m_error = n; @@ -593,17 +615,20 @@ void CBotStack::ResetError(int n, int start, int end) m_end = end; } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetPosError(CBotToken* token) { m_start = token->GetStart(); m_end = token->GetEnd(); } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetTimer(int n) { m_initimer = n; } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::Execute() { CBotCall* instr = nullptr; // the most highest instruction @@ -637,6 +662,7 @@ bool CBotStack::Execute() } // puts on the stack pointer to a variable +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetVar( CBotVar* var ) { if (m_var) delete m_var; // replacement of a variable @@ -644,6 +670,7 @@ void CBotStack::SetVar( CBotVar* var ) } // puts on the stack a copy of a variable +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetCopyVar( CBotVar* var ) { if (m_var) delete m_var; // replacement of a variable @@ -652,11 +679,13 @@ void CBotStack::SetCopyVar( CBotVar* var ) m_var->Copy( var ); } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::GetVar() { return m_var; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::GetPtVar() { CBotVar* p = m_var; @@ -664,6 +693,7 @@ CBotVar* CBotStack::GetPtVar() return p; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::GetCopyVar() { if (m_var == nullptr) return nullptr; @@ -672,15 +702,14 @@ CBotVar* CBotStack::GetCopyVar() return v; } +//////////////////////////////////////////////////////////////////////////////// long CBotStack::GetVal() { if (m_var == nullptr) return 0; return m_var->GetValInt(); } - - - +//////////////////////////////////////////////////////////////////////////////// void CBotStack::AddVar(CBotVar* pVar) { CBotStack* p = this; @@ -702,19 +731,14 @@ void CBotStack::AddVar(CBotVar* pVar) #endif } -/*void CBotStack::RestoreVar(CBotVar* pVar) -{ - if ( !m_bDontDelete ) __asm int 3; - delete m_listVar; - m_listVar = pVar; // direct replacement -}*/ - +//////////////////////////////////////////////////////////////////////////////// void CBotStack::SetBotCall(CBotProgram* p) { m_prog = p; m_bFunc = true; } +//////////////////////////////////////////////////////////////////////////////// CBotProgram* CBotStack::GetBotCall(bool bFirst) { if ( ! bFirst ) return m_prog; @@ -723,12 +747,13 @@ CBotProgram* CBotStack::GetBotCall(bool bFirst) return p->m_prog; } +//////////////////////////////////////////////////////////////////////////////// void* CBotStack::GetPUser() { return m_pUser; } - +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype) { CBotTypResult res; @@ -754,6 +779,7 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo return true; } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) { if ( m_next == nullptr ) return; @@ -762,7 +788,7 @@ void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this ); } - +//////////////////////////////////////////////////////////////////////////////// bool SaveVar(FILE* pf, CBotVar* pVar) { while ( true ) @@ -779,6 +805,7 @@ bool SaveVar(FILE* pf, CBotVar* pVar) } } +//////////////////////////////////////////////////////////////////////////////// void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) { CBotProgram* prog = m_prog; // Current program @@ -813,6 +840,7 @@ void CBotStack::GetRunPos(const char* &FunctionName, int &start, int &end) end = t->GetEnd(); } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotStack::GetStackVars(const char* &FunctionName, int level) { CBotProgram* prog = m_prog; // current program @@ -860,6 +888,7 @@ CBotVar* CBotStack::GetStackVars(const char* &FunctionName, int level) return p->m_listVar; } +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::SaveState(FILE* pf) { if ( this == nullptr ) // end of the tree? @@ -888,7 +917,7 @@ bool CBotStack::SaveState(FILE* pf) return m_next->SaveState(pf); // saves the following } - +//////////////////////////////////////////////////////////////////////////////// bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) { unsigned short w; @@ -927,7 +956,7 @@ bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) return pStack->RestoreState(pf, pStack->m_next); } - +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Save0State(FILE* pf) { if (!WriteWord(pf, 100+m_mPrivate))return false; // private variable? @@ -937,6 +966,7 @@ bool CBotVar::Save0State(FILE* pf) return WriteString(pf, m_token->GetString()); // and variable name } +//////////////////////////////////////////////////////////////////////////////// bool CBotVarInt::Save0State(FILE* pf) { if ( !m_defnum.IsEmpty() ) @@ -948,28 +978,31 @@ bool CBotVarInt::Save0State(FILE* pf) return CBotVar::Save0State(pf); } +//////////////////////////////////////////////////////////////////////////////// bool CBotVarInt::Save1State(FILE* pf) { return WriteWord(pf, m_val); // the value of the variable } +//////////////////////////////////////////////////////////////////////////////// bool CBotVarBoolean::Save1State(FILE* pf) { return WriteWord(pf, m_val); // the value of the variable } +//////////////////////////////////////////////////////////////////////////////// bool CBotVarFloat::Save1State(FILE* pf) { return WriteFloat(pf, m_val); // the value of the variable } +//////////////////////////////////////////////////////////////////////////////// bool CBotVarString::Save1State(FILE* pf) { return WriteString(pf, m_val); // the value of the variable } - - +//////////////////////////////////////////////////////////////////////////////// bool CBotVarClass::Save1State(FILE* pf) { if ( !WriteType(pf, m_type) ) return false; @@ -1004,6 +1037,7 @@ bool ParseInitType(int rawInitType, CBotVar::InitType* initType) } } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) { unsigned short w, wi, prv, st; @@ -1152,7 +1186,7 @@ int CBotCStack::m_end = 0; CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); //CBotToken* CBotCStack::m_retClass= nullptr; - +//////////////////////////////////////////////////////////////////////////////// CBotCStack::CBotCStack(CBotCStack* ppapa) { m_next = nullptr; @@ -1175,6 +1209,7 @@ CBotCStack::CBotCStack(CBotCStack* ppapa) m_var = nullptr; } +//////////////////////////////////////////////////////////////////////////////// // destructor CBotCStack::~CBotCStack() { @@ -1185,6 +1220,7 @@ CBotCStack::~CBotCStack() delete m_listVar; } +//////////////////////////////////////////////////////////////////////////////// // used only at compile CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) { @@ -1199,7 +1235,7 @@ CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) return p; } - +//////////////////////////////////////////////////////////////////////////////// CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) { if ( pfils == this ) return inst; @@ -1218,6 +1254,7 @@ CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) return inst; } +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) { if (m_var != nullptr) delete m_var; // value replaced? @@ -1234,6 +1271,7 @@ CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) return inst; } +//////////////////////////////////////////////////////////////////////////////// int CBotCStack::GetError(int& start, int& end) { start = m_start; @@ -1241,11 +1279,13 @@ int CBotCStack::GetError(int& start, int& end) return m_error; } +//////////////////////////////////////////////////////////////////////////////// int CBotCStack::GetError() { return m_error; } +//////////////////////////////////////////////////////////////////////////////// // type of instruction on the stack CBotTypResult CBotCStack::GetTypResult(int mode) { @@ -1254,6 +1294,7 @@ CBotTypResult CBotCStack::GetTypResult(int mode) return m_var->GetTypResult(mode); } +//////////////////////////////////////////////////////////////////////////////// // type of instruction on the stack int CBotCStack::GetType(int mode) { @@ -1262,6 +1303,7 @@ int CBotCStack::GetType(int mode) return m_var->GetType(mode); } +//////////////////////////////////////////////////////////////////////////////// // pointer on the stack is in what class? CBotClass* CBotCStack::GetClass() { @@ -1272,6 +1314,7 @@ CBotClass* CBotCStack::GetClass() return m_var->GetClass(); } +//////////////////////////////////////////////////////////////////////////////// // type of instruction on the stack void CBotCStack::SetType(CBotTypResult& type) { @@ -1282,7 +1325,7 @@ void CBotCStack::SetType(CBotTypResult& type) // seeks a variable on the stack // the token may be a result of TokenTypVar (object of a class) // or a pointer in the source - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotCStack::FindVar(CBotToken* &pToken) { CBotCStack* p = this; @@ -1304,12 +1347,14 @@ CBotVar* CBotCStack::FindVar(CBotToken* &pToken) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotCStack::FindVar(CBotToken& Token) { CBotToken* pt = &Token; return FindVar(pt); } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotCStack::CopyVar(CBotToken& Token) { CBotVar* pVar = FindVar( Token ); @@ -1321,18 +1366,20 @@ CBotVar* CBotCStack::CopyVar(CBotToken& Token) return pCopy; } +//////////////////////////////////////////////////////////////////////////////// bool CBotCStack::IsOk() { return (m_error == 0); } - +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::SetStartError( int pos ) { if ( m_error != 0) return; // does not change existing error m_start = pos; } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::SetError(int n, int pos) { if ( n!= 0 && m_error != 0) return; // does not change existing error @@ -1340,6 +1387,7 @@ void CBotCStack::SetError(int n, int pos) m_end = pos; } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::SetError(int n, CBotToken* p) { if (m_error) return; // does not change existing error @@ -1348,6 +1396,7 @@ void CBotCStack::SetError(int n, CBotToken* p) m_end = p->GetEnd(); } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::ResetError(int n, int start, int end) { m_error = n; @@ -1355,6 +1404,7 @@ void CBotCStack::ResetError(int n, int start, int end) m_end = end; } +//////////////////////////////////////////////////////////////////////////////// bool CBotCStack::NextToken(CBotToken* &p) { CBotToken* pp = p; @@ -1366,32 +1416,38 @@ bool CBotCStack::NextToken(CBotToken* &p) return false; } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::SetBotCall(CBotProgram* p) { m_prog = p; } +//////////////////////////////////////////////////////////////////////////////// CBotProgram* CBotCStack::GetBotCall() { return m_prog; } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::SetRetType(CBotTypResult& type) { m_retTyp = type; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotCStack::GetRetType() { return m_retTyp; } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::SetVar( CBotVar* var ) { if (m_var) delete m_var; // replacement of a variable m_var = var; } +//////////////////////////////////////////////////////////////////////////////// // puts on the stack a copy of a variable void CBotCStack::SetCopyVar( CBotVar* var ) { @@ -1402,11 +1458,13 @@ void CBotCStack::SetCopyVar( CBotVar* var ) m_var->Copy( var ); } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotCStack::GetVar() { return m_var; } +//////////////////////////////////////////////////////////////////////////////// void CBotCStack::AddVar(CBotVar* pVar) { CBotCStack* p = this; @@ -1427,7 +1485,7 @@ void CBotCStack::AddVar(CBotVar* pVar) } // test whether a variable is already defined locally - +//////////////////////////////////////////////////////////////////////////////// bool CBotCStack::CheckVarLocal(CBotToken* &pToken) { CBotCStack* p = this; @@ -1448,6 +1506,7 @@ bool CBotCStack::CheckVarLocal(CBotToken* &pToken) return false; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) { nIdent = 0; @@ -1469,7 +1528,7 @@ CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nId } // test if a procedure name is already defined somewhere - +//////////////////////////////////////////////////////////////////////////////// bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) { CBotString name = pToken->GetString(); diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h new file mode 100644 index 00000000..df69a0d4 --- /dev/null +++ b/src/CBot/CBotStack.h @@ -0,0 +1,270 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \class CBotStack + * \brief The CBotStack class Management of the execution stack. Actually the + * only thing it can do is to create an instance of a stack. To use for routine + * CBotProgram :: Execute(CBotStack) + */ +class CBotStack +{ +public: +#if STACKMEM + /** + * \brief FirstStack Allocate first stack + * \return pointer to created stack + */ + static CBotStack * FirstStack(); + + /** \brief Delete Remove current stack */ + void Delete(); +#endif + + /** + * \brief CBotStack Constructor of the stack + * \param ppapa Not used. + */ + CBotStack(CBotStack* ppapa); + + + /** \brief ~CBotStack Destructor */ + ~CBotStack(); + + /** + * \brief StackOver Check if end of stack is reached + * \return true if end of stack + */ + bool StackOver(); + + /** + * \brief GetError Get error number of the stack + * \param [out] start beginning of the stack + * \param [out] end end of stack + * \return error number + */ + int GetError(int& start, int& end); + + /** + * \brief GetError Get error number + * \return eror number + */ + int GetError();// rend le numéro d'erreur retourné + + /** + * \brief Reset Reset error at and set user + * \param [in] pUser User of stack + */ + void Reset(void* pUser); + + /** + * \brief SetType Determines the type. + * \param type Type of instruction on the stack. + */ + void SetType(CBotTypResult& type); + + /** + * \brief GetType Get the type of value on the stack. + * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). + * \return Type number. + */ + int GetType(int mode = 0); + + /** + * \brief Getes the type of complete value on the stack. + * \param [in] mode Used when getting class type (1 gives pointer, 2 gives intrinsic). + * \return Type of an element. + */ + CBotTypResult GetTypResult(int mode = 0); + + /** + * \brief Adds a local variable. + * \param [in] p Variable to be added. + */ + void AddVar(CBotVar* p); + + /** + * \brief Fetch a variable by its token. + * \brief This may be a composite variable + * \param [in] pToken Token upon which search is performed + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Fetch a variable by its token. + * \brief This may be a composite variable + * \param [in] pToken Token upon which search is performed + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(CBotToken& pToken, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Fetch variable by its name + * \param [in] name Name of variable to find + * \return Found variable + */ + CBotVar* FindVar(const char* name); + + /** + * \brief Fetch a variable on the stack according to its identification number + * \brief This is faster than comparing names + * \param [in] ident Identifier of a variable + * \param [in] bUpdate Not used. Probably need to be removed + * \param [in] bModif Not used. Probably need to be removed + * \return Found variable + */ + CBotVar* FindVar(long ident, bool bUpdate = false, + bool bModif = false); + + /** + * \brief Find variable by its token and returns a copy of it. + * \param Token Token upon which search is performed + * \param bUpdate Not used. + * \return Found variable, nullptr if not found + */ + CBotVar* CopyVar(CBotToken& Token, bool bUpdate = false); + + + CBotStack* AddStack(CBotInstr* instr = nullptr, bool bBlock = false); // extends the stack + CBotStack* AddStackEOX(CBotCall* instr = nullptr, bool bBlock = false); // extends the stack + CBotStack* RestoreStack(CBotInstr* instr = nullptr); + CBotStack* RestoreStackEOX(CBotCall* instr = nullptr); + + CBotStack* AddStack2(bool bBlock = false); // extends the stack + bool Return(CBotStack* pFils); // transmits the result over + bool ReturnKeep(CBotStack* pFils); // transmits the result without reducing the stack + bool BreakReturn(CBotStack* pfils, const char* name = nullptr); + // in case of eventual break + bool IfContinue(int state, const char* name); + // or "continue" + + bool IsOk(); + + bool SetState(int n, int lim = -10); // select a state + int GetState(); // in what state am I? + bool IncState(int lim = -10); // passes to the next state + bool IfStep(); // do step by step + bool Execute(); + + void SetVar( CBotVar* var ); + void SetCopyVar( CBotVar* var ); + CBotVar* GetVar(); + CBotVar* GetCopyVar(); + CBotVar* GetPtVar(); + bool GetRetVar(bool bRet); + long GetVal(); + + void SetError(int n, CBotToken* token = nullptr); + void SetPosError(CBotToken* token); + void ResetError(int n, int start, int end); + void SetBreak(int val, const char* name); + + void SetBotCall(CBotProgram* p); + CBotProgram* GetBotCall(bool bFirst = false); + void* GetPUser(); + bool GetBlock(); + + + bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotTypResult& rettype); + void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar); + + bool SaveState(FILE* pf); + bool RestoreState(FILE* pf, CBotStack* &pStack); + + static + void SetTimer(int n); + + void GetRunPos(const char* &FunctionName, int &start, int &end); + CBotVar* GetStackVars(const char* &FunctionName, int level); + + int m_temp; + +private: + CBotStack* m_next; + CBotStack* m_next2; + CBotStack* m_prev; + friend class CBotInstArray; + +#ifdef _DEBUG + int m_index; +#endif + int m_state; + int m_step; + static int m_error; + static int m_start; + static int m_end; + static + CBotVar* m_retvar; // result of a return + + CBotVar* m_var; // result of the operations + CBotVar* m_listVar; // variables declared at this level + + bool m_bBlock; // is part of a block (variables are local to this block) + bool m_bOver; // stack limits? +// bool m_bDontDelete; // special, not to destroy the variable during delete + CBotProgram* m_prog; // user-defined functions + + static + int m_initimer; + static + int m_timer; + static + CBotString m_labelBreak; + static + void* m_pUser; + + CBotInstr* m_instr; // the corresponding instruction + bool m_bFunc; // an input of a function? + CBotCall* m_call; // recovery point in a extern call + friend class CBotTry; +}; + +// inline routinees must be declared in file.h + +inline bool CBotStack::IsOk() +{ + return (m_error == 0); +} + +inline int CBotStack::GetState() +{ + return m_state; +} + +inline int CBotStack::GetError() +{ + return m_error; +} diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 2898a92a..75e26e6c 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -25,6 +25,8 @@ #include "CBot.h" +#include "CBotStack.h" + #include #include #include From 394a49f5aa985e17cfc3653ff6963b83f4388f1f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 16:49:06 +0100 Subject: [PATCH 48/91] Moving CBotCall class in its own header and source files. --- src/CBot/CBot.h | 53 ------- src/CBot/CBotCall.cpp | 311 +++++++++++++++++++++++++++++++++++++++ src/CBot/CBotCall.h | 146 ++++++++++++++++++ src/CBot/CBotClass.cpp | 3 + src/CBot/CBotProgram.cpp | 305 +------------------------------------- src/CBot/CBotStack.cpp | 1 + src/CBot/CBotUtils.cpp | 50 +++++++ src/CBot/CBotUtils.h | 36 +++++ src/CBot/CMakeLists.txt | 2 + 9 files changed, 552 insertions(+), 355 deletions(-) create mode 100644 src/CBot/CBotCall.cpp create mode 100644 src/CBot/CBotCall.h create mode 100644 src/CBot/CBotUtils.cpp create mode 100644 src/CBot/CBotUtils.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 15c870e4..5e944d15 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -29,7 +29,6 @@ #include "CBotDll.h" // public definitions #include "CBotToken.h" // token management -#define STACKRUN 1 /// \def return execution directly on a suspended routine #define STACKMEM 1 /// \def preserve memory for the execution stack #define MAXSTACK 990 /// \def stack size reserved @@ -525,58 +524,6 @@ extern void DEBUG( const char* text, int val, CBotStack* pile ); #endif /////////////////////////////////////////// -// class for routine calls (external) - -class CBotCall -{ -private: - static - CBotCall* m_ListCalls; - static - void* m_pUser; - long m_nFuncIdent; - -private: - CBotString m_name; - bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser) ; - CBotTypResult - (*m_rComp) (CBotVar* &pVar, void* pUser) ; - CBotCall* m_next; - -public: - CBotCall(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - ~CBotCall(); - - static - bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - - static - CBotTypResult - CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); - static - bool CheckCall(const char* name); - -// static -// int DoCall(CBotToken* &p, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); - static - int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); -#if STACKRUN - bool Run(CBotStack* pStack); - static - bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); -#endif - - CBotString GetName(); - CBotCall* Next(); - - static void SetPUser(void* pUser); - static void Free(); -}; - // class managing the methods declared by AddFunction on a class class CBotCallMethode diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp new file mode 100644 index 00000000..7796786a --- /dev/null +++ b/src/CBot/CBotCall.cpp @@ -0,0 +1,311 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotCall.h" + +#include "CBotToken.h" +#include "CBotStack.h" + +#include "CBotUtils.h" + +// Local include + +// Global include + + + +//////////////////////////////////////////////////////////////////////////////// + +CBotCall* CBotCall::m_ListCalls = nullptr; +void* CBotCall::m_pUser = nullptr; + +//////////////////////////////////////////////////////////////////////////////// +CBotCall::CBotCall(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) +{ + m_name = name; + m_rExec = rExec; + m_rComp = rCompile; + m_next = nullptr; + m_nFuncIdent = CBotVar::NextUniqNum(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCall::~CBotCall() +{ + if (m_next) delete m_next; + m_next = nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCall::Free() +{ + delete CBotCall::m_ListCalls; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCall::AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) +{ + CBotCall* p = m_ListCalls; + CBotCall* pp = nullptr; + + if ( p != nullptr ) while ( p->m_next != nullptr ) + { + if ( p->GetName() == name ) + { + // frees redefined function + if ( pp ) pp->m_next = p->m_next; + else m_ListCalls = p->m_next; + pp = p; + p = p->m_next; + pp->m_next = nullptr; // not to destroy the following list + delete pp; + continue; + } + pp = p; // previous pointer + p = p->m_next; + } + + pp = new CBotCall(name, rExec, rCompile); + + if (p) p->m_next = pp; + else m_ListCalls = pp; + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult CBotCall::CompileCall(CBotToken* &p, CBotVar** ppVar, CBotCStack* pStack, long& nIdent) +{ + nIdent = 0; + CBotCall* pt = m_ListCalls; + CBotString name = p->GetString(); + + while ( pt != nullptr ) + { + if ( pt->m_name == name ) + { + CBotVar* pVar = MakeListVars(ppVar); + CBotVar* pVar2 = pVar; + CBotTypResult r = pt->m_rComp(pVar2, m_pUser); + int ret = r.GetType(); + + // if a class is returned, it is actually a pointer + if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer ); + + if ( ret > 20 ) + { + if (pVar2) pStack->SetError(ret, p /*pVar2->GetToken()*/ ); + } + delete pVar; + nIdent = pt->m_nFuncIdent; + return r; + } + pt = pt->m_next; + } + return -1; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCall::SetPUser(void* pUser) +{ + m_pUser = pUser; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCall::CheckCall(const char* name) +{ + CBotCall* p = m_ListCalls; + + while ( p != nullptr ) + { + if ( name == p->GetName() ) return true; + p = p->m_next; + } + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotCall::GetName() +{ + return m_name; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCall* CBotCall::Next() +{ + return m_next; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype) +{ + CBotCall* pt = m_ListCalls; + + if ( nIdent ) while ( pt != nullptr ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + goto fund; + } + pt = pt->m_next; + } + + pt = m_ListCalls; + + if ( token != nullptr ) + { + CBotString name = token->GetString(); + while ( pt != nullptr ) + { + if ( pt->m_name == name ) + { + nIdent = pt->m_nFuncIdent; + goto fund; + } + pt = pt->m_next; + } + } + + return -1; + +fund: +#if !STACKRUN + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVarToDelete = pVar; + + // creates a variable to the result + CBotVar* pResult = rettype.Eq(0) ? nullptr : CBotVar::Create("", rettype); + + CBotVar* pRes = pResult; + int Exception = 0; + int res = pt->m_rExec(pVar, pResult, Exception, pStack->GetPUser()); + + if ( pResult != pRes ) delete pRes; // different result if made + delete pVarToDelete; + + if (res == false) + { + if (Exception!=0) + { + pStack->SetError(Exception, token); + } + delete pResult; + return false; + } + pStack->SetVar(pResult); + + if ( rettype.GetType() > 0 && pResult == nullptr ) + { + pStack->SetError(TX_NORETVAL, token); + } + nIdent = pt->m_nFuncIdent; + return true; + +#else + + CBotStack* pile = pStack->AddStackEOX(pt); + if ( pile == EOX ) return true; + + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVar, true); +// CBotVar* pVarToDelete = pVar; + + // creates a variable to the result + CBotVar* pResult = rettype.Eq(0) ? nullptr : CBotVar::Create("", rettype); + + pile->SetVar( pVar ); + + CBotStack* pile2 = pile->AddStack(); + pile2->SetVar( pResult ); + + pile->SetError(0, token); // for the position on error + away + return pt->Run( pStack ); + +#endif + +} + +#if STACKRUN + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) +{ + CBotCall* pt = m_ListCalls; + + { + CBotString name = token->GetString(); + while ( pt != nullptr ) + { + if ( pt->m_name == name ) + { + nIdent = pt->m_nFuncIdent; + + CBotStack* pile = pStack->RestoreStackEOX(pt); + if ( pile == nullptr ) return true; + + // CBotStack* pile2 = pile->RestoreStack(); + pile->RestoreStack(); + return true; + } + pt = pt->m_next; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCall::Run(CBotStack* pStack) +{ + CBotStack* pile = pStack->AddStackEOX(this); + if ( pile == EOX ) return true; + CBotVar* pVar = pile->GetVar(); + + CBotStack* pile2 = pile->AddStack(); + CBotVar* pResult = pile2->GetVar(); + CBotVar* pRes = pResult; + + int Exception = 0; + int res = m_rExec(pVar, pResult, Exception, pStack->GetPUser()); + + if (res == false) + { + if (Exception!=0) + { + pStack->SetError(Exception); + } + if ( pResult != pRes ) delete pResult; // different result if made + return false; + } + + if ( pResult != nullptr ) pStack->SetCopyVar( pResult ); + if ( pResult != pRes ) delete pResult; // different result if made + + return true; +} + +#endif diff --git a/src/CBot/CBotCall.h b/src/CBot/CBotCall.h new file mode 100644 index 00000000..79cab9a2 --- /dev/null +++ b/src/CBot/CBotCall.h @@ -0,0 +1,146 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + +#define STACKRUN 1 /// \def return execution directly on a suspended routine + +/*! + * \brief The CBotCall class. Class for routine calls (external). + */ +class CBotCall +{ +public: + + /*! + * \brief CBotCall + * \param name + * \param rExec + * \param rCompile + */ + CBotCall(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + + /*! + * \brief ~CBotCall + */ + ~CBotCall(); + + /*! + * \brief AddFunction + * \param name + * \param rExec + * \param rCompile + * \return + */ + static bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + + /*! + * \brief CompileCall Is acceptable by a call procedure name and given + * parameters. + * \param p + * \param ppVars + * \param pStack + * \param nIdent + * \return + */ + static CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, CBotCStack* pStack, long& nIdent); + + /*! + * \brief CheckCall + * \param name + * \return + */ + static bool CheckCall(const char* name); + + /*! + * \brief DoCall + * \param nIdent + * \param token + * \param ppVars + * \param pStack + * \param rettype + * \return + */ + static int DoCall(long& nIdent, CBotToken* token, CBotVar** ppVars, CBotStack* pStack, CBotTypResult& rettype); + +#if STACKRUN + + /*! + * \brief Run + * \param pStack + * \return + */ + bool Run(CBotStack* pStack); + + /*! + * \brief RestoreCall + * \param nIdent + * \param token + * \param ppVar + * \param pStack + * \return + */ + static bool RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack); +#endif + + /*! + * \brief GetName + * \return + */ + CBotString GetName(); + + /*! + * \brief Next + * \return + */ + CBotCall* Next(); + + /*! + * \brief SetPUser + * \param pUser + */ + static void SetPUser(void* pUser); + + /*! + * \brief Free + */ + static void Free(); + + +private: + static CBotCall* m_ListCalls; + static void* m_pUser; + long m_nFuncIdent; + + CBotString m_name; + bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser); + CBotTypResult (*m_rComp) (CBotVar* &pVar, void* pUser); + CBotCall* m_next; +}; diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index f98eaaaa..dc897977 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -22,6 +22,9 @@ // #include "CBot.h" + +#include "CBotCall.h" + #include "CBotInstr/CBotNew.h" #include "CBotInstr/CBotLeftExprVar.h" #include "CBotInstr/CBotTwoOpExpr.h" diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index f1862c1e..a20531e7 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -22,8 +22,11 @@ #include "CBot.h" +#include "CBotCall.h" #include "CBotStack.h" +#include "CBotUtils.h" + #include CBotProgram::CBotProgram() @@ -556,308 +559,6 @@ int CBotProgram::GetVersion() } -////////////////////////////////////////////////////////////////////////////////////////////////////// - -CBotCall* CBotCall::m_ListCalls = nullptr; - -CBotCall::CBotCall(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) -{ - m_name = name; - m_rExec = rExec; - m_rComp = rCompile; - m_next = nullptr; - m_nFuncIdent = CBotVar::NextUniqNum(); -} - -CBotCall::~CBotCall() -{ - if (m_next) delete m_next; - m_next = nullptr; -} - -void CBotCall::Free() -{ - delete CBotCall::m_ListCalls; -} - -bool CBotCall::AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) -{ - CBotCall* p = m_ListCalls; - CBotCall* pp = nullptr; - - if ( p != nullptr ) while ( p->m_next != nullptr ) - { - if ( p->GetName() == name ) - { - // frees redefined function - if ( pp ) pp->m_next = p->m_next; - else m_ListCalls = p->m_next; - pp = p; - p = p->m_next; - pp->m_next = nullptr; // not to destroy the following list - delete pp; - continue; - } - pp = p; // previous pointer - p = p->m_next; - } - - pp = new CBotCall(name, rExec, rCompile); - - if (p) p->m_next = pp; - else m_ListCalls = pp; - - return true; -} - - -// transforms the array of pointers to variables -// in a chained list of variables -CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false) -{ - int i = 0; - CBotVar* pVar = nullptr; - - while( true ) - { -// ppVars[i]; - if ( ppVars[i] == nullptr ) break; - - CBotVar* pp = CBotVar::Create(ppVars[i]); - if (bSetVal) pp->Copy(ppVars[i]); - else - if ( ppVars[i]->GetType() == CBotTypPointer ) - pp->SetClass( ppVars[i]->GetClass()); -// copy the pointer according to indirections - if (pVar == nullptr) pVar = pp; - else pVar->AddNext(pp); - i++; - } - return pVar; -} - -// is acceptable by a call procedure name -// and given parameters - -CBotTypResult CBotCall::CompileCall(CBotToken* &p, CBotVar** ppVar, CBotCStack* pStack, long& nIdent) -{ - nIdent = 0; - CBotCall* pt = m_ListCalls; - CBotString name = p->GetString(); - - while ( pt != nullptr ) - { - if ( pt->m_name == name ) - { - CBotVar* pVar = MakeListVars(ppVar); - CBotVar* pVar2 = pVar; - CBotTypResult r = pt->m_rComp(pVar2, m_pUser); - int ret = r.GetType(); - - // if a class is returned, it is actually a pointer - if ( ret == CBotTypClass ) r.SetType( ret = CBotTypPointer ); - - if ( ret > 20 ) - { - if (pVar2) pStack->SetError(ret, p /*pVar2->GetToken()*/ ); - } - delete pVar; - nIdent = pt->m_nFuncIdent; - return r; - } - pt = pt->m_next; - } - return -1; -} - -void* CBotCall::m_pUser = nullptr; - -void CBotCall::SetPUser(void* pUser) -{ - m_pUser = pUser; -} - -bool CBotCall::CheckCall(const char* name) -{ - CBotCall* p = m_ListCalls; - - while ( p != nullptr ) - { - if ( name == p->GetName() ) return true; - p = p->m_next; - } - return false; -} - - - -CBotString CBotCall::GetName() -{ - return m_name; -} - -CBotCall* CBotCall::Next() -{ - return m_next; -} - - -int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype) -{ - CBotCall* pt = m_ListCalls; - - if ( nIdent ) while ( pt != nullptr ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - goto fund; - } - pt = pt->m_next; - } - - pt = m_ListCalls; - - if ( token != nullptr ) - { - CBotString name = token->GetString(); - while ( pt != nullptr ) - { - if ( pt->m_name == name ) - { - nIdent = pt->m_nFuncIdent; - goto fund; - } - pt = pt->m_next; - } - } - - return -1; - -fund: -#if !STACKRUN - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVarToDelete = pVar; - - // creates a variable to the result - CBotVar* pResult = rettype.Eq(0) ? nullptr : CBotVar::Create("", rettype); - - CBotVar* pRes = pResult; - int Exception = 0; - int res = pt->m_rExec(pVar, pResult, Exception, pStack->GetPUser()); - - if ( pResult != pRes ) delete pRes; // different result if made - delete pVarToDelete; - - if (res == false) - { - if (Exception!=0) - { - pStack->SetError(Exception, token); - } - delete pResult; - return false; - } - pStack->SetVar(pResult); - - if ( rettype.GetType() > 0 && pResult == nullptr ) - { - pStack->SetError(TX_NORETVAL, token); - } - nIdent = pt->m_nFuncIdent; - return true; - -#else - - CBotStack* pile = pStack->AddStackEOX(pt); - if ( pile == EOX ) return true; - - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVar, true); -// CBotVar* pVarToDelete = pVar; - - // creates a variable to the result - CBotVar* pResult = rettype.Eq(0) ? nullptr : CBotVar::Create("", rettype); - - pile->SetVar( pVar ); - - CBotStack* pile2 = pile->AddStack(); - pile2->SetVar( pResult ); - - pile->SetError(0, token); // for the position on error + away - return pt->Run( pStack ); - -#endif - -} - -#if STACKRUN - -bool CBotCall::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack) -{ - CBotCall* pt = m_ListCalls; - - { - CBotString name = token->GetString(); - while ( pt != nullptr ) - { - if ( pt->m_name == name ) - { - nIdent = pt->m_nFuncIdent; - - CBotStack* pile = pStack->RestoreStackEOX(pt); - if ( pile == nullptr ) return true; - - // CBotStack* pile2 = pile->RestoreStack(); - pile->RestoreStack(); - return true; - } - pt = pt->m_next; - } - } - - return false; -} - -bool CBotCall::Run(CBotStack* pStack) -{ - CBotStack* pile = pStack->AddStackEOX(this); - if ( pile == EOX ) return true; - CBotVar* pVar = pile->GetVar(); - - CBotStack* pile2 = pile->AddStack(); - CBotVar* pResult = pile2->GetVar(); - CBotVar* pRes = pResult; - - int Exception = 0; - int res = m_rExec(pVar, pResult, Exception, pStack->GetPUser()); - - if (res == false) - { - if (Exception!=0) - { - pStack->SetError(Exception); - } - if ( pResult != pRes ) delete pResult; // different result if made - return false; - } - - if ( pResult != nullptr ) pStack->SetCopyVar( pResult ); - if ( pResult != pRes ) delete pResult; // different result if made - - return true; -} - -#endif - -/////////////////////////////////////////////////////////////////////////////////////// - CBotCallMethode::CBotCallMethode(const char* name, bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index b1715e15..0997f468 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotStack.h" +#include "CBotCall.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp new file mode 100644 index 00000000..5aba61c2 --- /dev/null +++ b/src/CBot/CBotUtils.cpp @@ -0,0 +1,50 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotUtils.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal) +{ + int i = 0; + CBotVar* pVar = nullptr; + + while( true ) + { +// ppVars[i]; + if ( ppVars[i] == nullptr ) break; + + CBotVar* pp = CBotVar::Create(ppVars[i]); + if (bSetVal) pp->Copy(ppVars[i]); + else + if ( ppVars[i]->GetType() == CBotTypPointer ) + pp->SetClass( ppVars[i]->GetClass()); +// copy the pointer according to indirections + if (pVar == nullptr) pVar = pp; + else pVar->AddNext(pp); + i++; + } + return pVar; +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h new file mode 100644 index 00000000..df8bb55d --- /dev/null +++ b/src/CBot/CBotUtils.h @@ -0,0 +1,36 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + +/*! + * \brief MakeListVars Transforms the array of pointers to variables in a + * chained list of variables + * \param ppVars + * \param bSetVal + * \return + */ +CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false); diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 484d023c..bf7deefc 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -7,6 +7,8 @@ set(SOURCES CBotString.cpp CBotToken.cpp CBotVar.cpp + CBotCall.cpp + CBotUtils.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp From 63ab9d730149bcd12e586321382e5437cd3d5c47 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 17:04:27 +0100 Subject: [PATCH 49/91] Moving CBotVarArray class in its own header and source files. --- src/CBot/CBot.h | 33 ------- src/CBot/CBotDefines.h | 28 ++++++ src/CBot/CBotInstr/CBotExprVar.cpp | 2 + src/CBot/CBotInstr/CBotIndexExpr.cpp | 2 + src/CBot/CBotInstr/CBotInstArray.cpp | 2 + src/CBot/CBotInstr/CBotLeftExpr.cpp | 2 + src/CBot/CBotVar.cpp | 115 +--------------------- src/CBot/CBotVar/CBotVarArray.cpp | 140 +++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarArray.h | 102 +++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 10 files changed, 283 insertions(+), 144 deletions(-) create mode 100644 src/CBot/CBotDefines.h create mode 100644 src/CBot/CBotVar/CBotVarArray.cpp create mode 100644 src/CBot/CBotVar/CBotVarArray.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 5e944d15..c5fc9de8 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -468,39 +468,6 @@ public: bool Ne(CBotVar* left, CBotVar* right) override; }; - -// classe pour les tableaux - -#define MAXARRAYSIZE 9999 - -class CBotVarArray : public CBotVar -{ -private: - CBotVarClass* - m_pInstance; // instance manager of table - - friend class CBotVar; // my daddy is a buddy - -public: - CBotVarArray( const CBotToken* name, CBotTypResult& type ); - ~CBotVarArray(); - - void SetPointer(CBotVar* p) override; - CBotVarClass* - GetPointer() override; - - void Copy(CBotVar* pSrc, bool bName=true) override; - CBotVar* GetItem(int n, bool bGrow=false) override; // makes an element according to its numeric index - // enlarged the table if necessary if bExtend -// CBotVar* GetItem(const char* name); // makes a element by literal index - CBotVar* GetItemList() override; // gives the first item in the list - - CBotString GetValString() override; // gets the contents of the array into a string - - bool Save1State(FILE* pf) override; -}; - - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h new file mode 100644 index 00000000..2c6e34d7 --- /dev/null +++ b/src/CBot/CBotDefines.h @@ -0,0 +1,28 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude + +// Local include + +// Global include + +#define MAXARRAYSIZE 9999 diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 0e531fae..344ce85c 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -26,6 +26,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVarArray.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.cpp b/src/CBot/CBotInstr/CBotIndexExpr.cpp index f4882c00..2bc37f1e 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.cpp +++ b/src/CBot/CBotInstr/CBotIndexExpr.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVarArray.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstArray.cpp b/src/CBot/CBotInstr/CBotInstArray.cpp index e1327a58..4bf2fd16 100644 --- a/src/CBot/CBotInstr/CBotInstArray.cpp +++ b/src/CBot/CBotInstr/CBotInstArray.cpp @@ -27,6 +27,8 @@ #include "CBotStack.h" +#include "CBotDefines.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp index 84c7b735..0785dd27 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.cpp +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -25,6 +25,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVarArray.h" + // Local include // Global include diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 75e26e6c..490af3f4 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -27,6 +27,10 @@ #include "CBotStack.h" +#include "CBotVar/CBotVarArray.h" + +#include "CBotDefines.h" + #include #include #include @@ -1774,117 +1778,6 @@ bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) return l != r; } -///////////////////////////////////////////////////////////////////////////// -// management of arrays - -CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type ) -{ - if ( !type.Eq(CBotTypArrayPointer) && - !type.Eq(CBotTypArrayBody)) assert(0); - - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; - - m_type = type; - m_type.SetType(CBotTypArrayPointer); - m_binit = CBotVar::InitType::UNDEF; - - m_pInstance = nullptr; // the list of the array elements -} - -CBotVarArray::~CBotVarArray() -{ - if ( m_pInstance != nullptr ) m_pInstance->DecrementUse(); // the lowest reference -} - -// copy a variable into another -void CBotVarArray::Copy(CBotVar* pSrc, bool bName) -{ - if ( pSrc->GetType() != CBotTypArrayPointer ) - assert(0); - - CBotVarArray* p = static_cast(pSrc); - - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; - m_pInstance = p->GetPointer(); - - if ( m_pInstance != nullptr ) - m_pInstance->IncrementUse(); // a reference increase - - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_pMyThis = nullptr;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // keeps indentificator the same (by default) - if (m_ident == 0 ) m_ident = p->m_ident; -} - -void CBotVarArray::SetPointer(CBotVar* pVarClass) -{ - m_binit = CBotVar::InitType::DEF; // init, even on a null pointer - - if ( m_pInstance == pVarClass) return; // Special, not decrement and reincrement - // because the decrement can destroy the object - - if ( pVarClass != nullptr ) - { - if ( pVarClass->GetType() == CBotTypArrayPointer ) - pVarClass = pVarClass->GetPointer(); // the real pointer to the object - - if ( !pVarClass->m_type.Eq(CBotTypClass) && - !pVarClass->m_type.Eq(CBotTypArrayBody)) - assert(0); - - (static_cast(pVarClass))->IncrementUse(); // incement the reference - } - - if ( m_pInstance != nullptr ) m_pInstance->DecrementUse(); - m_pInstance = static_cast(pVarClass); -} - - -CBotVarClass* CBotVarArray::GetPointer() -{ - if ( m_pInstance == nullptr ) return nullptr; - return m_pInstance->GetPointer(); -} - -CBotVar* CBotVarArray::GetItem(int n, bool bExtend) -{ - if ( m_pInstance == nullptr ) - { - if ( !bExtend ) return nullptr; - // creates an instance of the table - - CBotVarClass* instance = new CBotVarClass(nullptr, m_type); - SetPointer( instance ); - } - return m_pInstance->GetItem(n, bExtend); -} - -CBotVar* CBotVarArray::GetItemList() -{ - if ( m_pInstance == nullptr) return nullptr; - return m_pInstance->GetItemList(); -} - -CBotString CBotVarArray::GetValString() -{ - if ( m_pInstance == nullptr ) return ( CBotString( "Null pointer" ) ) ; - return m_pInstance->GetValString(); -} - -bool CBotVarArray::Save1State(FILE* pf) -{ - if ( !WriteType(pf, m_type) ) return false; - return SaveVar(pf, m_pInstance); // saves the instance that manages the table -} - - ///////////////////////////////////////////////////////////////////////////// // gestion des pointeurs à une instance donnée // TODO management of pointers to a given instance diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp new file mode 100644 index 00000000..35f55a52 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -0,0 +1,140 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarArray.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type ) +{ + if ( !type.Eq(CBotTypArrayPointer) && + !type.Eq(CBotTypArrayBody)) assert(0); + + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + + m_type = type; + m_type.SetType(CBotTypArrayPointer); + m_binit = CBotVar::InitType::UNDEF; + + m_pInstance = nullptr; // the list of the array elements +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarArray::~CBotVarArray() +{ + if ( m_pInstance != nullptr ) m_pInstance->DecrementUse(); // the lowest reference +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarArray::Copy(CBotVar* pSrc, bool bName) +{ + if ( pSrc->GetType() != CBotTypArrayPointer ) + assert(0); + + CBotVarArray* p = static_cast(pSrc); + + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; + m_pInstance = p->GetPointer(); + + if ( m_pInstance != nullptr ) + m_pInstance->IncrementUse(); // a reference increase + + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_pMyThis = nullptr;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarArray::SetPointer(CBotVar* pVarClass) +{ + m_binit = CBotVar::InitType::DEF; // init, even on a null pointer + + if ( m_pInstance == pVarClass) return; // Special, not decrement and reincrement + // because the decrement can destroy the object + + if ( pVarClass != nullptr ) + { + if ( pVarClass->GetType() == CBotTypArrayPointer ) + pVarClass = pVarClass->GetPointer(); // the real pointer to the object + + if ( !pVarClass->m_type.Eq(CBotTypClass) && + !pVarClass->m_type.Eq(CBotTypArrayBody)) + assert(0); + + (static_cast(pVarClass))->IncrementUse(); // incement the reference + } + + if ( m_pInstance != nullptr ) m_pInstance->DecrementUse(); + m_pInstance = static_cast(pVarClass); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass* CBotVarArray::GetPointer() +{ + if ( m_pInstance == nullptr ) return nullptr; + return m_pInstance->GetPointer(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarArray::GetItem(int n, bool bExtend) +{ + if ( m_pInstance == nullptr ) + { + if ( !bExtend ) return nullptr; + // creates an instance of the table + + CBotVarClass* instance = new CBotVarClass(nullptr, m_type); + SetPointer( instance ); + } + return m_pInstance->GetItem(n, bExtend); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarArray::GetItemList() +{ + if ( m_pInstance == nullptr) return nullptr; + return m_pInstance->GetItemList(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarArray::GetValString() +{ + if ( m_pInstance == nullptr ) return ( CBotString( "Null pointer" ) ) ; + return m_pInstance->GetValString(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarArray::Save1State(FILE* pf) +{ + if ( !WriteType(pf, m_type) ) return false; + return SaveVar(pf, m_pInstance); // saves the instance that manages the table +} diff --git a/src/CBot/CBotVar/CBotVarArray.h b/src/CBot/CBotVar/CBotVarArray.h new file mode 100644 index 00000000..158cdf81 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarArray.h @@ -0,0 +1,102 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" +#include "CBotDefines.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarArray class Classe pour les tableaux. + */ +class CBotVarArray : public CBotVar +{ +public: + + /*! + * \brief CBotVarArray + * \param name + * \param type + */ + CBotVarArray( const CBotToken* name, CBotTypResult& type ); + + /*! + * \brief ~CBotVarArray + */ + ~CBotVarArray(); + + /*! + * \brief SetPointer + * \param p + */ + void SetPointer(CBotVar* p) override; + + /*! + * \brief GetPointer + * \return + */ + CBotVarClass* GetPointer() override; + + /*! + * \brief Copy Copy a variable into another. + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief GetItem Makes an element according to its numeric index enlarged + * the table if necessary if bExtend. + * \param n + * \param bGrow + * \return + */ + CBotVar* GetItem(int n, bool bGrow=false) override; + + /*! + * \brief GetItemList Gives the first item in the list. + * \return + */ + CBotVar* GetItemList() override; + + /*! + * \brief GetValString Gets the contents of the array into a string. + * \return + */ + CBotString GetValString() override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + +private: + //! Instance manager of table. + CBotVarClass* m_pInstance; + //! My daddy is a buddy. + friend class CBotVar; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index bf7deefc..5bb6ad70 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -53,6 +53,7 @@ set(SOURCES CBotInstr/CBotListArray.cpp CBotInstr/CBotInstArray.cpp CBotInstr/CBotInt.cpp + CBotVar/CBotVarArray.cpp ) # Includes From 77d738634c5eeb8b4f33441191d28da7fe3f27f7 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 17:37:53 +0100 Subject: [PATCH 50/91] Moving CBotClass class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBotClass.cpp | 102 +++++--- src/CBot/CBotClass.h | 328 ++++++++++++++++++++++++ src/CBot/CBotDll.h | 118 --------- src/CBot/CBotFunction.cpp | 1 + src/CBot/CBotInstr/CBotClassInst.cpp | 1 + src/CBot/CBotInstr/CBotFieldExpr.cpp | 1 + src/CBot/CBotInstr/CBotInstrMethode.cpp | 1 + src/CBot/CBotInstr/CBotLeftExpr.cpp | 1 + src/CBot/CBotInstr/CBotNew.cpp | 1 + src/CBot/CBotProgram.cpp | 2 +- src/CBot/CBotVar.cpp | 1 + src/level/robotmain.cpp | 2 + src/script/scriptfunc.cpp | 3 + 14 files changed, 408 insertions(+), 155 deletions(-) create mode 100644 src/CBot/CBotClass.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 30f22461..76cfe2b5 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -73,6 +73,7 @@ #include "CBotInstr/CBotInt.h" #include "CBotStack.h" +#include "CBotClass.h" // Local include diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index dc897977..9c18f2c9 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -17,21 +17,27 @@ * along with this program. If not, see http://gnu.org/licenses */ -///////////////////////////////////////////////////////////////////// -// Management of variables of class type -// - -#include "CBot.h" - -#include "CBotCall.h" +// Modules inlcude +#include "CBotClass.h" #include "CBotInstr/CBotNew.h" #include "CBotInstr/CBotLeftExprVar.h" #include "CBotInstr/CBotTwoOpExpr.h" +#include "CBotCall.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::m_ExClass = nullptr; -CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic) +//////////////////////////////////////////////////////////////////////////////// +CBotClass::CBotClass(const char* name, + CBotClass* pPapa, + bool bIntrinsic) { m_pParent = pPapa; m_name = name; @@ -60,6 +66,7 @@ CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic) } +//////////////////////////////////////////////////////////////////////////////// CBotClass::~CBotClass() { // removes the list of class @@ -77,11 +84,15 @@ CBotClass::~CBotClass() delete m_next; // releases all of them on this level } -CBotClass* CBotClass::Create(const char* name, CBotClass* parent, bool intrinsic) +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotClass::Create(const char* name, + CBotClass* parent, + bool intrinsic) { return new CBotClass(name, parent, intrinsic); } +//////////////////////////////////////////////////////////////////////////////// void CBotClass::Free() { while ( m_ExClass != nullptr ) @@ -90,6 +101,7 @@ void CBotClass::Free() } } +//////////////////////////////////////////////////////////////////////////////// void CBotClass::Purge() { if ( this == nullptr ) return; @@ -108,6 +120,7 @@ void CBotClass::Purge() m_next = nullptr; // no longer belongs to this chain } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::Lock(CBotProgram* p) { int i = m_cptLock++; @@ -144,6 +157,7 @@ bool CBotClass::Lock(CBotProgram* p) return false; } +//////////////////////////////////////////////////////////////////////////////// void CBotClass::Unlock() { if ( --m_cptOne > 0 ) return ; @@ -162,6 +176,7 @@ void CBotClass::Unlock() m_ProgInLock[i] = nullptr; } +//////////////////////////////////////////////////////////////////////////////// void CBotClass::FreeLock(CBotProgram* p) { CBotClass* pClass = m_ExClass; @@ -182,9 +197,10 @@ void CBotClass::FreeLock(CBotProgram* p) } } - - -bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) +//////////////////////////////////////////////////////////////////////////////// +bool CBotClass::AddItem(CBotString name, + CBotTypResult type, + int mPrivate) { CBotToken token(name, CBotString()); CBotClass* pClass = type.GetClass(); @@ -207,7 +223,7 @@ bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate) return AddItem( pVar ); } - +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::AddItem(CBotVar* pVar) { pVar->SetUniqNum(++m_nbVar); @@ -218,6 +234,7 @@ bool CBotClass::AddItem(CBotVar* pVar) return true; } +//////////////////////////////////////////////////////////////////////////////// void CBotClass::AddNext(CBotClass* pClass) { CBotClass* p = this; @@ -226,17 +243,20 @@ void CBotClass::AddNext(CBotClass* pClass) p->m_next = pClass; } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotClass::GetName() { return m_name; } +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::GetParent() { if ( this == nullptr ) return nullptr; return m_pParent; } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::IsChildOf(CBotClass* pClass) { CBotClass* p = this; @@ -248,12 +268,13 @@ bool CBotClass::IsChildOf(CBotClass* pClass) return false; } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotClass::GetVar() { return m_pVar; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotClass::GetItem(const char* name) { CBotVar* p = m_pVar; @@ -267,6 +288,7 @@ CBotVar* CBotClass::GetItem(const char* name) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotClass::GetItemRef(int nIdent) { CBotVar* p = m_pVar; @@ -280,16 +302,19 @@ CBotVar* CBotClass::GetItemRef(int nIdent) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::IsIntrinsic() { return m_bIntrinsic; } +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::Find(CBotToken* &pToken) { return Find(pToken->GetString()); } +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::Find(const char* name) { CBotClass* p = m_ExClass; @@ -303,9 +328,10 @@ CBotClass* CBotClass::Find(const char* name) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::AddFunction(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) { // stores pointers to the two functions CBotCallMethode* p = m_pCalls; @@ -332,18 +358,19 @@ bool CBotClass::AddFunction(const char* name, return true; } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ) { m_rMaj = rMaj; return true; } -// compiles a method associated with an instance of class -// the method can be declared by the user or AddFunction - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotClass::CompileMethode(const char* name, - CBotVar* pThis, CBotVar** ppParams, - CBotCStack* pStack, long& nIdent) + CBotVar* pThis, + CBotVar** ppParams, + CBotCStack* pStack, + long& nIdent) { nIdent = 0; // forget the previous one if necessary @@ -360,11 +387,13 @@ CBotTypResult CBotClass::CompileMethode(const char* name, return r; } -// executes a method - -bool CBotClass::ExecuteMethode(long& nIdent, const char* name, - CBotVar* pThis, CBotVar** ppParams, - CBotVar* &pResult, CBotStack* &pStack, +//////////////////////////////////////////////////////////////////////////////// +bool CBotClass::ExecuteMethode(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppParams, + CBotVar* &pResult, + CBotStack* &pStack, CBotToken* pToken) { int ret = m_pCalls->DoCall(nIdent, name, pThis, ppParams, pResult, pStack, pToken); @@ -382,17 +411,17 @@ bool CBotClass::ExecuteMethode(long& nIdent, const char* name, return ret; } -// restored the execution stack - -void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, - CBotVar** ppParams, CBotStack* &pStack) +//////////////////////////////////////////////////////////////////////////////// +void CBotClass::RestoreMethode(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppParams, + CBotStack* &pStack) { m_pMethod->RestoreCall(nIdent, name, pThis, ppParams, pStack, this); } - - - +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::SaveStaticState(FILE* pf) { if (!WriteWord( pf, CBOTVERSION*2)) return false; @@ -429,6 +458,7 @@ bool CBotClass::SaveStaticState(FILE* pf) return true; } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::RestoreStaticState(FILE* pf) { CBotString ClassName, VarName; @@ -466,9 +496,9 @@ bool CBotClass::RestoreStaticState(FILE* pf) return true; } -// test if a procedure name is already defined somewhere - -bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) +//////////////////////////////////////////////////////////////////////////////// +bool CBotClass::CheckCall(CBotToken* &pToken, + CBotDefParam* pParam) { CBotString name = pToken->GetString(); diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h new file mode 100644 index 00000000..e2bc0006 --- /dev/null +++ b/src/CBot/CBotClass.h @@ -0,0 +1,328 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + +/*! + * \brief The CBotClass class Class to define new classes in the language CBOT + * for example to define the class CPoint (x, y). + */ +class CBotClass +{ +public: + //! Mark if is set or not + bool m_IsDef; + + /*! + * \brief CBotClass Constructor. Once a class is created, it is known around + * CBot intrinsic mode gives a class that is not managed by pointers. + * \param name + * \param pParent + * \param bIntrinsic + */ + CBotClass( const char* name, + CBotClass* pParent, + bool bIntrinsic = false ); + + /*! + * \brief CBotClass Destructor. + */ + ~CBotClass( ); + + /*! + * \brief Create + * \param name + * \param parent + * \param intrinsic + * \return + */ + static CBotClass* Create(const char* name, + CBotClass* parent, + bool intrinsic = false); + + /*! + * \brief AddFunction This call allows to add as external (**) new method + * used by the objects of this class. + * \param name + * \param rExec + * \param rCompile + * \return + */ + bool AddFunction(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); + + /*! + * \brief AddUpdateFunc Defines routine to be called to update the elements + * of the class. + * \param rMaj + * \return + */ + bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); + // + + /*! + * \brief AddItem Adds an element to the class. + * \param name + * \param type + * \param mPrivate + * \return + */ + bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); + + /*! + * \brief AddItem Adds an item by passing the pointer to an instance of a + * variable the object is taken as is, so do not destroyed. + * \param pVar + * \return + */ + bool AddItem(CBotVar* pVar); + + /*! + * \brief AddNext + * \param pClass + */ + void AddNext(CBotClass* pClass); + + /*! + * \brief GetName Gives the name of the class. + * \return + */ + CBotString GetName(); + + /*! + * \brief GetParent Gives the parent class (or nullptr). + * \return + */ + CBotClass* GetParent(); + + /*! + * \brief IsChildOf True if a class is derived (Extends) of another. + * \param pClass + * \return true also if the classes are identical + */ + bool IsChildOf(CBotClass* pClass); + + /*! + * \brief Find Trouve une classe d'après son nom + * \param pToken + * \return A class by it's its name. + */ + static CBotClass* Find(CBotToken* &pToken); + + /*! + * \brief Find + * \param name + * \return + */ + static CBotClass* Find(const char* name); + + /*! + * \brief GetVar Return the list of variables. + * \return + */ + CBotVar* GetVar(); + /*! + * \brief GetItem One of the variables according to its name. + * \param name + * \return + */ + CBotVar* GetItem(const char* name); + + /*! + * \brief GetItemRef + * \param nIdent + * \return + */ + CBotVar* GetItemRef(int nIdent); + + /*! + * \brief CompileMethode Compiles a method associated with an instance of + * class the method can be declared by the user or AddFunction. + * \param name + * \param pThis + * \param ppParams + * \param pStack + * \param nIdent + * \return + */ + CBotTypResult CompileMethode(const char* name, + CBotVar* pThis, + CBotVar** ppParams, + CBotCStack* pStack, + long& nIdent); + + /*! + * \brief ExecuteMethode Executes a method. + * \param nIdent + * \param name + * \param pThis + * \param ppParams + * \param pResult + * \param pStack + * \param pToken + * \return + */ + bool ExecuteMethode(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppParams, + CBotVar* &pResult, + CBotStack* &pStack, + CBotToken* pToken); + + /*! + * \brief RestoreMethode Restored the execution stack. + * \param nIdent + * \param name + * \param pThis + * \param ppParams + * \param pStack + */ + void RestoreMethode(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppParams, + CBotStack* &pStack); + + /*! + * \brief Compile Compiles a class declared by the user. + * \param p + * \param pStack + * \return + */ + static CBotClass* Compile(CBotToken* &p, + CBotCStack* pStack); + + /*! + * \brief Compile1 + * \param p + * \param pStack + * \return + */ + static CBotClass* Compile1(CBotToken* &p, + CBotCStack* pStack); + + /*! + * \brief CompileDefItem + * \param p + * \param pStack + * \param bSecond + * \return + */ + bool CompileDefItem(CBotToken* &p, + CBotCStack* pStack, + bool bSecond); + + /*! + * \brief IsIntrinsic + * \return + */ + bool IsIntrinsic(); + + /*! + * \brief Purge + */ + void Purge(); + + /*! + * \brief Free + */ + static void Free(); + + /*! + * \brief SaveStaticState + * \param pf + * \return + */ + static bool SaveStaticState(FILE* pf); + + /*! + * \brief RestoreStaticState + * \param pf + * \return + */ + static bool RestoreStaticState(FILE* pf); + + /*! + * \brief Lock + * \param p + * \return + */ + bool Lock(CBotProgram* p); + + /*! + * \brief Unlock + */ + void Unlock(); + + /*! + * \brief FreeLock + * \param p + */ + static void FreeLock(CBotProgram* p); + + /*! + * \brief CheckCall Test if a procedure name is already defined somewhere. + * \param pToken + * \param pParam + * \return + */ + bool CheckCall(CBotToken* &pToken, + CBotDefParam* pParam); + +private: + //! List of classes existing at a given time. + static CBotClass* m_ExClass; + //! For this general list. + CBotClass* m_ExNext; + //! For this general list. + CBotClass* m_ExPrev; + //! Parent class. + CBotClass* m_pParent; + //! Name of this class. + CBotString m_name; + //! Number of variables in the chain. + int m_nbVar; + //! Content of the class. + CBotVar* m_pVar; + //! Intrinsic class. + bool m_bIntrinsic; + //! The string class. + CBotClass* m_next; + //! List of methods defined in external. + CBotCallMethode* m_pCalls; + //! Compiled list of methods. + CBotFunction* m_pMethod; + void (*m_rMaj) ( CBotVar* pThis, void* pUser ); + friend class CBotVarClass; + //! For Lock / UnLock. + int m_cptLock; + //! Lock for reentrancy. + int m_cptOne; + //! Processes waiting for sync. + CBotProgram* m_ProgInLock[5]; +}; diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index deec62cb..c1e8b69f 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -847,124 +847,6 @@ virtual ~CBotVar( ); // destructor */ - -//////////////////////////////////////////////////////////////////////// -// management of classes -//////////////////////////////////////////////////////////////////////// - -// class to define new classes in the language CBOT -// for example to define the class CPoint (x, y) - -class CBotClass -{ -private: - static - CBotClass* m_ExClass; // list of classes existing at a given time - CBotClass* m_ExNext; // for this general list - CBotClass* m_ExPrev; // for this general list - -private: - CBotClass* m_pParent; // parent class - CBotString m_name; // name of this class - int m_nbVar; // number of variables in the chain - CBotVar* m_pVar; // content of the class - bool m_bIntrinsic; // intrinsic class - CBotClass* m_next; // the string class - CBotCallMethode* m_pCalls; // list of methods defined in external - CBotFunction* m_pMethod; // compiled list of methods - void (*m_rMaj) ( CBotVar* pThis, void* pUser ); - friend class CBotVarClass; - int m_cptLock; // for Lock / UnLock - int m_cptOne; // Lock for reentrancy - CBotProgram* m_ProgInLock[5];// processes waiting for sync - -public: - bool m_IsDef; // mark if is set or not - - CBotClass( const char* name, - CBotClass* pParent, bool bIntrinsic = false ); // constructor - // Once a class is created, it is known - // around CBoT - // intrinsic mode gives a class that is not managed by pointers - - ~CBotClass( ); // destructor - - static CBotClass* Create(const char* name, CBotClass* parent, bool intrinsic = false); - - bool AddFunction(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - // this call allows to add as external (**) - // new method used by the objects of this class - - bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) ); - // defines routine to be called to update the elements of the class - - bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC); - // adds an element to the class -// bool AddItem(CBotString name, CBotClass* pClass); - // the same for elements belonging to pClass - bool AddItem(CBotVar* pVar); - // adds an item by passing the pointer to an instance of a variable - // the object is taken as is, so do not destroyed - - - - // adds an element by giving an element of type CBotVar - void AddNext(CBotClass* pClass); - - CBotString GetName(); // gives the name of the class - CBotClass* GetParent(); // gives the parent class (or nullptr) - - // true if a class is derived (Extends) of another - // return true also if the classes are identical - bool IsChildOf(CBotClass* pClass); - - static - CBotClass* Find(CBotToken* &pToken); // trouve une classe d'après son nom - // return a class by it's its name - static - CBotClass* Find(const char* name); - - CBotVar* GetVar(); // return the list of variables - CBotVar* GetItem(const char* name); // one of the variables according to its name - CBotVar* GetItemRef(int nIdent); - - CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams, - CBotCStack* pStack, long& nIdent); - - bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken); - void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack); - - // compiles a class declared by the user - static - CBotClass* Compile(CBotToken* &p, CBotCStack* pStack); - static - CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack); - - bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond); - - bool IsIntrinsic(); - void Purge(); - static - void Free(); - - static - bool SaveStaticState(FILE* pf); - - static - bool RestoreStaticState(FILE* pf); - - bool Lock(CBotProgram* p); - void Unlock(); - static - void FreeLock(CBotProgram* p); - - bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); - -}; - - /* //////////////////////////////////////////////////////////////////////// // Examples of use diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotFunction.cpp index 64b85fdc..61a98870 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotFunction.cpp @@ -30,6 +30,7 @@ #include "CBotInstr/CBotListArray.h" #include "CBotStack.h" +#include "CBotClass.h" #include diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index e02328da..a78b4de9 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -24,6 +24,7 @@ #include "CBotInstArray.h" #include "CBotStack.h" +#include "CBotClass.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.cpp b/src/CBot/CBotInstr/CBotFieldExpr.cpp index ae8e583d..592d4a7e 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.cpp +++ b/src/CBot/CBotInstr/CBotFieldExpr.cpp @@ -21,6 +21,7 @@ #include "CBotFieldExpr.h" #include "CBotStack.h" +#include "CBotClass.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index 1ec99067..a33207fb 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -21,6 +21,7 @@ #include "CBotInstrMethode.h" #include "CBotStack.h" +#include "CBotClass.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp index 0785dd27..468854a9 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.cpp +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -24,6 +24,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotClass.h" #include "CBotVar/CBotVarArray.h" diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp index fef5c074..9da2a88e 100644 --- a/src/CBot/CBotInstr/CBotNew.cpp +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -21,6 +21,7 @@ #include "CBotNew.h" #include "CBotStack.h" +#include "CBotClass.h" // Local include diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index a20531e7..27485f85 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -24,7 +24,7 @@ #include "CBotCall.h" #include "CBotStack.h" - +#include "CBotClass.h" #include "CBotUtils.h" #include diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 490af3f4..8808cb0f 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -30,6 +30,7 @@ #include "CBotVar/CBotVarArray.h" #include "CBotDefines.h" +#include "CBotClass.h" #include #include diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 483f0804..8ff82eee 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -20,6 +20,8 @@ #include "level/robotmain.h" #include "CBot/CBotDll.h" +// TODO must be replaced by CBot.h +#include "CBot/CBotClass.h" #include "app/app.h" #include "app/input.h" diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index a49dc9c1..7ff61714 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -19,6 +19,9 @@ #include "script/scriptfunc.h" +// TODO must be replaced by CBot.h +#include "CBot/CBotClass.h" + #include "app/app.h" #include "common/global.h" From 70dc6785f2fecd4b03e60c0f78e092e55842fb93 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 18:05:35 +0100 Subject: [PATCH 51/91] Moving CBotProgram class in its own header and source files. --- src/CBot/CBot.h | 1 + src/CBot/CBotClass.h | 2 + src/CBot/CBotDll.h | 128 ------------------- src/CBot/CBotProgram.cpp | 172 +++++++++++++++----------- src/CBot/CBotProgram.h | 260 +++++++++++++++++++++++++++++++++++++++ src/script/script.h | 2 + 6 files changed, 366 insertions(+), 199 deletions(-) create mode 100644 src/CBot/CBotProgram.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index c5fc9de8..e98a1c6c 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -28,6 +28,7 @@ #include "resource.h" #include "CBotDll.h" // public definitions #include "CBotToken.h" // token management +#include "CBotProgram.h" #define STACKMEM 1 /// \def preserve memory for the execution stack #define MAXSTACK 990 /// \def stack size reserved diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index e2bc0006..3cc4f9f9 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotProgram.h" + // Local include // Global include diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index c1e8b69f..6bd16030 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -440,134 +440,6 @@ public: CBotString& ElementAt(int nIndex); }; -//////////////////////////////////////////////////////////////////// -// main class managing CBot program -// - -class CBotProgram -{ -private: - CBotFunction* m_Prog; // the user-defined functions - CBotFunction* m_pRun; // the basic function for the execution - CBotClass* m_pClass; // classes defined in this part - CBotStack* m_pStack; // execution stack - CBotVar* m_pInstance; // instance of the parent class - friend class CBotFunction; - - int m_ErrorCode; - int m_ErrorStart; - int m_ErrorEnd; - - long m_Ident; // associated identifier - -public: - bool m_bCompileClass; - -public: - static void Init(); - // initializes the module (defined keywords for errors) - // should be done once (and only one) at the beginning - static - void Free(); - // frees the static memory areas - - static - int GetVersion(); - // gives the version of the library CBOT - - - CBotProgram(); - CBotProgram(CBotVar* pInstance); - ~CBotProgram(); - - bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = nullptr); - // compiles the program given in text - // returns false if an error at compile - // see GetCompileError () to retrieve the error - // ListFonctions returns the names of functions declared as extern - // pUser can pass a pointer to routines defined by AddFunction - - void SetIdent(long n); - // associates an identifier with the instance CBotProgram - - long GetIdent(); - // gives the identifier - - int GetError(); - bool GetError(int& code, int& start, int& end); - bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); - // if true - // gives the error found in the compilation - // or execution - // delimits the start and end block where the error - // pProg lets you know what "module" has produced runtime error - static CBotString GetErrorText(int code); - - - bool Start(const char* name); - // defines what function should be executed - // returns false if the funtion name is not found - // the program does nothing, we must call Run () for this - - bool Run(void* pUser = nullptr, int timer = -1); - // executes the program - // returns false if the program was suspended - // returns true if the program ended with or without error - // timer = 0 allows to advance step by step - - bool GetRunPos(const char* &FunctionName, int &start, int &end); - // gives the position in the executing program - // returns false if it is not running (program completion) - // FunctionName is a pointer made to the name of the function - // start and end position in the text of the token processing - - CBotVar* GetStackVars(const char* &FunctionName, int level); - // provides the pointer to the variables on the execution stack - // level is an input parameter, 0 for the last level, -1, -2, etc. for the other levels - // the return value (CBotVar *) is a variable list (or nullptr) - // that can be processed as the list of parameters received by a routine - // FunctionName gives the name of the function where are these variables - // FunctionName == nullptr means that is more in a program (depending on level) - - void Stop(); - // stops execution of the program - // therefore quits "suspend" mode - - static - void SetTimer(int n); - // defines the number of steps (parts of instructions) to done - // in Run() before rendering hand "false" \TODO avant de rendre la main "false" - - static - bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); - // call this to add externally (**) - // a new function used by the program CBoT - - static - bool DefineNum(const char* name, long val); - - bool SaveState(FILE* pf); - // backup the execution status in the file - // the file must have been opened with the fopen call this dll (\TODO this library??) - // if the system crashes - bool RestoreState(FILE* pf); - // restores the state of execution from file - // the compiled program must obviously be the same - - bool GetPosition(const char* name, int& start, int& stop, - CBotGet modestart = GetPosExtern, - CBotGet modestop = GetPosBloc); - // gives the position of a routine in the original text - // the user can select the item to find from the beginning to the end - // see the above modes in CBotGet - - - CBotFunction* GetFunctions(); -}; - - /////////////////////////////////////////////////////////////////////////////// // routines for file management (* FILE) FILE* fOpen(const char* name, const char* mode); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 27485f85..23752d5d 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -17,9 +17,7 @@ * along with this program. If not, see http://gnu.org/licenses */ -////////////////////////////////////////////////////////////////////// -// database management of CBoT program - +// Modules inlcude #include "CBot.h" #include "CBotCall.h" @@ -27,8 +25,14 @@ #include "CBotClass.h" #include "CBotUtils.h" +#include "StringFunctions.cpp" + +// Local include + +// Global include #include +//////////////////////////////////////////////////////////////////////////////// CBotProgram::CBotProgram() { m_Prog = nullptr; @@ -41,6 +45,7 @@ CBotProgram::CBotProgram() m_Ident = 0; } +//////////////////////////////////////////////////////////////////////////////// CBotProgram::CBotProgram(CBotVar* pInstance) { m_Prog = nullptr; @@ -53,7 +58,7 @@ CBotProgram::CBotProgram(CBotVar* pInstance) m_Ident = 0; } - +//////////////////////////////////////////////////////////////////////////////// CBotProgram::~CBotProgram() { // delete m_pClass; @@ -70,7 +75,7 @@ CBotProgram::~CBotProgram() #endif } - +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, void* pUser ) { int error = 0; @@ -165,7 +170,7 @@ bool CBotProgram::Compile( const char* program, CBotStringArray& ListFonctions, return (m_Prog != nullptr); } - +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::Start(const char* name) { #if STACKMEM @@ -199,6 +204,7 @@ bool CBotProgram::Start(const char* name) return true; // we are ready for Run () } +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet modestart, CBotGet modestop) { CBotFunction* p = m_Prog; @@ -214,6 +220,7 @@ bool CBotProgram::GetPosition(const char* name, int& start, int& stop, CBotGet m return true; } +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::Run(void* pUser, int timer) { bool ok; @@ -268,6 +275,7 @@ error: return true; } +//////////////////////////////////////////////////////////////////////////////// void CBotProgram::Stop() { #if STACKMEM @@ -279,8 +287,7 @@ void CBotProgram::Stop() m_pRun = nullptr; } - - +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) { FunctionName = nullptr; @@ -291,6 +298,7 @@ bool CBotProgram::GetRunPos(const char* &FunctionName, int &start, int &end) return true; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotProgram::GetStackVars(const char* &FunctionName, int level) { FunctionName = nullptr; @@ -299,26 +307,31 @@ CBotVar* CBotProgram::GetStackVars(const char* &FunctionName, int level) return m_pStack->GetStackVars(FunctionName, level); } +//////////////////////////////////////////////////////////////////////////////// void CBotProgram::SetTimer(int n) { CBotStack::SetTimer( n ); } +//////////////////////////////////////////////////////////////////////////////// int CBotProgram::GetError() { return m_ErrorCode; } +//////////////////////////////////////////////////////////////////////////////// void CBotProgram::SetIdent(long n) { m_Ident = n; } +//////////////////////////////////////////////////////////////////////////////// long CBotProgram::GetIdent() { return m_Ident; } +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::GetError(int& code, int& start, int& end) { code = m_ErrorCode; @@ -327,6 +340,7 @@ bool CBotProgram::GetError(int& code, int& start, int& end) return code > 0; } +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) { code = m_ErrorCode; @@ -336,6 +350,7 @@ bool CBotProgram::GetError(int& code, int& start, int& end, CBotProgram* &pProg) return code > 0; } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotProgram::GetErrorText(int code) { CBotString TextError; @@ -350,12 +365,13 @@ CBotString CBotProgram::GetErrorText(int code) return TextError; } - +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotProgram::GetFunctions() { return m_Prog; } +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::AddFunction(const char* name, bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), CBotTypResult rCompile (CBotVar* &pVar, void* pUser)) @@ -364,7 +380,7 @@ bool CBotProgram::AddFunction(const char* name, return CBotCall::AddFunction(name, rExec, rCompile); } - +//////////////////////////////////////////////////////////////////////////////// bool WriteWord(FILE* pf, unsigned short w) { size_t lg; @@ -374,6 +390,7 @@ bool WriteWord(FILE* pf, unsigned short w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// bool ReadWord(FILE* pf, unsigned short& w) { size_t lg; @@ -383,6 +400,7 @@ bool ReadWord(FILE* pf, unsigned short& w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// bool WriteFloat(FILE* pf, float w) { size_t lg; @@ -392,6 +410,7 @@ bool WriteFloat(FILE* pf, float w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// bool ReadFloat(FILE* pf, float& w) { size_t lg; @@ -401,6 +420,7 @@ bool ReadFloat(FILE* pf, float& w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// bool WriteLong(FILE* pf, long w) { size_t lg; @@ -410,6 +430,7 @@ bool WriteLong(FILE* pf, long w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// bool ReadLong(FILE* pf, long& w) { size_t lg; @@ -419,6 +440,7 @@ bool ReadLong(FILE* pf, long& w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// bool WriteString(FILE* pf, CBotString s) { size_t lg1, lg2; @@ -430,6 +452,7 @@ bool WriteString(FILE* pf, CBotString s) return (lg1 == lg2); } +//////////////////////////////////////////////////////////////////////////////// bool ReadString(FILE* pf, CBotString& s) { unsigned short w; @@ -445,6 +468,7 @@ bool ReadString(FILE* pf, CBotString& s) return (lg1 == lg2); } +//////////////////////////////////////////////////////////////////////////////// bool WriteType(FILE* pf, CBotTypResult type) { int typ = type.GetType(); @@ -464,6 +488,7 @@ bool WriteType(FILE* pf, CBotTypResult type) return true; } +//////////////////////////////////////////////////////////////////////////////// bool ReadType(FILE* pf, CBotTypResult& type) { unsigned short w, ww; @@ -494,13 +519,40 @@ bool ReadType(FILE* pf, CBotTypResult& type) return true; } +//////////////////////////////////////////////////////////////////////////////// +bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) +{ + if ( pVar == nullptr ) return TX_LOWPARAM; + int i = 0; + pVar = pVar->GetItemList(); + + while ( pVar != nullptr ) + { + i++; + pVar = pVar->GetNext(); + } + + pResult->SetValInt(i); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) +{ + if ( pVar == nullptr ) return CBotTypResult( TX_LOWPARAM ); + if ( pVar->GetType() != CBotTypArrayPointer ) + return CBotTypResult( TX_BADPARAM ); + return CBotTypResult( CBotTypInt ); +} + +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::DefineNum(const char* name, long val) { return CBotToken::DefineNum(name, val); } - +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::SaveState(FILE* pf) { if (!WriteWord( pf, CBOTVERSION)) return false; @@ -519,7 +571,7 @@ bool CBotProgram::SaveState(FILE* pf) return true; } - +//////////////////////////////////////////////////////////////////////////////// bool CBotProgram::RestoreState(FILE* pf) { unsigned short w; @@ -553,12 +605,43 @@ bool CBotProgram::RestoreState(FILE* pf) return true; } +//////////////////////////////////////////////////////////////////////////////// int CBotProgram::GetVersion() { return CBOTVERSION; } +//////////////////////////////////////////////////////////////////////////////// +void CBotProgram::Init() +{ + CBotToken::DefineNum("CBotErrZeroDiv", TX_DIVZERO); // division by zero + CBotToken::DefineNum("CBotErrNotInit", TX_NOTINIT); // uninitialized variable + CBotToken::DefineNum("CBotErrBadThrow", TX_BADTHROW); // throw a negative value + //CBotToken::DefineNum("CBotErrNoRetVal", 6003); // function did not return results // TODO: Not used. I'm pretty sure not returning a value crashes the game :P + CBotToken::DefineNum("CBotErrNoRun", TX_NORUN); // active Run () without a function // TODO: Is this actually a runtime error? + CBotToken::DefineNum("CBotErrUndefFunc", TX_NOCALL); // Calling a function that no longer exists + CBotToken::DefineNum("CBotErrUndefClass", TX_NOCLASS); // Class no longer exists + CBotToken::DefineNum("CBotErrNullPointer", TX_NULLPT); // Attempted to use a null pointer + CBotToken::DefineNum("CBotErrNan", TX_OPNAN); // Can't do operations on nan + CBotToken::DefineNum("CBotErrOutOfBounds", TX_OUTARRAY); // Attempted access out of bounds of an array + CBotToken::DefineNum("CBotErrStackOverflow", TX_STACKOVER); // Stack overflow + CBotToken::DefineNum("CBotErrDeletedObject", TX_DELETEDPT); // Attempted to use deleted object + CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); + + InitStringFunctions(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotProgram::Free() +{ + CBotToken::Free() ; + CBotCall ::Free() ; + CBotClass::Free() ; +} + + +//////////////////////////////////////////////////////////////////////////////// CBotCallMethode::CBotCallMethode(const char* name, bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) @@ -570,6 +653,7 @@ CBotCallMethode::CBotCallMethode(const char* name, m_nFuncIdent = CBotVar::NextUniqNum(); } +//////////////////////////////////////////////////////////////////////////////// CBotCallMethode::~CBotCallMethode() { delete m_next; @@ -578,7 +662,7 @@ CBotCallMethode::~CBotCallMethode() // is acceptable by a call procedure name // and given parameters - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, CBotVar** ppVar, CBotCStack* pStack, long& nIdent) @@ -607,17 +691,19 @@ CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, return CBotTypResult(-1); } - +//////////////////////////////////////////////////////////////////////////////// CBotString CBotCallMethode::GetName() { return m_name; } +//////////////////////////////////////////////////////////////////////////////// CBotCallMethode* CBotCallMethode::Next() { return m_next; } +//////////////////////////////////////////////////////////////////////////////// void CBotCallMethode::AddNext(CBotCallMethode* pt) { CBotCallMethode* p = this; @@ -626,7 +712,7 @@ void CBotCallMethode::AddNext(CBotCallMethode* pt) p->m_next = pt; } - +//////////////////////////////////////////////////////////////////////////////// int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken) { CBotCallMethode* pt = this; @@ -698,59 +784,3 @@ int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBot return -1; } - -bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) -{ - if ( pVar == nullptr ) return TX_LOWPARAM; - - int i = 0; - pVar = pVar->GetItemList(); - - while ( pVar != nullptr ) - { - i++; - pVar = pVar->GetNext(); - } - - pResult->SetValInt(i); - return true; -} - -CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) -{ - if ( pVar == nullptr ) return CBotTypResult( TX_LOWPARAM ); - if ( pVar->GetType() != CBotTypArrayPointer ) - return CBotTypResult( TX_BADPARAM ); - return CBotTypResult( CBotTypInt ); -} - - -// TODO: Refactor this - including .cpp files is bad -#include "StringFunctions.cpp" - -void CBotProgram::Init() -{ - CBotToken::DefineNum("CBotErrZeroDiv", TX_DIVZERO); // division by zero - CBotToken::DefineNum("CBotErrNotInit", TX_NOTINIT); // uninitialized variable - CBotToken::DefineNum("CBotErrBadThrow", TX_BADTHROW); // throw a negative value - //CBotToken::DefineNum("CBotErrNoRetVal", 6003); // function did not return results // TODO: Not used. I'm pretty sure not returning a value crashes the game :P - CBotToken::DefineNum("CBotErrNoRun", TX_NORUN); // active Run () without a function // TODO: Is this actually a runtime error? - CBotToken::DefineNum("CBotErrUndefFunc", TX_NOCALL); // Calling a function that no longer exists - CBotToken::DefineNum("CBotErrUndefClass", TX_NOCLASS); // Class no longer exists - CBotToken::DefineNum("CBotErrNullPointer", TX_NULLPT); // Attempted to use a null pointer - CBotToken::DefineNum("CBotErrNan", TX_OPNAN); // Can't do operations on nan - CBotToken::DefineNum("CBotErrOutOfBounds", TX_OUTARRAY); // Attempted access out of bounds of an array - CBotToken::DefineNum("CBotErrStackOverflow", TX_STACKOVER); // Stack overflow - CBotToken::DefineNum("CBotErrDeletedObject", TX_DELETEDPT); // Attempted to use deleted object - - CBotProgram::AddFunction("sizeof", rSizeOf, cSizeOf ); - - InitStringFunctions(); -} - -void CBotProgram::Free() -{ - CBotToken::Free() ; - CBotCall ::Free() ; - CBotClass::Free() ; -} diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h new file mode 100644 index 00000000..02aa5372 --- /dev/null +++ b/src/CBot/CBotProgram.h @@ -0,0 +1,260 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude + +// Local include + +// Global include + + +/*! + * \brief The CBotProgram class Main class managing CBot program. + */ +class CBotProgram +{ +public: + + /*! + * \brief CBotProgram + */ + CBotProgram(); + + /*! + * \brief CBotProgram + * \param pInstance + */ + CBotProgram(CBotVar* pInstance); + + /*! + * \brief ~CBotProgram + */ + ~CBotProgram(); + + /*! + * \brief Init Initializes the module (defined keywords for errors) should + * be done once (and only one) at the beginning. + */ + static void Init(); + + /*! + * \brief Free Frees the static memory areas. + */ + static void Free(); + + /*! + * \brief GetVersion Gives the version of the library CBOT. + * \return + */ + static int GetVersion(); + + /*! + * \brief Compile compiles the program given in text. + * \param program + * \param ListFonctions Returns the names of functions declared as extern. + * \param pUser Can pass a pointer to routines defined by AddFunction. + * \return false if an error at compile. + * \see GetCompileError() to retrieve the error. + */ + bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = nullptr); + + /*! + * \brief SetIdent Associates an identifier with the instance CBotProgram. + * \param n + */ + void SetIdent(long n); + + /*! + * \brief GetIdent Gives the identifier. + * \return + */ + long GetIdent(); + + /*! + * \brief GetError + * \return + */ + int GetError(); + + /*! + * \brief GetError + * \param code + * \param start + * \param end + * \return + */ + bool GetError(int& code, int& start, int& end); + + /*! + * \brief GetError + * \param code + * \param start Delimits the start block where the error occured. + * \param end Delimits the end block where the error occured. + * \param pProg Lets you know what "module" has produced runtime error. + * \return If true gives the error found in the compilation or execution. + */ + bool GetError(int& code, int& start, int& end, CBotProgram* &pProg); + + /*! + * \brief GetErrorText + * \param code + * \return + */ + static CBotString GetErrorText(int code); + + /*! + * \brief Start Defines what function should be executed. The program does + * nothing, we must call Run () for this. + * \param name + * \return false if the funtion name is not found + */ + bool Start(const char* name); + + /*! + * \brief Run Executes the program. + * \param pUser + * \param timer timer = 0 allows to advance step by step. + * \return false if the program was suspended, true if the program ended + * with or without error. + */ + bool Run(void* pUser = nullptr, int timer = -1); + + /*! + * \brief GetRunPos Gives the position in the executing program + * \param FunctionName is a pointer made to the name of the function + * \param start start and end position in the text of the token processing + * \param end + * \return false if it is not running (program completion) + */ + bool GetRunPos(const char* &FunctionName, int &start, int &end); + + /*! + * \brief GetStackVars provides the pointer to the variables on the + * execution stack level is an input parameter, 0 for the last level, -1, + * -2, etc. for the other levels the return value (CBotVar *) is a variable. + * that can be processed as the list of parameters received by a routine + * list (or nullptr) + * \param FunctionName gives the name of the function where are these + * variables. FunctionName == nullptr means that is more in a program + * (depending on level) + * \param level + * \return + */ + CBotVar* GetStackVars(const char* &FunctionName, int level); + + /*! + * \brief Stop stops execution of the program therefore quits "suspend" mode + */ + void Stop(); + + /*! + * \brief SetTimer defines the number of steps (parts of instructions) to + * done in Run() before rendering hand "false" + * \TODO avant de rendre la main "false" + * \param n + */ + static void SetTimer(int n); + // + // + + /*! + * \brief AddFunction Call this to add externally (**) a new function used + * by the program CBoT. + * \param name + * \param rExec + * \param rCompile + * \return + */ + static bool AddFunction(const char* name, + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + + /*! + * \brief DefineNum + * \param name + * \param val + * \return + */ + static bool DefineNum(const char* name, long val); + + /*! + * \brief SaveState Backup the execution status in the file the file must + * have been opened with the fopen call this dll (\TODO this library??) + * if the system crashes + * \param pf + * \return + */ + bool SaveState(FILE* pf); + + /*! + * \brief RestoreState Restores the state of execution from file the + * compiled program must obviously be the same. + * \param pf + * \return + */ + bool RestoreState(FILE* pf); + + /*! + * \brief GetPosition Gives the position of a routine in the original text + * the user can select the item to find from the beginning to the end + * see the above modes in CBotGet. + * \param name + * \param start + * \param stop + * \param modestart + * \param modestop + * \return + */ + bool GetPosition(const char* name, int& start, int& stop, + CBotGet modestart = GetPosExtern, + CBotGet modestop = GetPosBloc); + + /*! + * \brief GetFunctions + * \return + */ + CBotFunction* GetFunctions(); + + /*! + * \brief m_bCompileClass + */ + bool m_bCompileClass; + +private: + + //! The user-defined functions. + CBotFunction* m_Prog; + //! The basic function for the execution. + CBotFunction* m_pRun; + //! Classes defined in this part. + CBotClass* m_pClass; + //! Execution stack. + CBotStack* m_pStack; + //! Instance of the parent class. + CBotVar* m_pInstance; + friend class CBotFunction; + + int m_ErrorCode; + int m_ErrorStart; + int m_ErrorEnd; + //! Associated identifier. + long m_Ident; +}; diff --git a/src/script/script.h b/src/script/script.h index d57d2711..d4a42eb7 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -24,7 +24,9 @@ #pragma once +// TODO replace by CBot.h #include "CBot/CBotDll.h" +#include "CBot/CBotProgram.h" #include #include From 4712e0ef6a1d3512e3ac0097e325b3d97a879422 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 18:31:57 +0100 Subject: [PATCH 52/91] Moving CBotFunction class in its own header and source files. --- src/CBot/CBot.h | 67 ----- src/CBot/CBotClass.cpp | 1 + src/CBot/{ => CBotInstr}/CBotFunction.cpp | 73 +++--- src/CBot/CBotInstr/CBotFunction.h | 282 ++++++++++++++++++++++ src/CBot/CBotProgram.cpp | 2 + src/CBot/CBotStack.cpp | 2 + src/CBot/CMakeLists.txt | 2 +- 7 files changed, 331 insertions(+), 98 deletions(-) rename src/CBot/{ => CBotInstr}/CBotFunction.cpp (93%) create mode 100644 src/CBot/CBotInstr/CBotFunction.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index e98a1c6c..33efc71f 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -550,70 +550,3 @@ public: CBotString GetParamString(); }; - - -// a function declaration - -class CBotFunction : CBotInstr -{ -private: - // management of list of (static) public functions - static - CBotFunction* m_listPublic; - CBotFunction* m_nextpublic; - CBotFunction* m_prevpublic; - friend class CBotCStack; -// long m_nThisIdent; - long m_nFuncIdent; - bool m_bSynchro; // synchronized method? - -private: - CBotDefParam* m_Param; // parameter list - CBotInstr* m_Block; // the instruction block - CBotFunction* m_next; - CBotToken m_retToken; // if returns CBotTypClass - CBotTypResult m_retTyp; // complete type of the result - - bool m_bPublic; // public function - bool m_bExtern; // extern function - CBotString m_MasterClass; // name of the class we derive - CBotProgram* m_pProg; - friend class CBotProgram; - friend class CBotClass; - - CBotToken m_extern; // for the position of the word "extern" - CBotToken m_openpar; - CBotToken m_closepar; - CBotToken m_openblk; - CBotToken m_closeblk; -public: - CBotFunction(); - ~CBotFunction(); - static - CBotFunction* Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* pFunc, bool bLocal = true); - static - CBotFunction* Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass); - bool Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = nullptr); - void RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance = nullptr); - - void AddNext(CBotFunction* p); - CBotTypResult CompileCall(const char* name, CBotVar** ppVars, long& nIdent); - CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic = true); - - int DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken); - void RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack); - int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass); - void RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass); - bool CheckParam(CBotDefParam* pParam); - - static - void AddPublic(CBotFunction* pfunc); - - CBotString GetName(); - CBotString GetParams(); - bool IsPublic(); - bool IsExtern(); - CBotFunction* Next(); - - bool GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop); -}; diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 9c18f2c9..f37d55c5 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -23,6 +23,7 @@ #include "CBotInstr/CBotNew.h" #include "CBotInstr/CBotLeftExprVar.h" #include "CBotInstr/CBotTwoOpExpr.h" +#include "CBotInstr/CBotFunction.h" #include "CBotCall.h" diff --git a/src/CBot/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp similarity index 93% rename from src/CBot/CBotFunction.cpp rename to src/CBot/CBotInstr/CBotFunction.cpp index 61a98870..61428da4 100644 --- a/src/CBot/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -17,9 +17,9 @@ * along with this program. If not, see http://gnu.org/licenses */ -/////////////////////////////////////////////////////////////////////// -// compilation of various functions declared by the user -// + +// Modules inlcude +#include "CBotFunction.h" #include "CBot.h" @@ -32,12 +32,13 @@ #include "CBotStack.h" #include "CBotClass.h" +// Local include + +// Global include #include -// various constructors / destructors -// \TODO translation:to liberate all according to esteblished tree -// pour libérer tout selon l'arbre établi +//////////////////////////////////////////////////////////////////////////////// CBotFunction::CBotFunction() { m_Param = nullptr; // empty parameter list @@ -53,8 +54,10 @@ CBotFunction::CBotFunction() m_bSynchro = false; } +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotFunction::m_listPublic = nullptr; +//////////////////////////////////////////////////////////////////////////////// CBotFunction::~CBotFunction() { delete m_Param; // empty parameter list @@ -80,16 +83,19 @@ CBotFunction::~CBotFunction() } } +//////////////////////////////////////////////////////////////////////////////// bool CBotFunction::IsPublic() { return m_bPublic; } +//////////////////////////////////////////////////////////////////////////////// bool CBotFunction::IsExtern() { return m_bExtern; } +//////////////////////////////////////////////////////////////////////////////// bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet modestop) { start = m_extern.GetStart(); @@ -131,7 +137,7 @@ bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet return true; } - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) { while ( IsOfType( p, ID_OPBRK ) ) @@ -146,6 +152,7 @@ CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) return type; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) { CBotClass* pClass = nullptr; @@ -183,9 +190,7 @@ CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) return CBotTypResult( -1 ); } -// compiles a new function -// bLocal allows of the declaration of parameters on the same level -// as the elements belonging to the class for methods +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, bool bLocal) { CBotToken* pp; @@ -298,7 +303,7 @@ bad: return pStack->ReturnFunc(nullptr, pStk); } -// pre-compile a new function +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClass* pClass) { CBotFunction* func = new CBotFunction(); @@ -385,6 +390,7 @@ bad: static int xx = 0; #endif +//////////////////////////////////////////////////////////////////////////////// bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) { CBotStack* pile = pj->AddStack(this, 2); // one end of stack local to this function @@ -434,7 +440,7 @@ bool CBotFunction::Execute(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) return pj->Return(pile); } - +//////////////////////////////////////////////////////////////////////////////// void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInstance) { CBotStack* pile = pj->RestoreStack(this); // one end of stack local to this function @@ -463,6 +469,7 @@ void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInst m_Block->RestoreState(pile2, true); } +//////////////////////////////////////////////////////////////////////////////// void CBotFunction::AddNext(CBotFunction* p) { CBotFunction* pp = this; @@ -471,7 +478,7 @@ void CBotFunction::AddNext(CBotFunction* p) pp->m_next = p; } - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long& nIdent) { nIdent = 0; @@ -482,10 +489,7 @@ CBotTypResult CBotFunction::CompileCall(const char* name, CBotVar** ppVars, long return type; } - -// is a function according to its unique identifier -// if the identifier is not found, looking by name and parameters - +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CBotVar** ppVars, CBotTypResult& TypeOrError, bool bPublic) { TypeOrError.SetType(TX_UNDEFCALL); // no routine of the name @@ -638,9 +642,7 @@ CBotFunction* CBotFunction::FindLocalOrPublic(long& nIdent, const char* name, CB return nullptr; } - -// fait un appel à une fonction - +//////////////////////////////////////////////////////////////////////////////// int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken) { CBotTypResult type; @@ -710,6 +712,7 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar** ppVars, CBotS return -1; } +//////////////////////////////////////////////////////////////////////////////// void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, CBotStack* pStack) { CBotTypResult type; @@ -766,11 +769,7 @@ void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar** ppVars, } } - - -// makes call of a method -// note: this is already on the stack, the pointer pThis is just to simplify - +//////////////////////////////////////////////////////////////////////////////// int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotToken* pToken, CBotClass* pClass) { CBotTypResult type; @@ -851,6 +850,7 @@ int CBotFunction::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar return -1; } +//////////////////////////////////////////////////////////////////////////////// void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotStack* pStack, CBotClass* pClass) { CBotTypResult type; @@ -883,7 +883,7 @@ void CBotFunction::RestoreCall(long& nIdent, const char* name, CBotVar* pThis, C } } -// see if the "signature" of parameters is identical +//////////////////////////////////////////////////////////////////////////////// bool CBotFunction::CheckParam(CBotDefParam* pParam) { CBotDefParam* pp = m_Param; @@ -898,11 +898,13 @@ bool CBotFunction::CheckParam(CBotDefParam* pParam) return ( pp == nullptr && pParam == nullptr ); } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotFunction::GetName() { return m_token.GetString(); } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotFunction::GetParams() { if ( m_Param == nullptr ) return CBotString("()"); @@ -921,11 +923,13 @@ CBotString CBotFunction::GetParams() return params; } +//////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotFunction::Next() { return m_next; } +//////////////////////////////////////////////////////////////////////////////// void CBotFunction::AddPublic(CBotFunction* func) { if ( m_listPublic != nullptr ) @@ -941,19 +945,20 @@ void CBotFunction::AddPublic(CBotFunction* func) ///////////////////////////////////////////////////////////////////////// // management of parameters - +//////////////////////////////////////////////////////////////////////////////// CBotDefParam::CBotDefParam() { m_next = nullptr; m_nIdent = 0; } +//////////////////////////////////////////////////////////////////////////////// CBotDefParam::~CBotDefParam() { delete m_next; } - +//////////////////////////////////////////////////////////////////////////////// // compiles a list of parameters CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) { @@ -1014,6 +1019,7 @@ CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// void CBotDefParam::AddNext(CBotDefParam* p) { CBotDefParam* pp = this; @@ -1022,7 +1028,7 @@ void CBotDefParam::AddNext(CBotDefParam* p) pp->m_next = p; } - +//////////////////////////////////////////////////////////////////////////////// bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) { int i = 0; @@ -1072,6 +1078,7 @@ bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) return true; } +//////////////////////////////////////////////////////////////////////////////// void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) { // int i = 0; @@ -1086,21 +1093,25 @@ void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) } } +//////////////////////////////////////////////////////////////////////////////// int CBotDefParam::GetType() { return m_type.GetType(); } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotDefParam::GetTypResult() { return m_type; } +//////////////////////////////////////////////////////////////////////////////// CBotDefParam* CBotDefParam::GetNext() { return m_next; } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotDefParam::GetParamString() { CBotString param; @@ -1118,6 +1129,7 @@ CBotString CBotDefParam::GetParamString() // pre-compile a new class // analysis is complete except the body of routines +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) { if ( !IsOfType(p, ID_PUBLIC) ) @@ -1173,6 +1185,7 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) { bool bStatic = false; @@ -1376,7 +1389,7 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) return pStack->IsOk(); } - +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) { if ( !IsOfType(p, ID_PUBLIC) ) return nullptr; diff --git a/src/CBot/CBotInstr/CBotFunction.h b/src/CBot/CBotInstr/CBotFunction.h new file mode 100644 index 00000000..486ffff8 --- /dev/null +++ b/src/CBot/CBotInstr/CBotFunction.h @@ -0,0 +1,282 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotFunction class A function declaration. Compilation of various + * functions declared by the user + */ +class CBotFunction : public CBotInstr +{ +public: + + /*! + * \brief CBotFunction + */ + CBotFunction(); + + /*! + * \brief ~CBotFunction + */ + ~CBotFunction(); + + /*! + * \brief Compile Compiles a new function + * \param p + * \param pStack + * \param pFunc + * \param bLocal allows of the declaration of parameters on the same level + * as the elements belonging to the class for methods. + * \return + */ + static CBotFunction* Compile(CBotToken* &p, + CBotCStack* pStack, + CBotFunction* pFunc, + bool bLocal = true); + + /*! + * \brief Compile1 Pre-compile a new function. + * \param p + * \param pStack + * \param pClass + * \return + */ + static CBotFunction* Compile1(CBotToken* &p, + CBotCStack* pStack, + CBotClass* pClass); + + /*! + * \brief Execute + * \param ppVars + * \param pj + * \param pInstance + * \return + */ + bool Execute(CBotVar** ppVars, + CBotStack* &pj, + CBotVar* pInstance = nullptr); + + /*! + * \brief RestoreState + * \param ppVars + * \param pj + * \param pInstance + */ + void RestoreState(CBotVar** ppVars, + CBotStack* &pj, + CBotVar* pInstance = nullptr); + + /*! + * \brief AddNext + * \param p + */ + void AddNext(CBotFunction* p); + + /*! + * \brief CompileCall + * \param name + * \param ppVars + * \param nIdent + * \return + */ + CBotTypResult CompileCall(const char* name, + CBotVar** ppVars, + long& nIdent); + + /*! + * \brief FindLocalOrPublic Is a function according to its unique identifier + * if the identifier is not found, looking by name and parameters. + * \param nIdent + * \param name + * \param ppVars + * \param TypeOrError + * \param bPublic + * \return + */ + CBotFunction* FindLocalOrPublic(long& nIdent, const char* name, + CBotVar** ppVars, + CBotTypResult& TypeOrError, + bool bPublic = true); + + /*! + * \brief DoCall Fait un appel à une fonction. + * \param nIdent + * \param name + * \param ppVars + * \param pStack + * \param pToken + * \return + */ + + int DoCall(long& nIdent, + const char* name, + CBotVar** ppVars, + CBotStack* pStack, + CBotToken* pToken); + + /*! + * \brief RestoreCall + * \param nIdent + * \param name + * \param ppVars + * \param pStack + */ + void RestoreCall(long& nIdent, + const char* name, + CBotVar** ppVars, + CBotStack* pStack); + + /*! + * \brief DoCall Makes call of a method note: this is already on the stack, + * the pointer pThis is just to simplify. + * \param nIdent + * \param name + * \param pThis + * \param ppVars + * \param pStack + * \param pToken + * \param pClass + * \return + */ + int DoCall(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppVars, + CBotStack* pStack, + CBotToken* pToken, + CBotClass* pClass); + + /*! + * \brief RestoreCall + * \param nIdent + * \param name + * \param pThis + * \param ppVars + * \param pStack + * \param pClass + */ + void RestoreCall(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppVars, + CBotStack* pStack, + CBotClass* pClass); + + /*! + * \brief CheckParam See if the "signature" of parameters is identical. + * \param pParam + * \return + */ + bool CheckParam(CBotDefParam* pParam); + + /*! + * \brief AddPublic + * \param pfunc + */ + static void AddPublic(CBotFunction* pfunc); + + /*! + * \brief GetName + * \return + */ + CBotString GetName(); + + /*! + * \brief GetParams + * \return + */ + CBotString GetParams(); + + /*! + * \brief IsPublic + * \return + */ + bool IsPublic(); + + /*! + * \brief IsExtern + * \return + */ + bool IsExtern(); + + /*! + * \brief Next + * \return + */ + CBotFunction* Next(); + + /*! + * \brief GetPosition + * \param start + * \param stop + * \param modestart + * \param modestop + * \return + */ + bool GetPosition(int& start, int& stop, + CBotGet modestart, + CBotGet modestop); + +private: + CBotFunction* m_nextpublic; + CBotFunction* m_prevpublic; + long m_nFuncIdent; + //! Synchronized method. + bool m_bSynchro; + + //! Parameter list. + CBotDefParam* m_Param; + //! The instruction block. + CBotInstr* m_Block; + CBotFunction* m_next; + //! If returns CBotTypClass. + CBotToken m_retToken; + //! Complete type of the result. + CBotTypResult m_retTyp; + //! Public function. + bool m_bPublic; + //! Extern function. + bool m_bExtern; + //! Name of the class we derive. + CBotString m_MasterClass; + CBotProgram* m_pProg; + //! For the position of the word "extern". + CBotToken m_extern; + CBotToken m_openpar; + CBotToken m_closepar; + CBotToken m_openblk; + CBotToken m_closeblk; + + //! Management of list of (static) public functions. + static CBotFunction* m_listPublic; + + friend class CBotProgram; + friend class CBotClass; + friend class CBotCStack; + +}; diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 23752d5d..8e492bac 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -25,6 +25,8 @@ #include "CBotClass.h" #include "CBotUtils.h" +#include "CBotInstr/CBotFunction.h" + #include "StringFunctions.cpp" // Local include diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 0997f468..0a767135 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -21,6 +21,8 @@ #include "CBotStack.h" #include "CBotCall.h" +#include "CBotInstr/CBotFunction.h" + // Local include // Global include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 5bb6ad70..7aee901b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,7 +1,6 @@ set(SOURCES CBot.cpp CBotClass.cpp - CBotFunction.cpp CBotProgram.cpp CBotStack.cpp CBotString.cpp @@ -53,6 +52,7 @@ set(SOURCES CBotInstr/CBotListArray.cpp CBotInstr/CBotInstArray.cpp CBotInstr/CBotInt.cpp + CBotInstr/CBotFunction.cpp CBotVar/CBotVarArray.cpp ) From 0a1b7da2a80aaaf1fe31174371da746ccde51a7e Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 18:40:16 +0100 Subject: [PATCH 53/91] Moving CBotClass functions into CBotClass.cpp. Moving gloable function used by CBotClass and CBotFunction into CBotUtils.cpp. --- src/CBot/CBotClass.cpp | 313 +++++++++++++++++++++++ src/CBot/CBotInstr/CBotFunction.cpp | 370 +--------------------------- src/CBot/CBotUtils.cpp | 57 +++++ src/CBot/CBotUtils.h | 17 ++ 4 files changed, 389 insertions(+), 368 deletions(-) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index f37d55c5..d2d03e92 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -24,8 +24,13 @@ #include "CBotInstr/CBotLeftExprVar.h" #include "CBotInstr/CBotTwoOpExpr.h" #include "CBotInstr/CBotFunction.h" +#include "CBotInstr/CBotExpression.h" +#include "CBotInstr/CBotListArray.h" +#include "CBotInstr/CBotEmpty.h" #include "CBotCall.h" +#include "CBotStack.h" +#include "CBotUtils.h" // Local include @@ -519,3 +524,311 @@ bool CBotClass::CheckCall(CBotToken* &pToken, return false; } + +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) +{ + if ( !IsOfType(p, ID_PUBLIC) ) + { + pStack->SetError(TX_NOPUBLIC, p); + return nullptr; + } + + if ( !IsOfType(p, ID_CLASS) ) return nullptr; + + CBotString name = p->GetString(); + + CBotClass* pOld = CBotClass::Find(name); + if ( pOld != nullptr && pOld->m_IsDef ) + { + pStack->SetError( TX_REDEFCLASS, p ); + return nullptr; + } + + // a name of the class is there? + if (IsOfType(p, TokenTypVar)) + { + CBotClass* pPapa = nullptr; + if ( IsOfType( p, ID_EXTENDS ) ) + { + CBotString name = p->GetString(); + pPapa = CBotClass::Find(name); + + if (!IsOfType(p, TokenTypVar) || pPapa == nullptr ) + { + pStack->SetError( TX_NOCLASS, p ); + return nullptr; + } + } + CBotClass* classe = (pOld == nullptr) ? new CBotClass(name, pPapa) : pOld; + classe->Purge(); // emptythe old definitions + classe->m_IsDef = false; // current definition + + if ( !IsOfType( p, ID_OPBLK) ) + { + pStack->SetError(TX_OPENBLK, p); + return nullptr; + } + + while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) + { + classe->CompileDefItem(p, pStack, false); + } + + if (pStack->IsOk()) return classe; + } + pStack->SetError(TX_ENDOF, p); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) +{ + bool bStatic = false; + int mProtect = PR_PUBLIC; + bool bSynchro = false; + + while (IsOfType(p, ID_SEP)) ; + + CBotTypResult type( -1 ); + + if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; + CBotToken* pBase = p; + + if ( IsOfType(p, ID_STATIC) ) bStatic = true; + if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; + if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; + if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; + if ( IsOfType(p, ID_STATIC) ) bStatic = true; + +// CBotClass* pClass = nullptr; + type = TypeParam(p, pStack); // type of the result + + if ( type.Eq(-1) ) + { + pStack->SetError(TX_NOTYP, p); + return false; + } + + while (pStack->IsOk()) + { + CBotToken* pp = p; + IsOfType(p, ID_NOT); // skips ~ eventual (destructor) + + if (IsOfType(p, TokenTypVar)) + { + CBotInstr* limites = nullptr; + while ( IsOfType( p, ID_OPBRK ) ) // a table? + { + CBotInstr* i = nullptr; + + if ( p->GetType() != ID_CLBRK ) + i = CBotExpression::Compile( p, pStack ); // expression for the value + else + i = new CBotEmpty(); // special if not a formula + + type = CBotTypResult(CBotTypArrayPointer, type); + + if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) + { + pStack->SetError(TX_CLBRK, p->GetStart()); + return false; + } + +/* CBotVar* pv = pStack->GetVar(); + if ( pv->GetType()>= CBotTypBoolean ) + { + pStack->SetError(TX_BADTYPE, p->GetStart()); + return false; + }*/ + + if (limites == nullptr) limites = i; + else limites->AddNext3(i); + } + + if ( p->GetType() == ID_OPENPAR ) + { + if ( !bSecond ) + { + p = pBase; + CBotFunction* f = + CBotFunction::Compile1(p, pStack, this); + + if ( f == nullptr ) return false; + + if (m_pMethod == nullptr) m_pMethod = f; + else m_pMethod->AddNext(f); + } + else + { + // return a method precompiled in pass 1 + CBotFunction* pf = m_pMethod; + CBotFunction* prev = nullptr; + while ( pf != nullptr ) + { + if (pf->GetName() == pp->GetString()) break; + prev = pf; + pf = pf->Next(); + } + + bool bConstructor = (pp->GetString() == GetName()); + CBotCStack* pile = pStack->TokenStack(nullptr, true); + + // make "this" known + CBotToken TokenThis(CBotString("this"), CBotString()); + CBotVar* pThis = CBotVar::Create(&TokenThis, CBotTypResult( CBotTypClass, this ) ); + pThis->SetUniqNum(-2); + pile->AddVar(pThis); + + if ( m_pParent ) + { + // makes "super" known + CBotToken TokenSuper(CBotString("super"), CBotString()); + CBotVar* pThis = CBotVar::Create(&TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) ); + pThis->SetUniqNum(-3); + pile->AddVar(pThis); + } + +// int num = 1; + CBotClass* my = this; + while (my != nullptr) + { + // places a copy of variables of a class (this) on a stack + CBotVar* pv = my->m_pVar; + while (pv != nullptr) + { + CBotVar* pcopy = CBotVar::Create(pv); + CBotVar::InitType initType = CBotVar::InitType::UNDEF; + if (!bConstructor || pv->IsStatic()) + initType = CBotVar::InitType::DEF; + pcopy->SetInit(initType); + pcopy->SetUniqNum(pv->GetUniqNum()); + pile->AddVar(pcopy); + pv = pv->GetNext(); + } + my = my->m_pParent; + } + + // compiles a method + p = pBase; + CBotFunction* f = + CBotFunction::Compile(p, pile, nullptr/*, false*/); + + if ( f != nullptr ) + { + f->m_pProg = pStack->GetBotCall(); + f->m_bSynchro = bSynchro; + // replaces the element in the chain + f->m_next = pf->m_next; + pf->m_next = nullptr; + delete pf; + if (prev == nullptr) m_pMethod = f; + else prev->m_next = f; + } + pStack->Return(nullptr, pile); + } + + return pStack->IsOk(); + } + + // definition of an element + if (type.Eq(0)) + { + pStack->SetError(TX_ENDOF, p); + return false; + } + + CBotInstr* i = nullptr; + if ( IsOfType(p, ID_ASS ) ) + { + if ( type.Eq(CBotTypArrayPointer) ) + { + i = CBotListArray::Compile(p, pStack, type.GetTypElem()); + } + else + { + // it has an assignmet to calculate + i = CBotTwoOpExpr::Compile(p, pStack); + } + if ( !pStack->IsOk() ) return false; + } + + + if ( !bSecond ) + { + CBotVar* pv = CBotVar::Create(pp->GetString(), type); + pv -> SetStatic( bStatic ); + pv -> SetPrivate( mProtect ); + + AddItem( pv ); + + pv->m_InitExpr = i; + pv->m_LimExpr = limites; + + + if ( pv->IsStatic() && pv->m_InitExpr != nullptr ) + { + CBotStack* pile = CBotStack::FirstStack(); // independent stack + while(pile->IsOk() && !pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer + pv->SetVal( pile->GetVar() ) ; + pile->Delete(); + } + } + else + delete i; + + if ( IsOfType(p, ID_COMMA) ) continue; + if ( IsOfType(p, ID_SEP) ) break; + } + pStack->SetError(TX_ENDOF, p); + } + return pStack->IsOk(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) +{ + if ( !IsOfType(p, ID_PUBLIC) ) return nullptr; + if ( !IsOfType(p, ID_CLASS) ) return nullptr; + + CBotString name = p->GetString(); + + // a name for the class is there? + if (IsOfType(p, TokenTypVar)) + { + // the class was created by Compile1 + CBotClass* pOld = CBotClass::Find(name); + + if ( IsOfType( p, ID_EXTENDS ) ) + { + // TODO: Not sure how correct is that - I have no idea how the precompilation (Compile1 method) works ~krzys_h + CBotString name = p->GetString(); + CBotClass* pPapa = CBotClass::Find(name); + + if (!IsOfType(p, TokenTypVar) || pPapa == nullptr) + { + pStack->SetError( TX_NOCLASS, p ); + return nullptr; + } + pOld->m_pParent = pPapa; + } + else + { + if (pOld != nullptr) + { + pOld->m_pParent = nullptr; + } + } + IsOfType( p, ID_OPBLK); // necessarily + + while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) + { + pOld->CompileDefItem(p, pStack, true); + } + + pOld->m_IsDef = true; // complete definition + if (pStack->IsOk()) return pOld; + } + pStack->SetError(TX_ENDOF, p); + return nullptr; +} diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 61428da4..94df0e7a 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -32,6 +32,8 @@ #include "CBotStack.h" #include "CBotClass.h" +#include "CBotUtils.h" + // Local include // Global include @@ -137,59 +139,6 @@ bool CBotFunction::GetPosition(int& start, int& stop, CBotGet modestart, CBotGet return true; } -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) -{ - while ( IsOfType( p, ID_OPBRK ) ) - { - if ( !IsOfType( p, ID_CLBRK ) ) - { - pile->SetError(TX_CLBRK, p->GetStart()); - return CBotTypResult( -1 ); - } - type = CBotTypResult( CBotTypArrayPointer, type ); - } - return type; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) -{ - CBotClass* pClass = nullptr; - - switch (p->GetType()) - { - case ID_INT: - p = p->GetNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypInt )); - case ID_FLOAT: - p = p->GetNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypFloat )); - case ID_BOOLEAN: - case ID_BOOL: - p = p->GetNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypBoolean )); - case ID_STRING: - p = p->GetNext(); - return ArrayType(p, pile, CBotTypResult( CBotTypString )); - case ID_VOID: - p = p->GetNext(); - return CBotTypResult( 0 ); - - case TokenTypVar: - pClass = CBotClass::Find(p); - if ( pClass != nullptr) - { - p = p->GetNext(); - return ArrayType(p, pile, - pClass->IsIntrinsic() ? - CBotTypResult( CBotTypIntrinsic, pClass ) : - CBotTypResult( CBotTypPointer, pClass ) ); - } - } - return CBotTypResult( -1 ); -} - //////////////////////////////////////////////////////////////////////////////// CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunction* finput, bool bLocal) { @@ -1122,318 +1071,3 @@ CBotString CBotDefParam::GetParamString() param += m_token.GetString(); return param; } - -////////////////////////////////////////////////////////////////////////////// -// statement of user classes - -// pre-compile a new class -// analysis is complete except the body of routines - -//////////////////////////////////////////////////////////////////////////////// -CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) -{ - if ( !IsOfType(p, ID_PUBLIC) ) - { - pStack->SetError(TX_NOPUBLIC, p); - return nullptr; - } - - if ( !IsOfType(p, ID_CLASS) ) return nullptr; - - CBotString name = p->GetString(); - - CBotClass* pOld = CBotClass::Find(name); - if ( pOld != nullptr && pOld->m_IsDef ) - { - pStack->SetError( TX_REDEFCLASS, p ); - return nullptr; - } - - // a name of the class is there? - if (IsOfType(p, TokenTypVar)) - { - CBotClass* pPapa = nullptr; - if ( IsOfType( p, ID_EXTENDS ) ) - { - CBotString name = p->GetString(); - pPapa = CBotClass::Find(name); - - if (!IsOfType(p, TokenTypVar) || pPapa == nullptr ) - { - pStack->SetError( TX_NOCLASS, p ); - return nullptr; - } - } - CBotClass* classe = (pOld == nullptr) ? new CBotClass(name, pPapa) : pOld; - classe->Purge(); // emptythe old definitions - classe->m_IsDef = false; // current definition - - if ( !IsOfType( p, ID_OPBLK) ) - { - pStack->SetError(TX_OPENBLK, p); - return nullptr; - } - - while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) - { - classe->CompileDefItem(p, pStack, false); - } - - if (pStack->IsOk()) return classe; - } - pStack->SetError(TX_ENDOF, p); - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) -{ - bool bStatic = false; - int mProtect = PR_PUBLIC; - bool bSynchro = false; - - while (IsOfType(p, ID_SEP)) ; - - CBotTypResult type( -1 ); - - if ( IsOfType(p, ID_SYNCHO) ) bSynchro = true; - CBotToken* pBase = p; - - if ( IsOfType(p, ID_STATIC) ) bStatic = true; - if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; - if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; - if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; - if ( IsOfType(p, ID_STATIC) ) bStatic = true; - -// CBotClass* pClass = nullptr; - type = TypeParam(p, pStack); // type of the result - - if ( type.Eq(-1) ) - { - pStack->SetError(TX_NOTYP, p); - return false; - } - - while (pStack->IsOk()) - { - CBotToken* pp = p; - IsOfType(p, ID_NOT); // skips ~ eventual (destructor) - - if (IsOfType(p, TokenTypVar)) - { - CBotInstr* limites = nullptr; - while ( IsOfType( p, ID_OPBRK ) ) // a table? - { - CBotInstr* i = nullptr; - - if ( p->GetType() != ID_CLBRK ) - i = CBotExpression::Compile( p, pStack ); // expression for the value - else - i = new CBotEmpty(); // special if not a formula - - type = CBotTypResult(CBotTypArrayPointer, type); - - if (!pStack->IsOk() || !IsOfType( p, ID_CLBRK ) ) - { - pStack->SetError(TX_CLBRK, p->GetStart()); - return false; - } - -/* CBotVar* pv = pStack->GetVar(); - if ( pv->GetType()>= CBotTypBoolean ) - { - pStack->SetError(TX_BADTYPE, p->GetStart()); - return false; - }*/ - - if (limites == nullptr) limites = i; - else limites->AddNext3(i); - } - - if ( p->GetType() == ID_OPENPAR ) - { - if ( !bSecond ) - { - p = pBase; - CBotFunction* f = - CBotFunction::Compile1(p, pStack, this); - - if ( f == nullptr ) return false; - - if (m_pMethod == nullptr) m_pMethod = f; - else m_pMethod->AddNext(f); - } - else - { - // return a method precompiled in pass 1 - CBotFunction* pf = m_pMethod; - CBotFunction* prev = nullptr; - while ( pf != nullptr ) - { - if (pf->GetName() == pp->GetString()) break; - prev = pf; - pf = pf->Next(); - } - - bool bConstructor = (pp->GetString() == GetName()); - CBotCStack* pile = pStack->TokenStack(nullptr, true); - - // make "this" known - CBotToken TokenThis(CBotString("this"), CBotString()); - CBotVar* pThis = CBotVar::Create(&TokenThis, CBotTypResult( CBotTypClass, this ) ); - pThis->SetUniqNum(-2); - pile->AddVar(pThis); - - if ( m_pParent ) - { - // makes "super" known - CBotToken TokenSuper(CBotString("super"), CBotString()); - CBotVar* pThis = CBotVar::Create(&TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) ); - pThis->SetUniqNum(-3); - pile->AddVar(pThis); - } - -// int num = 1; - CBotClass* my = this; - while (my != nullptr) - { - // places a copy of variables of a class (this) on a stack - CBotVar* pv = my->m_pVar; - while (pv != nullptr) - { - CBotVar* pcopy = CBotVar::Create(pv); - CBotVar::InitType initType = CBotVar::InitType::UNDEF; - if (!bConstructor || pv->IsStatic()) - initType = CBotVar::InitType::DEF; - pcopy->SetInit(initType); - pcopy->SetUniqNum(pv->GetUniqNum()); - pile->AddVar(pcopy); - pv = pv->GetNext(); - } - my = my->m_pParent; - } - - // compiles a method - p = pBase; - CBotFunction* f = - CBotFunction::Compile(p, pile, nullptr/*, false*/); - - if ( f != nullptr ) - { - f->m_pProg = pStack->GetBotCall(); - f->m_bSynchro = bSynchro; - // replaces the element in the chain - f->m_next = pf->m_next; - pf->m_next = nullptr; - delete pf; - if (prev == nullptr) m_pMethod = f; - else prev->m_next = f; - } - pStack->Return(nullptr, pile); - } - - return pStack->IsOk(); - } - - // definition of an element - if (type.Eq(0)) - { - pStack->SetError(TX_ENDOF, p); - return false; - } - - CBotInstr* i = nullptr; - if ( IsOfType(p, ID_ASS ) ) - { - if ( type.Eq(CBotTypArrayPointer) ) - { - i = CBotListArray::Compile(p, pStack, type.GetTypElem()); - } - else - { - // it has an assignmet to calculate - i = CBotTwoOpExpr::Compile(p, pStack); - } - if ( !pStack->IsOk() ) return false; - } - - - if ( !bSecond ) - { - CBotVar* pv = CBotVar::Create(pp->GetString(), type); - pv -> SetStatic( bStatic ); - pv -> SetPrivate( mProtect ); - - AddItem( pv ); - - pv->m_InitExpr = i; - pv->m_LimExpr = limites; - - - if ( pv->IsStatic() && pv->m_InitExpr != nullptr ) - { - CBotStack* pile = CBotStack::FirstStack(); // independent stack - while(pile->IsOk() && !pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer - pv->SetVal( pile->GetVar() ) ; - pile->Delete(); - } - } - else - delete i; - - if ( IsOfType(p, ID_COMMA) ) continue; - if ( IsOfType(p, ID_SEP) ) break; - } - pStack->SetError(TX_ENDOF, p); - } - return pStack->IsOk(); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack) -{ - if ( !IsOfType(p, ID_PUBLIC) ) return nullptr; - if ( !IsOfType(p, ID_CLASS) ) return nullptr; - - CBotString name = p->GetString(); - - // a name for the class is there? - if (IsOfType(p, TokenTypVar)) - { - // the class was created by Compile1 - CBotClass* pOld = CBotClass::Find(name); - - if ( IsOfType( p, ID_EXTENDS ) ) - { - // TODO: Not sure how correct is that - I have no idea how the precompilation (Compile1 method) works ~krzys_h - CBotString name = p->GetString(); - CBotClass* pPapa = CBotClass::Find(name); - - if (!IsOfType(p, TokenTypVar) || pPapa == nullptr) - { - pStack->SetError( TX_NOCLASS, p ); - return nullptr; - } - pOld->m_pParent = pPapa; - } - else - { - if (pOld != nullptr) - { - pOld->m_pParent = nullptr; - } - } - IsOfType( p, ID_OPBLK); // necessarily - - while ( pStack->IsOk() && !IsOfType( p, ID_CLBLK ) ) - { - pOld->CompileDefItem(p, pStack, true); - } - - pOld->m_IsDef = true; // complete definition - if (pStack->IsOk()) return pOld; - } - pStack->SetError(TX_ENDOF, p); - return nullptr; -} - diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 5aba61c2..bb2b9409 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -20,6 +20,10 @@ // Modules inlcude #include "CBotUtils.h" +#include "CBotToken.h" +#include "CBotClass.h" +#include "CBotStack.h" + // Local include // Global include @@ -48,3 +52,56 @@ CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal) } return pVar; } + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile) +{ + CBotClass* pClass = nullptr; + + switch (p->GetType()) + { + case ID_INT: + p = p->GetNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypInt )); + case ID_FLOAT: + p = p->GetNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypFloat )); + case ID_BOOLEAN: + case ID_BOOL: + p = p->GetNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypBoolean )); + case ID_STRING: + p = p->GetNext(); + return ArrayType(p, pile, CBotTypResult( CBotTypString )); + case ID_VOID: + p = p->GetNext(); + return CBotTypResult( 0 ); + + case TokenTypVar: + pClass = CBotClass::Find(p); + if ( pClass != nullptr) + { + p = p->GetNext(); + return ArrayType(p, pile, + pClass->IsIntrinsic() ? + CBotTypResult( CBotTypIntrinsic, pClass ) : + CBotTypResult( CBotTypPointer, pClass ) ); + } + } + return CBotTypResult( -1 ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) +{ + while ( IsOfType( p, ID_OPBRK ) ) + { + if ( !IsOfType( p, ID_CLBRK ) ) + { + pile->SetError(TX_CLBRK, p->GetStart()); + return CBotTypResult( -1 ); + } + type = CBotTypResult( CBotTypArrayPointer, type ); + } + return type; +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index df8bb55d..9d452d85 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -34,3 +34,20 @@ * \return */ CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal=false); + +/*! + * \brief TypeParam + * \param p + * \param pile + * \return + */ +CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile); + +/*! + * \brief ArrayType + * \param p + * \param pile + * \param type + * \return + */ +CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type); From 6d340e80ab2197ca0850052a774e31c2a16e4a82 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 18:51:28 +0100 Subject: [PATCH 54/91] Moving CBotDefParam class in its own header and source files. --- src/CBot/CBot.h | 26 ---- src/CBot/CBotDefParam.cpp | 207 ++++++++++++++++++++++++++++ src/CBot/CBotDefParam.h | 115 ++++++++++++++++ src/CBot/CBotInstr/CBotFunction.cpp | 185 +------------------------ src/CBot/CMakeLists.txt | 1 + 5 files changed, 324 insertions(+), 210 deletions(-) create mode 100644 src/CBot/CBotDefParam.cpp create mode 100644 src/CBot/CBotDefParam.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 33efc71f..56e483c0 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -524,29 +524,3 @@ public: }; -// a list of parameters - -class CBotDefParam -{ -private: - CBotToken m_token; // name of the parameter - CBotString m_typename; // type name - CBotTypResult m_type; // type of paramteter - CBotDefParam* m_next; // next parameter - long m_nIdent; - -public: - CBotDefParam(); - ~CBotDefParam(); - static - CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); - bool Execute(CBotVar** ppVars, CBotStack* &pj); - void RestoreState(CBotStack* &pj, bool bMain); - - void AddNext(CBotDefParam* p); - int GetType(); - CBotTypResult GetTypResult(); - CBotDefParam* GetNext(); - - CBotString GetParamString(); -}; diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp new file mode 100644 index 00000000..3a91f604 --- /dev/null +++ b/src/CBot/CBotDefParam.cpp @@ -0,0 +1,207 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotDefParam.h" + +#include "CBot.h" + +#include "CBotUtils.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotDefParam::CBotDefParam() +{ + m_next = nullptr; + m_nIdent = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotDefParam::~CBotDefParam() +{ + delete m_next; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) +{ + // mainly not pStack->TokenStack here + // declared variables must remain visible thereafter + + pStack->SetStartError(p->GetStart()); + + if (IsOfType(p, ID_OPENPAR)) + { + CBotDefParam* list = nullptr; + + while (!IsOfType(p, ID_CLOSEPAR)) + { + CBotDefParam* param = new CBotDefParam(); + if (list == nullptr) list = param; + else list->AddNext(param); // added to the list + +// CBotClass* pClass = nullptr;//= CBotClass::Find(p); + param->m_typename = p->GetString(); + CBotTypResult type = param->m_type = TypeParam(p, pStack); +// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object + + if (param->m_type.GetType() > 0) + { + CBotToken* pp = p; + param->m_token = *p; + if (pStack->IsOk() && IsOfType(p, TokenTypVar) ) + { + + // variable already declared? + if (pStack->CheckVarLocal(pp)) + { + pStack->SetError(TX_REDEFVAR, pp); + break; + } + + if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody); + CBotVar* var = CBotVar::Create(pp->GetString(), type); // creates the variable +// if ( pClass ) var->SetClass(pClass); + var->SetInit(CBotVar::InitType::IS_POINTER); // mark initialized + param->m_nIdent = CBotVar::NextUniqNum(); + var->SetUniqNum(param->m_nIdent); + pStack->AddVar(var); // place on the stack + + if (IsOfType(p, ID_COMMA) || p->GetType() == ID_CLOSEPAR) + continue; + } + pStack->SetError(TX_CLOSEPAR, p->GetStart()); + } + pStack->SetError(TX_NOTYP, p); + delete list; + return nullptr; + } + return list; + } + pStack->SetError(TX_OPENPAR, p->GetStart()); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotDefParam::AddNext(CBotDefParam* p) +{ + CBotDefParam* pp = this; + while (pp->m_next != nullptr) pp = pp->m_next; + + pp->m_next = p; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) +{ + int i = 0; + CBotDefParam* p = this; + + while ( p != nullptr ) + { + // creates a local variable on the stack + CBotVar* newvar = CBotVar::Create(p->m_token.GetString(), p->m_type); + + // serves to make the transformation of types: + if ( ppVars != nullptr && ppVars[i] != nullptr ) + { + switch (p->m_type.GetType()) + { + case CBotTypInt: + newvar->SetValInt(ppVars[i]->GetValInt()); + break; + case CBotTypFloat: + newvar->SetValFloat(ppVars[i]->GetValFloat()); + break; + case CBotTypString: + newvar->SetValString(ppVars[i]->GetValString()); + break; + case CBotTypBoolean: + newvar->SetValInt(ppVars[i]->GetValInt()); + break; + case CBotTypIntrinsic: + (static_cast(newvar))->Copy(ppVars[i], false); + break; + case CBotTypPointer: + case CBotTypArrayPointer: + { + newvar->SetPointer(ppVars[i]->GetPointer()); + } + break; + default: + assert(0); + } + } + newvar->SetUniqNum(p->m_nIdent); + pj->AddVar(newvar); // add a variable + p = p->m_next; + i++; + } + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) +{ +// int i = 0; + CBotDefParam* p = this; + + while ( p != nullptr ) + { + // creates a local variable on the stack + CBotVar* var = pj->FindVar(p->m_token.GetString()); + var->SetUniqNum(p->m_nIdent); + p = p->m_next; + } +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotDefParam::GetType() +{ + return m_type.GetType(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult CBotDefParam::GetTypResult() +{ + return m_type; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotDefParam* CBotDefParam::GetNext() +{ + return m_next; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotDefParam::GetParamString() +{ + CBotString param; + + param = m_typename; + param += ' '; + + param += m_token.GetString(); + return param; +} diff --git a/src/CBot/CBotDefParam.h b/src/CBot/CBotDefParam.h new file mode 100644 index 00000000..705660e3 --- /dev/null +++ b/src/CBot/CBotDefParam.h @@ -0,0 +1,115 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" +#include "CBotToken.h" +#include "CBotStack.h" + +// Local include + +// Global include + +class CBotCStack; +class CBotStack; +class CBotVar; + +/*! + * \brief The CBotDefParam class A list of parameters. + */ +class CBotDefParam +{ +public: + + /*! + * \brief CBotDefParam + */ + CBotDefParam(); + + /*! + * \brief ~CBotDefParam + */ + ~CBotDefParam(); + + /*! + * \brief Compile Compiles a list of parameters. + * \param p + * \param pStack + * \return + */ + static CBotDefParam* Compile(CBotToken* &p, CBotCStack* pStack); + + /*! + * \brief Execute + * \param ppVars + * \param pj + * \return + */ + bool Execute(CBotVar** ppVars, CBotStack* &pj); + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + void RestoreState(CBotStack* &pj, bool bMain); + + /*! + * \brief AddNext + * \param p + */ + void AddNext(CBotDefParam* p); + + /*! + * \brief GetType + * \return + */ + int GetType(); + + /*! + * \brief GetTypResult + * \return + */ + CBotTypResult GetTypResult(); + + /*! + * \brief GetNext + * \return + */ + CBotDefParam* GetNext(); + + /*! + * \brief GetParamString + * \return + */ + CBotString GetParamString(); + +private: + //! Name of the parameter. + CBotToken m_token; + //! Type name. + CBotString m_typename; + //! Type of paramteter. + CBotTypResult m_type; + //! Next parameter. + CBotDefParam* m_next; + long m_nIdent; +}; diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 94df0e7a..fd81a441 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -31,7 +31,7 @@ #include "CBotStack.h" #include "CBotClass.h" - +#include "CBotDefParam.h" #include "CBotUtils.h" // Local include @@ -888,186 +888,3 @@ void CBotFunction::AddPublic(CBotFunction* func) } m_listPublic = func; } - - - -///////////////////////////////////////////////////////////////////////// -// management of parameters - -//////////////////////////////////////////////////////////////////////////////// -CBotDefParam::CBotDefParam() -{ - m_next = nullptr; - m_nIdent = 0; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotDefParam::~CBotDefParam() -{ - delete m_next; -} - -//////////////////////////////////////////////////////////////////////////////// -// compiles a list of parameters -CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack) -{ - // mainly not pStack->TokenStack here - // declared variables must remain visible thereafter - - pStack->SetStartError(p->GetStart()); - - if (IsOfType(p, ID_OPENPAR)) - { - CBotDefParam* list = nullptr; - - while (!IsOfType(p, ID_CLOSEPAR)) - { - CBotDefParam* param = new CBotDefParam(); - if (list == nullptr) list = param; - else list->AddNext(param); // added to the list - -// CBotClass* pClass = nullptr;//= CBotClass::Find(p); - param->m_typename = p->GetString(); - CBotTypResult type = param->m_type = TypeParam(p, pStack); -// if ( type == CBotTypPointer ) type = CBotTypClass; // we must create a new object - - if (param->m_type.GetType() > 0) - { - CBotToken* pp = p; - param->m_token = *p; - if (pStack->IsOk() && IsOfType(p, TokenTypVar) ) - { - - // variable already declared? - if (pStack->CheckVarLocal(pp)) - { - pStack->SetError(TX_REDEFVAR, pp); - break; - } - - if ( type.Eq(CBotTypArrayPointer) ) type.SetType(CBotTypArrayBody); - CBotVar* var = CBotVar::Create(pp->GetString(), type); // creates the variable -// if ( pClass ) var->SetClass(pClass); - var->SetInit(CBotVar::InitType::IS_POINTER); // mark initialized - param->m_nIdent = CBotVar::NextUniqNum(); - var->SetUniqNum(param->m_nIdent); - pStack->AddVar(var); // place on the stack - - if (IsOfType(p, ID_COMMA) || p->GetType() == ID_CLOSEPAR) - continue; - } - pStack->SetError(TX_CLOSEPAR, p->GetStart()); - } - pStack->SetError(TX_NOTYP, p); - delete list; - return nullptr; - } - return list; - } - pStack->SetError(TX_OPENPAR, p->GetStart()); - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotDefParam::AddNext(CBotDefParam* p) -{ - CBotDefParam* pp = this; - while (pp->m_next != nullptr) pp = pp->m_next; - - pp->m_next = p; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj) -{ - int i = 0; - CBotDefParam* p = this; - - while ( p != nullptr ) - { - // creates a local variable on the stack - CBotVar* newvar = CBotVar::Create(p->m_token.GetString(), p->m_type); - - // serves to make the transformation of types: - if ( ppVars != nullptr && ppVars[i] != nullptr ) - { - switch (p->m_type.GetType()) - { - case CBotTypInt: - newvar->SetValInt(ppVars[i]->GetValInt()); - break; - case CBotTypFloat: - newvar->SetValFloat(ppVars[i]->GetValFloat()); - break; - case CBotTypString: - newvar->SetValString(ppVars[i]->GetValString()); - break; - case CBotTypBoolean: - newvar->SetValInt(ppVars[i]->GetValInt()); - break; - case CBotTypIntrinsic: - (static_cast(newvar))->Copy(ppVars[i], false); - break; - case CBotTypPointer: - case CBotTypArrayPointer: - { - newvar->SetPointer(ppVars[i]->GetPointer()); - } - break; - default: - assert(0); - } - } - newvar->SetUniqNum(p->m_nIdent); - pj->AddVar(newvar); // add a variable - p = p->m_next; - i++; - } - - return true; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotDefParam::RestoreState(CBotStack* &pj, bool bMain) -{ -// int i = 0; - CBotDefParam* p = this; - - while ( p != nullptr ) - { - // creates a local variable on the stack - CBotVar* var = pj->FindVar(p->m_token.GetString()); - var->SetUniqNum(p->m_nIdent); - p = p->m_next; - } -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotDefParam::GetType() -{ - return m_type.GetType(); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult CBotDefParam::GetTypResult() -{ - return m_type; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotDefParam* CBotDefParam::GetNext() -{ - return m_next; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotString CBotDefParam::GetParamString() -{ - CBotString param; - - param = m_typename; - param += ' '; - - param += m_token.GetString(); - return param; -} diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 7aee901b..c2a953f6 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCES CBotVar.cpp CBotCall.cpp CBotUtils.cpp + CBotDefParam.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp From 143eecd7918409af0b3e3b91545d5556bf68d1aa Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 18:59:15 +0100 Subject: [PATCH 55/91] Moving CBotCallMethode class in its own header and source files. --- src/CBot/CBot.h | 34 ------- src/CBot/CBotCallMethode.cpp | 179 +++++++++++++++++++++++++++++++++++ src/CBot/CBotCallMethode.h | 115 ++++++++++++++++++++++ src/CBot/CBotClass.cpp | 1 + src/CBot/CBotProgram.cpp | 145 ---------------------------- src/CBot/CMakeLists.txt | 1 + 6 files changed, 296 insertions(+), 179 deletions(-) create mode 100644 src/CBot/CBotCallMethode.cpp create mode 100644 src/CBot/CBotCallMethode.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 56e483c0..44b251be 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -490,37 +490,3 @@ extern float GetNumFloat( const char* p ); #if 0 extern void DEBUG( const char* text, int val, CBotStack* pile ); #endif - -/////////////////////////////////////////// -// class managing the methods declared by AddFunction on a class - -class CBotCallMethode -{ -private: - CBotString m_name; - bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user); - CBotTypResult - (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); - CBotCallMethode* m_next; - friend class CBotClass; - long m_nFuncIdent; - -public: - CBotCallMethode(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); - ~CBotCallMethode(); - - CBotTypResult - CompileCall(const char* name, CBotVar* pThis, - CBotVar** ppVars, CBotCStack* pStack, - long& nIdent); - - int DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pFunc); - - CBotString GetName(); - CBotCallMethode* Next(); - void AddNext(CBotCallMethode* p); - -}; - diff --git a/src/CBot/CBotCallMethode.cpp b/src/CBot/CBotCallMethode.cpp new file mode 100644 index 00000000..6611129c --- /dev/null +++ b/src/CBot/CBotCallMethode.cpp @@ -0,0 +1,179 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotCallMethode.h" + +#include "CBotUtils.h" +#include "CBotStack.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotCallMethode::CBotCallMethode(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) +{ + m_name = name; + m_rExec = rExec; + m_rComp = rCompile; + m_next = nullptr; + m_nFuncIdent = CBotVar::NextUniqNum(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCallMethode::~CBotCallMethode() +{ + delete m_next; + m_next = nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult CBotCallMethode::CompileCall(const char* name, + CBotVar* pThis, + CBotVar** ppVar, + CBotCStack* pStack, + long& nIdent) +{ + CBotCallMethode* pt = this; + nIdent = 0; + + while ( pt != nullptr ) + { + if ( pt->m_name == name ) + { + CBotVar* pVar = MakeListVars(ppVar, true); + CBotVar* pVar2 = pVar; + CBotTypResult r = pt->m_rComp(pThis, pVar2); + int ret = r.GetType(); + if ( ret > 20 ) + { + if (pVar2) pStack->SetError(ret, pVar2->GetToken()); + } + delete pVar; + nIdent = pt->m_nFuncIdent; + return r; + } + pt = pt->m_next; + } + return CBotTypResult(-1); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotCallMethode::GetName() +{ + return m_name; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCallMethode* CBotCallMethode::Next() +{ + return m_next; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCallMethode::AddNext(CBotCallMethode* pt) +{ + CBotCallMethode* p = this; + while ( p->m_next != nullptr ) p = p->m_next; + + p->m_next = pt; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotCallMethode::DoCall(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppVars, + CBotVar* &pResult, + CBotStack* pStack, + CBotToken* pToken) +{ + CBotCallMethode* pt = this; + + // search by the identifier + + if ( nIdent ) while ( pt != nullptr ) + { + if ( pt->m_nFuncIdent == nIdent ) + { + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVars, true); + CBotVar* pVarToDelete = pVar; + + // then calls the routine external to the module + + int Exception = 0; + int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser()); + pStack->SetVar(pResult); + + if (res == false) + { + if (Exception!=0) + { +// pStack->SetError(Exception, pVar->GetToken()); + pStack->SetError(Exception, pToken); + } + delete pVarToDelete; + return false; + } + delete pVarToDelete; + return true; + } + pt = pt->m_next; + } + + // search by name + + while ( pt != nullptr ) + { + if ( pt->m_name == name ) + { + // lists the parameters depending on the contents of the stack (pStackVar) + + CBotVar* pVar = MakeListVars(ppVars, true); + CBotVar* pVarToDelete = pVar; + + int Exception = 0; + int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser()); + pStack->SetVar(pResult); + + if (res == false) + { + if (Exception!=0) + { +// pStack->SetError(Exception, pVar->GetToken()); + pStack->SetError(Exception, pToken); + } + delete pVarToDelete; + return false; + } + delete pVarToDelete; + nIdent = pt->m_nFuncIdent; + return true; + } + pt = pt->m_next; + } + + return -1; +} diff --git a/src/CBot/CBotCallMethode.h b/src/CBot/CBotCallMethode.h new file mode 100644 index 00000000..8ea40254 --- /dev/null +++ b/src/CBot/CBotCallMethode.h @@ -0,0 +1,115 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotCallMethode class Class managing the methods declared by + * AddFunction on a class. + */ +class CBotCallMethode +{ +public: + + /*! + * \brief CBotCallMethode + * \param name + * \param rExec + * \param rCompile + */ + CBotCallMethode(const char* name, + bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), + CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)); + + /*! + * \brief ~CBotCallMethode + */ + ~CBotCallMethode(); + + /*! + * \brief CompileCall Is acceptable by a call procedure name and given + * parameters. + * \param name + * \param pThis + * \param ppVars + * \param pStack + * \param nIdent + * \return + */ + CBotTypResult CompileCall(const char* name, + CBotVar* pThis, + CBotVar** ppVars, + CBotCStack* pStack, + long& nIdent); + + /*! + * \brief DoCall + * \param nIdent + * \param name + * \param pThis + * \param ppVars + * \param pResult + * \param pStack + * \param pFunc + * \return + */ + int DoCall(long& nIdent, + const char* name, + CBotVar* pThis, + CBotVar** ppVars, + CBotVar* &pResult, + CBotStack* pStack, + CBotToken* pFunc); + + /*! + * \brief GetName + * \return + */ + CBotString GetName(); + + /*! + * \brief Next + * \return + */ + CBotCallMethode* Next(); + + /*! + * \brief AddNext + * \param p + */ + void AddNext(CBotCallMethode* p); + +private: + CBotString m_name; + bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user); + CBotTypResult (*m_rComp) (CBotVar* pThis, CBotVar* &pVar); + CBotCallMethode* m_next; + friend class CBotClass; + long m_nFuncIdent; + +}; + diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index d2d03e92..741dfb97 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -31,6 +31,7 @@ #include "CBotCall.h" #include "CBotStack.h" #include "CBotUtils.h" +#include "CBotCallMethode.h" // Local include diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 8e492bac..c06cfbe0 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -641,148 +641,3 @@ void CBotProgram::Free() CBotCall ::Free() ; CBotClass::Free() ; } - - -//////////////////////////////////////////////////////////////////////////////// -CBotCallMethode::CBotCallMethode(const char* name, - bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user), - CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar)) -{ - m_name = name; - m_rExec = rExec; - m_rComp = rCompile; - m_next = nullptr; - m_nFuncIdent = CBotVar::NextUniqNum(); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotCallMethode::~CBotCallMethode() -{ - delete m_next; - m_next = nullptr; -} - -// is acceptable by a call procedure name -// and given parameters -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult CBotCallMethode::CompileCall(const char* name, CBotVar* pThis, - CBotVar** ppVar, CBotCStack* pStack, - long& nIdent) -{ - CBotCallMethode* pt = this; - nIdent = 0; - - while ( pt != nullptr ) - { - if ( pt->m_name == name ) - { - CBotVar* pVar = MakeListVars(ppVar, true); - CBotVar* pVar2 = pVar; - CBotTypResult r = pt->m_rComp(pThis, pVar2); - int ret = r.GetType(); - if ( ret > 20 ) - { - if (pVar2) pStack->SetError(ret, pVar2->GetToken()); - } - delete pVar; - nIdent = pt->m_nFuncIdent; - return r; - } - pt = pt->m_next; - } - return CBotTypResult(-1); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotString CBotCallMethode::GetName() -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotCallMethode* CBotCallMethode::Next() -{ - return m_next; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCallMethode::AddNext(CBotCallMethode* pt) -{ - CBotCallMethode* p = this; - while ( p->m_next != nullptr ) p = p->m_next; - - p->m_next = pt; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotCallMethode::DoCall(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppVars, CBotVar* &pResult, CBotStack* pStack, CBotToken* pToken) -{ - CBotCallMethode* pt = this; - - // search by the identifier - - if ( nIdent ) while ( pt != nullptr ) - { - if ( pt->m_nFuncIdent == nIdent ) - { - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVars, true); - CBotVar* pVarToDelete = pVar; - - // then calls the routine external to the module - - int Exception = 0; - int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser()); - pStack->SetVar(pResult); - - if (res == false) - { - if (Exception!=0) - { -// pStack->SetError(Exception, pVar->GetToken()); - pStack->SetError(Exception, pToken); - } - delete pVarToDelete; - return false; - } - delete pVarToDelete; - return true; - } - pt = pt->m_next; - } - - // search by name - - while ( pt != nullptr ) - { - if ( pt->m_name == name ) - { - // lists the parameters depending on the contents of the stack (pStackVar) - - CBotVar* pVar = MakeListVars(ppVars, true); - CBotVar* pVarToDelete = pVar; - - int Exception = 0; - int res = pt->m_rExec(pThis, pVar, pResult, Exception, pStack->GetPUser()); - pStack->SetVar(pResult); - - if (res == false) - { - if (Exception!=0) - { -// pStack->SetError(Exception, pVar->GetToken()); - pStack->SetError(Exception, pToken); - } - delete pVarToDelete; - return false; - } - delete pVarToDelete; - nIdent = pt->m_nFuncIdent; - return true; - } - pt = pt->m_next; - } - - return -1; -} diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index c2a953f6..254bb665 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -9,6 +9,7 @@ set(SOURCES CBotCall.cpp CBotUtils.cpp CBotDefParam.cpp + CBotCallMethode.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp From bd20f6303c86b8c9cd7071ef86a704a2fa01c1f2 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 19:11:57 +0100 Subject: [PATCH 56/91] Moving CBotVarPointer class in its own header and source files. --- src/CBot/CBot.h | 37 ---- src/CBot/CBotInstr/CBotClassInst.cpp | 2 + src/CBot/CBotStack.cpp | 2 + src/CBot/CBotVar.cpp | 209 +---------------------- src/CBot/CBotVar/CBotVarPointer.cpp | 242 +++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarPointer.h | 160 ++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 7 files changed, 408 insertions(+), 245 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarPointer.cpp create mode 100644 src/CBot/CBotVar/CBotVarPointer.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 44b251be..a2d083a1 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -432,43 +432,6 @@ public: void ConstructorSet() override; }; - -// class for the management of pointers to a class instances -class CBotVarPointer : public CBotVar -{ -private: - CBotVarClass* - m_pVarClass; // contents - CBotClass* m_pClass; // class provided for this pointer - friend class CBotVar; // my daddy is a buddy - -public: - CBotVarPointer( const CBotToken* name, CBotTypResult& type ); - ~CBotVarPointer(); - - void Copy(CBotVar* pSrc, bool bName=true) override; - void SetClass(CBotClass* pClass) override; - CBotClass* GetClass() override; - CBotVar* GetItem(const char* name) override; // return an element of a class according to its name (*) - CBotVar* GetItemRef(int nIdent) override; - CBotVar* GetItemList() override; - - CBotString GetValString() override; - void SetPointer(CBotVar* p) override; - CBotVarClass* - GetPointer() override; - - void SetIdent(long n) override; // associates an identification number (unique) - long GetIdent(); // gives the identification number associated with - void ConstructorSet() override; - - bool Save1State(FILE* pf) override; - void Maj(void* pUser, bool bContinue) override; - - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; -}; - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index a78b4de9..d4731885 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -26,6 +26,8 @@ #include "CBotStack.h" #include "CBotClass.h" +#include "CBotVar/CBotVarPointer.h" + // Local include // Global include diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 0a767135..5678c8fb 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -23,6 +23,8 @@ #include "CBotInstr/CBotFunction.h" +#include "CBotVar/CBotVarPointer.h" + // Local include // Global include diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 8808cb0f..ad736396 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -28,6 +28,7 @@ #include "CBotStack.h" #include "CBotVar/CBotVarArray.h" +#include "CBotVar/CBotVarPointer.h" #include "CBotDefines.h" #include "CBotClass.h" @@ -1779,214 +1780,6 @@ bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) return l != r; } -///////////////////////////////////////////////////////////////////////////// -// gestion des pointeurs à une instance donnée -// TODO management of pointers to a given instance - -CBotVarPointer::CBotVarPointer(const CBotToken* name, CBotTypResult& type ) -{ - if ( !type.Eq(CBotTypPointer) && - !type.Eq(CBotTypNullPointer) && - !type.Eq(CBotTypClass) && // for convenience accepts Class and Intrinsic - !type.Eq(CBotTypIntrinsic) ) assert(0); - - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; - - m_type = type; - if ( !type.Eq(CBotTypNullPointer) ) - m_type.SetType(CBotTypPointer); // anyway, this is a pointer - m_binit = CBotVar::InitType::UNDEF; - m_pClass = nullptr; - m_pVarClass = nullptr; // will be defined by a SetPointer() - - SetClass(type.GetClass() ); -} - -CBotVarPointer::~CBotVarPointer() -{ - if ( m_pVarClass != nullptr ) m_pVarClass->DecrementUse(); // decrement reference -} - - -void CBotVarPointer::Maj(void* pUser, bool bContinu) -{ -/* if ( !bContinu && m_pMyThis != nullptr ) - m_pMyThis->Maj(pUser, false);*/ - - if ( m_pVarClass != nullptr) m_pVarClass->Maj(pUser, false); -} - -CBotVar* CBotVarPointer::GetItem(const char* name) -{ - if ( m_pVarClass == nullptr) // no existing instance? - return m_pClass->GetItem(name); // makes the pointer in the class itself - - return m_pVarClass->GetItem(name); -} - -CBotVar* CBotVarPointer::GetItemRef(int nIdent) -{ - if ( m_pVarClass == nullptr) // no existing instance? - return m_pClass->GetItemRef(nIdent);// makes the pointer to the class itself - - return m_pVarClass->GetItemRef(nIdent); -} - -CBotVar* CBotVarPointer::GetItemList() -{ - if ( m_pVarClass == nullptr) return nullptr; - return m_pVarClass->GetItemList(); -} - -CBotString CBotVarPointer::GetValString() -{ - CBotString s = "Pointer to "; - if ( m_pVarClass == nullptr ) s = "Null pointer" ; - else s += m_pVarClass->GetValString(); - return s; -} - - -void CBotVarPointer::ConstructorSet() -{ - if ( m_pVarClass != nullptr) m_pVarClass->ConstructorSet(); -} - -// initializes the pointer to the instance of a class - -void CBotVarPointer::SetPointer(CBotVar* pVarClass) -{ - m_binit = CBotVar::InitType::DEF; // init, even on a null pointer - - if ( m_pVarClass == pVarClass) return; // special, not decrement and reincrement - // because the decrement can destroy the object - - if ( pVarClass != nullptr ) - { - if ( pVarClass->GetType() == CBotTypPointer ) - pVarClass = pVarClass->GetPointer(); // the real pointer to the object - -// if ( pVarClass->GetType() != CBotTypClass ) - if ( !pVarClass->m_type.Eq(CBotTypClass) ) - assert(0); - - (static_cast(pVarClass))->IncrementUse(); // increment the reference - m_pClass = (static_cast(pVarClass))->m_pClass; - m_pUserPtr = pVarClass->m_pUserPtr; // not really necessary - m_type = CBotTypResult(CBotTypPointer, m_pClass); // what kind of a pointer - } - - if ( m_pVarClass != nullptr ) m_pVarClass->DecrementUse(); - m_pVarClass = static_cast(pVarClass); - -} - -CBotVarClass* CBotVarPointer::GetPointer() -{ - if ( m_pVarClass == nullptr ) return nullptr; - return m_pVarClass->GetPointer(); -} - -void CBotVarPointer::SetIdent(long n) -{ - if ( m_pVarClass == nullptr ) return; - m_pVarClass->SetIdent( n ); -} - -long CBotVarPointer::GetIdent() -{ - if ( m_pVarClass == nullptr ) return 0; - return m_pVarClass->m_ItemIdent; -} - - -void CBotVarPointer::SetClass(CBotClass* pClass) -{ -// int nIdent = 0; - m_type.m_pClass = m_pClass = pClass; - if ( m_pVarClass != nullptr ) m_pVarClass->SetClass(pClass); //, nIdent); -} - -CBotClass* CBotVarPointer::GetClass() -{ - if ( m_pVarClass != nullptr ) return m_pVarClass->GetClass(); - - return m_pClass; -} - - -bool CBotVarPointer::Save1State(FILE* pf) -{ - if ( m_pClass ) - { - if (!WriteString(pf, m_pClass->GetName())) return false; // name of the class - } - else - { - if (!WriteString(pf, "")) return false; - } - - if (!WriteLong(pf, GetIdent())) return false; // the unique reference - - // also saves the proceedings copies - return SaveVar(pf, GetPointer()); -} - -// copy a variable into another -void CBotVarPointer::Copy(CBotVar* pSrc, bool bName) -{ - if ( pSrc->GetType() != CBotTypPointer && - pSrc->GetType() != CBotTypNullPointer) - assert(0); - - CBotVarPointer* p = static_cast(pSrc); - - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; -// m_pVarClass = p->m_pVarClass; - m_pVarClass = p->GetPointer(); - - if ( m_pVarClass != nullptr ) - m_pVarClass->IncrementUse(); // incerement the reference - - m_pClass = p->m_pClass; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - m_pMyThis = nullptr;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // keeps indentificator the same (by default) - if (m_ident == 0 ) m_ident = p->m_ident; -} - -bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right) -{ - CBotVarClass* l = left->GetPointer(); - CBotVarClass* r = right->GetPointer(); - - if ( l == r ) return true; - if ( l == nullptr && r->GetUserPtr() == OBJECTDELETED ) return true; - if ( r == nullptr && l->GetUserPtr() == OBJECTDELETED ) return true; - return false; -} - -bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) -{ - CBotVarClass* l = left->GetPointer(); - CBotVarClass* r = right->GetPointer(); - - if ( l == r ) return false; - if ( l == nullptr && r->GetUserPtr() == OBJECTDELETED ) return false; - if ( r == nullptr && l->GetUserPtr() == OBJECTDELETED ) return false; - return true; -} - - - /////////////////////////////////////////////////////// // management of results types diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp new file mode 100644 index 00000000..c669a2a0 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -0,0 +1,242 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarPointer.h" +#include "CBotToken.h" +#include "CBot.h" +#include "CBotClass.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotVarPointer::CBotVarPointer(const CBotToken* name, CBotTypResult& type ) +{ + if ( !type.Eq(CBotTypPointer) && + !type.Eq(CBotTypNullPointer) && + !type.Eq(CBotTypClass) && // for convenience accepts Class and Intrinsic + !type.Eq(CBotTypIntrinsic) ) assert(0); + + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + + m_type = type; + if ( !type.Eq(CBotTypNullPointer) ) + m_type.SetType(CBotTypPointer); // anyway, this is a pointer + m_binit = CBotVar::InitType::UNDEF; + m_pClass = nullptr; + m_pVarClass = nullptr; // will be defined by a SetPointer() + + SetClass(type.GetClass() ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarPointer::~CBotVarPointer() +{ + if ( m_pVarClass != nullptr ) m_pVarClass->DecrementUse(); // decrement reference +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarPointer::Maj(void* pUser, bool bContinu) +{ +/* if ( !bContinu && m_pMyThis != nullptr ) + m_pMyThis->Maj(pUser, false);*/ + + if ( m_pVarClass != nullptr) m_pVarClass->Maj(pUser, false); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarPointer::GetItem(const char* name) +{ + if ( m_pVarClass == nullptr) // no existing instance? + return m_pClass->GetItem(name); // makes the pointer in the class itself + + return m_pVarClass->GetItem(name); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarPointer::GetItemRef(int nIdent) +{ + if ( m_pVarClass == nullptr) // no existing instance? + return m_pClass->GetItemRef(nIdent);// makes the pointer to the class itself + + return m_pVarClass->GetItemRef(nIdent); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarPointer::GetItemList() +{ + if ( m_pVarClass == nullptr) return nullptr; + return m_pVarClass->GetItemList(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarPointer::GetValString() +{ + CBotString s = "Pointer to "; + if ( m_pVarClass == nullptr ) s = "Null pointer" ; + else s += m_pVarClass->GetValString(); + return s; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarPointer::ConstructorSet() +{ + if ( m_pVarClass != nullptr) m_pVarClass->ConstructorSet(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarPointer::SetPointer(CBotVar* pVarClass) +{ + m_binit = CBotVar::InitType::DEF; // init, even on a null pointer + + if ( m_pVarClass == pVarClass) return; // special, not decrement and reincrement + // because the decrement can destroy the object + + if ( pVarClass != nullptr ) + { + if ( pVarClass->GetType() == CBotTypPointer ) + pVarClass = pVarClass->GetPointer(); // the real pointer to the object + +// if ( pVarClass->GetType() != CBotTypClass ) + if ( !pVarClass->m_type.Eq(CBotTypClass) ) + assert(0); + + (static_cast(pVarClass))->IncrementUse(); // increment the reference + m_pClass = (static_cast(pVarClass))->m_pClass; + m_pUserPtr = pVarClass->m_pUserPtr; // not really necessary + m_type = CBotTypResult(CBotTypPointer, m_pClass); // what kind of a pointer + } + + if ( m_pVarClass != nullptr ) m_pVarClass->DecrementUse(); + m_pVarClass = static_cast(pVarClass); + +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass* CBotVarPointer::GetPointer() +{ + if ( m_pVarClass == nullptr ) return nullptr; + return m_pVarClass->GetPointer(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarPointer::SetIdent(long n) +{ + if ( m_pVarClass == nullptr ) return; + m_pVarClass->SetIdent( n ); +} + +//////////////////////////////////////////////////////////////////////////////// +long CBotVarPointer::GetIdent() +{ + if ( m_pVarClass == nullptr ) return 0; + return m_pVarClass->m_ItemIdent; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarPointer::SetClass(CBotClass* pClass) +{ +// int nIdent = 0; + m_type.m_pClass = m_pClass = pClass; + if ( m_pVarClass != nullptr ) m_pVarClass->SetClass(pClass); //, nIdent); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotVarPointer::GetClass() +{ + if ( m_pVarClass != nullptr ) return m_pVarClass->GetClass(); + + return m_pClass; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarPointer::Save1State(FILE* pf) +{ + if ( m_pClass ) + { + if (!WriteString(pf, m_pClass->GetName())) return false; // name of the class + } + else + { + if (!WriteString(pf, "")) return false; + } + + if (!WriteLong(pf, GetIdent())) return false; // the unique reference + + // also saves the proceedings copies + return SaveVar(pf, GetPointer()); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarPointer::Copy(CBotVar* pSrc, bool bName) +{ + if ( pSrc->GetType() != CBotTypPointer && + pSrc->GetType() != CBotTypNullPointer) + assert(0); + + CBotVarPointer* p = static_cast(pSrc); + + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; +// m_pVarClass = p->m_pVarClass; + m_pVarClass = p->GetPointer(); + + if ( m_pVarClass != nullptr ) + m_pVarClass->IncrementUse(); // incerement the reference + + m_pClass = p->m_pClass; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = nullptr; + m_pMyThis = nullptr;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right) +{ + CBotVarClass* l = left->GetPointer(); + CBotVarClass* r = right->GetPointer(); + + if ( l == r ) return true; + if ( l == nullptr && r->GetUserPtr() == OBJECTDELETED ) return true; + if ( r == nullptr && l->GetUserPtr() == OBJECTDELETED ) return true; + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right) +{ + CBotVarClass* l = left->GetPointer(); + CBotVarClass* r = right->GetPointer(); + + if ( l == r ) return false; + if ( l == nullptr && r->GetUserPtr() == OBJECTDELETED ) return false; + if ( r == nullptr && l->GetUserPtr() == OBJECTDELETED ) return false; + return true; +} diff --git a/src/CBot/CBotVar/CBotVarPointer.h b/src/CBot/CBotVar/CBotVarPointer.h new file mode 100644 index 00000000..dbb6ea39 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarPointer.h @@ -0,0 +1,160 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarPointer class Class for the management of pointers to a + * class instances. + */ +class CBotVarPointer : public CBotVar +{ +public: + + /*! + * \brief CBotVarPointer + * \param name + * \param type + */ + CBotVarPointer( const CBotToken* name, CBotTypResult& type ); + + /*! + * \brief ~CBotVarPointer + */ + ~CBotVarPointer(); + + /*! + * \brief Copy Copy a variable into another. + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief SetClass + * \param pClass + */ + void SetClass(CBotClass* pClass) override; + + /*! + * \brief GetClass + * \return + */ + CBotClass* GetClass() override; + + /*! + * \brief GetItem Return an element of a class according to its name (*). + * \param name + * \return + */ + CBotVar* GetItem(const char* name) override; + + /*! + * \brief GetItemRef + * \param nIdent + * \return + */ + CBotVar* GetItemRef(int nIdent) override; + + /*! + * \brief GetItemList + * \return + */ + CBotVar* GetItemList() override; + + /*! + * \brief GetValString + * \return + */ + CBotString GetValString() override; + + /*! + * \brief SetPointer Initializes the pointer to the instance of a class. + * \param p + */ + void SetPointer(CBotVar* p) override; + + /*! + * \brief GetPointer + * \return + */ + CBotVarClass* GetPointer() override; + + /*! + * \brief SetIdent Associates an identification number (unique). + * \param n + */ + void SetIdent(long n) override; + + /*! + * \brief GetIdent Gives the identification number associated with. + * \return + */ + long GetIdent(); + + /*! + * \brief ConstructorSet + */ + void ConstructorSet() override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + + /*! + * \brief Maj + * \param pUser + * \param bContinue + */ + void Maj(void* pUser, bool bContinue) override; + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + bool Eq(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + bool Ne(CBotVar* left, CBotVar* right) override; + +private: + //! Contents. + CBotVarClass* m_pVarClass; + //! Class provided for this pointer. + CBotClass* m_pClass; + friend class CBotVar; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 254bb665..a8ce7ac8 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -56,6 +56,7 @@ set(SOURCES CBotInstr/CBotInt.cpp CBotInstr/CBotFunction.cpp CBotVar/CBotVarArray.cpp + CBotVar/CBotVarPointer.cpp ) # Includes From c624d65649c24cf79958c92f7ee27cfc5bab588f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 19:25:35 +0100 Subject: [PATCH 57/91] Moving CBotVarClass class in its own header and source files. --- src/CBot/CBot.h | 59 -------- src/CBot/CBotDefParam.cpp | 2 + src/CBot/CBotInstr/CBotClassInst.cpp | 1 + src/CBot/CBotInstr/CBotFieldExpr.cpp | 2 + src/CBot/CBotStack.cpp | 1 + src/CBot/CBotVar.cpp | 92 +------------ src/CBot/CBotVar/CBotVarArray.cpp | 1 + src/CBot/CBotVar/CBotVarClass.cpp | 110 +++++++++++++++ src/CBot/CBotVar/CBotVarClass.h | 194 +++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarPointer.cpp | 2 +- src/CBot/CMakeLists.txt | 1 + 11 files changed, 314 insertions(+), 151 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarClass.cpp create mode 100644 src/CBot/CBotVar/CBotVarClass.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index a2d083a1..8c5f4114 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -373,65 +373,6 @@ public: }; -// class management class instances -class CBotVarClass : public CBotVar -{ -private: - static - CBotVarClass* m_ExClass; // list of existing instances at some point - CBotVarClass* m_ExNext; // for this general list - CBotVarClass* m_ExPrev; // for this general list - -private: - CBotClass* m_pClass; // the class definition - CBotVarClass* m_pParent; // the instance of a parent class - CBotVar* m_pVar; // contents - friend class CBotVar; // my daddy is a buddy WHAT? :D(\TODO mon papa est un copain ) - friend class CBotVarPointer; // and also the pointer - int m_CptUse; // counter usage - long m_ItemIdent; // identifier (unique) of an instance - bool m_bConstructor; // set if a constructor has been called - -public: - CBotVarClass( const CBotToken* name, const CBotTypResult& type ); -// CBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - ~CBotVarClass(); -// void InitCBotVarClass( const CBotToken* name, CBotTypResult& type, int &nIdent ); - - void Copy(CBotVar* pSrc, bool bName=true) override; - void SetClass(CBotClass* pClass) override; //, int &nIdent); - CBotClass* GetClass() override; - CBotVar* GetItem(const char* name) override; // return an element of a class according to its name (*) - CBotVar* GetItemRef(int nIdent) override; - - CBotVar* GetItem(int n, bool bExtend) override; - CBotVar* GetItemList() override; - - CBotString GetValString() override; - - bool Save1State(FILE* pf) override; - void Maj(void* pUser, bool bContinue) override; - - void IncrementUse(); // a reference to incrementation - void DecrementUse(); // a reference to decrementation - - CBotVarClass* - GetPointer() override; - void SetItemList(CBotVar* pVar); - - void SetIdent(long n) override; - - static CBotVarClass* Find(long id); - - -// CBotVar* GetMyThis(); - - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; - - void ConstructorSet() override; -}; - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp index 3a91f604..601e5676 100644 --- a/src/CBot/CBotDefParam.cpp +++ b/src/CBot/CBotDefParam.cpp @@ -24,6 +24,8 @@ #include "CBotUtils.h" +#include "CBotVar/CBotVarClass.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index d4731885..8426422b 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -27,6 +27,7 @@ #include "CBotClass.h" #include "CBotVar/CBotVarPointer.h" +#include "CBotVar/CBotVarClass.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.cpp b/src/CBot/CBotInstr/CBotFieldExpr.cpp index 592d4a7e..bad06d93 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.cpp +++ b/src/CBot/CBotInstr/CBotFieldExpr.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" #include "CBotClass.h" +#include "CBotVar/CBotVarClass.h" + // Local include // Global include diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 5678c8fb..1a21dd2c 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -24,6 +24,7 @@ #include "CBotInstr/CBotFunction.h" #include "CBotVar/CBotVarPointer.h" +#include "CBotVar/CBotVarClass.h" // Local include diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index ad736396..dc2517d9 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -29,6 +29,7 @@ #include "CBotVar/CBotVarArray.h" #include "CBotVar/CBotVarPointer.h" +#include "CBotVar/CBotVarClass.h" #include "CBotDefines.h" #include "CBotClass.h" @@ -117,97 +118,6 @@ CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) m_val = 0; } -CBotVarClass* CBotVarClass::m_ExClass = nullptr; - -CBotVarClass::CBotVarClass( const CBotToken* name, const CBotTypResult& type) -{ -/* -// int nIdent = 0; - InitCBotVarClass( name, type ) //, nIdent ); -} - -CBotVarClass::CBotVarClass( const CBotToken* name, CBotTypResult& type) //, int &nIdent ) -{ - InitCBotVarClass( name, type ); //, nIdent ); -} - -void CBotVarClass::InitCBotVarClass( const CBotToken* name, CBotTypResult& type ) //, int &nIdent ) -{*/ - if ( !type.Eq(CBotTypClass) && - !type.Eq(CBotTypIntrinsic) && // by convenience there accepts these types - !type.Eq(CBotTypPointer) && - !type.Eq(CBotTypArrayPointer) && - !type.Eq(CBotTypArrayBody)) assert(0); - - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = OBJECTCREATED;//nullptr; - m_InitExpr = nullptr; - m_LimExpr = nullptr; - m_pVar = nullptr; - m_type = type; - if ( type.Eq(CBotTypArrayPointer) ) m_type.SetType( CBotTypArrayBody ); - else if ( !type.Eq(CBotTypArrayBody) ) m_type.SetType( CBotTypClass ); - // officel type for this object - - m_pClass = nullptr; - m_pParent = nullptr; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = 0; - m_bConstructor = false; - m_CptUse = 0; - m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); - - // se place tout seul dans la liste - // TODO stands alone in the list (stands only in a list) - if (m_ExClass) m_ExClass->m_ExPrev = this; - m_ExNext = m_ExClass; - m_ExPrev = nullptr; - m_ExClass = this; - - CBotClass* pClass = type.GetClass(); - CBotClass* pClass2 = pClass->GetParent(); - if ( pClass2 != nullptr ) - { - // also creates an instance of the parent class - m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(),pClass2) ); //, nIdent); - } - - SetClass( pClass ); //, nIdent ); - -} - -CBotVarClass::~CBotVarClass( ) -{ - if ( m_CptUse != 0 ) - assert(0); - - if ( m_pParent ) delete m_pParent; - m_pParent = nullptr; - - // frees the indirect object if necessary -// if ( m_Indirect != nullptr ) -// m_Indirect->DecrementUse(); - - // removes the class list - if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; - else m_ExClass = m_ExNext; - - if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; - m_ExPrev = nullptr; - m_ExNext = nullptr; - - delete m_pVar; -} - -void CBotVarClass::ConstructorSet() -{ - m_bConstructor = true; -} - - CBotVar::~CBotVar( ) { delete m_token; diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp index 35f55a52..b030e7ae 100644 --- a/src/CBot/CBotVar/CBotVarArray.cpp +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -19,6 +19,7 @@ // Modules inlcude #include "CBotVarArray.h" +#include "CBotVarClass.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp new file mode 100644 index 00000000..73d14ac5 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -0,0 +1,110 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarClass.h" + +#include "CBotClass.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass* CBotVarClass::m_ExClass = nullptr; + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass::CBotVarClass( const CBotToken* name, const CBotTypResult& type) +{ + if ( !type.Eq(CBotTypClass) && + !type.Eq(CBotTypIntrinsic) && // by convenience there accepts these types + !type.Eq(CBotTypPointer) && + !type.Eq(CBotTypArrayPointer) && + !type.Eq(CBotTypArrayBody)) assert(0); + + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = OBJECTCREATED;//nullptr; + m_InitExpr = nullptr; + m_LimExpr = nullptr; + m_pVar = nullptr; + m_type = type; + if ( type.Eq(CBotTypArrayPointer) ) m_type.SetType( CBotTypArrayBody ); + else if ( !type.Eq(CBotTypArrayBody) ) m_type.SetType( CBotTypClass ); + // officel type for this object + + m_pClass = nullptr; + m_pParent = nullptr; + m_binit = InitType::UNDEF; + m_bStatic = false; + m_mPrivate = 0; + m_bConstructor = false; + m_CptUse = 0; + m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); + + // se place tout seul dans la liste + // TODO stands alone in the list (stands only in a list) + if (m_ExClass) m_ExClass->m_ExPrev = this; + m_ExNext = m_ExClass; + m_ExPrev = nullptr; + m_ExClass = this; + + CBotClass* pClass = type.GetClass(); + CBotClass* pClass2 = pClass->GetParent(); + if ( pClass2 != nullptr ) + { + // also creates an instance of the parent class + m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(),pClass2) ); //, nIdent); + } + + SetClass( pClass ); //, nIdent ); + +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass::~CBotVarClass( ) +{ + if ( m_CptUse != 0 ) + assert(0); + + if ( m_pParent ) delete m_pParent; + m_pParent = nullptr; + + // frees the indirect object if necessary +// if ( m_Indirect != nullptr ) +// m_Indirect->DecrementUse(); + + // removes the class list + if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext; + else m_ExClass = m_ExNext; + + if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev; + m_ExPrev = nullptr; + m_ExNext = nullptr; + + delete m_pVar; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::ConstructorSet() +{ + m_bConstructor = true; +} diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h new file mode 100644 index 00000000..b6a256ff --- /dev/null +++ b/src/CBot/CBotVar/CBotVarClass.h @@ -0,0 +1,194 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBot.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarClass class Class management class instances. + */ +class CBotVarClass : public CBotVar +{ +public: + + /*! + * \brief CBotVarClass + * \param name + * \param type + */ + CBotVarClass( const CBotToken* name, const CBotTypResult& type ); + + /*! + * \brief ~CBotVarClass + */ + ~CBotVarClass(); + + /*! + * \brief Copy + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief SetClass + * \param pClass + */ + void SetClass(CBotClass* pClass) override; + + /*! + * \brief GetClass + * \return + */ + CBotClass* GetClass() override; + + /*! + * \brief GetItem Return an element of a class according to its name (*). + * \param name + * \return + */ + CBotVar* GetItem(const char* name) override; + + /*! + * \brief GetItemRef + * \param nIdent + * \return + */ + CBotVar* GetItemRef(int nIdent) override; + + /*! + * \brief GetItem + * \param n + * \param bExtend + * \return + */ + CBotVar* GetItem(int n, bool bExtend) override; + + /*! + * \brief GetItemList + * \return + */ + CBotVar* GetItemList() override; + + /*! + * \brief GetValString + * \return + */ + CBotString GetValString() override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + + /*! + * \brief Maj + * \param pUser + * \param bContinue + */ + void Maj(void* pUser, bool bContinue) override; + + /*! + * \brief IncrementUse A reference to incrementation. + */ + void IncrementUse(); + + /*! + * \brief DecrementUse A reference to decrementation. + */ + void DecrementUse(); + + /*! + * \brief GetPointer + * \return + */ + CBotVarClass* GetPointer() override; + + /*! + * \brief SetItemList + * \param pVar + */ + void SetItemList(CBotVar* pVar); + + /*! + * \brief SetIdent + * \param n + */ + void SetIdent(long n) override; + + /*! + * \brief Find + * \param id + * \return + */ + static CBotVarClass* Find(long id); + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + bool Eq(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + bool Ne(CBotVar* left, CBotVar* right) override; + + /*! + * \brief ConstructorSet + */ + void ConstructorSet() override; + +private: + //! List of existing instances at some point. + static CBotVarClass* m_ExClass; + //! For this general list. + CBotVarClass* m_ExNext; + //! For this general list. + CBotVarClass* m_ExPrev; + //! The class definition. + CBotClass* m_pClass; + //! The instance of a parent class. + CBotVarClass* m_pParent; + //! Contents. + CBotVar* m_pVar; + //! Counter usage. + int m_CptUse; + //! Identifier (unique) of an instance. + long m_ItemIdent; + //! Set if a constructor has been called. + bool m_bConstructor; + + friend class CBotVar; + friend class CBotVarPointer; +}; diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp index c669a2a0..df333221 100644 --- a/src/CBot/CBotVar/CBotVarPointer.cpp +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -22,7 +22,7 @@ #include "CBotToken.h" #include "CBot.h" #include "CBotClass.h" - +#include "CBotVarClass.h" // Local include // Global include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index a8ce7ac8..098868ed 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -57,6 +57,7 @@ set(SOURCES CBotInstr/CBotFunction.cpp CBotVar/CBotVarArray.cpp CBotVar/CBotVarPointer.cpp + CBotVar/CBotVarClass.cpp ) # Includes From ade4aefb0e6855fa98f64515933b7883512e212c Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 21:13:50 +0100 Subject: [PATCH 58/91] Moving CBotVarBoolean class in its own header and source files. --- src/CBot/CBot.h | 28 ----- src/CBot/CBotProgram.cpp | 10 -- src/CBot/CBotStack.cpp | 6 -- src/CBot/CBotUtils.cpp | 10 ++ src/CBot/CBotUtils.h | 8 ++ src/CBot/CBotVar.cpp | 115 +------------------- src/CBot/CBotVar/CBotVarBoolean.cpp | 156 ++++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarBoolean.h | 133 ++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 9 files changed, 309 insertions(+), 158 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarBoolean.cpp create mode 100644 src/CBot/CBotVar/CBotVarBoolean.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 8c5f4114..13a11918 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -344,34 +344,6 @@ public: bool Save1State(FILE* pf) override; }; -// class for the management of boolean -class CBotVarBoolean : public CBotVar -{ -private: - bool m_val; // the value - -public: - CBotVarBoolean( const CBotToken* name ); -// ~CBotVarBoolean(); - - void SetValInt(int val, const char* s = nullptr) override; - void SetValFloat(float val) override; - int GetValInt() override; - float GetValFloat() override; - CBotString GetValString() override; - - void Copy(CBotVar* pSrc, bool bName=true) override; - - void And(CBotVar* left, CBotVar* right) override; - void Or(CBotVar* left, CBotVar* right) override; - void XOr(CBotVar* left, CBotVar* right) override; - void Not() override; - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; - - bool Save1State(FILE* pf) override; -}; - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index c06cfbe0..9817f370 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -382,16 +382,6 @@ bool CBotProgram::AddFunction(const char* name, return CBotCall::AddFunction(name, rExec, rCompile); } -//////////////////////////////////////////////////////////////////////////////// -bool WriteWord(FILE* pf, unsigned short w) -{ - size_t lg; - - lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); - - return (lg == 1); -} - //////////////////////////////////////////////////////////////////////////////// bool ReadWord(FILE* pf, unsigned short& w) { diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 1a21dd2c..421bf5df 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -990,12 +990,6 @@ bool CBotVarInt::Save1State(FILE* pf) return WriteWord(pf, m_val); // the value of the variable } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarBoolean::Save1State(FILE* pf) -{ - return WriteWord(pf, m_val); // the value of the variable -} - //////////////////////////////////////////////////////////////////////////////// bool CBotVarFloat::Save1State(FILE* pf) { diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index bb2b9409..3fe76d06 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -105,3 +105,13 @@ CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type) } return type; } + +//////////////////////////////////////////////////////////////////////////////// +bool WriteWord(FILE* pf, unsigned short w) +{ + size_t lg; + + lg = fwrite(&w, sizeof( unsigned short ), 1, pf ); + + return (lg == 1); +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 9d452d85..47a83d71 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -51,3 +51,11 @@ CBotTypResult TypeParam(CBotToken* &p, CBotCStack* pile); * \return */ CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type); + +/*! + * \brief WriteWord + * \param pf + * \param w + * \return + */ +bool WriteWord(FILE* pf, unsigned short w); diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index dc2517d9..ce0c5f06 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -30,6 +30,7 @@ #include "CBotVar/CBotVarArray.h" #include "CBotVar/CBotVarPointer.h" #include "CBotVar/CBotVarClass.h" +#include "CBotVar/CBotVarBoolean.h" #include "CBotDefines.h" #include "CBotClass.h" @@ -102,22 +103,6 @@ CBotVarString::CBotVarString( const CBotToken* name ) m_val.Empty(); } -CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; - m_InitExpr = nullptr; - m_LimExpr = nullptr; - m_type = CBotTypBoolean; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = 0; - - m_val = 0; -} - CBotVar::~CBotVar( ) { delete m_token; @@ -1136,104 +1121,6 @@ bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) } -////////////////////////////////////////////////////////////////////////////////////// - -// copy a variable into another -void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarBoolean* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - m_pMyThis = nullptr;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // keeps indentificator the same (by default) - if (m_ident == 0 ) m_ident = p->m_ident; -} - - - - -void CBotVarBoolean::SetValInt(int val, const char* s) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarBoolean::SetValFloat(float val) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -int CBotVarBoolean::GetValInt() -{ - return m_val; -} - -float CBotVarBoolean::GetValFloat() -{ - return static_cast(m_val); -} - -CBotString CBotVarBoolean::GetValString() -{ - CBotString ret; - - CBotString res; - - if ( m_binit == CBotVar::InitType::UNDEF ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } - - ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); - return ret; -} - -void CBotVarBoolean::And(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() && right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} -void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() || right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() ^ right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarBoolean::Not() -{ - m_val = m_val ? false : true ; -} - -bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() == right->GetValInt(); -} - -bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() != right->GetValInt(); -} - ////////////////////////////////////////////////////////////////////////////////////// // copy a variable into another diff --git a/src/CBot/CBotVar/CBotVarBoolean.cpp b/src/CBot/CBotVar/CBotVarBoolean.cpp new file mode 100644 index 00000000..f1c59347 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarBoolean.cpp @@ -0,0 +1,156 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarBoolean.h" + +#include "CBotUtils.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + m_InitExpr = nullptr; + m_LimExpr = nullptr; + m_type = CBotTypBoolean; + m_binit = InitType::UNDEF; + m_bStatic = false; + m_mPrivate = 0; + + m_val = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarBoolean* p = static_cast(pSrc); + + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = nullptr; + m_pMyThis = nullptr;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::SetValInt(int val, const char* s) +{ + m_val = static_cast(val); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::SetValFloat(float val) +{ + m_val = static_cast(val); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarBoolean::GetValInt() +{ + return m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +float CBotVarBoolean::GetValFloat() +{ + return static_cast(m_val); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarBoolean::GetValString() +{ + CBotString ret; + + CBotString res; + + if ( m_binit == CBotVar::InitType::UNDEF ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == CBotVar::InitType::IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } + + ret.LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); + return ret; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::And(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() && right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() || right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() ^ right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarBoolean::Not() +{ + m_val = m_val ? false : true ; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() == right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() != right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarBoolean::Save1State(FILE* pf) +{ + return WriteWord(pf, m_val); // the value of the variable +} diff --git a/src/CBot/CBotVar/CBotVarBoolean.h b/src/CBot/CBotVar/CBotVarBoolean.h new file mode 100644 index 00000000..e57c22f7 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarBoolean.h @@ -0,0 +1,133 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotToken.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarBoolean class Class for the management of boolean. + */ +class CBotVarBoolean : public CBotVar +{ +public: + + /*! + * \brief CBotVarBoolean + * \param name + */ + CBotVarBoolean( const CBotToken* name ); + + /*! + * \brief SetValInt + * \param val + * \param s + */ + void SetValInt(int val, const char* s = nullptr) override; + + /*! + * \brief SetValFloat + * \param val + */ + void SetValFloat(float val) override; + + /*! + * \brief GetValInt + * \return + */ + int GetValInt() override; + + /*! + * \brief GetValFloat + * \return + */ + float GetValFloat() override; + + /*! + * \brief GetValString + * \return + */ + CBotString GetValString() override; + + /*! + * \brief Copy Copy a variable into another. + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief And + * \param left + * \param right + */ + void And(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Or + * \param left + * \param right + */ + void Or(CBotVar* left, CBotVar* right) override; + + /*! + * \brief XOr + * \param left + * \param right + */ + void XOr(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Not + */ + void Not() override; + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + bool Eq(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + bool Ne(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + +private: + //! The value. + bool m_val; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 098868ed..283e01c8 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -58,6 +58,7 @@ set(SOURCES CBotVar/CBotVarArray.cpp CBotVar/CBotVarPointer.cpp CBotVar/CBotVarClass.cpp + CBotVar/CBotVarBoolean.cpp ) # Includes From 1b3b2ea5a1e4f3186f0e33f4f1eebce259e18f88 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 21:24:54 +0100 Subject: [PATCH 59/91] Moving CBotVarString class in its own header and source files. --- src/CBot/CBot.h | 29 ------ src/CBot/CBotProgram.cpp | 12 --- src/CBot/CBotStack.cpp | 6 -- src/CBot/CBotUtils.cpp | 12 +++ src/CBot/CBotUtils.h | 8 ++ src/CBot/CBotVar.cpp | 102 +-------------------- src/CBot/CBotVar/CBotVarString.cpp | 139 +++++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarString.h | 127 ++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 9 files changed, 288 insertions(+), 148 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarString.cpp create mode 100644 src/CBot/CBotVar/CBotVarString.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 13a11918..2c23fa77 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -316,35 +316,6 @@ public: bool Save1State(FILE* pf) override; }; - -// class for management of strings (String) -class CBotVarString : public CBotVar -{ -private: - CBotString m_val; // the value - -public: - CBotVarString( const CBotToken* name ); -// ~CBotVarString(); - - void SetValString(const char* p) override; - CBotString GetValString() override; - - void Copy(CBotVar* pSrc, bool bName=true) override; - - void Add(CBotVar* left, CBotVar* right) override; // addition - - bool Lo(CBotVar* left, CBotVar* right) override; - bool Hi(CBotVar* left, CBotVar* right) override; - bool Ls(CBotVar* left, CBotVar* right) override; - bool Hs(CBotVar* left, CBotVar* right) override; - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; - - bool Save1State(FILE* pf) override; -}; - - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 9817f370..9ce35958 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -432,18 +432,6 @@ bool ReadLong(FILE* pf, long& w) return (lg == 1); } -//////////////////////////////////////////////////////////////////////////////// -bool WriteString(FILE* pf, CBotString s) -{ - size_t lg1, lg2; - - lg1 = s.GetLength(); - if (!WriteWord(pf, lg1)) return false; - - lg2 = fwrite(s, 1, lg1, pf ); - return (lg1 == lg2); -} - //////////////////////////////////////////////////////////////////////////////// bool ReadString(FILE* pf, CBotString& s) { diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 421bf5df..574c81cf 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -996,12 +996,6 @@ bool CBotVarFloat::Save1State(FILE* pf) return WriteFloat(pf, m_val); // the value of the variable } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarString::Save1State(FILE* pf) -{ - return WriteString(pf, m_val); // the value of the variable -} - //////////////////////////////////////////////////////////////////////////////// bool CBotVarClass::Save1State(FILE* pf) { diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 3fe76d06..4596b6f2 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -115,3 +115,15 @@ bool WriteWord(FILE* pf, unsigned short w) return (lg == 1); } + +//////////////////////////////////////////////////////////////////////////////// +bool WriteString(FILE* pf, CBotString s) +{ + size_t lg1, lg2; + + lg1 = s.GetLength(); + if (!WriteWord(pf, lg1)) return false; + + lg2 = fwrite(s, 1, lg1, pf ); + return (lg1 == lg2); +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 47a83d71..000c90af 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -59,3 +59,11 @@ CBotTypResult ArrayType(CBotToken* &p, CBotCStack* pile, CBotTypResult type); * \return */ bool WriteWord(FILE* pf, unsigned short w); + +/*! + * \brief WriteString + * \param pf + * \param s + * \return + */ +bool WriteString(FILE* pf, CBotString s); diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index ce0c5f06..b87f718d 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -31,6 +31,7 @@ #include "CBotVar/CBotVarPointer.h" #include "CBotVar/CBotVarClass.h" #include "CBotVar/CBotVarBoolean.h" +#include "CBotVar/CBotVarString.h" #include "CBotDefines.h" #include "CBotClass.h" @@ -87,22 +88,6 @@ CBotVarFloat::CBotVarFloat( const CBotToken* name ) m_val = 0; } -CBotVarString::CBotVarString( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; - m_InitExpr = nullptr; - m_LimExpr = nullptr; - m_type = CBotTypString; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = 0; - - m_val.Empty(); -} - CBotVar::~CBotVar( ) { delete m_token; @@ -1120,91 +1105,6 @@ bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) return left->GetValFloat() != right->GetValFloat(); } - -////////////////////////////////////////////////////////////////////////////////////// - -// copy a variable into another -void CBotVarString::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarString* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - m_pMyThis = nullptr;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // keeps indentificator the same (by default) - if (m_ident == 0 ) m_ident = p->m_ident; -} - - -void CBotVarString::SetValString(const char* p) -{ - m_val = p; - m_binit = CBotVar::InitType::DEF; -} - -CBotString CBotVarString::GetValString() -{ - if ( m_binit == CBotVar::InitType::UNDEF ) - { - CBotString res; - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - CBotString res; - res.LoadString(TX_NAN); - return res; - } - - return m_val; -} - - -void CBotVarString::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValString() + right->GetValString(); - m_binit = CBotVar::InitType::DEF; -} - -bool CBotVarString::Eq(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -bool CBotVarString::Ne(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() != right->GetValString()); -} - - -bool CBotVarString::Lo(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -bool CBotVarString::Hi(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -bool CBotVarString::Ls(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -bool CBotVarString::Hs(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - - //////////////////////////////////////////////////////////////// // copy a variable into another diff --git a/src/CBot/CBotVar/CBotVarString.cpp b/src/CBot/CBotVar/CBotVarString.cpp new file mode 100644 index 00000000..7b740147 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarString.cpp @@ -0,0 +1,139 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarString.h" + +#include "CBotToken.h" + +#include "CBotUtils.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotVarString::CBotVarString( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + m_InitExpr = nullptr; + m_LimExpr = nullptr; + m_type = CBotTypString; + m_binit = InitType::UNDEF; + m_bStatic = false; + m_mPrivate = 0; + + m_val.Empty(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarString::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarString* p = static_cast(pSrc); + + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = nullptr; + m_pMyThis = nullptr;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarString::SetValString(const char* p) +{ + m_val = p; + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarString::GetValString() +{ + if ( m_binit == CBotVar::InitType::UNDEF ) + { + CBotString res; + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == CBotVar::InitType::IS_NAN ) + { + CBotString res; + res.LoadString(TX_NAN); + return res; + } + + return m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarString::Add(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValString() + right->GetValString(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Eq(CBotVar* left, CBotVar* right) +{ + return (left->GetValString() == right->GetValString()); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Ne(CBotVar* left, CBotVar* right) +{ + return (left->GetValString() != right->GetValString()); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Lo(CBotVar* left, CBotVar* right) +{ + return (left->GetValString() == right->GetValString()); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Hi(CBotVar* left, CBotVar* right) +{ + return (left->GetValString() == right->GetValString()); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Ls(CBotVar* left, CBotVar* right) +{ + return (left->GetValString() == right->GetValString()); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Hs(CBotVar* left, CBotVar* right) +{ + return (left->GetValString() == right->GetValString()); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarString::Save1State(FILE* pf) +{ + return WriteString(pf, m_val); // the value of the variable +} diff --git a/src/CBot/CBotVar/CBotVarString.h b/src/CBot/CBotVar/CBotVarString.h new file mode 100644 index 00000000..31c776e2 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarString.h @@ -0,0 +1,127 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarString class Class for management of strings (String). + */ +class CBotVarString : public CBotVar +{ +public: + + /*! + * \brief CBotVarString + * \param name + */ + CBotVarString( const CBotToken* name ); + + /*! + * \brief SetValString + * \param p + */ + void SetValString(const char* p) override; + + /*! + * \brief GetValString + * \return + */ + CBotString GetValString() override; + + /*! + * \brief Copy Copy a variable into another. + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief Add + * \param left + * \param right + */ + void Add(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Lo + * \param left + * \param right + * \return + */ + bool Lo(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Hi + * \param left + * \param right + * \return + */ + bool Hi(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ls + * \param left + * \param right + * \return + */ + bool Ls(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Hs + * \param left + * \param right + * \return + */ + bool Hs(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + bool Eq(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + bool Ne(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + +private: + //! The value. + CBotString m_val; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 283e01c8..1dd05b8b 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -59,6 +59,7 @@ set(SOURCES CBotVar/CBotVarPointer.cpp CBotVar/CBotVarClass.cpp CBotVar/CBotVarBoolean.cpp + CBotVar/CBotVarString.cpp ) # Includes From 44021e91f7ae3a5f710c4f28840a390d59c35277 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 21:37:56 +0100 Subject: [PATCH 60/91] Moving CBotVarFloat class in its own header and source files. --- src/CBot/CBot.h | 40 ------ src/CBot/CBotProgram.cpp | 10 -- src/CBot/CBotStack.cpp | 6 - src/CBot/CBotUtils.cpp | 10 ++ src/CBot/CBotUtils.h | 8 ++ src/CBot/CBotVar.cpp | 178 +---------------------- src/CBot/CBotVar/CBotVarFloat.cpp | 226 ++++++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarFloat.h | 198 ++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 9 files changed, 444 insertions(+), 233 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarFloat.cpp create mode 100644 src/CBot/CBotVar/CBotVarFloat.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 2c23fa77..a742d47a 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -276,46 +276,6 @@ public: }; -// Class for managing real numbers (float) -class CBotVarFloat : public CBotVar -{ -private: - float m_val; // the value - -public: - CBotVarFloat( const CBotToken* name ); -// ~CBotVarFloat(); - - void SetValInt(int val, const char* s = nullptr) override; - void SetValFloat(float val) override; - int GetValInt() override; - float GetValFloat() override; - CBotString GetValString() override; - - void Copy(CBotVar* pSrc, bool bName=true) override; - - - void Add(CBotVar* left, CBotVar* right) override; // addition - void Sub(CBotVar* left, CBotVar* right) override; // substraction - void Mul(CBotVar* left, CBotVar* right) override; // multiplication - int Div(CBotVar* left, CBotVar* right) override; // division - int Modulo(CBotVar* left, CBotVar* right) override; // remainder of division - void Power(CBotVar* left, CBotVar* right) override; // power - - bool Lo(CBotVar* left, CBotVar* right) override; - bool Hi(CBotVar* left, CBotVar* right) override; - bool Ls(CBotVar* left, CBotVar* right) override; - bool Hs(CBotVar* left, CBotVar* right) override; - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; - - void Neg() override; - void Inc() override; - void Dec() override; - - bool Save1State(FILE* pf) override; -}; - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 9ce35958..44eaca50 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -392,16 +392,6 @@ bool ReadWord(FILE* pf, unsigned short& w) return (lg == 1); } -//////////////////////////////////////////////////////////////////////////////// -bool WriteFloat(FILE* pf, float w) -{ - size_t lg; - - lg = fwrite(&w, sizeof( float ), 1, pf ); - - return (lg == 1); -} - //////////////////////////////////////////////////////////////////////////////// bool ReadFloat(FILE* pf, float& w) { diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 574c81cf..e8310081 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -990,12 +990,6 @@ bool CBotVarInt::Save1State(FILE* pf) return WriteWord(pf, m_val); // the value of the variable } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Save1State(FILE* pf) -{ - return WriteFloat(pf, m_val); // the value of the variable -} - //////////////////////////////////////////////////////////////////////////////// bool CBotVarClass::Save1State(FILE* pf) { diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 4596b6f2..eee71e44 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -127,3 +127,13 @@ bool WriteString(FILE* pf, CBotString s) lg2 = fwrite(s, 1, lg1, pf ); return (lg1 == lg2); } + +//////////////////////////////////////////////////////////////////////////////// +bool WriteFloat(FILE* pf, float w) +{ + size_t lg; + + lg = fwrite(&w, sizeof( float ), 1, pf ); + + return (lg == 1); +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 000c90af..354595b8 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -67,3 +67,11 @@ bool WriteWord(FILE* pf, unsigned short w); * \return */ bool WriteString(FILE* pf, CBotString s); + +/*! + * \brief WriteFloat + * \param pf + * \param w + * \return + */ +bool WriteFloat(FILE* pf, float w); diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index b87f718d..154405e1 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -32,6 +32,7 @@ #include "CBotVar/CBotVarClass.h" #include "CBotVar/CBotVarBoolean.h" #include "CBotVar/CBotVarString.h" +#include "CBotVar/CBotVarFloat.h" #include "CBotDefines.h" #include "CBotClass.h" @@ -72,22 +73,6 @@ CBotVarInt::CBotVarInt( const CBotToken* name ) m_val = 0; } -CBotVarFloat::CBotVarFloat( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; - m_InitExpr = nullptr; - m_LimExpr = nullptr; - m_type = CBotTypFloat; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = 0; - - m_val = 0; -} - CBotVar::~CBotVar( ) { delete m_token; @@ -944,167 +929,6 @@ bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) return left->GetValInt() != right->GetValInt(); } - -////////////////////////////////////////////////////////////////////////////////////// - -// copy a variable into another -void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarFloat* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - m_pMyThis = nullptr;//p->m_pMyThis; - m_pUserPtr = p->m_pUserPtr; - - // keeps indentificator the same (by default) - if (m_ident == 0 ) m_ident = p->m_ident; -} - - - - -void CBotVarFloat::SetValInt(int val, const char* s) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarFloat::SetValFloat(float val) -{ - m_val = val; - m_binit = CBotVar::InitType::DEF; -} - -int CBotVarFloat::GetValInt() -{ - return static_cast(m_val); -} - -float CBotVarFloat::GetValFloat() -{ - return m_val; -} - -CBotString CBotVarFloat::GetValString() -{ - CBotString res; - - if ( m_binit == CBotVar::InitType::UNDEF ) - { - res.LoadString(TX_UNDEF); - return res; - } - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } - - char buffer[300]; - sprintf(buffer, "%.2f", m_val); - res = buffer; - - return res; -} - - -void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValFloat() * right->GetValFloat(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarFloat::Power(CBotVar* left, CBotVar* right) -{ - m_val = static_cast(pow( left->GetValFloat() , right->GetValFloat() )); - m_binit = CBotVar::InitType::DEF; -} - -int CBotVarFloat::Div(CBotVar* left, CBotVar* right) -{ - float r = right->GetValFloat(); - if ( r != 0 ) - { - m_val = left->GetValFloat() / r; - m_binit = CBotVar::InitType::DEF; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) -{ - float r = right->GetValFloat(); - if ( r != 0 ) - { - m_val = static_cast(fmod( left->GetValFloat() , r )); - m_binit = CBotVar::InitType::DEF; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -void CBotVarFloat::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValFloat() + right->GetValFloat(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValFloat() - right->GetValFloat(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarFloat::Neg() -{ - m_val = -m_val; -} - -void CBotVarFloat::Inc() -{ - m_val++; -} - -void CBotVarFloat::Dec() -{ - m_val--; -} - - -bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() < right->GetValFloat(); -} - -bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() > right->GetValFloat(); -} - -bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() <= right->GetValFloat(); -} - -bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() >= right->GetValFloat(); -} - -bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() == right->GetValFloat(); -} - -bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() != right->GetValFloat(); -} - //////////////////////////////////////////////////////////////// // copy a variable into another diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp new file mode 100644 index 00000000..465c2940 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -0,0 +1,226 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarFloat.h" + +#include "CBotToken.h" + +#include "CBotUtils.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotVarFloat::CBotVarFloat( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + m_InitExpr = nullptr; + m_LimExpr = nullptr; + m_type = CBotTypFloat; + m_binit = InitType::UNDEF; + m_bStatic = false; + m_mPrivate = 0; + + m_val = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarFloat* p = static_cast(pSrc); + + if (bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_next = nullptr; + m_pMyThis = nullptr;//p->m_pMyThis; + m_pUserPtr = p->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::SetValInt(int val, const char* s) +{ + m_val = static_cast(val); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::SetValFloat(float val) +{ + m_val = val; + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarFloat::GetValInt() +{ + return static_cast(m_val); +} + +//////////////////////////////////////////////////////////////////////////////// +float CBotVarFloat::GetValFloat() +{ + return m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarFloat::GetValString() +{ + CBotString res; + + if ( m_binit == CBotVar::InitType::UNDEF ) + { + res.LoadString(TX_UNDEF); + return res; + } + if ( m_binit == CBotVar::InitType::IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } + + char buffer[300]; + sprintf(buffer, "%.2f", m_val); + res = buffer; + + return res; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValFloat() * right->GetValFloat(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Power(CBotVar* left, CBotVar* right) +{ + m_val = static_cast(pow( left->GetValFloat() , right->GetValFloat() )); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarFloat::Div(CBotVar* left, CBotVar* right) +{ + float r = right->GetValFloat(); + if ( r != 0 ) + { + m_val = left->GetValFloat() / r; + m_binit = CBotVar::InitType::DEF; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) +{ + float r = right->GetValFloat(); + if ( r != 0 ) + { + m_val = static_cast(fmod( left->GetValFloat() , r )); + m_binit = CBotVar::InitType::DEF; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Add(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValFloat() + right->GetValFloat(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValFloat() - right->GetValFloat(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Neg() +{ + m_val = -m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Inc() +{ + m_val++; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarFloat::Dec() +{ + m_val--; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) +{ + return left->GetValFloat() < right->GetValFloat(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) +{ + return left->GetValFloat() > right->GetValFloat(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) +{ + return left->GetValFloat() <= right->GetValFloat(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) +{ + return left->GetValFloat() >= right->GetValFloat(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) +{ + return left->GetValFloat() == right->GetValFloat(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) +{ + return left->GetValFloat() != right->GetValFloat(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarFloat::Save1State(FILE* pf) +{ + return WriteFloat(pf, m_val); // the value of the variable +} diff --git a/src/CBot/CBotVar/CBotVarFloat.h b/src/CBot/CBotVar/CBotVarFloat.h new file mode 100644 index 00000000..fb30cd49 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarFloat.h @@ -0,0 +1,198 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarFloat class Class for managing real numbers (float). + */ +class CBotVarFloat : public CBotVar +{ +public: + + /*! + * \brief CBotVarFloat + * \param name + */ + CBotVarFloat( const CBotToken* name ); + + /*! + * \brief SetValInt + * \param val + * \param s + */ + void SetValInt(int val, const char* s = nullptr) override; + + /*! + * \brief SetValFloat + * \param val + */ + void SetValFloat(float val) override; + + /*! + * \brief GetValInt + * \return + */ + int GetValInt() override; + + /*! + * \brief GetValFloat + * \return + */ + float GetValFloat() override; + + /*! + * \brief GetValString + * \return + */ + CBotString GetValString() override; + + /*! + * \brief Copy Copy a variable into another. + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief Add Addition. + * \param left + * \param right + */ + void Add(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Sub Substraction. + * \param left + * \param right + */ + void Sub(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Mul Multiplication. + * \param left + * \param right + */ + void Mul(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Div Division. + * \param left + * \param right + * \return + */ + int Div(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Modulo Remainder of division. + * \param left + * \param right + * \return + */ + int Modulo(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Power + * \param left + * \param right + */ + void Power(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Lo + * \param left + * \param right + * \return + */ + bool Lo(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Hi + * \param left + * \param right + * \return + */ + bool Hi(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ls + * \param left + * \param right + * \return + */ + bool Ls(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Hs + * \param left + * \param right + * \return + */ + bool Hs(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + bool Eq(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + bool Ne(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Neg + */ + void Neg() override; + + /*! + * \brief Inc + */ + void Inc() override; + + /*! + * \brief Dec + */ + void Dec() override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + +private: + //! The value. + float m_val; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 1dd05b8b..33474db8 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -60,6 +60,7 @@ set(SOURCES CBotVar/CBotVarClass.cpp CBotVar/CBotVarBoolean.cpp CBotVar/CBotVarString.cpp + CBotVar/CBotVarFloat.cpp ) # Includes From a4f14650c6a6b1070f5d155b14daab33d43c525b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 21:52:05 +0100 Subject: [PATCH 61/91] Moving CBotVarInt class in its own header and source files. --- src/CBot/CBot.h | 54 ------ src/CBot/CBotStack.cpp | 18 -- src/CBot/CBotVar.cpp | 235 +------------------------ src/CBot/CBotVar/CBotVarInt.cpp | 295 ++++++++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarInt.h | 255 +++++++++++++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 6 files changed, 552 insertions(+), 306 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarInt.cpp create mode 100644 src/CBot/CBotVar/CBotVarInt.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index a742d47a..98d3f9ce 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -222,60 +222,6 @@ public: #define MAX(a,b) ((a>b) ? a : b) - -// class for the management of integer numbers (int) -class CBotVarInt : public CBotVar -{ -private: - int m_val; // the value - CBotString m_defnum; // the name if given by DefineNum - friend class CBotVar; - -public: - CBotVarInt( const CBotToken* name ); -// ~CBotVarInt(); - - void SetValInt(int val, const char* s = nullptr) override; - void SetValFloat(float val) override; - int GetValInt() override; - float GetValFloat() override; - CBotString GetValString() override; - - void Copy(CBotVar* pSrc, bool bName=true) override; - - - void Add(CBotVar* left, CBotVar* right) override; // addition - void Sub(CBotVar* left, CBotVar* right) override; // substraction - void Mul(CBotVar* left, CBotVar* right) override; // multiplication - int Div(CBotVar* left, CBotVar* right) override; // division - int Modulo(CBotVar* left, CBotVar* right) override; // remainder of division - void Power(CBotVar* left, CBotVar* right) override; // power - - bool Lo(CBotVar* left, CBotVar* right) override; - bool Hi(CBotVar* left, CBotVar* right) override; - bool Ls(CBotVar* left, CBotVar* right) override; - bool Hs(CBotVar* left, CBotVar* right) override; - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; - - void XOr(CBotVar* left, CBotVar* right) override; - void Or(CBotVar* left, CBotVar* right) override; - void And(CBotVar* left, CBotVar* right) override; - - void SL(CBotVar* left, CBotVar* right) override; - void SR(CBotVar* left, CBotVar* right) override; - void ASR(CBotVar* left, CBotVar* right) override; - - void Neg() override; - void Not() override; - void Inc() override; - void Dec() override; - - bool Save0State(FILE* pf) override; - bool Save1State(FILE* pf) override; - -}; - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index e8310081..63ee21d4 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -972,24 +972,6 @@ bool CBotVar::Save0State(FILE* pf) return WriteString(pf, m_token->GetString()); // and variable name } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarInt::Save0State(FILE* pf) -{ - if ( !m_defnum.IsEmpty() ) - { - if(!WriteWord(pf, 200 )) return false; // special marker - if(!WriteString(pf, m_defnum)) return false; // name of the value - } - - return CBotVar::Save0State(pf); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarInt::Save1State(FILE* pf) -{ - return WriteWord(pf, m_val); // the value of the variable -} - //////////////////////////////////////////////////////////////////////////////// bool CBotVarClass::Save1State(FILE* pf) { diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 154405e1..74bfb646 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -33,6 +33,7 @@ #include "CBotVar/CBotVarBoolean.h" #include "CBotVar/CBotVarString.h" #include "CBotVar/CBotVarFloat.h" +#include "CBotVar/CBotVarInt.h" #include "CBotDefines.h" #include "CBotClass.h" @@ -57,22 +58,6 @@ CBotVar::CBotVar( ) m_mPrivate = 0; } -CBotVarInt::CBotVarInt( const CBotToken* name ) -{ - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; - m_InitExpr = nullptr; - m_LimExpr = nullptr; - m_type = CBotTypInt; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = 0; - - m_val = 0; -} - CBotVar::~CBotVar( ) { delete m_token; @@ -711,224 +696,6 @@ CBotClass* CBotVar::GetClass() return nullptr; } -/* -void CBotVar::SetIndirection(CBotVar* pVar) -{ - // nop, only CBotVarPointer::SetIndirection -} -*/ - -////////////////////////////////////////////////////////////////////////////////////// - -// copy a variable in to another -void CBotVarInt::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarInt* p = static_cast(pSrc); - - if ( bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; - m_pMyThis = nullptr; - m_pUserPtr = p->m_pUserPtr; - - // identificator is the same (by défaut) - if (m_ident == 0 ) m_ident = p->m_ident; - - m_defnum = p->m_defnum; -} - - - - -void CBotVarInt::SetValInt(int val, const char* defnum) -{ - m_val = val; - m_binit = CBotVar::InitType::DEF; - m_defnum = defnum; -} - - - -void CBotVarInt::SetValFloat(float val) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -int CBotVarInt::GetValInt() -{ - return m_val; -} - -float CBotVarInt::GetValFloat() -{ - return static_cast(m_val); -} - -CBotString CBotVarInt::GetValString() -{ - if ( !m_defnum.IsEmpty() ) return m_defnum; - - CBotString res; - - if ( m_binit == CBotVar::InitType::UNDEF ) - { - res.LoadString(TX_UNDEF); - return res; - } - - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - res.LoadString(TX_NAN); - return res; - } - - char buffer[300]; - sprintf(buffer, "%d", m_val); - res = buffer; - - return res; -} - - -void CBotVarInt::Mul(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() * right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::Power(CBotVar* left, CBotVar* right) -{ - m_val = static_cast( pow( static_cast( left->GetValInt()) , static_cast( left->GetValInt()) )); - m_binit = CBotVar::InitType::DEF; -} - -int CBotVarInt::Div(CBotVar* left, CBotVar* right) -{ - int r = right->GetValInt(); - if ( r != 0 ) - { - m_val = left->GetValInt() / r; - m_binit = CBotVar::InitType::DEF; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) -{ - int r = right->GetValInt(); - if ( r != 0 ) - { - m_val = left->GetValInt() % r; - m_binit = CBotVar::InitType::DEF; - } - return ( r == 0 ? TX_DIVZERO : 0 ); -} - -void CBotVarInt::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() + right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::Sub(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() - right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::XOr(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() ^ right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::And(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() & right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::Or(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() | right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::SL(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() << right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::ASR(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValInt() >> right->GetValInt(); - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::SR(CBotVar* left, CBotVar* right) -{ - int source = left->GetValInt(); - int shift = right->GetValInt(); - if (shift>=1) source &= 0x7fffffff; - m_val = source >> shift; - m_binit = CBotVar::InitType::DEF; -} - -void CBotVarInt::Neg() -{ - m_val = -m_val; -} - -void CBotVarInt::Not() -{ - m_val = ~m_val; -} - -void CBotVarInt::Inc() -{ - m_val++; - m_defnum.Empty(); -} - -void CBotVarInt::Dec() -{ - m_val--; - m_defnum.Empty(); -} - -bool CBotVarInt::Lo(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() < right->GetValInt(); -} - -bool CBotVarInt::Hi(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() > right->GetValInt(); -} - -bool CBotVarInt::Ls(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() <= right->GetValInt(); -} - -bool CBotVarInt::Hs(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() >= right->GetValInt(); -} - -bool CBotVarInt::Eq(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() == right->GetValInt(); -} - -bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() != right->GetValInt(); -} - //////////////////////////////////////////////////////////////// // copy a variable into another diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp new file mode 100644 index 00000000..76f89c12 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -0,0 +1,295 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotVarInt.h" + +#include "CBotToken.h" + +#include "CBotUtils.h" + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +CBotVarInt::CBotVarInt( const CBotToken* name ) +{ + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; + m_InitExpr = nullptr; + m_LimExpr = nullptr; + m_type = CBotTypInt; + m_binit = InitType::UNDEF; + m_bStatic = false; + m_mPrivate = 0; + + m_val = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Copy(CBotVar* pSrc, bool bName) +{ + CBotVarInt* p = static_cast(pSrc); + + if ( bName) *m_token = *p->m_token; + m_type = p->m_type; + m_val = p->m_val; + m_binit = p->m_binit; + m_pMyThis = nullptr; + m_pUserPtr = p->m_pUserPtr; + + // identificator is the same (by défaut) + if (m_ident == 0 ) m_ident = p->m_ident; + + m_defnum = p->m_defnum; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::SetValInt(int val, const char* defnum) +{ + m_val = val; + m_binit = CBotVar::InitType::DEF; + m_defnum = defnum; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::SetValFloat(float val) +{ + m_val = static_cast(val); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarInt::GetValInt() +{ + return m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +float CBotVarInt::GetValFloat() +{ + return static_cast(m_val); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarInt::GetValString() +{ + if ( !m_defnum.IsEmpty() ) return m_defnum; + + CBotString res; + + if ( m_binit == CBotVar::InitType::UNDEF ) + { + res.LoadString(TX_UNDEF); + return res; + } + + if ( m_binit == CBotVar::InitType::IS_NAN ) + { + res.LoadString(TX_NAN); + return res; + } + + char buffer[300]; + sprintf(buffer, "%d", m_val); + res = buffer; + + return res; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Mul(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() * right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Power(CBotVar* left, CBotVar* right) +{ + m_val = static_cast( pow( static_cast( left->GetValInt()) , static_cast( left->GetValInt()) )); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarInt::Div(CBotVar* left, CBotVar* right) +{ + int r = right->GetValInt(); + if ( r != 0 ) + { + m_val = left->GetValInt() / r; + m_binit = CBotVar::InitType::DEF; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotVarInt::Modulo(CBotVar* left, CBotVar* right) +{ + int r = right->GetValInt(); + if ( r != 0 ) + { + m_val = left->GetValInt() % r; + m_binit = CBotVar::InitType::DEF; + } + return ( r == 0 ? TX_DIVZERO : 0 ); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Add(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() + right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Sub(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() - right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::XOr(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() ^ right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::And(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() & right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Or(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() | right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::SL(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() << right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::ASR(CBotVar* left, CBotVar* right) +{ + m_val = left->GetValInt() >> right->GetValInt(); + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::SR(CBotVar* left, CBotVar* right) +{ + int source = left->GetValInt(); + int shift = right->GetValInt(); + if (shift>=1) source &= 0x7fffffff; + m_val = source >> shift; + m_binit = CBotVar::InitType::DEF; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Neg() +{ + m_val = -m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Not() +{ + m_val = ~m_val; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Inc() +{ + m_val++; + m_defnum.Empty(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarInt::Dec() +{ + m_val--; + m_defnum.Empty(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Lo(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() < right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Hi(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() > right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Ls(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() <= right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Hs(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() >= right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Eq(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() == right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Ne(CBotVar* left, CBotVar* right) +{ + return left->GetValInt() != right->GetValInt(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Save0State(FILE* pf) +{ + if ( !m_defnum.IsEmpty() ) + { + if(!WriteWord(pf, 200 )) return false; // special marker + if(!WriteString(pf, m_defnum)) return false; // name of the value + } + + return CBotVar::Save0State(pf); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarInt::Save1State(FILE* pf) +{ + return WriteWord(pf, m_val); // the value of the variable +} diff --git a/src/CBot/CBotVar/CBotVarInt.h b/src/CBot/CBotVar/CBotVarInt.h new file mode 100644 index 00000000..741fdc10 --- /dev/null +++ b/src/CBot/CBotVar/CBotVarInt.h @@ -0,0 +1,255 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVarInt class Class for the management of integer numbers (int). + */ +class CBotVarInt : public CBotVar +{ +public: + + /*! + * \brief CBotVarInt + * \param name + */ + CBotVarInt( const CBotToken* name ); + + /*! + * \brief SetValInt + * \param val + * \param s + */ + void SetValInt(int val, const char* s = nullptr) override; + + /*! + * \brief SetValFloat + * \param val + */ + void SetValFloat(float val) override; + + /*! + * \brief GetValInt + * \return + */ + int GetValInt() override; + + /*! + * \brief GetValFloat + * \return + */ + float GetValFloat() override; + + /*! + * \brief GetValString + * \return + */ + CBotString GetValString() override; + + /*! + * \brief Copy Copy a variable in to another. + * \param pSrc + * \param bName + */ + void Copy(CBotVar* pSrc, bool bName=true) override; + + /*! + * \brief Add + * \param left + * \param right + */ + void Add(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Sub + * \param left + * \param right + */ + void Sub(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Mul + * \param left + * \param right + */ + void Mul(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Div + * \param left + * \param right + * \return + */ + int Div(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Modulo + * \param left + * \param right + * \return + */ + int Modulo(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Power + * \param left + * \param right + */ + void Power(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Lo + * \param left + * \param right + * \return + */ + bool Lo(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Hi + * \param left + * \param right + * \return + */ + bool Hi(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ls + * \param left + * \param right + * \return + */ + bool Ls(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Hs + * \param left + * \param right + * \return + */ + bool Hs(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + bool Eq(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + bool Ne(CBotVar* left, CBotVar* right) override; + + /*! + * \brief XOr + * \param left + * \param right + */ + void XOr(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Or + * \param left + * \param right + */ + void Or(CBotVar* left, CBotVar* right) override; + + /*! + * \brief And + * \param left + * \param right + */ + void And(CBotVar* left, CBotVar* right) override; + + /*! + * \brief SL + * \param left + * \param right + */ + void SL(CBotVar* left, CBotVar* right) override; + + /*! + * \brief SR + * \param left + * \param right + */ + void SR(CBotVar* left, CBotVar* right) override; + + /*! + * \brief ASR + * \param left + * \param right + */ + void ASR(CBotVar* left, CBotVar* right) override; + + /*! + * \brief Neg + */ + void Neg() override; + + /*! + * \brief Not + */ + void Not() override; + + /*! + * \brief Inc + */ + void Inc() override; + + /*! + * \brief Dec + */ + void Dec() override; + + /*! + * \brief Save0State + * \param pf + * \return + */ + bool Save0State(FILE* pf) override; + + /*! + * \brief Save1State + * \param pf + * \return + */ + bool Save1State(FILE* pf) override; + +private: + //! The value. + int m_val; + //! The name if given by DefineNum. + CBotString m_defnum; + friend class CBotVar; +}; diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 33474db8..6e8a8039 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -61,6 +61,7 @@ set(SOURCES CBotVar/CBotVarBoolean.cpp CBotVar/CBotVarString.cpp CBotVar/CBotVarFloat.cpp + CBotVar/CBotVarInt.cpp ) # Includes From 2eeab6d4d0c9114f73d753b21838cddfceae14d9 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 22:00:17 +0100 Subject: [PATCH 62/91] Moving CBotVarClass function from CBotVar.cpp to CBotVarClass.cpp. --- src/CBot/CBotStack.cpp | 9 - src/CBot/CBotVar.cpp | 373 ---------------------------- src/CBot/CBotVar/CBotVarClass.cpp | 391 ++++++++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarClass.h | 9 +- 4 files changed, 396 insertions(+), 386 deletions(-) diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 63ee21d4..87aa0bb2 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -972,15 +972,6 @@ bool CBotVar::Save0State(FILE* pf) return WriteString(pf, m_token->GetString()); // and variable name } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarClass::Save1State(FILE* pf) -{ - if ( !WriteType(pf, m_type) ) return false; - if ( !WriteLong(pf, m_ItemIdent) ) return false; - - return SaveVar(pf, m_pVar); // content of the object -} - namespace { bool ParseInitType(int rawInitType, CBotVar::InitType* initType) diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar.cpp index 74bfb646..d5c248e7 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar.cpp @@ -35,7 +35,6 @@ #include "CBotVar/CBotVarFloat.h" #include "CBotVar/CBotVarInt.h" -#include "CBotDefines.h" #include "CBotClass.h" #include @@ -696,378 +695,6 @@ CBotClass* CBotVar::GetClass() return nullptr; } -//////////////////////////////////////////////////////////////// - -// copy a variable into another -void CBotVarClass::Copy(CBotVar* pSrc, bool bName) -{ - pSrc = pSrc->GetPointer(); // if source given by a pointer - - if ( pSrc->GetType() != CBotTypClass ) - assert(0); - - CBotVarClass* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - - m_type = p->m_type; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_pClass = p->m_pClass; - if ( p->m_pParent ) - { - assert(0); // "que faire du pParent"; - } - -// m_next = nullptr; - m_pUserPtr = p->m_pUserPtr; - m_pMyThis = nullptr;//p->m_pMyThis; - m_ItemIdent = p->m_ItemIdent; - - // keeps indentificator the same (by default) - if (m_ident == 0 ) m_ident = p->m_ident; - - delete m_pVar; - m_pVar = nullptr; - - CBotVar* pv = p->m_pVar; - while( pv != nullptr ) - { - CBotVar* pn = CBotVar::Create(pv); - pn->Copy( pv ); - if ( m_pVar == nullptr ) m_pVar = pn; - else m_pVar->AddNext(pn); - - pv = pv->GetNext(); - } -} - -void CBotVarClass::SetItemList(CBotVar* pVar) -{ - delete m_pVar; - m_pVar = pVar; // replaces the existing pointer -} - -void CBotVarClass::SetIdent(long n) -{ - m_ItemIdent = n; -} - -void CBotVarClass::SetClass(CBotClass* pClass)//, int &nIdent) -{ - m_type.m_pClass = pClass; - - if ( m_pClass == pClass ) return; - - m_pClass = pClass; - - // initializes the variables associated with this class - delete m_pVar; - m_pVar = nullptr; - - if (pClass == nullptr) return; - - CBotVar* pv = pClass->GetVar(); // first on a list - while ( pv != nullptr ) - { - // seeks the maximum dimensions of the table - CBotInstr* p = pv->m_LimExpr; // the different formulas - if ( p != nullptr ) - { - CBotStack* pile = CBotStack::FirstStack(); // an independent stack - int n = 0; - int max[100]; - - while (p != nullptr) - { - while( pile->IsOk() && !p->Execute(pile) ) ; // calculate size without interruptions - CBotVar* v = pile->GetVar(); // result - max[n] = v->GetValInt(); // value - n++; - p = p->GetNext3(); - } - while (n<100) max[n++] = 0; - - pv->m_type.SetArray( max ); // stores the limitations - pile->Delete(); - } - - CBotVar* pn = CBotVar::Create( pv ); // a copy - pn->SetStatic(pv->IsStatic()); - pn->SetPrivate(pv->GetPrivate()); - - if ( pv->m_InitExpr != nullptr ) // expression for initialization? - { -#if STACKMEM - CBotStack* pile = CBotStack::FirstStack(); // an independent stack - - while(pile->IsOk() && !pv->m_InitExpr->Execute(pile, pn)); // evaluates the expression without timer - - pile->Delete(); -#else - CBotStack* pile = new CBotStack(nullptr); // an independent stack - while(!pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer - pn->SetVal( pile->GetVar() ) ; - delete pile; -#endif - } - -// pn->SetUniqNum(CBotVar::NextUniqNum()); // enumerate elements - pn->SetUniqNum(pv->GetUniqNum()); //++nIdent - pn->m_pMyThis = this; - - if ( m_pVar == nullptr) m_pVar = pn; - else m_pVar->AddNext( pn ); - pv = pv->GetNext(); - } -} - -CBotClass* CBotVarClass::GetClass() -{ - return m_pClass; -} - - -void CBotVarClass::Maj(void* pUser, bool bContinu) -{ -/* if (!bContinu && m_pMyThis != nullptr) - m_pMyThis->Maj(pUser, true);*/ - - // an update routine exist? - - if ( m_pClass->m_rMaj == nullptr ) return; - - // retrieves the user pointer according to the class - // or according to the parameter passed to CBotProgram::Run() - - if ( m_pUserPtr != nullptr) pUser = m_pUserPtr; - if ( pUser == OBJECTDELETED || - pUser == OBJECTCREATED ) return; - m_pClass->m_rMaj( this, pUser ); -} - -CBotVar* CBotVarClass::GetItem(const char* name) -{ - CBotVar* p = m_pVar; - - while ( p != nullptr ) - { - if ( p->GetName() == name ) return p; - p = p->GetNext(); - } - - if ( m_pParent != nullptr ) return m_pParent->GetItem(name); - return nullptr; -} - -CBotVar* CBotVarClass::GetItemRef(int nIdent) -{ - CBotVar* p = m_pVar; - - while ( p != nullptr ) - { - if ( p->GetUniqNum() == nIdent ) return p; - p = p->GetNext(); - } - - if ( m_pParent != nullptr ) return m_pParent->GetItemRef(nIdent); - return nullptr; -} - -// for the management of an array -// bExtend can enlarge the table, but not beyond the threshold size of SetArray () - -CBotVar* CBotVarClass::GetItem(int n, bool bExtend) -{ - CBotVar* p = m_pVar; - - if ( n < 0 ) return nullptr; - if ( n > MAXARRAYSIZE ) return nullptr; - - if ( m_type.GetLimite() >= 0 && n >= m_type.GetLimite() ) return nullptr; - - if ( p == nullptr && bExtend ) - { - p = CBotVar::Create("", m_type.GetTypElem()); - m_pVar = p; - } - - if ( n == 0 ) return p; - - while ( n-- > 0 ) - { - if ( p->m_next == nullptr ) - { - if ( bExtend ) p->m_next = CBotVar::Create("", m_type.GetTypElem()); - if ( p->m_next == nullptr ) return nullptr; - } - p = p->m_next; - } - - return p; -} - -CBotVar* CBotVarClass::GetItemList() -{ - return m_pVar; -} - - -CBotString CBotVarClass::GetValString() -{ -// if ( m_Indirect != nullptr) return m_Indirect->GetValString(); - - CBotString res; - - if ( m_pClass != nullptr ) // not used for an array - { - res = m_pClass->GetName() + CBotString("( "); - - CBotVarClass* my = this; - while ( my != nullptr ) - { - CBotVar* pv = my->m_pVar; - while ( pv != nullptr ) - { - res += pv->GetName() + CBotString("="); - - if ( pv->IsStatic() ) - { - CBotVar* pvv = my->m_pClass->GetItem(pv->GetName()); - res += pvv->GetValString(); - } - else - { - res += pv->GetValString(); - } - pv = pv->GetNext(); - if ( pv != nullptr ) res += ", "; - } - my = my->m_pParent; - if ( my != nullptr ) - { - res += ") extends "; - res += my->m_pClass->GetName(); - res += " ("; - } - } - } - else - { - res = "( "; - - CBotVar* pv = m_pVar; - while ( pv != nullptr ) - { - res += pv->GetValString(); - if ( pv->GetNext() != nullptr ) res += ", "; - pv = pv->GetNext(); - } - } - - res += " )"; - return res; -} - -void CBotVarClass::IncrementUse() -{ - m_CptUse++; -} - -void CBotVarClass::DecrementUse() -{ - m_CptUse--; - if ( m_CptUse == 0 ) - { - // if there is one, call the destructor - // but only if a constructor had been called. - if ( m_bConstructor ) - { - m_CptUse++; // does not return to the destructor - - // m_error is static in the stack - // saves the value for return - int err, start, end; - CBotStack* pile = nullptr; - err = pile->GetError(start,end); // stack == nullptr it does not bother! - - pile = CBotStack::FirstStack(); // clears the error - CBotVar* ppVars[1]; - ppVars[0] = nullptr; - - CBotVar* pThis = CBotVar::Create("this", CBotTypNullPointer); - pThis->SetPointer(this); - CBotVar* pResult = nullptr; - - CBotString nom = CBotString("~") + m_pClass->GetName(); - long ident = 0; - - while ( pile->IsOk() && !m_pClass->ExecuteMethode(ident, nom, pThis, ppVars, pResult, pile, nullptr)) ; // waits for the end - - pile->ResetError(err, start,end); - - pile->Delete(); - delete pThis; - m_CptUse--; - } - - delete this; // self-destructs! - } -} - -CBotVarClass* CBotVarClass::GetPointer() -{ - return this; -} - - -// makes an instance according to its unique number - -CBotVarClass* CBotVarClass::Find(long id) -{ - CBotVarClass* p = m_ExClass; - - while ( p != nullptr ) - { - if ( p->m_ItemIdent == id ) return p; - p = p->m_ExNext; - } - - return nullptr; -} - -bool CBotVarClass::Eq(CBotVar* left, CBotVar* right) -{ - CBotVar* l = left->GetItemList(); - CBotVar* r = right->GetItemList(); - - while ( l != nullptr && r != nullptr ) - { - if ( l->Ne(l, r) ) return false; - l = l->GetNext(); - r = r->GetNext(); - } - - // should always arrived simultaneously at the end (same classes) - return l == r; -} - -bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) -{ - CBotVar* l = left->GetItemList(); - CBotVar* r = right->GetItemList(); - - while ( l != nullptr && r != nullptr ) - { - if ( l->Ne(l, r) ) return true; - l = l->GetNext(); - r = r->GetNext(); - } - - // should always arrived simultaneously at the end (same classes) - return l != r; -} - /////////////////////////////////////////////////////// // management of results types diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index 73d14ac5..f07fbe9f 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -22,6 +22,10 @@ #include "CBotClass.h" +#include "CBotStack.h" + +#include "CBotDefines.h" + // Local include // Global include @@ -108,3 +112,390 @@ void CBotVarClass::ConstructorSet() { m_bConstructor = true; } + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::Copy(CBotVar* pSrc, bool bName) +{ + pSrc = pSrc->GetPointer(); // if source given by a pointer + + if ( pSrc->GetType() != CBotTypClass ) + assert(0); + + CBotVarClass* p = static_cast(pSrc); + + if (bName) *m_token = *p->m_token; + + m_type = p->m_type; + m_binit = p->m_binit; +//- m_bStatic = p->m_bStatic; + m_pClass = p->m_pClass; + if ( p->m_pParent ) + { + assert(0); // "que faire du pParent"; + } + +// m_next = nullptr; + m_pUserPtr = p->m_pUserPtr; + m_pMyThis = nullptr;//p->m_pMyThis; + m_ItemIdent = p->m_ItemIdent; + + // keeps indentificator the same (by default) + if (m_ident == 0 ) m_ident = p->m_ident; + + delete m_pVar; + m_pVar = nullptr; + + CBotVar* pv = p->m_pVar; + while( pv != nullptr ) + { + CBotVar* pn = CBotVar::Create(pv); + pn->Copy( pv ); + if ( m_pVar == nullptr ) m_pVar = pn; + else m_pVar->AddNext(pn); + + pv = pv->GetNext(); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::SetItemList(CBotVar* pVar) +{ + delete m_pVar; + m_pVar = pVar; // replaces the existing pointer +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::SetIdent(long n) +{ + m_ItemIdent = n; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::SetClass(CBotClass* pClass)//, int &nIdent) +{ + m_type.m_pClass = pClass; + + if ( m_pClass == pClass ) return; + + m_pClass = pClass; + + // initializes the variables associated with this class + delete m_pVar; + m_pVar = nullptr; + + if (pClass == nullptr) return; + + CBotVar* pv = pClass->GetVar(); // first on a list + while ( pv != nullptr ) + { + // seeks the maximum dimensions of the table + CBotInstr* p = pv->m_LimExpr; // the different formulas + if ( p != nullptr ) + { + CBotStack* pile = CBotStack::FirstStack(); // an independent stack + int n = 0; + int max[100]; + + while (p != nullptr) + { + while( pile->IsOk() && !p->Execute(pile) ) ; // calculate size without interruptions + CBotVar* v = pile->GetVar(); // result + max[n] = v->GetValInt(); // value + n++; + p = p->GetNext3(); + } + while (n<100) max[n++] = 0; + + pv->m_type.SetArray( max ); // stores the limitations + pile->Delete(); + } + + CBotVar* pn = CBotVar::Create( pv ); // a copy + pn->SetStatic(pv->IsStatic()); + pn->SetPrivate(pv->GetPrivate()); + + if ( pv->m_InitExpr != nullptr ) // expression for initialization? + { +#if STACKMEM + CBotStack* pile = CBotStack::FirstStack(); // an independent stack + + while(pile->IsOk() && !pv->m_InitExpr->Execute(pile, pn)); // evaluates the expression without timer + + pile->Delete(); +#else + CBotStack* pile = new CBotStack(nullptr); // an independent stack + while(!pv->m_InitExpr->Execute(pile)); // evaluates the expression without timer + pn->SetVal( pile->GetVar() ) ; + delete pile; +#endif + } + +// pn->SetUniqNum(CBotVar::NextUniqNum()); // enumerate elements + pn->SetUniqNum(pv->GetUniqNum()); //++nIdent + pn->m_pMyThis = this; + + if ( m_pVar == nullptr) m_pVar = pn; + else m_pVar->AddNext( pn ); + pv = pv->GetNext(); + } +} + +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotVarClass::GetClass() +{ + return m_pClass; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::Maj(void* pUser, bool bContinu) +{ +/* if (!bContinu && m_pMyThis != nullptr) + m_pMyThis->Maj(pUser, true);*/ + + // an update routine exist? + + if ( m_pClass->m_rMaj == nullptr ) return; + + // retrieves the user pointer according to the class + // or according to the parameter passed to CBotProgram::Run() + + if ( m_pUserPtr != nullptr) pUser = m_pUserPtr; + if ( pUser == OBJECTDELETED || + pUser == OBJECTCREATED ) return; + m_pClass->m_rMaj( this, pUser ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarClass::GetItem(const char* name) +{ + CBotVar* p = m_pVar; + + while ( p != nullptr ) + { + if ( p->GetName() == name ) return p; + p = p->GetNext(); + } + + if ( m_pParent != nullptr ) return m_pParent->GetItem(name); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarClass::GetItemRef(int nIdent) +{ + CBotVar* p = m_pVar; + + while ( p != nullptr ) + { + if ( p->GetUniqNum() == nIdent ) return p; + p = p->GetNext(); + } + + if ( m_pParent != nullptr ) return m_pParent->GetItemRef(nIdent); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarClass::GetItem(int n, bool bExtend) +{ + CBotVar* p = m_pVar; + + if ( n < 0 ) return nullptr; + if ( n > MAXARRAYSIZE ) return nullptr; + + if ( m_type.GetLimite() >= 0 && n >= m_type.GetLimite() ) return nullptr; + + if ( p == nullptr && bExtend ) + { + p = CBotVar::Create("", m_type.GetTypElem()); + m_pVar = p; + } + + if ( n == 0 ) return p; + + while ( n-- > 0 ) + { + if ( p->m_next == nullptr ) + { + if ( bExtend ) p->m_next = CBotVar::Create("", m_type.GetTypElem()); + if ( p->m_next == nullptr ) return nullptr; + } + p = p->m_next; + } + + return p; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotVarClass::GetItemList() +{ + return m_pVar; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString CBotVarClass::GetValString() +{ +// if ( m_Indirect != nullptr) return m_Indirect->GetValString(); + + CBotString res; + + if ( m_pClass != nullptr ) // not used for an array + { + res = m_pClass->GetName() + CBotString("( "); + + CBotVarClass* my = this; + while ( my != nullptr ) + { + CBotVar* pv = my->m_pVar; + while ( pv != nullptr ) + { + res += pv->GetName() + CBotString("="); + + if ( pv->IsStatic() ) + { + CBotVar* pvv = my->m_pClass->GetItem(pv->GetName()); + res += pvv->GetValString(); + } + else + { + res += pv->GetValString(); + } + pv = pv->GetNext(); + if ( pv != nullptr ) res += ", "; + } + my = my->m_pParent; + if ( my != nullptr ) + { + res += ") extends "; + res += my->m_pClass->GetName(); + res += " ("; + } + } + } + else + { + res = "( "; + + CBotVar* pv = m_pVar; + while ( pv != nullptr ) + { + res += pv->GetValString(); + if ( pv->GetNext() != nullptr ) res += ", "; + pv = pv->GetNext(); + } + } + + res += " )"; + return res; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::IncrementUse() +{ + m_CptUse++; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotVarClass::DecrementUse() +{ + m_CptUse--; + if ( m_CptUse == 0 ) + { + // if there is one, call the destructor + // but only if a constructor had been called. + if ( m_bConstructor ) + { + m_CptUse++; // does not return to the destructor + + // m_error is static in the stack + // saves the value for return + int err, start, end; + CBotStack* pile = nullptr; + err = pile->GetError(start,end); // stack == nullptr it does not bother! + + pile = CBotStack::FirstStack(); // clears the error + CBotVar* ppVars[1]; + ppVars[0] = nullptr; + + CBotVar* pThis = CBotVar::Create("this", CBotTypNullPointer); + pThis->SetPointer(this); + CBotVar* pResult = nullptr; + + CBotString nom = CBotString("~") + m_pClass->GetName(); + long ident = 0; + + while ( pile->IsOk() && !m_pClass->ExecuteMethode(ident, nom, pThis, ppVars, pResult, pile, nullptr)) ; // waits for the end + + pile->ResetError(err, start,end); + + pile->Delete(); + delete pThis; + m_CptUse--; + } + + delete this; // self-destructs! + } +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass* CBotVarClass::GetPointer() +{ + return this; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVarClass* CBotVarClass::Find(long id) +{ + CBotVarClass* p = m_ExClass; + + while ( p != nullptr ) + { + if ( p->m_ItemIdent == id ) return p; + p = p->m_ExNext; + } + + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarClass::Eq(CBotVar* left, CBotVar* right) +{ + CBotVar* l = left->GetItemList(); + CBotVar* r = right->GetItemList(); + + while ( l != nullptr && r != nullptr ) + { + if ( l->Ne(l, r) ) return false; + l = l->GetNext(); + r = r->GetNext(); + } + + // should always arrived simultaneously at the end (same classes) + return l == r; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarClass::Ne(CBotVar* left, CBotVar* right) +{ + CBotVar* l = left->GetItemList(); + CBotVar* r = right->GetItemList(); + + while ( l != nullptr && r != nullptr ) + { + if ( l->Ne(l, r) ) return true; + l = l->GetNext(); + r = r->GetNext(); + } + + // should always arrived simultaneously at the end (same classes) + return l != r; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotVarClass::Save1State(FILE* pf) +{ + if ( !WriteType(pf, m_type) ) return false; + if ( !WriteLong(pf, m_ItemIdent) ) return false; + + return SaveVar(pf, m_pVar); // content of the object +} diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h index b6a256ff..b7b878f9 100644 --- a/src/CBot/CBotVar/CBotVarClass.h +++ b/src/CBot/CBotVar/CBotVarClass.h @@ -47,7 +47,7 @@ public: ~CBotVarClass(); /*! - * \brief Copy + * \brief Copy Copy a variable into another. * \param pSrc * \param bName */ @@ -80,9 +80,10 @@ public: CBotVar* GetItemRef(int nIdent) override; /*! - * \brief GetItem + * \brief GetItem For the management of an array. * \param n - * \param bExtend + * \param bExtend can enlarge the table, but not beyond the threshold size + * of SetArray(). * \return */ CBotVar* GetItem(int n, bool bExtend) override; @@ -142,7 +143,7 @@ public: void SetIdent(long n) override; /*! - * \brief Find + * \brief Find Makes an instance according to its unique number. * \param id * \return */ From 660f17454af4f312d1c4942ec388ac5ae688f546 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 23:18:47 +0100 Subject: [PATCH 63/91] Moving CBotVar class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBotCall.cpp | 2 + src/CBot/CBotCallMethode.cpp | 2 + src/CBot/CBotClass.cpp | 2 + src/CBot/CBotClass.h | 2 + src/CBot/CBotDefines.h | 7 + src/CBot/CBotDll.h | 225 --------- src/CBot/CBotInstr/CBotBoolean.cpp | 2 + src/CBot/CBotInstr/CBotCatch.cpp | 2 + src/CBot/CBotInstr/CBotEmpty.cpp | 2 + src/CBot/CBotInstr/CBotExprAlpha.cpp | 2 + src/CBot/CBotInstr/CBotExprBool.cpp | 2 + src/CBot/CBotInstr/CBotExprNan.cpp | 2 + src/CBot/CBotInstr/CBotExprNull.cpp | 2 + src/CBot/CBotInstr/CBotExprNum.cpp | 2 + src/CBot/CBotInstr/CBotExprUnaire.cpp | 2 + src/CBot/CBotInstr/CBotExprVar.h | 2 + src/CBot/CBotInstr/CBotExpression.cpp | 2 + src/CBot/CBotInstr/CBotFloat.cpp | 2 + src/CBot/CBotInstr/CBotFunction.cpp | 2 + src/CBot/CBotInstr/CBotIString.cpp | 2 + src/CBot/CBotInstr/CBotInstArray.cpp | 2 + src/CBot/CBotInstr/CBotInstrCall.cpp | 2 + src/CBot/CBotInstr/CBotInstrMethode.cpp | 2 + src/CBot/CBotInstr/CBotInt.cpp | 2 + src/CBot/CBotInstr/CBotLeftExprVar.cpp | 2 + src/CBot/CBotInstr/CBotListArray.cpp | 2 + src/CBot/CBotInstr/CBotNew.cpp | 2 + src/CBot/CBotInstr/CBotParExpr.cpp | 2 + src/CBot/CBotInstr/CBotPostIncExpr.cpp | 2 + src/CBot/CBotInstr/CBotPreIncExpr.cpp | 2 + src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 2 + src/CBot/CBotUtils.cpp | 2 + src/CBot/{ => CBotVar}/CBotVar.cpp | 120 ++++- src/CBot/CBotVar/CBotVar.h | 624 ++++++++++++++++++++++++ src/CBot/CBotVar/CBotVarArray.h | 2 + src/CBot/CBotVar/CBotVarBoolean.h | 2 + src/CBot/CBotVar/CBotVarClass.h | 2 + src/CBot/CBotVar/CBotVarFloat.h | 2 + src/CBot/CBotVar/CBotVarInt.h | 2 + src/CBot/CBotVar/CBotVarPointer.h | 2 +- src/CBot/CBotVar/CBotVarString.h | 2 + src/CBot/CMakeLists.txt | 2 +- src/CBot/StringFunctions.cpp | 3 + src/script/script.cpp | 1 + src/script/scriptfunc.cpp | 1 + 46 files changed, 810 insertions(+), 248 deletions(-) rename src/CBot/{ => CBotVar}/CBotVar.cpp (70%) create mode 100644 src/CBot/CBotVar/CBotVar.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 76cfe2b5..d528d1f7 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -75,6 +75,7 @@ #include "CBotStack.h" #include "CBotClass.h" +#include "CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp index 7796786a..75beb6fb 100644 --- a/src/CBot/CBotCall.cpp +++ b/src/CBot/CBotCall.cpp @@ -25,6 +25,8 @@ #include "CBotUtils.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotCallMethode.cpp b/src/CBot/CBotCallMethode.cpp index 6611129c..2bcc41c8 100644 --- a/src/CBot/CBotCallMethode.cpp +++ b/src/CBot/CBotCallMethode.cpp @@ -23,6 +23,8 @@ #include "CBotUtils.h" #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 741dfb97..9e20d667 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -33,6 +33,8 @@ #include "CBotUtils.h" #include "CBotCallMethode.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index 3cc4f9f9..fe385461 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -24,6 +24,8 @@ #include "CBotProgram.h" +#include "CBotDefines.h" + // Local include // Global include diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index 2c6e34d7..d09e3ca2 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -26,3 +26,10 @@ // Global include #define MAXARRAYSIZE 9999 + + +// variable type SetPrivate / IsPrivate +#define PR_PUBLIC 0 // public variable +#define PR_READ 1 // read only +#define PR_PROTECT 2 // protected (inheritance) +#define PR_PRIVATE 3 // strictly private diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 6bd16030..56b43642 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -494,231 +494,6 @@ bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) #endif -///////////////////////////////////////////////////////////////////////////////// -// Class for managing variables - -// may be useful to the outside of the module -// ( it is currently not expected to be able to create these objects in outer ) - -// variable type SetPrivate / IsPrivate -#define PR_PUBLIC 0 // public variable -#define PR_READ 1 // read only -#define PR_PROTECT 2 // protected (inheritance) -#define PR_PRIVATE 3 // strictly private - -class CBotVar -{ -public: - // results of GetInit() - enum class InitType : int { UNDEF = 0, DEF = 1, IS_POINTER = 2, IS_NAN = 999 }; - -protected: - CBotToken* m_token; // the corresponding token - - CBotVar* m_next; // list of variables - friend class CBotStack; - friend class CBotCStack; - friend class CBotInstrCall; - friend class CBotProgram; - - CBotTypResult m_type; // type of value - - InitType m_binit; // not initialized? - CBotVarClass* m_pMyThis; // ^ corresponding this element - void* m_pUserPtr; // ^user data if necessary - bool m_bStatic; // static element (in class) - int m_mPrivate; // element public, protected or private? - - CBotInstr* m_InitExpr; // expression for the original content - CBotInstr* m_LimExpr; // list of limits for a table - friend class CBotClass; - friend class CBotVarClass; - friend class CBotVarPointer; - friend class CBotVarArray; - - long m_ident; // unique identifier - static long m_identcpt; // counter - -public: - CBotVar(); -virtual ~CBotVar( ); // destructor - - static - CBotVar* Create( const char* name, CBotTypResult type); - // creates from a complete type - - static - CBotVar* Create( const char* name, CBotClass* pClass); - // creates from one instance of a known class - - static - CBotVar* Create( const CBotToken* name, int type ); - static - CBotVar* Create( const CBotToken* name, CBotTypResult type ); - - static - CBotVar* Create( const char* name, int type, CBotClass* pClass); - - static - CBotVar* Create( CBotVar* pVar ); - - static void Destroy(CBotVar* var); - - - void SetUserPtr(void* pUser); - // associate a user pointer to an instance - - virtual void SetIdent(long UniqId); - // associates a unique identifier to an instance - // ( it is used to ensure that the id is unique) - - void* GetUserPtr(); - // makes the pointer associated with the variable - - CBotString GetName(); // the name of the variable, if known - //////////////////////////////////////////////////////////////////////////////////// - void SetName(const char* name); // changes the name of the variable - - int GetType(int mode = 0); // returns the base type (int) of the variable - // TODO check it - //////////////////////////////////////////////////////////////////////////////////////// - - CBotTypResult GetTypResult(int mode = 0); // returns the complete type of the variable - - - CBotToken* GetToken(); - void SetType(CBotTypResult& type); - - void SetInit(InitType initType); // is the variable in the state UNDEF, DEF, NAN - InitType GetInit() const; // gives the state of the variable - bool IsUndefined() const { return GetInit() == InitType::UNDEF; } - bool IsDefined() const { return GetInit() == InitType::DEF; } - bool IsNAN() const { return GetInit() == InitType::IS_NAN; } - - void SetStatic(bool bStatic); - bool IsStatic(); - - void SetPrivate(int mPrivate); - bool IsPrivate(int mode = PR_PROTECT); - int GetPrivate(); - - virtual - void ConstructorSet(); - - void SetVal(CBotVar* var); // remprend une valeur - // TODO remprend value - virtual - CBotVar* GetItem(const char* name); // returns an element of a class according to its name (*) - virtual - CBotVar* GetItemRef(int nIdent); // idem à partir du n° ref - // TODO ditto from ref no. - virtual - CBotVar* GetItem(int row, bool bGrow = false); - - virtual - CBotVar* GetItemList(); // lists the elements - - CBotVar* GetStaticVar(); // makes the pointer to the variable if it is static - - bool IsElemOfClass(const char* name); - // said if the element belongs to the class "name" - // makes true if the object is a subclass - - CBotVar* GetNext(); // next variable in the list (parameters) - //////////////////////////////////////////////////////////////////////////////////////////// - - void AddNext(CBotVar* pVar); // added to a list - - virtual - void Copy(CBotVar* pSrc, bool bName = true); // makes a copy of the variable - - virtual void SetValInt(int val, const char* name = nullptr); - // initialized with an integer value (#) - ///////////////////////////////////////////////////////////////////////////////// - - virtual void SetValFloat(float val); // initialized with a real value (#) - //////////////////////////////////////////////////////////////////////////////// - - virtual void SetValString(const char* p);// initialized with a string value (#) - //////////////////////////////////////////////////////////////////////////////// - - virtual int GetValInt(); // request the full value (#) - //////////////////////////////////////////////////////////////////////// - - virtual float GetValFloat(); // gets real value (#) - /////////////////////////////////////////////////////////////////////// - - virtual - CBotString GetValString(); // request the string value (#) - /////////////////////////////////////////////////////////////////////// - - virtual void SetClass(CBotClass* pClass); - virtual - CBotClass* GetClass(); - - virtual void SetPointer(CBotVar* p); - virtual - CBotVarClass* GetPointer(); -// virtual void SetIndirection(CBotVar* pVar); - - virtual void Add(CBotVar* left, CBotVar* right); // addition - virtual void Sub(CBotVar* left, CBotVar* right); // subtraction - virtual void Mul(CBotVar* left, CBotVar* right); // multiplication - virtual int Div(CBotVar* left, CBotVar* right); // division - virtual int Modulo(CBotVar* left, CBotVar* right); // remainder of division - virtual void Power(CBotVar* left, CBotVar* right); // power - - virtual bool Lo(CBotVar* left, CBotVar* right); - virtual bool Hi(CBotVar* left, CBotVar* right); - virtual bool Ls(CBotVar* left, CBotVar* right); - virtual bool Hs(CBotVar* left, CBotVar* right); - virtual bool Eq(CBotVar* left, CBotVar* right); - virtual bool Ne(CBotVar* left, CBotVar* right); - - virtual void And(CBotVar* left, CBotVar* right); - virtual void Or(CBotVar* left, CBotVar* right); - virtual void XOr(CBotVar* left, CBotVar* right); - virtual void ASR(CBotVar* left, CBotVar* right); - virtual void SR(CBotVar* left, CBotVar* right); - virtual void SL(CBotVar* left, CBotVar* right); - - virtual void Neg(); - virtual void Not(); - virtual void Inc(); - virtual void Dec(); - - - virtual bool Save0State(FILE* pf); - virtual bool Save1State(FILE* pf); - static bool RestoreState(FILE* pf, CBotVar* &pVar); - - void debug(); - -// virtual -// CBotVar* GetMyThis(); - - virtual - void Maj(void* pUser = nullptr, bool bContinue = true); - - void SetUniqNum(long n); - long GetUniqNum(); - static long NextUniqNum(); -}; - -/* NOTE (#) - methods SetValInt() SetValFloat() et SetValString() - can be called with objects which are respectively integer, real or string - Always be sure of the type of the variable before calling these methods - - if ( pVar->GetType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! - - methods GetValInt(), GetValFloat() et GetValString() - use value conversions, - GetValString() works on numbers (makes the corresponding string) - but do not make GetValInt () with a string variable! -*/ - - /* //////////////////////////////////////////////////////////////////////// // Examples of use diff --git a/src/CBot/CBotInstr/CBotBoolean.cpp b/src/CBot/CBotInstr/CBotBoolean.cpp index 6fda2dbf..ea0103e5 100644 --- a/src/CBot/CBotInstr/CBotBoolean.cpp +++ b/src/CBot/CBotInstr/CBotBoolean.cpp @@ -25,6 +25,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index 77b5294d..fdbc1d45 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -24,6 +24,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotEmpty.cpp b/src/CBot/CBotInstr/CBotEmpty.cpp index 0f0ad7ce..4318eadd 100644 --- a/src/CBot/CBotInstr/CBotEmpty.cpp +++ b/src/CBot/CBotInstr/CBotEmpty.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.cpp b/src/CBot/CBotInstr/CBotExprAlpha.cpp index d0edb1d2..60923754 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.cpp +++ b/src/CBot/CBotInstr/CBotExprAlpha.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprBool.cpp b/src/CBot/CBotInstr/CBotExprBool.cpp index 3a252676..ebefa6dd 100644 --- a/src/CBot/CBotInstr/CBotExprBool.cpp +++ b/src/CBot/CBotInstr/CBotExprBool.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNan.cpp b/src/CBot/CBotInstr/CBotExprNan.cpp index 7335219b..a2cb1c65 100644 --- a/src/CBot/CBotInstr/CBotExprNan.cpp +++ b/src/CBot/CBotInstr/CBotExprNan.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNull.cpp b/src/CBot/CBotInstr/CBotExprNull.cpp index d80ef035..cef5eca8 100644 --- a/src/CBot/CBotInstr/CBotExprNull.cpp +++ b/src/CBot/CBotInstr/CBotExprNull.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp index 24ba0ac7..6082c0e1 100644 --- a/src/CBot/CBotInstr/CBotExprNum.cpp +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprUnaire.cpp b/src/CBot/CBotInstr/CBotExprUnaire.cpp index 2b7c0d6e..4732eda1 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.cpp +++ b/src/CBot/CBotInstr/CBotExprUnaire.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprVar.h b/src/CBot/CBotInstr/CBotExprVar.h index 46017e51..a4535659 100644 --- a/src/CBot/CBotInstr/CBotExprVar.h +++ b/src/CBot/CBotInstr/CBotExprVar.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotDefines.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp index 3637049b..bdf096a5 100644 --- a/src/CBot/CBotInstr/CBotExpression.cpp +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFloat.cpp b/src/CBot/CBotInstr/CBotFloat.cpp index 70a2c97b..1a721458 100644 --- a/src/CBot/CBotInstr/CBotFloat.cpp +++ b/src/CBot/CBotInstr/CBotFloat.cpp @@ -25,6 +25,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index fd81a441..6700fab7 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -34,6 +34,8 @@ #include "CBotDefParam.h" #include "CBotUtils.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIString.cpp b/src/CBot/CBotInstr/CBotIString.cpp index 91e1a925..3c348111 100644 --- a/src/CBot/CBotInstr/CBotIString.cpp +++ b/src/CBot/CBotInstr/CBotIString.cpp @@ -24,6 +24,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstArray.cpp b/src/CBot/CBotInstr/CBotInstArray.cpp index 4bf2fd16..f53cb65e 100644 --- a/src/CBot/CBotInstr/CBotInstArray.cpp +++ b/src/CBot/CBotInstr/CBotInstArray.cpp @@ -29,6 +29,8 @@ #include "CBotDefines.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp index cec9810b..c2c33b0f 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.cpp +++ b/src/CBot/CBotInstr/CBotInstrCall.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index a33207fb..dabcd463 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" #include "CBotClass.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInt.cpp b/src/CBot/CBotInstr/CBotInt.cpp index f94d53c5..1649de55 100644 --- a/src/CBot/CBotInstr/CBotInt.cpp +++ b/src/CBot/CBotInstr/CBotInt.cpp @@ -26,6 +26,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.cpp b/src/CBot/CBotInstr/CBotLeftExprVar.cpp index ff0bbf15..d0c3482b 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.cpp +++ b/src/CBot/CBotInstr/CBotLeftExprVar.cpp @@ -22,6 +22,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp index dc6e2b29..682505b7 100644 --- a/src/CBot/CBotInstr/CBotListArray.cpp +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -25,6 +25,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp index 9da2a88e..094e5a08 100644 --- a/src/CBot/CBotInstr/CBotNew.cpp +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" #include "CBotClass.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index 5d2839e8..fc34b513 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -32,6 +32,8 @@ #include "CBotExprNan.h" #include "CBotExpression.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.cpp b/src/CBot/CBotInstr/CBotPostIncExpr.cpp index f7fbddab..938274e6 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPostIncExpr.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.cpp b/src/CBot/CBotInstr/CBotPreIncExpr.cpp index 5fe3d365..30ee9a97 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPreIncExpr.cpp @@ -23,6 +23,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index 816efefb..4ad5ad03 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -25,6 +25,8 @@ #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index eee71e44..b5f9c235 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -24,6 +24,8 @@ #include "CBotClass.h" #include "CBotStack.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp similarity index 70% rename from src/CBot/CBotVar.cpp rename to src/CBot/CBotVar/CBotVar.cpp index d5c248e7..e2c1fe78 100644 --- a/src/CBot/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -17,13 +17,9 @@ * along with this program. If not, see http://gnu.org/licenses */ -//////////////////////////////////////////////////////////////////// -// Definition for the class CBotVar -// variables management of the language CBoT -// it never creates an instance of the class mother CBotVar - -#include "CBot.h" +// Modules inlcude +#include "CBotVar.h" #include "CBotStack.h" @@ -37,12 +33,17 @@ #include "CBotClass.h" +// Local include + +// Global include #include #include #include +//////////////////////////////////////////////////////////////////////////////// long CBotVar::m_identcpt = 0; +//////////////////////////////////////////////////////////////////////////////// CBotVar::CBotVar( ) { m_next = nullptr; @@ -57,12 +58,14 @@ CBotVar::CBotVar( ) m_mPrivate = 0; } +//////////////////////////////////////////////////////////////////////////////// CBotVar::~CBotVar( ) { delete m_token; delete m_next; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::debug() { // const char* p = static_cast( m_token->GetString()); @@ -80,11 +83,13 @@ void CBotVar::debug() } } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::ConstructorSet() { // nop } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetUserPtr(void* pUser) { m_pUserPtr = pUser; @@ -93,6 +98,7 @@ void CBotVar::SetUserPtr(void* pUser) (static_cast(this))->m_pVarClass->SetUserPtr(pUser); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetIdent(long n) { if (m_type.Eq(CBotTypPointer) && @@ -100,6 +106,7 @@ void CBotVar::SetIdent(long n) (static_cast(this))->m_pVarClass->SetIdent(n); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetUniqNum(long n) { m_ident = n; @@ -107,23 +114,26 @@ void CBotVar::SetUniqNum(long n) if ( n == 0 ) assert(0); } +//////////////////////////////////////////////////////////////////////////////// long CBotVar::NextUniqNum() { if (++m_identcpt < 10000) m_identcpt = 10000; return m_identcpt; } +//////////////////////////////////////////////////////////////////////////////// long CBotVar::GetUniqNum() { return m_ident; } - +//////////////////////////////////////////////////////////////////////////////// void* CBotVar::GetUserPtr() { return m_pUserPtr; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Save1State(FILE* pf) { // this routine "virtual" must never be called, @@ -133,21 +143,21 @@ bool CBotVar::Save1State(FILE* pf) return false; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Maj(void* pUser, bool bContinu) { /* if (!bContinu && m_pMyThis != nullptr) m_pMyThis->Maj(pUser, true);*/ } - -// creates a variable depending on its type - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::Create(const CBotToken* name, int type ) { CBotTypResult t(type); return Create(name, t); } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::Create(const CBotToken* name, CBotTypResult type) { switch (type.GetType()) @@ -201,13 +211,14 @@ CBotVar* CBotVar::Create(const CBotToken* name, CBotTypResult type) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::Create( CBotVar* pVar ) { CBotVar* p = Create(pVar->m_token->GetString(), pVar->GetTypResult(2)); return p; } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::Create( const char* n, CBotTypResult type) { CBotToken name(n); @@ -272,6 +283,7 @@ CBotVar* CBotVar::Create( const char* n, CBotTypResult type) return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::Create( const char* name, int type, CBotClass* pClass) { CBotToken token( name, "" ); @@ -293,6 +305,7 @@ CBotVar* CBotVar::Create( const char* name, int type, CBotClass* pClass) return pVar; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::Create( const char* name, CBotClass* pClass) { CBotToken token( name, "" ); @@ -301,11 +314,13 @@ CBotVar* CBotVar::Create( const char* name, CBotClass* pClass) return pVar; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Destroy(CBotVar* var) { delete var; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult CBotVar::GetTypResult(int mode) { CBotTypResult r = m_type; @@ -318,6 +333,7 @@ CBotTypResult CBotVar::GetTypResult(int mode) return r; } +//////////////////////////////////////////////////////////////////////////////// int CBotVar::GetType(int mode) { if ( mode == 1 && m_type.Eq(CBotTypClass) ) @@ -327,11 +343,13 @@ int CBotVar::GetType(int mode) return m_type.GetType(); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetType(CBotTypResult& type) { m_type = type; } +//////////////////////////////////////////////////////////////////////////////// CBotVar::InitType CBotVar::GetInit() const { if ( m_type.Eq(CBotTypClass) ) return InitType::DEF; // always set! @@ -339,6 +357,7 @@ CBotVar::InitType CBotVar::GetInit() const return m_binit; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetInit(CBotVar::InitType bInit) { m_binit = bInit; @@ -368,46 +387,53 @@ void CBotVar::SetInit(CBotVar::InitType bInit) } } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotVar::GetName() { return m_token->GetString(); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetName(const char* name) { m_token->SetString(name); } +//////////////////////////////////////////////////////////////////////////////// CBotToken* CBotVar::GetToken() { return m_token; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::GetItem(const char* name) { assert(0); return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::GetItemRef(int nIdent) { assert(0); return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::GetItemList() { assert(0); return nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::GetItem(int row, bool bGrow) { assert(0); return nullptr; } -// check if a variable belongs to a given class +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::IsElemOfClass(const char* name) { CBotClass* pc = nullptr; @@ -430,7 +456,7 @@ bool CBotVar::IsElemOfClass(const char* name) return false; } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::GetStaticVar() { // makes the pointer to the variable if it is static @@ -440,12 +466,13 @@ CBotVar* CBotVar::GetStaticVar() return pClass->GetItem( m_token->GetString() ); } - +//////////////////////////////////////////////////////////////////////////////// CBotVar* CBotVar::GetNext() { return m_next; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::AddNext(CBotVar* pVar) { CBotVar* p = this; @@ -454,6 +481,7 @@ void CBotVar::AddNext(CBotVar* pVar) p->m_next = pVar; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetVal(CBotVar* var) { switch (var->GetType()) @@ -489,37 +517,43 @@ void CBotVar::SetVal(CBotVar* var) m_binit = var->m_binit; // copie l'état nan s'il y a } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetStatic(bool bStatic) { m_bStatic = bStatic; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetPrivate(int mPrivate) { m_mPrivate = mPrivate; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::IsStatic() { return m_bStatic; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::IsPrivate(int mode) { return m_mPrivate >= mode; } +//////////////////////////////////////////////////////////////////////////////// int CBotVar::GetPrivate() { return m_mPrivate; } - +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetPointer(CBotVar* pVarClass) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// CBotVarClass* CBotVar::GetPointer() { assert(0); @@ -528,167 +562,198 @@ CBotVarClass* CBotVar::GetPointer() // All these functions must be defined in the subclasses // derived from class CBotVar - +//////////////////////////////////////////////////////////////////////////////// int CBotVar::GetValInt() { assert(0); return 0; } +//////////////////////////////////////////////////////////////////////////////// float CBotVar::GetValFloat() { assert(0); return 0; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetValInt(int c, const char* s) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetValFloat(float c) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Mul(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Power(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// int CBotVar::Div(CBotVar* left, CBotVar* right) { assert(0); return 0; } +//////////////////////////////////////////////////////////////////////////////// int CBotVar::Modulo(CBotVar* left, CBotVar* right) { assert(0); return 0; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Add(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Sub(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Lo(CBotVar* left, CBotVar* right) { assert(0); return false; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Hi(CBotVar* left, CBotVar* right) { assert(0); return false; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Ls(CBotVar* left, CBotVar* right) { assert(0); return false; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Hs(CBotVar* left, CBotVar* right) { assert(0); return false; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Eq(CBotVar* left, CBotVar* right) { assert(0); return false; } +//////////////////////////////////////////////////////////////////////////////// bool CBotVar::Ne(CBotVar* left, CBotVar* right) { assert(0); return false; } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::And(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Or(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::XOr(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::ASR(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SR(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SL(CBotVar* left, CBotVar* right) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Neg() { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Not() { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Inc() { assert(0); } + +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Dec() { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::Copy(CBotVar* pSrc, bool bName) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetValString(const char* p) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// CBotString CBotVar::GetValString() { assert(0); return CBotString(); } +//////////////////////////////////////////////////////////////////////////////// void CBotVar::SetClass(CBotClass* pClass) { assert(0); } +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotVar::GetClass() { assert(0); @@ -698,7 +763,7 @@ CBotClass* CBotVar::GetClass() /////////////////////////////////////////////////////// // management of results types - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::CBotTypResult(int type) { m_type = type; @@ -707,6 +772,7 @@ CBotTypResult::CBotTypResult(int type) m_limite = -1; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::CBotTypResult(int type, const char* name) { m_type = type; @@ -723,6 +789,7 @@ CBotTypResult::CBotTypResult(int type, const char* name) } } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::CBotTypResult(int type, CBotClass* pClass) { m_type = type; @@ -733,6 +800,7 @@ CBotTypResult::CBotTypResult(int type, CBotClass* pClass) if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::CBotTypResult(int type, CBotTypResult elem) { m_type = type; @@ -745,6 +813,7 @@ CBotTypResult::CBotTypResult(int type, CBotTypResult elem) m_pNext = new CBotTypResult( elem ); } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::CBotTypResult(const CBotTypResult& typ) { m_type = typ.m_type; @@ -756,6 +825,7 @@ CBotTypResult::CBotTypResult(const CBotTypResult& typ) m_pNext = new CBotTypResult( *typ.m_pNext ); } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::CBotTypResult() { m_type = 0; @@ -764,11 +834,13 @@ CBotTypResult::CBotTypResult() m_pClass = nullptr; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult::~CBotTypResult() { delete m_pNext; } +//////////////////////////////////////////////////////////////////////////////// int CBotTypResult::GetType(int mode) const { #ifdef _DEBUG @@ -786,31 +858,37 @@ int CBotTypResult::GetType(int mode) const return m_type; } +//////////////////////////////////////////////////////////////////////////////// void CBotTypResult::SetType(int n) { m_type = n; } +//////////////////////////////////////////////////////////////////////////////// CBotClass* CBotTypResult::GetClass() const { return m_pClass; } +//////////////////////////////////////////////////////////////////////////////// CBotTypResult& CBotTypResult::GetTypElem() const { return *m_pNext; } +//////////////////////////////////////////////////////////////////////////////// int CBotTypResult::GetLimite() const { return m_limite; } +//////////////////////////////////////////////////////////////////////////////// void CBotTypResult::SetLimite(int n) { m_limite = n; } +//////////////////////////////////////////////////////////////////////////////// void CBotTypResult::SetArray( int* max ) { m_limite = *max; @@ -822,8 +900,7 @@ void CBotTypResult::SetArray( int* max ) } } - - +//////////////////////////////////////////////////////////////////////////////// bool CBotTypResult::Compare(const CBotTypResult& typ) const { if ( m_type != typ.m_type ) return false; @@ -840,13 +917,14 @@ bool CBotTypResult::Compare(const CBotTypResult& typ) const return true; } +//////////////////////////////////////////////////////////////////////////////// bool CBotTypResult::Eq(int type) const { return m_type == type; } -CBotTypResult& - CBotTypResult::operator=(const CBotTypResult& src) +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult& CBotTypResult::operator=(const CBotTypResult& src) { m_type = src.m_type; m_limite = src.m_limite; diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h new file mode 100644 index 00000000..8f465fc8 --- /dev/null +++ b/src/CBot/CBotVar/CBotVar.h @@ -0,0 +1,624 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "../CBotDll.h" + +#include "../CBotDefines.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotVar class Class for managing variables. May be useful to the + * outside of the module ( it is currently not expected to be able to create + * these objects in outer ). It never creates an instance of the class mother + * CBotVar. + */ +class CBotVar +{ +public: + /*! + * \brief The InitType enum Results of GetInit(). + */ + enum class InitType : int + { + UNDEF = 0, + DEF = 1, + IS_POINTER = 2, + IS_NAN = 999 + }; + + /*! + * \brief CBotVar + */ + CBotVar(); + + /*! + * \brief ~CBotVar Destructor. + */ + virtual ~CBotVar( ); + + /*! + * \brief Create Creates from a complete type. + * \param name + * \param type + * \return + */ + static CBotVar* Create( const char* name, CBotTypResult type); + + /*! + * \brief Create Creates from one instance of a known class. + * \param name + * \param pClass + * \return + */ + static CBotVar* Create( const char* name, CBotClass* pClass); + + /*! + * \brief Create Creates a variable depending on its type. + * \param name + * \param type + * \return + */ + static CBotVar* Create( const CBotToken* name, int type ); + + /*! + * \brief Create + * \param name + * \param type + * \return + */ + static CBotVar* Create( const CBotToken* name, CBotTypResult type ); + + /*! + * \brief Create + * \param name + * \param type + * \param pClass + * \return + */ + static CBotVar* Create( const char* name, int type, CBotClass* pClass); + + /*! + * \brief Create + * \param pVar + * \return + */ + static CBotVar* Create( CBotVar* pVar ); + + /*! + * \brief Destroy + * \param var + */ + static void Destroy(CBotVar* var); + + /*! + * \brief SetUserPtr Associate a user pointer to an instance. + * \param pUser + */ + void SetUserPtr(void* pUser); + + /*! + * \brief SetIdent Associates a unique identifier to an instance + * ( it is used to ensure that the id is unique) + * \param UniqId + */ + virtual void SetIdent(long UniqId); + + /*! + * \brief GetUserPtr Makes the pointer associated with the variable. + * \return + */ + void* GetUserPtr(); + + /*! + * \brief GetName The name of the variable, if known. + * \return + */ + CBotString GetName(); + + /*! + * \brief SetName Changes the name of the variable + * \param name + */ + void SetName(const char* name); + + /*! + * \brief GetType Returns the base type (int) of the variable + * \param mode + * \return + * \todo Check it? + */ + int GetType(int mode = 0); + + /*! + * \brief GetTypResult Returns the complete type of the variable. + * \param mode + * \return + */ + CBotTypResult GetTypResult(int mode = 0); + + /*! + * \brief GetToken + * \return + */ + CBotToken* GetToken(); + + /*! + * \brief SetType + * \param type + */ + void SetType(CBotTypResult& type); + + /*! + * \brief SetInit Is the variable in the state UNDEF, DEF, NAN. + * \param initType + */ + void SetInit(InitType initType); + + /*! + * \brief GetInit Gives the state of the variable. + * \return + */ + InitType GetInit() const; + + /*! + * \brief IsUndefined + * \return + */ + bool IsUndefined() const { return GetInit() == InitType::UNDEF; } + + /*! + * \brief IsDefined + * \return + */ + bool IsDefined() const { return GetInit() == InitType::DEF; } + + /*! + * \brief IsNAN + * \return + */ + bool IsNAN() const { return GetInit() == InitType::IS_NAN; } + + /*! + * \brief SetStatic + * \param bStatic + */ + void SetStatic(bool bStatic); + + /*! + * \brief IsStatic + * \return + */ + bool IsStatic(); + + /*! + * \brief SetPrivate + * \param mPrivate + */ + void SetPrivate(int mPrivate); + + /*! + * \brief IsPrivate + * \param mode + * \return + */ + bool IsPrivate(int mode = PR_PROTECT); + + /*! + * \brief GetPrivate + * \return + */ + int GetPrivate(); + + /*! + * \brief ConstructorSet + */ + virtual void ConstructorSet(); + + /*! + * \brief SetVal Set the value. + * \param var + */ + void SetVal(CBotVar* var); + + /*! + * \brief GetItem Returns an element of a class according to its name (*). + * \param name + * \return + */ + virtual CBotVar* GetItem(const char* name); + + /*! + * \brief GetItemRef + * \param nIdent + * \return + */ + virtual CBotVar* GetItemRef(int nIdent); + + /*! + * \brief GetItem + * \param row + * \param bGrow + * \return + */ + virtual CBotVar* GetItem(int row, bool bGrow = false); + + /*! + * \brief GetItemList Lists the elements. + * \return + */ + virtual CBotVar* GetItemList(); + + /*! + * \brief GetStaticVar Makes the pointer to the variable if it is static. + * \return + */ + CBotVar* GetStaticVar(); + + /*! + * \brief IsElemOfClass Check if a variable belongs to a given class said if + * the element belongs to the class "name" makes true if the object is a + * subclass. + * \param name + * \return + */ + bool IsElemOfClass(const char* name); + + /*! + * \brief GetNext Next variable in the list (parameters). + * \return + */ + CBotVar* GetNext(); + + /*! + * \brief AddNext Added to a list. + * \param pVar + */ + void AddNext(CBotVar* pVar); + + /*! + * \brief Copy Makes a copy of the variable. + * \param pSrc + * \param bName + */ + virtual void Copy(CBotVar* pSrc, bool bName = true); + + /*! + * \brief SetValInt Initialized with an integer value (#) + * \param val + * \param name + */ + virtual void SetValInt(int val, const char* name = nullptr); + + /*! + * \brief SetValFloat Initialized with a real value (#). + * \param val + */ + virtual void SetValFloat(float val); + + /*! + * \brief SetValString Initialized with a string value (#). + * \param p + */ + virtual void SetValString(const char* p); + + /*! + * \brief GetValInt Request the full value (#). + * \return + */ + virtual int GetValInt(); + + /*! + * \brief GetValFloat Gets real value (#). + * \return + */ + virtual float GetValFloat(); + + /*! + * \brief GetValString Request the string value (#). + * \return + */ + virtual CBotString GetValString(); + + /*! + * \brief SetClass + * \param pClass + */ + virtual void SetClass(CBotClass* pClass); + + /*! + * \brief GetClass + * \return + */ + virtual CBotClass* GetClass(); + + /*! + * \brief SetPointer + * \param p + */ + virtual void SetPointer(CBotVar* p); + + /*! + * \brief GetPointer + * \return + */ + virtual CBotVarClass* GetPointer(); + + /*! + * \brief Add Addition + * \param left + * \param right + */ + virtual void Add(CBotVar* left, CBotVar* right); + + /*! + * \brief Sub Subtraction + * \param left + * \param right + */ + virtual void Sub(CBotVar* left, CBotVar* right); + + /*! + * \brief Mul Multiplication + * \param left + * \param right + */ + virtual void Mul(CBotVar* left, CBotVar* right); + + /*! + * \brief Div Division + * \param left + * \param right + * \return + */ + virtual int Div(CBotVar* left, CBotVar* right); + + /*! + * \brief Modulo Remainder of division + * \param left + * \param right + * \return + */ + virtual int Modulo(CBotVar* left, CBotVar* right); + + /*! + * \brief Power + * \param left + * \param right + */ + virtual void Power(CBotVar* left, CBotVar* right); + + /*! + * \brief Lo + * \param left + * \param right + * \return + */ + virtual bool Lo(CBotVar* left, CBotVar* right); + + /*! + * \brief Hi + * \param left + * \param right + * \return + */ + virtual bool Hi(CBotVar* left, CBotVar* right); + + /*! + * \brief Ls + * \param left + * \param right + * \return + */ + virtual bool Ls(CBotVar* left, CBotVar* right); + + /*! + * \brief Hs + * \param left + * \param right + * \return + */ + virtual bool Hs(CBotVar* left, CBotVar* right); + + /*! + * \brief Eq + * \param left + * \param right + * \return + */ + virtual bool Eq(CBotVar* left, CBotVar* right); + + /*! + * \brief Ne + * \param left + * \param right + * \return + */ + virtual bool Ne(CBotVar* left, CBotVar* right); + + /*! + * \brief And + * \param left + * \param right + */ + virtual void And(CBotVar* left, CBotVar* right); + + /*! + * \brief Or + * \param left + * \param right + */ + virtual void Or(CBotVar* left, CBotVar* right); + + /*! + * \brief XOr + * \param left + * \param right + */ + virtual void XOr(CBotVar* left, CBotVar* right); + + /*! + * \brief ASR + * \param left + * \param right + */ + virtual void ASR(CBotVar* left, CBotVar* right); + + /*! + * \brief SR + * \param left + * \param right + */ + virtual void SR(CBotVar* left, CBotVar* right); + + /*! + * \brief SL + * \param left + * \param right + */ + virtual void SL(CBotVar* left, CBotVar* right); + + /*! + * \brief Neg + */ + virtual void Neg(); + + /*! + * \brief Not + */ + virtual void Not(); + + /*! + * \brief Inc + */ + virtual void Inc(); + + /*! + * \brief Dec + */ + virtual void Dec(); + + /*! + * \brief Save0State + * \param pf + * \return + */ + virtual bool Save0State(FILE* pf); + + /*! + * \brief Save1State + * \param pf + * \return + */ + virtual bool Save1State(FILE* pf); + + /*! + * \brief RestoreState + * \param pf + * \param pVar + * \return + */ + static bool RestoreState(FILE* pf, CBotVar* &pVar); + + /*! + * \brief debug + */ + void debug(); + + /*! + * \brief Maj + * \param pUser + * \param bContinue + */ + virtual void Maj(void* pUser = nullptr, bool bContinue = true); + + /*! + * \brief SetUniqNum + * \param n + */ + void SetUniqNum(long n); + + /*! + * \brief GetUniqNum + * \return + */ + long GetUniqNum(); + + /*! + * \brief NextUniqNum + * \return + */ + static long NextUniqNum(); + +protected: + + //! The corresponding token. + CBotToken* m_token; + //! List of variables. + CBotVar* m_next; + //! Type of value. + CBotTypResult m_type; + //! Not initialized. + InitType m_binit; + //! Corresponding this element. + CBotVarClass* m_pMyThis; + //! User data if necessary. + void* m_pUserPtr; + //! Static element (in class). + bool m_bStatic; + //! Element public, protected or private. + int m_mPrivate; + //! Expression for the original content. + CBotInstr* m_InitExpr; + //! List of limits for a table. + CBotInstr* m_LimExpr; + //! Unique identifier. + long m_ident; + + //! Counter + static long m_identcpt; + + friend class CBotStack; + friend class CBotCStack; + friend class CBotInstrCall; + friend class CBotProgram; + friend class CBotClass; + friend class CBotVarClass; + friend class CBotVarPointer; + friend class CBotVarArray; +}; + +/* NOTE (#) + methods SetValInt() SetValFloat() et SetValString() + can be called with objects which are respectively integer, real or string + Always be sure of the type of the variable before calling these methods + + if ( pVar->GetType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !! + + methods GetValInt(), GetValFloat() et GetValString() + use value conversions, + GetValString() works on numbers (makes the corresponding string) + but do not make GetValInt () with a string variable! +*/ diff --git a/src/CBot/CBotVar/CBotVarArray.h b/src/CBot/CBotVar/CBotVarArray.h index 158cdf81..65222ac6 100644 --- a/src/CBot/CBotVar/CBotVarArray.h +++ b/src/CBot/CBotVar/CBotVarArray.h @@ -23,6 +23,8 @@ #include "CBot.h" #include "CBotDefines.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarBoolean.h b/src/CBot/CBotVar/CBotVarBoolean.h index e57c22f7..f21cf285 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.h +++ b/src/CBot/CBotVar/CBotVarBoolean.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotToken.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h index b7b878f9..5089c408 100644 --- a/src/CBot/CBotVar/CBotVarClass.h +++ b/src/CBot/CBotVar/CBotVarClass.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarFloat.h b/src/CBot/CBotVar/CBotVarFloat.h index fb30cd49..07a3ecc6 100644 --- a/src/CBot/CBotVar/CBotVarFloat.h +++ b/src/CBot/CBotVar/CBotVarFloat.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarInt.h b/src/CBot/CBotVar/CBotVarInt.h index 741fdc10..65ebb8c3 100644 --- a/src/CBot/CBotVar/CBotVarInt.h +++ b/src/CBot/CBotVar/CBotVarInt.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarPointer.h b/src/CBot/CBotVar/CBotVarPointer.h index dbb6ea39..278fbd8d 100644 --- a/src/CBot/CBotVar/CBotVarPointer.h +++ b/src/CBot/CBotVar/CBotVarPointer.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotDll.h" +#include "CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarString.h b/src/CBot/CBotVar/CBotVarString.h index 31c776e2..93007bee 100644 --- a/src/CBot/CBotVar/CBotVarString.h +++ b/src/CBot/CBotVar/CBotVarString.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 6e8a8039..87303c44 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -5,7 +5,6 @@ set(SOURCES CBotStack.cpp CBotString.cpp CBotToken.cpp - CBotVar.cpp CBotCall.cpp CBotUtils.cpp CBotDefParam.cpp @@ -62,6 +61,7 @@ set(SOURCES CBotVar/CBotVarString.cpp CBotVar/CBotVarFloat.cpp CBotVar/CBotVarInt.cpp + CBotVar/CBotVar.cpp ) # Includes diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 975421b2..640efc19 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -17,12 +17,15 @@ * along with this program. If not, see http://gnu.org/licenses */ +#include "CBotVar/CBotVar.h" + // definition of string functions // gives the length of a chain // execution + bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter diff --git a/src/script/script.cpp b/src/script/script.cpp index 4ba1f173..d87a41a6 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -43,6 +43,7 @@ #include "ui/controls/list.h" #include "CBot/CBotToken.h" +#include "CBot/CBotVar/CBotVar.h" const int CBOT_IPF = 100; // CBOT: default number of instructions / frame diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index 7ff61714..252f09df 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -21,6 +21,7 @@ // TODO must be replaced by CBot.h #include "CBot/CBotClass.h" +#include "CBot/CBotVar/CBotVar.h" #include "app/app.h" From 013be673ce18ef139b5b3a4580c0eae3b5c883d5 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 15 Nov 2015 23:41:24 +0100 Subject: [PATCH 64/91] Moving CBotCStack class in its own header and source files. --- src/CBot/CBot.cpp | 1 + src/CBot/CBot.h | 76 ---- src/CBot/CBotCStack.cpp | 405 ++++++++++++++++++++++ src/CBot/CBotCStack.h | 267 ++++++++++++++ src/CBot/CBotCall.cpp | 3 +- src/CBot/CBotCallMethode.cpp | 1 + src/CBot/CBotClass.cpp | 1 + src/CBot/CBotDefParam.cpp | 1 + src/CBot/CBotInstr/CBotBlock.cpp | 2 + src/CBot/CBotInstr/CBotBoolExpr.cpp | 2 + src/CBot/CBotInstr/CBotBoolean.cpp | 1 + src/CBot/CBotInstr/CBotBreak.cpp | 1 + src/CBot/CBotInstr/CBotCase.cpp | 1 + src/CBot/CBotInstr/CBotCatch.cpp | 1 + src/CBot/CBotInstr/CBotClassInst.cpp | 1 + src/CBot/CBotInstr/CBotCondition.cpp | 2 + src/CBot/CBotInstr/CBotDo.cpp | 1 + src/CBot/CBotInstr/CBotExprAlpha.cpp | 1 + src/CBot/CBotInstr/CBotExprBool.cpp | 1 + src/CBot/CBotInstr/CBotExprNum.cpp | 1 + src/CBot/CBotInstr/CBotExprUnaire.cpp | 1 + src/CBot/CBotInstr/CBotExprVar.cpp | 1 + src/CBot/CBotInstr/CBotExpression.cpp | 1 + src/CBot/CBotInstr/CBotFieldExpr.cpp | 1 + src/CBot/CBotInstr/CBotFloat.cpp | 1 + src/CBot/CBotInstr/CBotFor.cpp | 1 + src/CBot/CBotInstr/CBotFunction.cpp | 1 + src/CBot/CBotInstr/CBotIString.cpp | 1 + src/CBot/CBotInstr/CBotIf.cpp | 1 + src/CBot/CBotInstr/CBotIndexExpr.cpp | 1 + src/CBot/CBotInstr/CBotInstArray.cpp | 1 + src/CBot/CBotInstr/CBotInstrCall.cpp | 1 + src/CBot/CBotInstr/CBotInstrMethode.cpp | 1 + src/CBot/CBotInstr/CBotInt.cpp | 1 + src/CBot/CBotInstr/CBotLeftExpr.cpp | 1 + src/CBot/CBotInstr/CBotLeftExprVar.cpp | 1 + src/CBot/CBotInstr/CBotListArray.cpp | 1 + src/CBot/CBotInstr/CBotListExpression.cpp | 1 + src/CBot/CBotInstr/CBotListInstr.cpp | 1 + src/CBot/CBotInstr/CBotNew.cpp | 1 + src/CBot/CBotInstr/CBotParExpr.cpp | 2 + src/CBot/CBotInstr/CBotReturn.cpp | 1 + src/CBot/CBotInstr/CBotSwitch.cpp | 1 + src/CBot/CBotInstr/CBotThrow.cpp | 1 + src/CBot/CBotInstr/CBotTry.cpp | 1 + src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 1 + src/CBot/CBotInstr/CBotWhile.cpp | 1 + src/CBot/CBotProgram.cpp | 1 + src/CBot/CBotStack.cpp | 389 --------------------- src/CBot/CBotUtils.cpp | 1 + src/CBot/CMakeLists.txt | 1 + 51 files changed, 724 insertions(+), 466 deletions(-) create mode 100644 src/CBot/CBotCStack.cpp create mode 100644 src/CBot/CBotCStack.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index d528d1f7..1c8b4379 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -73,6 +73,7 @@ #include "CBotInstr/CBotInt.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 98d3f9ce..e42d1851 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -52,82 +52,6 @@ class CBotIf; // if (...) {...} else {...} class CBotDefParam; // paramerer list of a function - - - -//////////////////////////////////////////////////////////////////////// -// Management of the stack of compilation -//////////////////////////////////////////////////////////////////////// - - -class CBotCStack -{ -private: - CBotCStack* m_next; - CBotCStack* m_prev; - - static int m_error; - static int m_end; - int m_start; - - CBotVar* m_var; // result of the operations - - bool m_bBlock; // is part of a block (variables are local to this block) - CBotVar* m_listVar; - - static - CBotProgram* m_prog; // list of compiled functions - static - CBotTypResult m_retTyp; -// static -// CBotToken* m_retClass; - -public: - CBotCStack(CBotCStack* ppapa); - ~CBotCStack(); - - bool IsOk(); - int GetError(); - int GetError(int& start, int& end); - // gives error number - - void SetType(CBotTypResult& type);// determines the type - CBotTypResult GetTypResult(int mode = 0); // gives the type of value on the stack - int GetType(int mode = 0); // gives the type of value on the stack - CBotClass* GetClass(); // gives the class of the value on the stack - - void AddVar(CBotVar* p); // adds a local variable - CBotVar* FindVar(CBotToken* &p); // finds a variable - CBotVar* FindVar(CBotToken& Token); - bool CheckVarLocal(CBotToken* &pToken); - CBotVar* CopyVar(CBotToken& Token); // finds and makes a copy - - CBotCStack* TokenStack(CBotToken* pToken = nullptr, bool bBlock = false); - CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); // transmits the result upper - CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); // transmits the result upper - - void SetVar( CBotVar* var ); - void SetCopyVar( CBotVar* var ); - CBotVar* GetVar(); - - void SetStartError(int pos); - void SetError(int n, int pos); - void SetError(int n, CBotToken* p); - void ResetError(int n, int start, int end); - - void SetRetType(CBotTypResult& type); - CBotTypResult GetRetType(); - -// void SetBotCall(CBotFunction* &pFunc); - void SetBotCall(CBotProgram* p); - CBotProgram* GetBotCall(); - CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); - bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); - - bool NextToken(CBotToken* &p); -}; - - extern bool SaveVar(FILE* pf, CBotVar* pVar); diff --git a/src/CBot/CBotCStack.cpp b/src/CBot/CBotCStack.cpp new file mode 100644 index 00000000..6d9b820e --- /dev/null +++ b/src/CBot/CBotCStack.cpp @@ -0,0 +1,405 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + + +// Modules inlcude +#include "CBotCStack.h" + +#include "CBotToken.h" +#include "CBotCall.h" + +#include "CBotVar/CBotVar.h" + +#include "CBotInstr/CBotFunction.h" + +// Local include + +// Global include + + + +//////////////////////////////////////////////////////////////////////////////// +CBotProgram* CBotCStack::m_prog = nullptr; // init the static variable +int CBotCStack::m_error = 0; +int CBotCStack::m_end = 0; +CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); + +//////////////////////////////////////////////////////////////////////////////// +CBotCStack::CBotCStack(CBotCStack* ppapa) +{ + m_next = nullptr; + m_prev = ppapa; + + if (ppapa == nullptr) + { + m_error = 0; + m_start = 0; + m_end = 0; + m_bBlock = true; + } + else + { + m_start = ppapa->m_start; + m_bBlock = false; + } + + m_listVar = nullptr; + m_var = nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCStack::~CBotCStack() +{ + if (m_next != nullptr) delete m_next; + if (m_prev != nullptr) m_prev->m_next = nullptr; // removes chain + + delete m_var; + delete m_listVar; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) +{ + if (m_next != nullptr) return m_next; // include on an existing stack + + CBotCStack* p = new CBotCStack(this); + m_next = p; // channel element + p->m_bBlock = bBlock; + + if (pToken != nullptr) p->SetStartError(pToken->GetStart()); + + return p; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) +{ + if ( pfils == this ) return inst; + + if (m_var != nullptr) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = nullptr; // not to destroy the variable + + if (m_error) + { + m_start = pfils->m_start; // retrieves the position of the error + m_end = pfils->m_end; + } + + delete pfils; + return inst; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) +{ + if (m_var != nullptr) delete m_var; // value replaced? + m_var = pfils->m_var; // result transmitted + pfils->m_var = nullptr; // not to destroy the variable + + if (m_error) + { + m_start = pfils->m_start; // retrieves the position of the error + m_end = pfils->m_end; + } + + delete pfils; + return inst; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotCStack::GetError(int& start, int& end) +{ + start = m_start; + end = m_end; + return m_error; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotCStack::GetError() +{ + return m_error; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult CBotCStack::GetTypResult(int mode) +{ + if (m_var == nullptr) + return CBotTypResult(99); + return m_var->GetTypResult(mode); +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotCStack::GetType(int mode) +{ + if (m_var == nullptr) + return 99; + return m_var->GetType(mode); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotCStack::GetClass() +{ + if ( m_var == nullptr ) + return nullptr; + if ( m_var->GetType(1) != CBotTypPointer ) return nullptr; + + return m_var->GetClass(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetType(CBotTypResult& type) +{ + if (m_var == nullptr) return; + m_var->SetType( type ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotCStack::FindVar(CBotToken* &pToken) +{ + CBotCStack* p = this; + CBotString name = pToken->GetString(); + + while (p != nullptr) + { + CBotVar* pp = p->m_listVar; + while ( pp != nullptr) + { + if (name == pp->GetName()) + { + return pp; + } + pp = pp->m_next; + } + p = p->m_prev; + } + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotCStack::FindVar(CBotToken& Token) +{ + CBotToken* pt = &Token; + return FindVar(pt); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotCStack::CopyVar(CBotToken& Token) +{ + CBotVar* pVar = FindVar( Token ); + + if ( pVar == nullptr) return nullptr; + + CBotVar* pCopy = CBotVar::Create( "", pVar->GetType() ); + pCopy->Copy(pVar); + return pCopy; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCStack::IsOk() +{ + return (m_error == 0); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetStartError( int pos ) +{ + if ( m_error != 0) return; // does not change existing error + m_start = pos; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetError(int n, int pos) +{ + if ( n!= 0 && m_error != 0) return; // does not change existing error + m_error = n; + m_end = pos; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetError(int n, CBotToken* p) +{ + if (m_error) return; // does not change existing error + m_error = n; + m_start = p->GetStart(); + m_end = p->GetEnd(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::ResetError(int n, int start, int end) +{ + m_error = n; + m_start = start; + m_end = end; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCStack::NextToken(CBotToken* &p) +{ + CBotToken* pp = p; + + p = p->GetNext(); + if (p!=nullptr) return true; + + SetError(TX_ENDOF, pp->GetEnd()); + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetBotCall(CBotProgram* p) +{ + m_prog = p; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotProgram* CBotCStack::GetBotCall() +{ + return m_prog; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetRetType(CBotTypResult& type) +{ + m_retTyp = type; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult CBotCStack::GetRetType() +{ + return m_retTyp; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetVar( CBotVar* var ) +{ + if (m_var) delete m_var; // replacement of a variable + m_var = var; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::SetCopyVar( CBotVar* var ) +{ + if (m_var) delete m_var; // replacement of a variable + + if ( var == nullptr ) return; + m_var = CBotVar::Create("", var->GetTypResult(2)); + m_var->Copy( var ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotVar* CBotCStack::GetVar() +{ + return m_var; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotCStack::AddVar(CBotVar* pVar) +{ + CBotCStack* p = this; + + // returns to the father element + while (p != nullptr && p->m_bBlock == 0) p = p->m_prev; + + if ( p == nullptr ) return; + + CBotVar** pp = &p->m_listVar; + while ( *pp != nullptr ) pp = &(*pp)->m_next; + + *pp = pVar; // added after + +#ifdef _DEBUG + if ( pVar->GetUniqNum() == 0 ) assert(0); +#endif +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCStack::CheckVarLocal(CBotToken* &pToken) +{ + CBotCStack* p = this; + CBotString name = pToken->GetString(); + + while (p != nullptr) + { + CBotVar* pp = p->m_listVar; + while ( pp != nullptr) + { + if (name == pp->GetName()) + return true; + pp = pp->m_next; + } + if ( p->m_bBlock ) return false; + p = p->m_prev; + } + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) +{ + nIdent = 0; + CBotTypResult val(-1); + + val = CBotCall::CompileCall(p, ppVars, this, nIdent); + if (val.GetType() < 0) + { + val = m_prog->GetFunctions()->CompileCall(p->GetString(), ppVars, nIdent); + if ( val.GetType() < 0 ) + { + // pVar = nullptr; // the error is not on a particular parameter + SetError( -val.GetType(), p ); + val.SetType(-val.GetType()); + return val; + } + } + return val; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) +{ + CBotString name = pToken->GetString(); + + if ( CBotCall::CheckCall(name) ) return true; + + CBotFunction* pp = m_prog->GetFunctions(); + while ( pp != nullptr ) + { + if ( pToken->GetString() == pp->GetName() ) + { + // are parameters exactly the same? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->Next(); + } + + pp = CBotFunction::m_listPublic; + while ( pp != nullptr ) + { + if ( pToken->GetString() == pp->GetName() ) + { + // are parameters exactly the same? + if ( pp->CheckParam( pParam ) ) + return true; + } + pp = pp->m_nextpublic; + } + + return false; +} diff --git a/src/CBot/CBotCStack.h b/src/CBot/CBotCStack.h new file mode 100644 index 00000000..28e9e886 --- /dev/null +++ b/src/CBot/CBotCStack.h @@ -0,0 +1,267 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +#include "CBotProgram.h" + +// Local include + +// Global include + + +/*! + * \brief The CBotCStack class Management of the stack of compilation. + */ +class CBotCStack +{ +public: + + /*! + * \brief CBotCStack + * \param ppapa + */ + CBotCStack(CBotCStack* ppapa); + + /*! + * \brief CBotCStack Destructor. + */ + ~CBotCStack(); + + /*! + * \brief IsOk + * \return + */ + bool IsOk(); + + /*! + * \brief GetError + * \return + */ + int GetError(); + + /*! + * \brief GetError Gives error number + * \param start + * \param end + * \return + */ + int GetError(int& start, int& end); + + /*! + * \brief SetType Set the type of instruction on the stack. + * \param type + */ + void SetType(CBotTypResult& type); + + /*! + * \brief GetTypResult Gives the type of value on the stack. Type of + * instruction on the stack. + * \param mode + * \return + */ + CBotTypResult GetTypResult(int mode = 0); + + /*! + * \brief GetType Gives the type of value on the stack. + * \param mode + * \return + */ + int GetType(int mode = 0); + + /*! + * \brief GetClass Gives the class of the value on the stack. + * \return + */ + CBotClass* GetClass(); + + /*! + * \brief AddVar Adds a local variable. + * \param p + */ + void AddVar(CBotVar* p); + + /*! + * \brief FindVar Finds a variable. Seeks a variable on the stack the token + * may be a result of TokenTypVar (object of a class) or a pointer in the + * source. + * \param p + * \return + */ + CBotVar* FindVar(CBotToken* &p); + + /*! + * \brief FindVar + * \param Token + * \return + */ + CBotVar* FindVar(CBotToken& Token); + + /*! + * \brief CheckVarLocal Test whether a variable is already defined locally. + * \param pToken + * \return + */ + bool CheckVarLocal(CBotToken* &pToken); + + /*! + * \brief CopyVar Finds and makes a copy. + * \param Token + * \return + */ + CBotVar* CopyVar(CBotToken& Token); + + /*! + * \brief TokenStack Used only at compile. + * \param pToken + * \param bBlock + * \return + */ + CBotCStack* TokenStack(CBotToken* pToken = nullptr, bool bBlock = false); + + /*! + * \brief Return Transmits the result upper. + * \param p + * \param pParent + * \return + */ + CBotInstr* Return(CBotInstr* p, CBotCStack* pParent); + + /*! + * \brief ReturnFunc Transmits the result upper. + * \param p + * \param pParent + * \return + */ + CBotFunction* ReturnFunc(CBotFunction* p, CBotCStack* pParent); + + /*! + * \brief SetVar + * \param var + */ + void SetVar( CBotVar* var ); + + /*! + * \brief SetCopyVar Puts on the stack a copy of a variable. + * \param var + */ + void SetCopyVar( CBotVar* var ); + + /*! + * \brief GetVar + * \return + */ + CBotVar* GetVar(); + + /*! + * \brief SetStartError + * \param pos + */ + void SetStartError(int pos); + + /*! + * \brief SetError + * \param n + * \param pos + */ + void SetError(int n, int pos); + + /*! + * \brief SetError + * \param n + * \param p + */ + void SetError(int n, CBotToken* p); + + /*! + * \brief ResetError + * \param n + * \param start + * \param end + */ + void ResetError(int n, int start, int end); + + /*! + * \brief SetRetType + * \param type + */ + void SetRetType(CBotTypResult& type); + + /*! + * \brief GetRetType + * \return + */ + CBotTypResult GetRetType(); + + /*! + * \brief SetBotCall + * \param p + */ + void SetBotCall(CBotProgram* p); + + /*! + * \brief GetBotCall + * \return + */ + CBotProgram* GetBotCall(); + + /*! + * \brief CompileCall + * \param p + * \param ppVars + * \param nIdent + * \return + */ + CBotTypResult CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent); + + /*! + * \brief CheckCall Test if a procedure name is already defined somewhere. + * \param pToken + * \param pParam + * \return + */ + bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam); + + /*! + * \brief NextToken + * \param p + * \return + */ + bool NextToken(CBotToken* &p); + +private: + CBotCStack* m_next; + CBotCStack* m_prev; + + static int m_error; + static int m_end; + int m_start; + + //! Result of the operations. + CBotVar* m_var; + //! Is part of a block (variables are local to this block). + bool m_bBlock; + CBotVar* m_listVar; + //! List of compiled functions. + static CBotProgram* m_prog; + static CBotTypResult m_retTyp; +}; diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp index 75beb6fb..ee853d8a 100644 --- a/src/CBot/CBotCall.cpp +++ b/src/CBot/CBotCall.cpp @@ -22,11 +22,12 @@ #include "CBotToken.h" #include "CBotStack.h" - +#include "CBotCStack.h" #include "CBotUtils.h" #include "CBotVar/CBotVar.h" + // Local include // Global include diff --git a/src/CBot/CBotCallMethode.cpp b/src/CBot/CBotCallMethode.cpp index 2bcc41c8..da915dfb 100644 --- a/src/CBot/CBotCallMethode.cpp +++ b/src/CBot/CBotCallMethode.cpp @@ -22,6 +22,7 @@ #include "CBotUtils.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 9e20d667..94a05346 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -30,6 +30,7 @@ #include "CBotCall.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotUtils.h" #include "CBotCallMethode.h" diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp index 601e5676..9af6043f 100644 --- a/src/CBot/CBotDefParam.cpp +++ b/src/CBot/CBotDefParam.cpp @@ -23,6 +23,7 @@ #include "CBot.h" #include "CBotUtils.h" +#include "CBotCStack.h" #include "CBotVar/CBotVarClass.h" diff --git a/src/CBot/CBotInstr/CBotBlock.cpp b/src/CBot/CBotInstr/CBotBlock.cpp index ed87b53b..ba2b5ce5 100644 --- a/src/CBot/CBotInstr/CBotBlock.cpp +++ b/src/CBot/CBotInstr/CBotBlock.cpp @@ -19,6 +19,8 @@ // Modules inlcude #include "CBotBlock.h" + +#include "CBotCStack.h" #include "CBotListInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.cpp b/src/CBot/CBotInstr/CBotBoolExpr.cpp index 2815c8ed..db6b36bd 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.cpp +++ b/src/CBot/CBotInstr/CBotBoolExpr.cpp @@ -21,6 +21,8 @@ #include "CBotBoolExpr.h" #include "CBotTwoOpExpr.h" +#include "CBotCStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBoolean.cpp b/src/CBot/CBotInstr/CBotBoolean.cpp index ea0103e5..64c4d3d3 100644 --- a/src/CBot/CBotInstr/CBotBoolean.cpp +++ b/src/CBot/CBotInstr/CBotBoolean.cpp @@ -24,6 +24,7 @@ #include "CBotInstArray.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotBreak.cpp b/src/CBot/CBotInstr/CBotBreak.cpp index 5879f0f8..9d4100cb 100644 --- a/src/CBot/CBotInstr/CBotBreak.cpp +++ b/src/CBot/CBotInstr/CBotBreak.cpp @@ -21,6 +21,7 @@ #include "CBotBreak.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCase.cpp b/src/CBot/CBotInstr/CBotCase.cpp index f188f8ed..3ae5b706 100644 --- a/src/CBot/CBotInstr/CBotCase.cpp +++ b/src/CBot/CBotInstr/CBotCase.cpp @@ -22,6 +22,7 @@ #include "CBotExprNum.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index fdbc1d45..26f7d17d 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -23,6 +23,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index 8426422b..5d2e0a66 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -24,6 +24,7 @@ #include "CBotInstArray.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotVar/CBotVarPointer.h" diff --git a/src/CBot/CBotInstr/CBotCondition.cpp b/src/CBot/CBotInstr/CBotCondition.cpp index d87c451b..453e26c8 100644 --- a/src/CBot/CBotInstr/CBotCondition.cpp +++ b/src/CBot/CBotInstr/CBotCondition.cpp @@ -21,6 +21,8 @@ #include "CBotCondition.h" #include "CBotBoolExpr.h" +#include "CBotCStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp index 9cfeddb3..c24baff5 100644 --- a/src/CBot/CBotInstr/CBotDo.cpp +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -23,6 +23,7 @@ #include "CBotCondition.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.cpp b/src/CBot/CBotInstr/CBotExprAlpha.cpp index 60923754..1f93b89a 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.cpp +++ b/src/CBot/CBotInstr/CBotExprAlpha.cpp @@ -21,6 +21,7 @@ #include "CBotExprAlpha.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotExprBool.cpp b/src/CBot/CBotInstr/CBotExprBool.cpp index ebefa6dd..3df5095f 100644 --- a/src/CBot/CBotInstr/CBotExprBool.cpp +++ b/src/CBot/CBotInstr/CBotExprBool.cpp @@ -21,6 +21,7 @@ #include "CBotExprBool.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp index 6082c0e1..65dbd780 100644 --- a/src/CBot/CBotInstr/CBotExprNum.cpp +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -21,6 +21,7 @@ #include "CBotExprNum.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotExprUnaire.cpp b/src/CBot/CBotInstr/CBotExprUnaire.cpp index 4732eda1..31056bb3 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.cpp +++ b/src/CBot/CBotInstr/CBotExprUnaire.cpp @@ -22,6 +22,7 @@ #include "CBotParExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 344ce85c..ee4a748e 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -25,6 +25,7 @@ #include "CBotFieldExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVarArray.h" diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp index bdf096a5..b6b97837 100644 --- a/src/CBot/CBotInstr/CBotExpression.cpp +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -22,6 +22,7 @@ #include "CBotTwoOpExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotFieldExpr.cpp b/src/CBot/CBotInstr/CBotFieldExpr.cpp index bad06d93..7c4ff58c 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.cpp +++ b/src/CBot/CBotInstr/CBotFieldExpr.cpp @@ -21,6 +21,7 @@ #include "CBotFieldExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotVar/CBotVarClass.h" diff --git a/src/CBot/CBotInstr/CBotFloat.cpp b/src/CBot/CBotInstr/CBotFloat.cpp index 1a721458..df69b07f 100644 --- a/src/CBot/CBotInstr/CBotFloat.cpp +++ b/src/CBot/CBotInstr/CBotFloat.cpp @@ -24,6 +24,7 @@ #include "CBotInstArray.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index 7cbca510..2a97b86b 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -24,6 +24,7 @@ #include "CBotBoolExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 6700fab7..d325020e 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -30,6 +30,7 @@ #include "CBotInstr/CBotListArray.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotDefParam.h" #include "CBotUtils.h" diff --git a/src/CBot/CBotInstr/CBotIString.cpp b/src/CBot/CBotInstr/CBotIString.cpp index 3c348111..6feefc41 100644 --- a/src/CBot/CBotInstr/CBotIString.cpp +++ b/src/CBot/CBotInstr/CBotIString.cpp @@ -23,6 +23,7 @@ #include "CBotTwoOpExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotIf.cpp b/src/CBot/CBotInstr/CBotIf.cpp index b0cdbc88..1075f65f 100644 --- a/src/CBot/CBotInstr/CBotIf.cpp +++ b/src/CBot/CBotInstr/CBotIf.cpp @@ -23,6 +23,7 @@ #include "CBotInstr/CBotCondition.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.cpp b/src/CBot/CBotInstr/CBotIndexExpr.cpp index 2bc37f1e..471089c3 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.cpp +++ b/src/CBot/CBotInstr/CBotIndexExpr.cpp @@ -21,6 +21,7 @@ #include "CBotIndexExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVarArray.h" diff --git a/src/CBot/CBotInstr/CBotInstArray.cpp b/src/CBot/CBotInstr/CBotInstArray.cpp index f53cb65e..7d2a441e 100644 --- a/src/CBot/CBotInstr/CBotInstArray.cpp +++ b/src/CBot/CBotInstr/CBotInstArray.cpp @@ -26,6 +26,7 @@ #include "CBotEmpty.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotDefines.h" diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp index c2c33b0f..ca21b87d 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.cpp +++ b/src/CBot/CBotInstr/CBotInstrCall.cpp @@ -22,6 +22,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index dabcd463..46adf723 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -21,6 +21,7 @@ #include "CBotInstrMethode.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotInt.cpp b/src/CBot/CBotInstr/CBotInt.cpp index 1649de55..7ea8cbf6 100644 --- a/src/CBot/CBotInstr/CBotInt.cpp +++ b/src/CBot/CBotInstr/CBotInt.cpp @@ -25,6 +25,7 @@ #include "CBotTwoOpExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp index 468854a9..e09a2263 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.cpp +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -24,6 +24,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotVar/CBotVarArray.h" diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.cpp b/src/CBot/CBotInstr/CBotLeftExprVar.cpp index d0c3482b..18af17c7 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.cpp +++ b/src/CBot/CBotInstr/CBotLeftExprVar.cpp @@ -21,6 +21,7 @@ #include "CBotLeftExprVar.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp index 682505b7..6ac0d6a0 100644 --- a/src/CBot/CBotInstr/CBotListArray.cpp +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -24,6 +24,7 @@ #include "CBotTwoOpExpr.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index afa5f877..670f3fb5 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -26,6 +26,7 @@ #include "CBotInt.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp index fde2103c..576012b8 100644 --- a/src/CBot/CBotInstr/CBotListInstr.cpp +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -22,6 +22,7 @@ #include "CBotBlock.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp index 094e5a08..6f7d8605 100644 --- a/src/CBot/CBotInstr/CBotNew.cpp +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -21,6 +21,7 @@ #include "CBotNew.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index fc34b513..21b40d18 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -34,6 +34,8 @@ #include "CBotVar/CBotVar.h" +#include "CBotCStack.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotReturn.cpp b/src/CBot/CBotInstr/CBotReturn.cpp index 3837b26c..70c849d2 100644 --- a/src/CBot/CBotInstr/CBotReturn.cpp +++ b/src/CBot/CBotInstr/CBotReturn.cpp @@ -22,6 +22,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index 0a527d2f..ef53fd77 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -26,6 +26,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotThrow.cpp b/src/CBot/CBotInstr/CBotThrow.cpp index c02f2214..4fc8cddd 100644 --- a/src/CBot/CBotInstr/CBotThrow.cpp +++ b/src/CBot/CBotInstr/CBotThrow.cpp @@ -22,6 +22,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTry.cpp b/src/CBot/CBotInstr/CBotTry.cpp index 2ac2ae64..d3aecf85 100644 --- a/src/CBot/CBotInstr/CBotTry.cpp +++ b/src/CBot/CBotInstr/CBotTry.cpp @@ -22,6 +22,7 @@ #include "CBotBlock.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index 4ad5ad03..ab5bc653 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -24,6 +24,7 @@ #include "CBotExpression.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotInstr/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp index bfc2061f..eb38aea1 100644 --- a/src/CBot/CBotInstr/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -23,6 +23,7 @@ #include "CBotCondition.h" #include "CBotStack.h" +#include "CBotCStack.h" // Local include diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 44eaca50..4e3f2c49 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -22,6 +22,7 @@ #include "CBotCall.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotClass.h" #include "CBotUtils.h" diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 87aa0bb2..91f91a93 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -1133,392 +1133,3 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) } return true; } - - - - -//////////////////////////////////////////////////////////////////////////// -// management of the compile stack -//////////////////////////////////////////////////////////////////////////// - -CBotProgram* CBotCStack::m_prog = nullptr; // init the static variable -int CBotCStack::m_error = 0; -int CBotCStack::m_end = 0; -CBotTypResult CBotCStack::m_retTyp = CBotTypResult(0); -//CBotToken* CBotCStack::m_retClass= nullptr; - -//////////////////////////////////////////////////////////////////////////////// -CBotCStack::CBotCStack(CBotCStack* ppapa) -{ - m_next = nullptr; - m_prev = ppapa; - - if (ppapa == nullptr) - { - m_error = 0; - m_start = 0; - m_end = 0; - m_bBlock = true; - } - else - { - m_start = ppapa->m_start; - m_bBlock = false; - } - - m_listVar = nullptr; - m_var = nullptr; -} - -//////////////////////////////////////////////////////////////////////////////// -// destructor -CBotCStack::~CBotCStack() -{ - if (m_next != nullptr) delete m_next; - if (m_prev != nullptr) m_prev->m_next = nullptr; // removes chain - - delete m_var; - delete m_listVar; -} - -//////////////////////////////////////////////////////////////////////////////// -// used only at compile -CBotCStack* CBotCStack::TokenStack(CBotToken* pToken, bool bBlock) -{ - if (m_next != nullptr) return m_next; // include on an existing stack - - CBotCStack* p = new CBotCStack(this); - m_next = p; // channel element - p->m_bBlock = bBlock; - - if (pToken != nullptr) p->SetStartError(pToken->GetStart()); - - return p; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotInstr* CBotCStack::Return(CBotInstr* inst, CBotCStack* pfils) -{ - if ( pfils == this ) return inst; - - if (m_var != nullptr) delete m_var; // value replaced? - m_var = pfils->m_var; // result transmitted - pfils->m_var = nullptr; // not to destroy the variable - - if (m_error) - { - m_start = pfils->m_start; // retrieves the position of the error - m_end = pfils->m_end; - } - - delete pfils; - return inst; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotFunction* CBotCStack::ReturnFunc(CBotFunction* inst, CBotCStack* pfils) -{ - if (m_var != nullptr) delete m_var; // value replaced? - m_var = pfils->m_var; // result transmitted - pfils->m_var = nullptr; // not to destroy the variable - - if (m_error) - { - m_start = pfils->m_start; // retrieves the position of the error - m_end = pfils->m_end; - } - - delete pfils; - return inst; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotCStack::GetError(int& start, int& end) -{ - start = m_start; - end = m_end; - return m_error; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotCStack::GetError() -{ - return m_error; -} - -//////////////////////////////////////////////////////////////////////////////// -// type of instruction on the stack -CBotTypResult CBotCStack::GetTypResult(int mode) -{ - if (m_var == nullptr) - return CBotTypResult(99); - return m_var->GetTypResult(mode); -} - -//////////////////////////////////////////////////////////////////////////////// -// type of instruction on the stack -int CBotCStack::GetType(int mode) -{ - if (m_var == nullptr) - return 99; - return m_var->GetType(mode); -} - -//////////////////////////////////////////////////////////////////////////////// -// pointer on the stack is in what class? -CBotClass* CBotCStack::GetClass() -{ - if ( m_var == nullptr ) - return nullptr; - if ( m_var->GetType(1) != CBotTypPointer ) return nullptr; - - return m_var->GetClass(); -} - -//////////////////////////////////////////////////////////////////////////////// -// type of instruction on the stack -void CBotCStack::SetType(CBotTypResult& type) -{ - if (m_var == nullptr) return; - m_var->SetType( type ); -} - -// seeks a variable on the stack -// the token may be a result of TokenTypVar (object of a class) -// or a pointer in the source -//////////////////////////////////////////////////////////////////////////////// -CBotVar* CBotCStack::FindVar(CBotToken* &pToken) -{ - CBotCStack* p = this; - CBotString name = pToken->GetString(); - - while (p != nullptr) - { - CBotVar* pp = p->m_listVar; - while ( pp != nullptr) - { - if (name == pp->GetName()) - { - return pp; - } - pp = pp->m_next; - } - p = p->m_prev; - } - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotVar* CBotCStack::FindVar(CBotToken& Token) -{ - CBotToken* pt = &Token; - return FindVar(pt); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotVar* CBotCStack::CopyVar(CBotToken& Token) -{ - CBotVar* pVar = FindVar( Token ); - - if ( pVar == nullptr) return nullptr; - - CBotVar* pCopy = CBotVar::Create( "", pVar->GetType() ); - pCopy->Copy(pVar); - return pCopy; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotCStack::IsOk() -{ - return (m_error == 0); -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::SetStartError( int pos ) -{ - if ( m_error != 0) return; // does not change existing error - m_start = pos; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::SetError(int n, int pos) -{ - if ( n!= 0 && m_error != 0) return; // does not change existing error - m_error = n; - m_end = pos; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::SetError(int n, CBotToken* p) -{ - if (m_error) return; // does not change existing error - m_error = n; - m_start = p->GetStart(); - m_end = p->GetEnd(); -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::ResetError(int n, int start, int end) -{ - m_error = n; - m_start = start; - m_end = end; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotCStack::NextToken(CBotToken* &p) -{ - CBotToken* pp = p; - - p = p->GetNext(); - if (p!=nullptr) return true; - - SetError(TX_ENDOF, pp->GetEnd()); - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::SetBotCall(CBotProgram* p) -{ - m_prog = p; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotProgram* CBotCStack::GetBotCall() -{ - return m_prog; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::SetRetType(CBotTypResult& type) -{ - m_retTyp = type; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult CBotCStack::GetRetType() -{ - return m_retTyp; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::SetVar( CBotVar* var ) -{ - if (m_var) delete m_var; // replacement of a variable - m_var = var; -} - -//////////////////////////////////////////////////////////////////////////////// -// puts on the stack a copy of a variable -void CBotCStack::SetCopyVar( CBotVar* var ) -{ - if (m_var) delete m_var; // replacement of a variable - - if ( var == nullptr ) return; - m_var = CBotVar::Create("", var->GetTypResult(2)); - m_var->Copy( var ); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotVar* CBotCStack::GetVar() -{ - return m_var; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotCStack::AddVar(CBotVar* pVar) -{ - CBotCStack* p = this; - - // returns to the father element - while (p != nullptr && p->m_bBlock == 0) p = p->m_prev; - - if ( p == nullptr ) return; - - CBotVar** pp = &p->m_listVar; - while ( *pp != nullptr ) pp = &(*pp)->m_next; - - *pp = pVar; // added after - -#ifdef _DEBUG - if ( pVar->GetUniqNum() == 0 ) assert(0); -#endif -} - -// test whether a variable is already defined locally -//////////////////////////////////////////////////////////////////////////////// -bool CBotCStack::CheckVarLocal(CBotToken* &pToken) -{ - CBotCStack* p = this; - CBotString name = pToken->GetString(); - - while (p != nullptr) - { - CBotVar* pp = p->m_listVar; - while ( pp != nullptr) - { - if (name == pp->GetName()) - return true; - pp = pp->m_next; - } - if ( p->m_bBlock ) return false; - p = p->m_prev; - } - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nIdent) -{ - nIdent = 0; - CBotTypResult val(-1); - - val = CBotCall::CompileCall(p, ppVars, this, nIdent); - if (val.GetType() < 0) - { - val = m_prog->GetFunctions()->CompileCall(p->GetString(), ppVars, nIdent); - if ( val.GetType() < 0 ) - { - // pVar = nullptr; // the error is not on a particular parameter - SetError( -val.GetType(), p ); - val.SetType(-val.GetType()); - return val; - } - } - return val; -} - -// test if a procedure name is already defined somewhere -//////////////////////////////////////////////////////////////////////////////// -bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam) -{ - CBotString name = pToken->GetString(); - - if ( CBotCall::CheckCall(name) ) return true; - - CBotFunction* pp = m_prog->GetFunctions(); - while ( pp != nullptr ) - { - if ( pToken->GetString() == pp->GetName() ) - { - // are parameters exactly the same? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->Next(); - } - - pp = CBotFunction::m_listPublic; - while ( pp != nullptr ) - { - if ( pToken->GetString() == pp->GetName() ) - { - // are parameters exactly the same? - if ( pp->CheckParam( pParam ) ) - return true; - } - pp = pp->m_nextpublic; - } - - return false; -} diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index b5f9c235..2df62cc2 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -23,6 +23,7 @@ #include "CBotToken.h" #include "CBotClass.h" #include "CBotStack.h" +#include "CBotCStack.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 87303c44..e69beaea 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -3,6 +3,7 @@ set(SOURCES CBotClass.cpp CBotProgram.cpp CBotStack.cpp + CBotCStack.cpp CBotString.cpp CBotToken.cpp CBotCall.cpp From ef4e2f08a36d5eb5e9ece4b03a758b133e27ae8b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Mon, 16 Nov 2015 22:00:01 +0100 Subject: [PATCH 65/91] Moving CBotInstr class in its own header and source files. --- src/CBot/CBot.cpp | 336 --------------------- src/CBot/CBot.h | 90 ------ src/CBot/CBotDefines.h | 2 + src/CBot/CBotInstr/CBotBlock.h | 2 + src/CBot/CBotInstr/CBotBoolExpr.h | 2 + src/CBot/CBotInstr/CBotBoolean.h | 2 + src/CBot/CBotInstr/CBotBreak.h | 2 + src/CBot/CBotInstr/CBotCase.h | 2 + src/CBot/CBotInstr/CBotCatch.h | 2 + src/CBot/CBotInstr/CBotClassInst.h | 2 + src/CBot/CBotInstr/CBotCondition.h | 2 + src/CBot/CBotInstr/CBotDo.h | 2 + src/CBot/CBotInstr/CBotEmpty.h | 2 + src/CBot/CBotInstr/CBotExprAlpha.h | 2 + src/CBot/CBotInstr/CBotExprBool.h | 2 + src/CBot/CBotInstr/CBotExprNan.h | 2 + src/CBot/CBotInstr/CBotExprNull.h | 2 + src/CBot/CBotInstr/CBotExprNum.h | 2 + src/CBot/CBotInstr/CBotExprUnaire.h | 2 + src/CBot/CBotInstr/CBotExprVar.h | 2 + src/CBot/CBotInstr/CBotExpression.h | 3 + src/CBot/CBotInstr/CBotFieldExpr.h | 2 + src/CBot/CBotInstr/CBotFloat.h | 2 + src/CBot/CBotInstr/CBotFor.h | 2 + src/CBot/CBotInstr/CBotFunction.h | 2 + src/CBot/CBotInstr/CBotIString.h | 2 + src/CBot/CBotInstr/CBotIf.h | 2 + src/CBot/CBotInstr/CBotIndexExpr.h | 2 + src/CBot/CBotInstr/CBotInstArray.h | 2 + src/CBot/CBotInstr/CBotInstr.cpp | 374 ++++++++++++++++++++++++ src/CBot/CBotInstr/CBotInstr.h | 266 +++++++++++++++++ src/CBot/CBotInstr/CBotInstrCall.h | 2 + src/CBot/CBotInstr/CBotInstrMethode.h | 2 + src/CBot/CBotInstr/CBotInt.h | 2 + src/CBot/CBotInstr/CBotLeftExpr.h | 2 + src/CBot/CBotInstr/CBotLeftExprVar.h | 2 + src/CBot/CBotInstr/CBotListArray.h | 2 + src/CBot/CBotInstr/CBotListExpression.h | 2 + src/CBot/CBotInstr/CBotListInstr.h | 2 + src/CBot/CBotInstr/CBotLogicExpr.h | 2 + src/CBot/CBotInstr/CBotNew.h | 2 + src/CBot/CBotInstr/CBotParExpr.h | 2 +- src/CBot/CBotInstr/CBotPostIncExpr.h | 2 + src/CBot/CBotInstr/CBotPreIncExpr.h | 2 + src/CBot/CBotInstr/CBotReturn.h | 2 + src/CBot/CBotInstr/CBotSwitch.h | 2 + src/CBot/CBotInstr/CBotThrow.h | 2 + src/CBot/CBotInstr/CBotTry.h | 2 + src/CBot/CBotInstr/CBotTwoOpExpr.h | 2 + src/CBot/CBotInstr/CBotWhile.h | 2 + src/CBot/CBotVar/CBotVarClass.cpp | 4 +- src/CBot/CMakeLists.txt | 1 + 52 files changed, 735 insertions(+), 429 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotInstr.cpp create mode 100644 src/CBot/CBotInstr/CBotInstr.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 1c8b4379..1822df7b 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -83,342 +83,6 @@ // Global include #include - -CBotInstr::CBotInstr() -{ - name = "CBotInstr"; - m_next = nullptr; - m_next2b = nullptr; - m_next3 = nullptr; - m_next3b = nullptr; -} - -CBotInstr::~CBotInstr() -{ - delete m_next; - delete m_next2b; - delete m_next3; - delete m_next3b; -} - -// counter of nested loops, -// to determine the break and continue valid -// list of labels used - - -int CBotInstr::m_LoopLvl = 0; -CBotStringArray CBotInstr::m_labelLvl = CBotStringArray(); - -// adds a level with a label -void CBotInstr::IncLvl(CBotString& label) -{ - m_labelLvl.SetSize(m_LoopLvl+1); - m_labelLvl[m_LoopLvl] = label; - m_LoopLvl++; -} - -// adds a level (switch statement) -void CBotInstr::IncLvl() -{ - m_labelLvl.SetSize(m_LoopLvl+1); - m_labelLvl[m_LoopLvl] = "#SWITCH"; - m_LoopLvl++; -} - -// free a level -void CBotInstr::DecLvl() -{ - m_LoopLvl--; - m_labelLvl[m_LoopLvl].Empty(); -} - -// control validity of break and continue -bool CBotInstr::ChkLvl(const CBotString& label, int type) -{ - int i = m_LoopLvl; - while (--i>=0) - { - if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; - if (label.IsEmpty()) return true; - if (m_labelLvl[i] == label) return true; - } - return false; -} - -bool CBotInstr::IsOfClass(CBotString n) -{ - return name == n; -} - - -//////////////////////////////////////////////////////////////////////////// -// database management class CBotInstr - -// set the token corresponding to the instruction - -void CBotInstr::SetToken(CBotToken* p) -{ - m_token = *p; -} - -// return the type of the token assicated with the instruction - -int CBotInstr::GetTokenType() -{ - return m_token.GetType(); -} - -// return associated token - -CBotToken* CBotInstr::GetToken() -{ - return &m_token; -} - -// adds the statement following the other - -void CBotInstr::AddNext(CBotInstr* n) -{ - CBotInstr* p = this; - while (p->m_next != nullptr) p = p->m_next; - p->m_next = n; -} - -void CBotInstr::AddNext3(CBotInstr* n) -{ - CBotInstr* p = this; - while (p->m_next3 != nullptr) p = p->m_next3; - p->m_next3 = n; -} - -void CBotInstr::AddNext3b(CBotInstr* n) -{ - CBotInstr* p = this; - while (p->m_next3b != nullptr) p = p->m_next3b; - p->m_next3b = n; -} - -// returns next statement - -CBotInstr* CBotInstr::GetNext() -{ - return m_next; -} - -CBotInstr* CBotInstr::GetNext3() -{ - return m_next3; -} - -CBotInstr* CBotInstr::GetNext3b() -{ - return m_next3b; -} - -/////////////////////////////////////////////////////////////////////////// -// compile an instruction which can be -// while, do, try, throw, if, for, switch, break, continue, return -// int, float, boolean, string, -// declaration of an instance of a class -// arbitrary expression - - -CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) -{ - CBotToken* pp = p; - - if (p == nullptr) return nullptr; - - int type = p->GetType(); // what is the next token - - // is it a lable? - if (IsOfType(pp, TokenTypVar) && - IsOfType(pp, ID_DOTS)) - { - type = pp->GetType(); - // these instructions accept only lable - if (!IsOfTypeList(pp, ID_WHILE, ID_FOR, ID_DO, 0)) - { - pStack->SetError(TX_LABEL, pp->GetStart()); - return nullptr; - } - } - - // call routine corresponding to the compilation token found - switch (type) - { - case ID_WHILE: - return CBotWhile::Compile(p, pStack); - - case ID_FOR: - return CBotFor::Compile(p, pStack); - - case ID_DO: - return CBotDo::Compile(p, pStack); - - case ID_BREAK: - case ID_CONTINUE: - return CBotBreak::Compile(p, pStack); - - case ID_SWITCH: - return CBotSwitch::Compile(p, pStack); - - case ID_TRY: - return CBotTry::Compile(p, pStack); - - case ID_THROW: - return CBotThrow::Compile(p, pStack); - - case ID_INT: - return CBotInt::Compile(p, pStack); - - case ID_FLOAT: - return CBotFloat::Compile(p, pStack); - - case ID_STRING: - return CBotIString::Compile(p, pStack); - - case ID_BOOLEAN: - case ID_BOOL: - return CBotBoolean::Compile(p, pStack); - - case ID_IF: - return CBotIf::Compile(p, pStack); - - case ID_RETURN: - return CBotReturn::Compile(p, pStack); - - case ID_ELSE: - pStack->SetStartError(p->GetStart()); - pStack->SetError(TX_ELSEWITHOUTIF, p->GetEnd()); - return nullptr; - - case ID_CASE: - pStack->SetStartError(p->GetStart()); - pStack->SetError(TX_OUTCASE, p->GetEnd()); - return nullptr; - } - - pStack->SetStartError(p->GetStart()); - - // should not be a reserved word DefineNum - if (p->GetType() == TokenTypDef) - { - pStack->SetError(TX_RESERVED, p); - return nullptr; - } - - // this might be an instance of class definnition - CBotToken* ppp = p; - if (IsOfType(ppp, TokenTypVar)) - { - if (CBotClass::Find(p) != nullptr) - { - // yes, compiles the declaration of the instance - return CBotClassInst::Compile(p, pStack); - } - } - - // this can be an arythmetic instruction - CBotInstr* inst = CBotExpression::Compile(p, pStack); - if (IsOfType(p, ID_SEP)) - { - return inst; - } - pStack->SetError(TX_ENDOF, p->GetStart()); - delete inst; - return nullptr; -} - -bool CBotInstr::Execute(CBotStack* &pj) -{ - CBotString ClassManquante = name; - assert(0); // should never go through this routine - // but use the routines of the subclasses - return false; -} - -bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) -{ - if (!Execute(pj)) return false; - pVar->SetVal(pj->GetVar()); - return true; -} - -void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) -{ - CBotString ClassManquante = name; - assert(0); // should never go through this routine - // but use the routines of the subclasses -} - - -bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) -{ - assert(0); // dad do not know, see the girls - return false; -} - -bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) -{ - assert(0); // dad do not know, see the girls - return false; -} - -void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) -{ - assert(0); // dad do not know, see the girls -} - -// this routine is defined only for the subclass CBotCase -// this allows to make the call on all instructions CompCase -// to see if it's a case to the desired value. - -bool CBotInstr::CompCase(CBotStack* &pj, int val) -{ - return false; -} - -CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) -{ - if (IsOfType(p, ID_OPBRK)) - { - if (!IsOfType(p, ID_CLBRK)) - { - pStack->SetError(TX_CLBRK, p->GetStart()); - return nullptr; - } - - CBotInstr* inst = CompileArray(p, pStack, CBotTypResult(CBotTypArrayPointer, type), false); - if (inst != nullptr || !pStack->IsOk()) return inst; - } - - // compiles an array declaration - if (first) return nullptr ; - - CBotInstr* inst = CBotInstArray::Compile(p, pStack, type); - if (inst == nullptr) return nullptr; - - if (IsOfType(p, ID_COMMA)) // several definitions - { - if (nullptr != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false))) // compiles next one - { - return inst; - } - delete inst; - return nullptr; - } - - if (IsOfType(p, ID_SEP)) // end of instruction - { - return inst; - } - - delete inst; - pStack->SetError(TX_ENDOF, p->GetStart()); - return nullptr; -} - ////////////////////////////////////////////////////////////////////////////////////////// // compile a list of parameters diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index e42d1851..71c2279c 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -54,96 +54,6 @@ class CBotDefParam; // paramerer list of a function extern bool SaveVar(FILE* pf, CBotVar* pVar); - -///////////////////////////////////////////////////////////////////// -// class defining an instruction -class CBotInstr -{ -private: - static - CBotStringArray - m_labelLvl; -protected: - CBotToken m_token; // keeps the token - CBotString name; // debug - CBotInstr* m_next; // linked command - CBotInstr* m_next2b; // second list definition chain - CBotInstr* m_next3; // third list for indices and fields - CBotInstr* m_next3b; // necessary for reporting tables -/* - for example, the following program - int x[]; x[1] = 4; - int y[x[1]][10], z; - is generated - CBotInstrArray - m_next3b-> CBotEmpty - m_next-> - CBotExpression .... - m_next-> - CBotInstrArray - m_next3b-> CBotExpression ('x') ( m_next3-> CBotIndexExpr ('1') ) - m_next3b-> CBotExpression ('10') - m_next2-> 'z' - m_next->... - -*/ - - static - int m_LoopLvl; - friend class CBotClassInst; - friend class CBotInt; - friend class CBotListArray; - -public: - CBotInstr(); - virtual - ~CBotInstr(); - - static - CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack); - static - CBotInstr* CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first = true); - - virtual - bool Execute(CBotStack* &pj); - virtual - bool Execute(CBotStack* &pj, CBotVar* pVar); - virtual - void RestoreState(CBotStack* &pj, bool bMain); - - virtual - bool ExecuteVar(CBotVar* &pVar, CBotCStack* &pile); - virtual - bool ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend); - virtual - void RestoreStateVar(CBotStack* &pile, bool bMain); - - virtual - bool CompCase(CBotStack* &pj, int val); - - void SetToken(CBotToken* p); - int GetTokenType(); - CBotToken* GetToken(); - - void AddNext(CBotInstr* n); - CBotInstr* GetNext(); - void AddNext3(CBotInstr* n); - CBotInstr* GetNext3(); - void AddNext3b(CBotInstr* n); - CBotInstr* GetNext3b(); - - static - void IncLvl(CBotString& label); - static - void IncLvl(); - static - void DecLvl(); - static - bool ChkLvl(const CBotString& label, int type); - - bool IsOfClass(CBotString name); -}; - #define MAX(a,b) ((a>b) ? a : b) extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index d09e3ca2..ae72dbbb 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -21,6 +21,8 @@ // Modules inlcude +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBlock.h b/src/CBot/CBotInstr/CBotBlock.h index f413e756..6c7c6334 100644 --- a/src/CBot/CBotInstr/CBotBlock.h +++ b/src/CBot/CBotInstr/CBotBlock.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.h b/src/CBot/CBotInstr/CBotBoolExpr.h index df7354ee..e50d8a63 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.h +++ b/src/CBot/CBotInstr/CBotBoolExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBoolean.h b/src/CBot/CBotInstr/CBotBoolean.h index 57674200..217daadc 100644 --- a/src/CBot/CBotInstr/CBotBoolean.h +++ b/src/CBot/CBotInstr/CBotBoolean.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBreak.h b/src/CBot/CBotInstr/CBotBreak.h index 0ad39408..ae35a70f 100644 --- a/src/CBot/CBotInstr/CBotBreak.h +++ b/src/CBot/CBotInstr/CBotBreak.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCase.h b/src/CBot/CBotInstr/CBotCase.h index 9ba1b86f..e5043330 100644 --- a/src/CBot/CBotInstr/CBotCase.h +++ b/src/CBot/CBotInstr/CBotCase.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCatch.h b/src/CBot/CBotInstr/CBotCatch.h index 9756c8b8..d4450ec1 100644 --- a/src/CBot/CBotInstr/CBotCatch.h +++ b/src/CBot/CBotInstr/CBotCatch.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotClassInst.h b/src/CBot/CBotInstr/CBotClassInst.h index 370a46cd..3ee791f0 100644 --- a/src/CBot/CBotInstr/CBotClassInst.h +++ b/src/CBot/CBotInstr/CBotClassInst.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCondition.h b/src/CBot/CBotInstr/CBotCondition.h index 08996db1..3958cadb 100644 --- a/src/CBot/CBotInstr/CBotCondition.h +++ b/src/CBot/CBotInstr/CBotCondition.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotDo.h b/src/CBot/CBotInstr/CBotDo.h index 670a0701..11fe40f4 100644 --- a/src/CBot/CBotInstr/CBotDo.h +++ b/src/CBot/CBotInstr/CBotDo.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotEmpty.h b/src/CBot/CBotInstr/CBotEmpty.h index 04f9fafd..dc50b49b 100644 --- a/src/CBot/CBotInstr/CBotEmpty.h +++ b/src/CBot/CBotInstr/CBotEmpty.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.h b/src/CBot/CBotInstr/CBotExprAlpha.h index 3f820607..3cadc322 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.h +++ b/src/CBot/CBotInstr/CBotExprAlpha.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprBool.h b/src/CBot/CBotInstr/CBotExprBool.h index a2609e26..90f53daa 100644 --- a/src/CBot/CBotInstr/CBotExprBool.h +++ b/src/CBot/CBotInstr/CBotExprBool.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNan.h b/src/CBot/CBotInstr/CBotExprNan.h index 0d6bbf6f..1f0d58d8 100644 --- a/src/CBot/CBotInstr/CBotExprNan.h +++ b/src/CBot/CBotInstr/CBotExprNan.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNull.h b/src/CBot/CBotInstr/CBotExprNull.h index 187cb261..90b3f89a 100644 --- a/src/CBot/CBotInstr/CBotExprNull.h +++ b/src/CBot/CBotInstr/CBotExprNull.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprNum.h b/src/CBot/CBotInstr/CBotExprNum.h index dc213319..07fca7e9 100644 --- a/src/CBot/CBotInstr/CBotExprNum.h +++ b/src/CBot/CBotInstr/CBotExprNum.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprUnaire.h b/src/CBot/CBotInstr/CBotExprUnaire.h index 25d21251..75b30a94 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.h +++ b/src/CBot/CBotInstr/CBotExprUnaire.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExprVar.h b/src/CBot/CBotInstr/CBotExprVar.h index a4535659..638cf8fe 100644 --- a/src/CBot/CBotInstr/CBotExprVar.h +++ b/src/CBot/CBotInstr/CBotExprVar.h @@ -24,6 +24,8 @@ #include "CBotDefines.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotExpression.h b/src/CBot/CBotInstr/CBotExpression.h index d25fd39c..ddf2e257 100644 --- a/src/CBot/CBotInstr/CBotExpression.h +++ b/src/CBot/CBotInstr/CBotExpression.h @@ -21,8 +21,11 @@ // Modules inlcude #include "CBot.h" + #include "CBotLeftExpr.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.h b/src/CBot/CBotInstr/CBotFieldExpr.h index 13f9fc37..41e828bd 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.h +++ b/src/CBot/CBotInstr/CBotFieldExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFloat.h b/src/CBot/CBotInstr/CBotFloat.h index 6223071c..309697b0 100644 --- a/src/CBot/CBotInstr/CBotFloat.h +++ b/src/CBot/CBotInstr/CBotFloat.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFor.h b/src/CBot/CBotInstr/CBotFor.h index 01edc666..d7d4256d 100644 --- a/src/CBot/CBotInstr/CBotFor.h +++ b/src/CBot/CBotInstr/CBotFor.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotFunction.h b/src/CBot/CBotInstr/CBotFunction.h index 486ffff8..f1fc3c8d 100644 --- a/src/CBot/CBotInstr/CBotFunction.h +++ b/src/CBot/CBotInstr/CBotFunction.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIString.h b/src/CBot/CBotInstr/CBotIString.h index 1d91ea26..2ea8e4ee 100644 --- a/src/CBot/CBotInstr/CBotIString.h +++ b/src/CBot/CBotInstr/CBotIString.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIf.h b/src/CBot/CBotInstr/CBotIf.h index 6ea6ee9c..e39e5226 100644 --- a/src/CBot/CBotInstr/CBotIf.h +++ b/src/CBot/CBotInstr/CBotIf.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.h b/src/CBot/CBotInstr/CBotIndexExpr.h index 730a4de4..a8e7e07a 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.h +++ b/src/CBot/CBotInstr/CBotIndexExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstArray.h b/src/CBot/CBotInstr/CBotInstArray.h index ca2cbe07..4ad025d6 100644 --- a/src/CBot/CBotInstr/CBotInstArray.h +++ b/src/CBot/CBotInstr/CBotInstArray.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstr.cpp b/src/CBot/CBotInstr/CBotInstr.cpp new file mode 100644 index 00000000..7d376f7c --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstr.cpp @@ -0,0 +1,374 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotInstr.h" + +#include "CBotInstr/CBotFor.h" +#include "CBotInstr/CBotDo.h" +#include "CBotInstr/CBotBreak.h" +#include "CBotInstr/CBotSwitch.h" +#include "CBotInstr/CBotTry.h" +#include "CBotInstr/CBotThrow.h" +#include "CBotInstr/CBotInt.h" +#include "CBotInstr/CBotFloat.h" +#include "CBotInstr/CBotWhile.h" +#include "CBotInstr/CBotIString.h" +#include "CBotInstr/CBotBoolean.h" +#include "CBotInstr/CBotIf.h" +#include "CBotInstr/CBotReturn.h" +#include "CBotInstr/CBotClassInst.h" +#include "CBotInstr/CBotExpression.h" +#include "CBotInstr/CBotInstArray.h" + +#include "CBotVar/CBotVar.h" + +#include "CBotClass.h" +#include "CBotStack.h" + + +// Local include + +// Global include +#include + +//////////////////////////////////////////////////////////////////////////////// +int CBotInstr::m_LoopLvl = 0; +CBotStringArray CBotInstr::m_labelLvl = CBotStringArray(); + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr::CBotInstr() +{ + name = "CBotInstr"; + m_next = nullptr; + m_next2b = nullptr; + m_next3 = nullptr; + m_next3b = nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr::~CBotInstr() +{ + delete m_next; + delete m_next2b; + delete m_next3; + delete m_next3b; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::IncLvl(CBotString& label) +{ + m_labelLvl.SetSize(m_LoopLvl+1); + m_labelLvl[m_LoopLvl] = label; + m_LoopLvl++; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::IncLvl() +{ + m_labelLvl.SetSize(m_LoopLvl+1); + m_labelLvl[m_LoopLvl] = "#SWITCH"; + m_LoopLvl++; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::DecLvl() +{ + m_LoopLvl--; + m_labelLvl[m_LoopLvl].Empty(); +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::ChkLvl(const CBotString& label, int type) +{ + int i = m_LoopLvl; + while (--i>=0) + { + if ( type == ID_CONTINUE && m_labelLvl[i] == "#SWITCH") continue; + if (label.IsEmpty()) return true; + if (m_labelLvl[i] == label) return true; + } + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::IsOfClass(CBotString n) +{ + return name == n; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::SetToken(CBotToken* p) +{ + m_token = *p; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotInstr::GetTokenType() +{ + return m_token.GetType(); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotToken* CBotInstr::GetToken() +{ + return &m_token; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::AddNext(CBotInstr* n) +{ + CBotInstr* p = this; + while (p->m_next != nullptr) p = p->m_next; + p->m_next = n; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::AddNext3(CBotInstr* n) +{ + CBotInstr* p = this; + while (p->m_next3 != nullptr) p = p->m_next3; + p->m_next3 = n; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::AddNext3b(CBotInstr* n) +{ + CBotInstr* p = this; + while (p->m_next3b != nullptr) p = p->m_next3b; + p->m_next3b = n; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstr::GetNext() +{ + return m_next; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstr::GetNext3() +{ + return m_next3; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstr::GetNext3b() +{ + return m_next3b; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstr::Compile(CBotToken* &p, CBotCStack* pStack) +{ + CBotToken* pp = p; + + if (p == nullptr) return nullptr; + + int type = p->GetType(); // what is the next token + + // is it a lable? + if (IsOfType(pp, TokenTypVar) && + IsOfType(pp, ID_DOTS)) + { + type = pp->GetType(); + // these instructions accept only lable + if (!IsOfTypeList(pp, ID_WHILE, ID_FOR, ID_DO, 0)) + { + pStack->SetError(TX_LABEL, pp->GetStart()); + return nullptr; + } + } + + // call routine corresponding to the compilation token found + switch (type) + { + case ID_WHILE: + return CBotWhile::Compile(p, pStack); + + case ID_FOR: + return CBotFor::Compile(p, pStack); + + case ID_DO: + return CBotDo::Compile(p, pStack); + + case ID_BREAK: + case ID_CONTINUE: + return CBotBreak::Compile(p, pStack); + + case ID_SWITCH: + return CBotSwitch::Compile(p, pStack); + + case ID_TRY: + return CBotTry::Compile(p, pStack); + + case ID_THROW: + return CBotThrow::Compile(p, pStack); + + case ID_INT: + return CBotInt::Compile(p, pStack); + + case ID_FLOAT: + return CBotFloat::Compile(p, pStack); + + case ID_STRING: + return CBotIString::Compile(p, pStack); + + case ID_BOOLEAN: + case ID_BOOL: + return CBotBoolean::Compile(p, pStack); + + case ID_IF: + return CBotIf::Compile(p, pStack); + + case ID_RETURN: + return CBotReturn::Compile(p, pStack); + + case ID_ELSE: + pStack->SetStartError(p->GetStart()); + pStack->SetError(TX_ELSEWITHOUTIF, p->GetEnd()); + return nullptr; + + case ID_CASE: + pStack->SetStartError(p->GetStart()); + pStack->SetError(TX_OUTCASE, p->GetEnd()); + return nullptr; + } + + pStack->SetStartError(p->GetStart()); + + // should not be a reserved word DefineNum + if (p->GetType() == TokenTypDef) + { + pStack->SetError(TX_RESERVED, p); + return nullptr; + } + + // this might be an instance of class definnition + CBotToken* ppp = p; + if (IsOfType(ppp, TokenTypVar)) + { + if (CBotClass::Find(p) != nullptr) + { + // yes, compiles the declaration of the instance + return CBotClassInst::Compile(p, pStack); + } + } + + // this can be an arythmetic instruction + CBotInstr* inst = CBotExpression::Compile(p, pStack); + if (IsOfType(p, ID_SEP)) + { + return inst; + } + pStack->SetError(TX_ENDOF, p->GetStart()); + delete inst; + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::Execute(CBotStack* &pj) +{ + CBotString ClassManquante = name; + assert(0); // should never go through this routine + // but use the routines of the subclasses + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::Execute(CBotStack* &pj, CBotVar* pVar) +{ + if (!Execute(pj)) return false; + pVar->SetVal(pj->GetVar()); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::RestoreState(CBotStack* &pj, bool bMain) +{ + CBotString ClassManquante = name; + assert(0); // should never go through this routine + // but use the routines of the subclasses +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotCStack* &pile) +{ + assert(0); // dad do not know, see the girls + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prevToken, bool bStep, bool bExtend) +{ + assert(0); // dad do not know, see the girls + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotInstr::RestoreStateVar(CBotStack* &pile, bool bMain) +{ + assert(0); // dad do not know, see the girls +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotInstr::CompCase(CBotStack* &pj, int val) +{ + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CBotInstr::CompileArray(CBotToken* &p, CBotCStack* pStack, CBotTypResult type, bool first) +{ + if (IsOfType(p, ID_OPBRK)) + { + if (!IsOfType(p, ID_CLBRK)) + { + pStack->SetError(TX_CLBRK, p->GetStart()); + return nullptr; + } + + CBotInstr* inst = CompileArray(p, pStack, CBotTypResult(CBotTypArrayPointer, type), false); + if (inst != nullptr || !pStack->IsOk()) return inst; + } + + // compiles an array declaration + if (first) return nullptr ; + + CBotInstr* inst = CBotInstArray::Compile(p, pStack, type); + if (inst == nullptr) return nullptr; + + if (IsOfType(p, ID_COMMA)) // several definitions + { + if (nullptr != ( inst->m_next2b = CBotInstArray::CompileArray(p, pStack, type, false))) // compiles next one + { + return inst; + } + delete inst; + return nullptr; + } + + if (IsOfType(p, ID_SEP)) // end of instruction + { + return inst; + } + + delete inst; + pStack->SetError(TX_ENDOF, p->GetStart()); + return nullptr; +} diff --git a/src/CBot/CBotInstr/CBotInstr.h b/src/CBot/CBotInstr/CBotInstr.h new file mode 100644 index 00000000..1f25115e --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstr.h @@ -0,0 +1,266 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotToken.h" + +#include "CBotCStack.h" + +// Local include + +// Global include + +/* + for example, the following program + int x[]; x[1] = 4; + int y[x[1]][10], z; + is generated + CBotInstrArray + m_next3b-> CBotEmpty + m_next-> + CBotExpression .... + m_next-> + CBotInstrArray + m_next3b-> CBotExpression ('x') ( m_next3-> CBotIndexExpr ('1') ) + m_next3b-> CBotExpression ('10') + m_next2-> 'z' + m_next->... + +*/ +/*! + * \brief The CBotInstr class Class defining an instruction. + */ +class CBotInstr +{ +public: + + /*! + * \brief CBotInstr + */ + CBotInstr(); + + /*! + * \brief ~CBotInstr + */ + virtual ~CBotInstr(); + + /*! + * \brief Compile Compile an instruction which can be while, do, try, + * throw, if, for, switch, break, continue, return, int, float, boolean, + * string, declaration of an instance of a class, arbitrary expression. + * \param p + * \param pStack + * \return + */ + static CBotInstr* Compile(CBotToken* &p, + CBotCStack* pStack); + + /*! + * \brief CompileArray + * \param p + * \param pStack + * \param type + * \param first + * \return + */ + static CBotInstr* CompileArray(CBotToken* &p, + CBotCStack* pStack, + CBotTypResult type, + bool first = true); + + /*! + * \brief Execute + * \param pj + * \return + */ + virtual bool Execute(CBotStack* &pj); + + /*! + * \brief Execute + * \param pj + * \param pVar + * \return + */ + virtual bool Execute(CBotStack* &pj, + CBotVar* pVar); + + /*! + * \brief RestoreState + * \param pj + * \param bMain + */ + virtual void RestoreState(CBotStack* &pj, + bool bMain); + + /*! + * \brief ExecuteVar + * \param pVar + * \param pile + * \return + */ + virtual bool ExecuteVar(CBotVar* &pVar, + CBotCStack* &pile); + + /*! + * \brief ExecuteVar + * \param pVar + * \param pile + * \param prevToken + * \param bStep + * \param bExtend + * \return + */ + virtual bool ExecuteVar(CBotVar* &pVar, + CBotStack* &pile, + CBotToken* prevToken, + bool bStep, + bool bExtend); + + /*! + * \brief RestoreStateVar + * \param pile + * \param bMain + */ + virtual void RestoreStateVar(CBotStack* &pile, + bool bMain); + + /*! + * \brief CompCase This routine is defined only for the subclass CBotCase + * this allows to make the call on all instructions CompCase to see if it's + * a case to the desired value.. + * \param pj + * \param val + * \return + */ + virtual bool CompCase(CBotStack* &pj, + int val); + + /*! + * \brief SetToken Set the token corresponding to the instruction. + * \param p + */ + void SetToken(CBotToken* p); + + /*! + * \brief GetTokenType Return the type of the token assicated with the + * instruction. + * \return + */ + int GetTokenType(); + + /*! + * \brief GetToken Return associated token. + * \return + */ + CBotToken* GetToken(); + + /*! + * \brief AddNext Adds the statement following the other. + * \param n + */ + void AddNext(CBotInstr* n); + + /*! + * \brief GetNext Returns next statement. + * \return + */ + CBotInstr* GetNext(); + + /*! + * \brief AddNext3 + * \param n + */ + void AddNext3(CBotInstr* n); + + /*! + * \brief GetNext3 + * \return + */ + CBotInstr* GetNext3(); + + /*! + * \brief AddNext3b + * \param n + */ + void AddNext3b(CBotInstr* n); + + /*! + * \brief GetNext3b + * \return + */ + CBotInstr* GetNext3b(); + + /*! + * \brief IncLvl Adds a level with a label. + * \param label + */ + static void IncLvl(CBotString& label); + + /*! + * \brief IncLvl Adds a level (switch statement). + */ + static void IncLvl(); + + /*! + * \brief DecLvl Free a level. + */ + static void DecLvl(); + + /*! + * \brief ChkLvl Control validity of break and continue. + * \param label + * \param type + * \return + */ + static bool ChkLvl(const CBotString& label, int type); + + /*! + * \brief IsOfClass + * \param name + * \return + */ + bool IsOfClass(CBotString name); + +protected: + + //! Keeps the token. + CBotToken m_token; + //! Debug. + CBotString name; + //! Linked command. + CBotInstr* m_next; + //! Second list definition chain. + CBotInstr* m_next2b; + //! Third list for indices and fields. + CBotInstr* m_next3; + //! Necessary for reporting tables. + CBotInstr* m_next3b; + + //! Counter of nested loops, to determine the break and continue valid. + static int m_LoopLvl; + friend class CBotClassInst; + friend class CBotInt; + friend class CBotListArray; + +private: + //! List of labels used. + static CBotStringArray m_labelLvl; +}; diff --git a/src/CBot/CBotInstr/CBotInstrCall.h b/src/CBot/CBotInstr/CBotInstrCall.h index 7e194f8a..fa634fed 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.h +++ b/src/CBot/CBotInstr/CBotInstrCall.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.h b/src/CBot/CBotInstr/CBotInstrMethode.h index ca4ad90b..ea398115 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.h +++ b/src/CBot/CBotInstr/CBotInstrMethode.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotInt.h b/src/CBot/CBotInstr/CBotInt.h index 702f5290..085e6265 100644 --- a/src/CBot/CBotInstr/CBotInt.h +++ b/src/CBot/CBotInstr/CBotInt.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.h b/src/CBot/CBotInstr/CBotLeftExpr.h index f0dba0b4..aaf9ee47 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.h +++ b/src/CBot/CBotInstr/CBotLeftExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.h b/src/CBot/CBotInstr/CBotLeftExprVar.h index a264dbae..fd693727 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.h +++ b/src/CBot/CBotInstr/CBotLeftExprVar.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListArray.h b/src/CBot/CBotInstr/CBotListArray.h index 921273d1..586cc864 100644 --- a/src/CBot/CBotInstr/CBotListArray.h +++ b/src/CBot/CBotInstr/CBotListArray.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListExpression.h b/src/CBot/CBotInstr/CBotListExpression.h index c4e67115..451b2181 100644 --- a/src/CBot/CBotInstr/CBotListExpression.h +++ b/src/CBot/CBotInstr/CBotListExpression.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotListInstr.h b/src/CBot/CBotInstr/CBotListInstr.h index 763c3a88..0aafaf59 100644 --- a/src/CBot/CBotInstr/CBotListInstr.h +++ b/src/CBot/CBotInstr/CBotListInstr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotLogicExpr.h b/src/CBot/CBotInstr/CBotLogicExpr.h index 2bffc243..db6c67f6 100644 --- a/src/CBot/CBotInstr/CBotLogicExpr.h +++ b/src/CBot/CBotInstr/CBotLogicExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotNew.h b/src/CBot/CBotInstr/CBotNew.h index 77e2b2c6..02598b1a 100644 --- a/src/CBot/CBotInstr/CBotNew.h +++ b/src/CBot/CBotInstr/CBotNew.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotParExpr.h b/src/CBot/CBotInstr/CBotParExpr.h index 60c09933..690d6efb 100644 --- a/src/CBot/CBotInstr/CBotParExpr.h +++ b/src/CBot/CBotInstr/CBotParExpr.h @@ -22,7 +22,7 @@ // Modules inlcude #include "CBot.h" -#include "CBotStack.h" +#include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.h b/src/CBot/CBotInstr/CBotPostIncExpr.h index 5a3240c8..3a30fcef 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.h +++ b/src/CBot/CBotInstr/CBotPostIncExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.h b/src/CBot/CBotInstr/CBotPreIncExpr.h index ede694d4..896c0c9a 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.h +++ b/src/CBot/CBotInstr/CBotPreIncExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotReturn.h b/src/CBot/CBotInstr/CBotReturn.h index 978c6c10..1816bf2d 100644 --- a/src/CBot/CBotInstr/CBotReturn.h +++ b/src/CBot/CBotInstr/CBotReturn.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotSwitch.h b/src/CBot/CBotInstr/CBotSwitch.h index c3f5e02c..b4cff5e4 100644 --- a/src/CBot/CBotInstr/CBotSwitch.h +++ b/src/CBot/CBotInstr/CBotSwitch.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotThrow.h b/src/CBot/CBotInstr/CBotThrow.h index 997550aa..b5227ad7 100644 --- a/src/CBot/CBotInstr/CBotThrow.h +++ b/src/CBot/CBotInstr/CBotThrow.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotTry.h b/src/CBot/CBotInstr/CBotTry.h index 0a8d2b9a..bdc3d4d8 100644 --- a/src/CBot/CBotInstr/CBotTry.h +++ b/src/CBot/CBotInstr/CBotTry.h @@ -21,6 +21,8 @@ #include "CBot.h" #include "CBotCatch.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.h b/src/CBot/CBotInstr/CBotTwoOpExpr.h index 1bcc2f8f..720755fc 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.h +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotWhile.h b/src/CBot/CBotInstr/CBotWhile.h index 5d63909a..528fe8f1 100644 --- a/src/CBot/CBotInstr/CBotWhile.h +++ b/src/CBot/CBotInstr/CBotWhile.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index f07fbe9f..4d6ee787 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -21,11 +21,11 @@ #include "CBotVarClass.h" #include "CBotClass.h" - #include "CBotStack.h" - #include "CBotDefines.h" +#include "CBotInstr/CBotInstr.h" + // Local include // Global include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index e69beaea..80cb2d0e 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES CBotUtils.cpp CBotDefParam.cpp CBotCallMethode.cpp + CBotInstr/CBotInstr.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp From 67dff4ef6506755eb7312f0ec305d69f56d2a04d Mon Sep 17 00:00:00 2001 From: Grunaka Date: Mon, 16 Nov 2015 22:17:33 +0100 Subject: [PATCH 66/91] Moving CBotStringArray class in its own header and source files. --- src/CBot/CBotDll.h | 21 ----- src/CBot/CBotProgram.h | 1 + src/CBot/CBotString.cpp | 144 ----------------------------------- src/CBot/CBotStringArray.cpp | 136 +++++++++++++++++++++++++++++++++ src/CBot/CBotStringArray.h | 85 +++++++++++++++++++++ src/CBot/CBotToken.h | 2 + src/CBot/CBotUtils.cpp | 20 +++++ src/CBot/CBotUtils.h | 42 ++++++++++ src/CBot/CMakeLists.txt | 1 + 9 files changed, 287 insertions(+), 165 deletions(-) create mode 100644 src/CBot/CBotStringArray.cpp create mode 100644 src/CBot/CBotStringArray.h diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 56b43642..16b400e3 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -419,27 +419,6 @@ private: static const char * MapIdToString(EID id); }; - -// Class used to array management - -class CBotStringArray : public CBotString -{ -private: - int m_nSize; // number of elements - int m_nMaxSize; // reserved size - CBotString* m_pData; // ^data - -public: - CBotStringArray(); - ~CBotStringArray(); - void SetSize(int nb); - int GetSize(); - void Add(const CBotString& str); - CBotString& operator[](int nIndex); - - CBotString& ElementAt(int nIndex); -}; - /////////////////////////////////////////////////////////////////////////////// // routines for file management (* FILE) FILE* fOpen(const char* name, const char* mode); diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index 02aa5372..bef08a8c 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -20,6 +20,7 @@ #pragma once // Modules inlcude +#include "CBotStringArray.h" // Local include diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 1f31f7f2..12cc546b 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -524,147 +524,3 @@ const char * CBotString::MapIdToString(EID id) return emptyString; } } - -/////////////////////////////////////////////////////////////////////////////////////////// -// arrays of strings - -CBotStringArray::CBotStringArray() -{ - m_pData = nullptr; - m_nSize = m_nMaxSize = 0; -} - -CBotStringArray::~CBotStringArray() -{ - SetSize(0); // destroys data ! -} - - -int CBotStringArray::GetSize() -{ - return m_nSize; -} - -void CBotStringArray::Add(const CBotString& str) -{ - SetSize(m_nSize+1); - - m_pData[m_nSize-1] = str; -} - -/////////////////////////////////////////////////////////////////////// -// utility routines - -static inline void ConstructElement(CBotString* pNewData) -{ - memset(pNewData, 0, sizeof(CBotString)); -} - -static inline void DestructElement(CBotString* pOldData) -{ - pOldData->~CBotString(); -} - -static inline void CopyElement(CBotString* pSrc, CBotString* pDest) -{ - *pSrc = *pDest; -} - -static void ConstructElements(CBotString* pNewData, int nCount) -{ - while (nCount--) - { - ConstructElement(pNewData); - pNewData++; - } -} - -static void DestructElements(CBotString* pOldData, int nCount) -{ - while (nCount--) - { - DestructElement(pOldData); - pOldData++; - } -} - -// set the array size - -void CBotStringArray::SetSize(int nNewSize) -{ - if (nNewSize == 0) - { - // shrink to nothing - - DestructElements(m_pData, m_nSize); - delete[] reinterpret_cast(m_pData); - m_pData = nullptr; - m_nSize = m_nMaxSize = 0; - } - else if (m_pData == nullptr) - { - // create one with exact size - m_pData = reinterpret_cast (new unsigned char[nNewSize * sizeof(CBotString)]); - - ConstructElements(m_pData, nNewSize); - - m_nSize = m_nMaxSize = nNewSize; - } - else if (nNewSize <= m_nMaxSize) - { - // it fits - if (nNewSize > m_nSize) - { - // initialize the new elements - - ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); - - } - - else if (m_nSize > nNewSize) // destroy the old elements - DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); - - m_nSize = nNewSize; - } - else - { - // otherwise, grow array - int nGrowBy; - { - // heuristically determine growth when nGrowBy == 0 - // (this avoids heap fragmentation in many situations) - nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); - } - int nNewMax; - if (nNewSize < m_nMaxSize + nGrowBy) - nNewMax = m_nMaxSize + nGrowBy; // granularity - else - nNewMax = nNewSize; // no slush - - CBotString* pNewData = reinterpret_cast (new unsigned char[nNewMax * sizeof(CBotString)]); - - // copy new data from old - memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); - - // construct remaining elements - ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); - - - // Get rid of old stuff (note: no destructors called) - delete[] reinterpret_cast(m_pData); - m_pData = pNewData; - m_nSize = nNewSize; - m_nMaxSize = nNewMax; - } -} - - -CBotString& CBotStringArray::operator[](int nIndex) -{ - return ElementAt(nIndex); -} - -CBotString& CBotStringArray::ElementAt(int nIndex) -{ - return m_pData[nIndex]; -} diff --git a/src/CBot/CBotStringArray.cpp b/src/CBot/CBotStringArray.cpp new file mode 100644 index 00000000..ecf53bf0 --- /dev/null +++ b/src/CBot/CBotStringArray.cpp @@ -0,0 +1,136 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotStringArray.h" + +#include "CBotUtils.h" + +// Local include + +// Global include + + +//////////////////////////////////////////////////////////////////////////////// +CBotStringArray::CBotStringArray() +{ + m_pData = nullptr; + m_nSize = m_nMaxSize = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotStringArray::~CBotStringArray() +{ + SetSize(0); // destroys data ! +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotStringArray::GetSize() +{ + return m_nSize; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotStringArray::Add(const CBotString& str) +{ + SetSize(m_nSize+1); + + m_pData[m_nSize-1] = str; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotStringArray::SetSize(int nNewSize) +{ + if (nNewSize == 0) + { + // shrink to nothing + + DestructElements(m_pData, m_nSize); + delete[] reinterpret_cast(m_pData); + m_pData = nullptr; + m_nSize = m_nMaxSize = 0; + } + else if (m_pData == nullptr) + { + // create one with exact size + m_pData = reinterpret_cast (new unsigned char[nNewSize * sizeof(CBotString)]); + + ConstructElements(m_pData, nNewSize); + + m_nSize = m_nMaxSize = nNewSize; + } + else if (nNewSize <= m_nMaxSize) + { + // it fits + if (nNewSize > m_nSize) + { + // initialize the new elements + + ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); + + } + + else if (m_nSize > nNewSize) // destroy the old elements + DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); + + m_nSize = nNewSize; + } + else + { + // otherwise, grow array + int nGrowBy; + { + // heuristically determine growth when nGrowBy == 0 + // (this avoids heap fragmentation in many situations) + nGrowBy = std::min(1024, std::max(4, m_nSize / 8)); + } + int nNewMax; + if (nNewSize < m_nMaxSize + nGrowBy) + nNewMax = m_nMaxSize + nGrowBy; // granularity + else + nNewMax = nNewSize; // no slush + + CBotString* pNewData = reinterpret_cast (new unsigned char[nNewMax * sizeof(CBotString)]); + + // copy new data from old + memcpy(pNewData, m_pData, m_nSize * sizeof(CBotString)); + + // construct remaining elements + ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); + + + // Get rid of old stuff (note: no destructors called) + delete[] reinterpret_cast(m_pData); + m_pData = pNewData; + m_nSize = nNewSize; + m_nMaxSize = nNewMax; + } +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString& CBotStringArray::operator[](int nIndex) +{ + return ElementAt(nIndex); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotString& CBotStringArray::ElementAt(int nIndex) +{ + return m_pData[nIndex]; +} diff --git a/src/CBot/CBotStringArray.h b/src/CBot/CBotStringArray.h new file mode 100644 index 00000000..761f874a --- /dev/null +++ b/src/CBot/CBotStringArray.h @@ -0,0 +1,85 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotDll.h" + +// Local include + +// Global include + +/*! + * \brief The CBotStringArray class Class used to arrays of strings management. + */ +class CBotStringArray : public CBotString +{ +public: + + /*! + * \brief CBotStringArray + */ + CBotStringArray(); + + /*! + * \brief ~CBotStringArray + */ + ~CBotStringArray(); + + /*! + * \brief SetSize Set the array size. + * \param nb + */ + void SetSize(int nb); + + /*! + * \brief GetSize + * \return + */ + int GetSize(); + + /*! + * \brief Add + * \param str + */ + void Add(const CBotString& str); + + /*! + * \brief operator [] + * \param nIndex + * \return + */ + CBotString& operator[](int nIndex); + + /*! + * \brief ElementAt + * \param nIndex + * \return + */ + CBotString& ElementAt(int nIndex); + +private: + + //! Number of elements. + int m_nSize; + //! Reserved size. + int m_nMaxSize; + CBotString* m_pData; +}; diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index b82cdfea..e8fd0432 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotStringArray.h" + // Local include // Global include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 2df62cc2..824b3fd6 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -140,3 +140,23 @@ bool WriteFloat(FILE* pf, float w) return (lg == 1); } + +//////////////////////////////////////////////////////////////////////////////// +void ConstructElements(CBotString* pNewData, int nCount) +{ + while (nCount--) + { + ConstructElement(pNewData); + pNewData++; + } +} + +//////////////////////////////////////////////////////////////////////////////// +void DestructElements(CBotString* pOldData, int nCount) +{ + while (nCount--) + { + DestructElement(pOldData); + pOldData++; + } +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 354595b8..f25c8c60 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -75,3 +75,45 @@ bool WriteString(FILE* pf, CBotString s); * \return */ bool WriteFloat(FILE* pf, float w); + +/*! + * \brief ConstructElement + * \param pNewData + */ +static inline void ConstructElement(CBotString* pNewData) +{ + memset(pNewData, 0, sizeof(CBotString)); +} + +/*! + * \brief DestructElement + * \param pOldData + */ +static inline void DestructElement(CBotString* pOldData) +{ + pOldData->~CBotString(); +} + +/*! + * \brief CopyElement + * \param pSrc + * \param pDest + */ +static inline void CopyElement(CBotString* pSrc, CBotString* pDest) +{ + *pSrc = *pDest; +} + +/*! + * \brief ConstructElements + * \param pNewData + * \param nCount + */ +void ConstructElements(CBotString* pNewData, int nCount); + +/*! + * \brief DestructElements + * \param pOldData + * \param nCount + */ +void DestructElements(CBotString* pOldData, int nCount); diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 80cb2d0e..28c73204 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -10,6 +10,7 @@ set(SOURCES CBotUtils.cpp CBotDefParam.cpp CBotCallMethode.cpp + CBotStringArray.cpp CBotInstr/CBotInstr.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp From 942d7195e4ad90fc107778b7f5536de992385c8b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Mon, 16 Nov 2015 22:34:30 +0100 Subject: [PATCH 67/91] Moving CBotString class in its own header and source files. --- src/CBot/CBot.h | 1 - src/CBot/CBotCall.h | 2 + src/CBot/CBotDll.h | 188 ------------------------- src/CBot/CBotString.h | 214 +++++++++++++++++++++++++++++ src/CBot/CBotStringArray.h | 2 +- src/CBot/CBotUtils.cpp | 18 +++ src/CBot/CBotUtils.h | 19 +-- src/CBot/CBotVar/CBotVar.h | 2 + test/unit/CBot/CBotString_test.cpp | 2 + 9 files changed, 246 insertions(+), 202 deletions(-) create mode 100644 src/CBot/CBotString.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 71c2279c..0b619fe9 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -67,7 +67,6 @@ extern bool ReadLong(FILE* pf, long& w); extern bool WriteFloat(FILE* pf, float w); extern bool WriteLong(FILE* pf, long w); extern bool ReadFloat(FILE* pf, float& w); -extern bool WriteString(FILE* pf, CBotString s); extern bool ReadString(FILE* pf, CBotString& s); extern bool WriteType(FILE* pf, CBotTypResult type); extern bool ReadType(FILE* pf, CBotTypResult& type); diff --git a/src/CBot/CBotCall.h b/src/CBot/CBotCall.h index 79cab9a2..103d1fc4 100644 --- a/src/CBot/CBotCall.h +++ b/src/CBot/CBotCall.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotString.h" + // Local include // Global include diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 16b400e3..40e4931d 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -231,194 +231,6 @@ private: // for example exceptions returned by external routines // and " throw " with any number. - -//////////////////////////////////////////////////////////////////////// -/** - * \brief CBotString Class used to work on strings - */ -class CBotString -{ -public: - - /** - * \brief CBotString Default constructor. - */ - CBotString(); - - /** - * \brief CBotString - * \param p - */ - CBotString(const char* p); - - /** - * \brief CBotString - * \param p - */ - CBotString(const CBotString& p); - - /** - * \brief CBotString Destructor. - */ - ~CBotString(); - - /** - * \brief Empty Clear the internal string. - */ - void Empty(); - - /** - * \brief IsEmpty Check if the string is empty. - * \return True if the sting is empty false otherwise. - */ - bool IsEmpty() const; - - /** - * \brief GetLength Get the string length. - * \return The size of the string. - */ - int GetLength(); - - /** - * \brief Find Find the position of a character in a string starting from - * the beginning of the string. - * \param c The character to find. - * \return The position of the character or -1 if the character was not - * found. - * \see ReverseFind(const char c) - */ - int Find(const char c); - - /** - * \brief Find Find the position of a string in a string starting from the - * beginning of the string. - * \param lpsz The string to find. - * \return The position of the string or -1 if the string was not - * found. - * \see ReverseFind(const char* lpsz) - */ - int Find(const char* lpsz); - - /** - * \brief Find Find the position of a character in a string starting from - * the end of the string. - * \param c The character to find. - * \return The position of the character or -1 if the character was not - * found. - * \see Find(const char c) - */ - int ReverseFind(const char c); - - /** - * \brief Find Find the position of a string in a string starting from the - * end of the string. - * \param lpsz The string to find. - * \return The string of the character or -1 if the string was not - * found. - * \see Find(const char* lpsz) - */ - 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. - * \param nFirst The start index of the character in the string. - * \param lg The size limit. Default value is 2000. - * \return The exctracted string. - */ - CBotString Mid(int start, int lg=-1); - - /** - * \brief Left Return a part of a string starting from the left. - * \param nCount The number of character to retreive. - * \return The exctracted string. - */ - CBotString Left(int nCount) const; - - /** - * \brief Right Return a part of a string starting from the right. - * \param nCount The number of character to retreive. - * \return The exctracted string. - */ - CBotString Right(int nCount) const; - - /** - * \brief Compare Compare a given string to an other. - * \param lpsz The string to compare. - * \return 0 if the two string matches. Less than 0 if the current - * string is less than lpsz. Greater than 0 if the current - * string is greater than lpsz. - */ - int Compare(const char* lpsz) const; - - /** - * \brief MakeUpper Uppercase the string. - */ - void MakeUpper(); - - /** - * \brief MakeLower Lowercase the string. - */ - void MakeLower(); - - /** - * @brief CStr Convert the CBotString to a C string. - * @return A C string string. - */ - const char* CStr() const; - - /** - * \brief Overloaded oprators to work on CBotString classes - */ - const CBotString& operator=(const CBotString& stringSrc); - const CBotString& operator=(const char ch); - const CBotString& operator=(const char* pString); - CBotString operator+(const CBotString& str); - - const CBotString& operator+=(const char ch); - const CBotString& operator+=(const CBotString& str); - bool operator==(const CBotString& str); - bool operator==(const char* p); - bool operator!=(const CBotString& str); - bool operator!=(const char* p); - bool operator>(const CBotString& str); - bool operator>(const char* p); - bool operator>=(const CBotString& str); - bool operator>=(const char* p); - bool operator<(const CBotString& str); - bool operator<(const char* p); - bool operator<=(const CBotString& str); - bool operator<=(const char* p); - - operator const char*() const; // as a C string - - -private: - - //! \brief Pointer to string - char* m_ptr; - - //! \brief Length of the string - int m_lg; - - //! \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); -}; - /////////////////////////////////////////////////////////////////////////////// // routines for file management (* FILE) FILE* fOpen(const char* name, const char* mode); diff --git a/src/CBot/CBotString.h b/src/CBot/CBotString.h new file mode 100644 index 00000000..3297c2b7 --- /dev/null +++ b/src/CBot/CBotString.h @@ -0,0 +1,214 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotUtils.h" + +// Local include + +// Global include +#include + +/** + * \brief CBotString Class used to work on strings + */ +class CBotString +{ +public: + + /** + * \brief CBotString Default constructor. + */ + CBotString(); + + /** + * \brief CBotString + * \param p + */ + CBotString(const char* p); + + /** + * \brief CBotString + * \param p + */ + CBotString(const CBotString& p); + + /** + * \brief CBotString Destructor. + */ + ~CBotString(); + + /** + * \brief Empty Clear the internal string. + */ + void Empty(); + + /** + * \brief IsEmpty Check if the string is empty. + * \return True if the sting is empty false otherwise. + */ + bool IsEmpty() const; + + /** + * \brief GetLength Get the string length. + * \return The size of the string. + */ + int GetLength(); + + /** + * \brief Find Find the position of a character in a string starting from + * the beginning of the string. + * \param c The character to find. + * \return The position of the character or -1 if the character was not + * found. + * \see ReverseFind(const char c) + */ + int Find(const char c); + + /** + * \brief Find Find the position of a string in a string starting from the + * beginning of the string. + * \param lpsz The string to find. + * \return The position of the string or -1 if the string was not + * found. + * \see ReverseFind(const char* lpsz) + */ + int Find(const char* lpsz); + + /** + * \brief Find Find the position of a character in a string starting from + * the end of the string. + * \param c The character to find. + * \return The position of the character or -1 if the character was not + * found. + * \see Find(const char c) + */ + int ReverseFind(const char c); + + /** + * \brief Find Find the position of a string in a string starting from the + * end of the string. + * \param lpsz The string to find. + * \return The string of the character or -1 if the string was not + * found. + * \see Find(const char* lpsz) + */ + 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. + * \param nFirst The start index of the character in the string. + * \param lg The size limit. Default value is 2000. + * \return The exctracted string. + */ + CBotString Mid(int start, int lg=-1); + + /** + * \brief Left Return a part of a string starting from the left. + * \param nCount The number of character to retreive. + * \return The exctracted string. + */ + CBotString Left(int nCount) const; + + /** + * \brief Right Return a part of a string starting from the right. + * \param nCount The number of character to retreive. + * \return The exctracted string. + */ + CBotString Right(int nCount) const; + + /** + * \brief Compare Compare a given string to an other. + * \param lpsz The string to compare. + * \return 0 if the two string matches. Less than 0 if the current + * string is less than lpsz. Greater than 0 if the current + * string is greater than lpsz. + */ + int Compare(const char* lpsz) const; + + /** + * \brief MakeUpper Uppercase the string. + */ + void MakeUpper(); + + /** + * \brief MakeLower Lowercase the string. + */ + void MakeLower(); + + /** + * @brief CStr Convert the CBotString to a C string. + * @return A C string string. + */ + const char* CStr() const; + + /** + * \brief Overloaded oprators to work on CBotString classes + */ + const CBotString& operator=(const CBotString& stringSrc); + const CBotString& operator=(const char ch); + const CBotString& operator=(const char* pString); + CBotString operator+(const CBotString& str); + + const CBotString& operator+=(const char ch); + const CBotString& operator+=(const CBotString& str); + bool operator==(const CBotString& str); + bool operator==(const char* p); + bool operator!=(const CBotString& str); + bool operator!=(const char* p); + bool operator>(const CBotString& str); + bool operator>(const char* p); + bool operator>=(const CBotString& str); + bool operator>=(const char* p); + bool operator<(const CBotString& str); + bool operator<(const char* p); + bool operator<=(const CBotString& str); + bool operator<=(const char* p); + + operator const char*() const; // as a C string + + +private: + + //! \brief Pointer to string + char* m_ptr; + + //! \brief Length of the string + int m_lg; + + //! \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/CBotStringArray.h b/src/CBot/CBotStringArray.h index 761f874a..bc13233c 100644 --- a/src/CBot/CBotStringArray.h +++ b/src/CBot/CBotStringArray.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotDll.h" +#include "CBotString.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 824b3fd6..1a226dbc 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -141,6 +141,24 @@ bool WriteFloat(FILE* pf, float w) return (lg == 1); } +//////////////////////////////////////////////////////////////////////////////// +void ConstructElement(CBotString* pNewData) +{ + memset(pNewData, 0, sizeof(CBotString)); +} + +//////////////////////////////////////////////////////////////////////////////// +void DestructElement(CBotString* pOldData) +{ + pOldData->~CBotString(); +} + +//////////////////////////////////////////////////////////////////////////////// +void CopyElement(CBotString* pSrc, CBotString* pDest) +{ + *pSrc = *pDest; +} + //////////////////////////////////////////////////////////////////////////////// void ConstructElements(CBotString* pNewData, int nCount) { diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index f25c8c60..e14b4748 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -22,10 +22,14 @@ // Modules inlcude #include "CBotDll.h" +#include "CBotString.h" + // Local include // Global include +class CBotString; + /*! * \brief MakeListVars Transforms the array of pointers to variables in a * chained list of variables @@ -80,29 +84,20 @@ bool WriteFloat(FILE* pf, float w); * \brief ConstructElement * \param pNewData */ -static inline void ConstructElement(CBotString* pNewData) -{ - memset(pNewData, 0, sizeof(CBotString)); -} +void ConstructElement(CBotString* pNewData); /*! * \brief DestructElement * \param pOldData */ -static inline void DestructElement(CBotString* pOldData) -{ - pOldData->~CBotString(); -} +void DestructElement(CBotString* pOldData); /*! * \brief CopyElement * \param pSrc * \param pDest */ -static inline void CopyElement(CBotString* pSrc, CBotString* pDest) -{ - *pSrc = *pDest; -} +void CopyElement(CBotString* pSrc, CBotString* pDest); /*! * \brief ConstructElements diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index 8f465fc8..68132ad2 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -24,6 +24,8 @@ #include "../CBotDefines.h" +#include "../CBotString.h" + // Local include // Global include diff --git a/test/unit/CBot/CBotString_test.cpp b/test/unit/CBot/CBotString_test.cpp index 97576a99..b0d1ede8 100644 --- a/test/unit/CBot/CBotString_test.cpp +++ b/test/unit/CBot/CBotString_test.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBot/CBotDll.h" +#include "CBot/CBotString.h" + // Local include // Global include From 3b0561056a0247096e089464ba86b59df3438795 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Tue, 17 Nov 2015 21:06:25 +0100 Subject: [PATCH 68/91] Moving CBotTypResult class in its own header and source files. --- src/CBot/CBotClass.h | 8 +- src/CBot/CBotDll.h | 84 -------------- src/CBot/CBotProgram.h | 3 +- src/CBot/CBotTypResult.cpp | 208 +++++++++++++++++++++++++++++++++++ src/CBot/CBotTypResult.h | 112 +++++++++++++++++++ src/CBot/CBotUtils.h | 1 + src/CBot/CBotVar/CBotVar.cpp | 178 ------------------------------ src/CBot/CMakeLists.txt | 1 + src/script/scriptfunc.h | 1 + 9 files changed, 331 insertions(+), 265 deletions(-) create mode 100644 src/CBot/CBotTypResult.cpp create mode 100644 src/CBot/CBotTypResult.h diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index fe385461..b776fb5a 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -22,14 +22,18 @@ // Modules inlcude #include "CBotDll.h" -#include "CBotProgram.h" - #include "CBotDefines.h" +#include "CBotTypResult.h" + +#include "CBotString.h" + // Local include // Global include +class CBotProgram; + /*! * \brief The CBotClass class Class to define new classes in the language CBOT * for example to define the class CPoint (x, y). diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index 40e4931d..fd5939d2 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -69,90 +69,6 @@ class CBotCStack; // stack #define OBJECTCREATED (reinterpret_cast(-2)) -/** \brief CBotTypResult class to define the complete type of a result*/ -class CBotTypResult -{ -public: - /** - * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) - * \param type type of created result, see CBotType - */ - CBotTypResult(int type); - // for simple types (CBotTypInt à CBotTypString) - - - CBotTypResult(int type, const char* name); - // for pointer types and intrinsic classes - - CBotTypResult(int type, CBotClass* pClass); - // for the instance of a class - - CBotTypResult(int type, CBotTypResult elem); - // for arrays of variables - - CBotTypResult(const CBotTypResult& typ); - // for assignments - - CBotTypResult(); - // for default - - ~CBotTypResult(); - - int GetType(int mode = 0) const; - // returns type CBotType* as a result - - void SetType(int n); - // modifies a type - - CBotClass* GetClass() const; - // makes the pointer to the class (for CBotTypClass, CBotTypPointer) - - int GetLimite() const; - // returns limit size of table (CBotTypArray) - - void SetLimite(int n); - // set limit to the table - - void SetArray(int* max ); - // set limits for a list of dimensions (arrays of arrays) - - CBotTypResult& GetTypElem() const; - // returns type of array elements (CBotTypArray) - // rend le type des éléments du tableau (CBotTypArray) - - bool Compare(const CBotTypResult& typ) const; - // compares whether the types are compatible - bool Eq(int type) const; - // compare type - - CBotTypResult& operator=(const CBotTypResult& src); - // copy a complete type in another - -private: - int m_type; - CBotTypResult* m_pNext; // for the types of type - CBotClass* m_pClass; // for the derivatives of class - int m_limite; // limits of tables - friend class CBotVarClass; - friend class CBotVarPointer; -}; - -/* -// to define a result as output, using for example - - // to return a simple Float - return CBotTypResult( CBotTypFloat ); - - - // to return a string array - return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); - - // to return un array of array of "point" class - CBotTypResult typPoint( CBotTypIntrinsic, "point" ); - CBotTypResult arrPoint( CBotTypArray, typPoint ); - return CBotTypResult( CBotTypArray, arrPoint ); -*/ - //////////////////////////////////////////////////////////////////////// // Error Handling of compilation and execution diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index bef08a8c..f13a1af8 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -20,13 +20,14 @@ #pragma once // Modules inlcude +#include "CBotTypResult.h" +#include "CBotString.h" #include "CBotStringArray.h" // Local include // Global include - /*! * \brief The CBotProgram class Main class managing CBot program. */ diff --git a/src/CBot/CBotTypResult.cpp b/src/CBot/CBotTypResult.cpp new file mode 100644 index 00000000..0ba8ff55 --- /dev/null +++ b/src/CBot/CBotTypResult.cpp @@ -0,0 +1,208 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotTypResult.h" + +#include "CBotEnums.h" + +#include "CBotClass.h" + +// Local include + +// Global include + + + +/////////////////////////////////////////////////////// +// management of results types + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::CBotTypResult(int type) +{ + m_type = type; + m_pNext = nullptr; + m_pClass = nullptr; + m_limite = -1; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::CBotTypResult(int type, const char* name) +{ + m_type = type; + m_pNext = nullptr; + m_pClass = nullptr; + m_limite = -1; + + if ( type == CBotTypPointer || + type == CBotTypClass || + type == CBotTypIntrinsic ) + { + m_pClass = CBotClass::Find(name); + if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; + } +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::CBotTypResult(int type, CBotClass* pClass) +{ + m_type = type; + m_pNext = nullptr; + m_pClass = pClass; + m_limite = -1; + + if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::CBotTypResult(int type, CBotTypResult elem) +{ + m_type = type; + m_pNext = nullptr; + m_pClass = nullptr; + m_limite = -1; + + if ( type == CBotTypArrayPointer || + type == CBotTypArrayBody ) + m_pNext = new CBotTypResult( elem ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::CBotTypResult(const CBotTypResult& typ) +{ + m_type = typ.m_type; + m_pClass = typ.m_pClass; + m_pNext = nullptr; + m_limite = typ.m_limite; + + if ( typ.m_pNext ) + m_pNext = new CBotTypResult( *typ.m_pNext ); +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::CBotTypResult() +{ + m_type = 0; + m_limite = -1; + m_pNext = nullptr; + m_pClass = nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult::~CBotTypResult() +{ + delete m_pNext; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotTypResult::GetType(int mode) const +{ +#ifdef _DEBUG + if ( m_type == CBotTypPointer || + m_type == CBotTypClass || + m_type == CBotTypIntrinsic ) + + if ( m_pClass == nullptr ) assert(0); + + + if ( m_type == CBotTypArrayPointer ) + if ( m_pNext == nullptr ) assert(0); +#endif + if ( mode == 3 && m_type == CBotTypNullPointer ) return CBotTypPointer; + return m_type; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotTypResult::SetType(int n) +{ + m_type = n; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotClass* CBotTypResult::GetClass() const +{ + return m_pClass; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult& CBotTypResult::GetTypElem() const +{ + return *m_pNext; +} + +//////////////////////////////////////////////////////////////////////////////// +int CBotTypResult::GetLimite() const +{ + return m_limite; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotTypResult::SetLimite(int n) +{ + m_limite = n; +} + +//////////////////////////////////////////////////////////////////////////////// +void CBotTypResult::SetArray( int* max ) +{ + m_limite = *max; + if (m_limite < 1) m_limite = -1; + + if ( m_pNext != nullptr ) // last dimension? + { + m_pNext->SetArray( max+1 ); + } +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotTypResult::Compare(const CBotTypResult& typ) const +{ + if ( m_type != typ.m_type ) return false; + + if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); + + if ( m_type == CBotTypPointer || + m_type == CBotTypClass || + m_type == CBotTypIntrinsic ) + { + return m_pClass == typ.m_pClass; + } + + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool CBotTypResult::Eq(int type) const +{ + return m_type == type; +} + +//////////////////////////////////////////////////////////////////////////////// +CBotTypResult& CBotTypResult::operator=(const CBotTypResult& src) +{ + m_type = src.m_type; + m_limite = src.m_limite; + m_pClass = src.m_pClass; + m_pNext = nullptr; + if ( src.m_pNext != nullptr ) + { + m_pNext = new CBotTypResult(*src.m_pNext); + } + return *this; +} diff --git a/src/CBot/CBotTypResult.h b/src/CBot/CBotTypResult.h new file mode 100644 index 00000000..97ac52af --- /dev/null +++ b/src/CBot/CBotTypResult.h @@ -0,0 +1,112 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude + +// Local include + +// Global include + +class CBotClass; + +/* +// to define a result as output, using for example + + // to return a simple Float + return CBotTypResult( CBotTypFloat ); + + + // to return a string array + return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) ); + + // to return un array of array of "point" class + CBotTypResult typPoint( CBotTypIntrinsic, "point" ); + CBotTypResult arrPoint( CBotTypArray, typPoint ); + return CBotTypResult( CBotTypArray, arrPoint ); +*/ + +/** \brief CBotTypResult class to define the complete type of a result*/ +class CBotTypResult +{ +public: + /** + * \brief CBotTypResult constructor for simple types (CBotTypInt to CBotTypString) + * \param type type of created result, see CBotType + */ + CBotTypResult(int type); + // for simple types (CBotTypInt à CBotTypString) + + + CBotTypResult(int type, const char* name); + // for pointer types and intrinsic classes + + CBotTypResult(int type, CBotClass* pClass); + // for the instance of a class + + CBotTypResult(int type, CBotTypResult elem); + // for arrays of variables + + CBotTypResult(const CBotTypResult& typ); + // for assignments + + CBotTypResult(); + // for default + + ~CBotTypResult(); + + int GetType(int mode = 0) const; + // returns type CBotType* as a result + + void SetType(int n); + // modifies a type + + CBotClass* GetClass() const; + // makes the pointer to the class (for CBotTypClass, CBotTypPointer) + + int GetLimite() const; + // returns limit size of table (CBotTypArray) + + void SetLimite(int n); + // set limit to the table + + void SetArray(int* max ); + // set limits for a list of dimensions (arrays of arrays) + + CBotTypResult& GetTypElem() const; + // returns type of array elements (CBotTypArray) + // rend le type des éléments du tableau (CBotTypArray) + + bool Compare(const CBotTypResult& typ) const; + // compares whether the types are compatible + bool Eq(int type) const; + // compare type + + CBotTypResult& operator=(const CBotTypResult& src); + // copy a complete type in another + +private: + int m_type; + CBotTypResult* m_pNext; // for the types of type + CBotClass* m_pClass; // for the derivatives of class + int m_limite; // limits of tables + friend class CBotVarClass; + friend class CBotVarPointer; +}; diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index e14b4748..739800eb 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -23,6 +23,7 @@ #include "CBotDll.h" #include "CBotString.h" +#include "CBotTypResult.h" // Local include diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index e2c1fe78..e5ce073e 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -759,181 +759,3 @@ CBotClass* CBotVar::GetClass() assert(0); return nullptr; } - -/////////////////////////////////////////////////////// -// management of results types - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::CBotTypResult(int type) -{ - m_type = type; - m_pNext = nullptr; - m_pClass = nullptr; - m_limite = -1; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::CBotTypResult(int type, const char* name) -{ - m_type = type; - m_pNext = nullptr; - m_pClass = nullptr; - m_limite = -1; - - if ( type == CBotTypPointer || - type == CBotTypClass || - type == CBotTypIntrinsic ) - { - m_pClass = CBotClass::Find(name); - if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; - } -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::CBotTypResult(int type, CBotClass* pClass) -{ - m_type = type; - m_pNext = nullptr; - m_pClass = pClass; - m_limite = -1; - - if ( m_pClass && m_pClass->IsIntrinsic() ) m_type = CBotTypIntrinsic; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::CBotTypResult(int type, CBotTypResult elem) -{ - m_type = type; - m_pNext = nullptr; - m_pClass = nullptr; - m_limite = -1; - - if ( type == CBotTypArrayPointer || - type == CBotTypArrayBody ) - m_pNext = new CBotTypResult( elem ); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::CBotTypResult(const CBotTypResult& typ) -{ - m_type = typ.m_type; - m_pClass = typ.m_pClass; - m_pNext = nullptr; - m_limite = typ.m_limite; - - if ( typ.m_pNext ) - m_pNext = new CBotTypResult( *typ.m_pNext ); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::CBotTypResult() -{ - m_type = 0; - m_limite = -1; - m_pNext = nullptr; - m_pClass = nullptr; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult::~CBotTypResult() -{ - delete m_pNext; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotTypResult::GetType(int mode) const -{ -#ifdef _DEBUG - if ( m_type == CBotTypPointer || - m_type == CBotTypClass || - m_type == CBotTypIntrinsic ) - - if ( m_pClass == nullptr ) assert(0); - - - if ( m_type == CBotTypArrayPointer ) - if ( m_pNext == nullptr ) assert(0); -#endif - if ( mode == 3 && m_type == CBotTypNullPointer ) return CBotTypPointer; - return m_type; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotTypResult::SetType(int n) -{ - m_type = n; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotClass* CBotTypResult::GetClass() const -{ - return m_pClass; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult& CBotTypResult::GetTypElem() const -{ - return *m_pNext; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotTypResult::GetLimite() const -{ - return m_limite; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotTypResult::SetLimite(int n) -{ - m_limite = n; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotTypResult::SetArray( int* max ) -{ - m_limite = *max; - if (m_limite < 1) m_limite = -1; - - if ( m_pNext != nullptr ) // last dimension? - { - m_pNext->SetArray( max+1 ); - } -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotTypResult::Compare(const CBotTypResult& typ) const -{ - if ( m_type != typ.m_type ) return false; - - if ( m_type == CBotTypArrayPointer ) return m_pNext->Compare(*typ.m_pNext); - - if ( m_type == CBotTypPointer || - m_type == CBotTypClass || - m_type == CBotTypIntrinsic ) - { - return m_pClass == typ.m_pClass; - } - - return true; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotTypResult::Eq(int type) const -{ - return m_type == type; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotTypResult& CBotTypResult::operator=(const CBotTypResult& src) -{ - m_type = src.m_type; - m_limite = src.m_limite; - m_pClass = src.m_pClass; - m_pNext = nullptr; - if ( src.m_pNext != nullptr ) - { - m_pNext = new CBotTypResult(*src.m_pNext); - } - return *this; -} - diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 28c73204..d8bda9ff 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES CBotDefParam.cpp CBotCallMethode.cpp CBotStringArray.cpp + CBotTypResult.cpp CBotInstr/CBotInstr.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp diff --git a/src/script/scriptfunc.h b/src/script/scriptfunc.h index 9428879a..07b21cb9 100644 --- a/src/script/scriptfunc.h +++ b/src/script/scriptfunc.h @@ -25,6 +25,7 @@ #pragma once #include "CBot/CBotDll.h" +#include "CBot/CBotTypResult.h" #include "common/error.h" From 51f0675e413c9fbdd1c12dcfc1956c92285c3f60 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 14:36:50 +0100 Subject: [PATCH 69/91] Moving macro MAX from CBot.h to CBotTwoOpExpr.cpp. --- src/CBot/CBot.h | 2 -- src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 0b619fe9..a732d7ae 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -54,8 +54,6 @@ class CBotDefParam; // paramerer list of a function extern bool SaveVar(FILE* pf, CBotVar* pVar); -#define MAX(a,b) ((a>b) ? a : b) - extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index ab5bc653..6a7477f4 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -33,6 +33,8 @@ // Global include #include +#define MAX(a,b) ((a>b) ? a : b) + //////////////////////////////////////////////////////////////////////////////// CBotTwoOpExpr::CBotTwoOpExpr() { From 7b200a09228a5c6683d6556102b6413716a661f6 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 15:47:46 +0100 Subject: [PATCH 70/91] Moving global CompileParams, TypeCompatible and TypesCompatibles from CBot.cpp to CBotInstrUtils.cpp. --- src/CBot/CBot.cpp | 139 -------------------- src/CBot/CBot.h | 5 - src/CBot/CBotInstr/CBotClassInst.cpp | 3 + src/CBot/CBotInstr/CBotExpression.cpp | 3 + src/CBot/CBotInstr/CBotFunction.cpp | 2 + src/CBot/CBotInstr/CBotInstrMethode.cpp | 2 + src/CBot/CBotInstr/CBotInstrUtils.cpp | 167 ++++++++++++++++++++++++ src/CBot/CBotInstr/CBotInstrUtils.h | 61 +++++++++ src/CBot/CBotInstr/CBotListArray.cpp | 2 + src/CBot/CBotInstr/CBotNew.cpp | 2 + src/CBot/CBotInstr/CBotReturn.cpp | 3 + src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 3 + src/CBot/CMakeLists.txt | 1 + 13 files changed, 249 insertions(+), 144 deletions(-) create mode 100644 src/CBot/CBotInstr/CBotInstrUtils.cpp create mode 100644 src/CBot/CBotInstr/CBotInstrUtils.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index 1822df7b..da8109f4 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -83,145 +83,6 @@ // Global include #include -////////////////////////////////////////////////////////////////////////////////////////// - -// compile a list of parameters - -CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) -{ - bool first = true; - CBotInstr* ret = nullptr; // to return to the list - - CBotCStack* pile = pStack; - int i = 0; - - if (IsOfType(p, ID_OPENPAR)) - { - int start, end; - if (!IsOfType(p, ID_CLOSEPAR)) while (true) - { - start = p->GetStart(); - pile = pile->TokenStack(); // keeps the result on the stack - - if (first) pStack->SetStartError(start); - first = false; - - CBotInstr* param = CBotExpression::Compile(p, pile); - end = p->GetStart(); - - if (!pile->IsOk()) - { - return pStack->Return(nullptr, pile); - } - - if (ret == nullptr) ret = param; - else ret->AddNext(param); // construct the list - - if (param != nullptr) - { - if (pile->GetTypResult().Eq(99)) - { - delete pStack->TokenStack(); - pStack->SetError(TX_VOID, p->GetStart()); - return nullptr; - } - ppVars[i] = pile->GetVar(); - ppVars[i]->GetToken()->SetPos(start, end); - i++; - - if (IsOfType(p, ID_COMMA)) continue; // skips the comma - if (IsOfType(p, ID_CLOSEPAR)) break; - } - - pStack->SetError(TX_CLOSEPAR, p->GetStart()); - delete pStack->TokenStack(); - return nullptr; - } - } - ppVars[i] = nullptr; - return ret; -} - -///////////////////////////////////////////////////////////// -// check if two results are consistent to make an operation - -bool TypeCompatible(CBotTypResult& type1, CBotTypResult& type2, int op) -{ - int t1 = type1.GetType(); - int t2 = type2.GetType(); - - int max = (t1 > t2) ? t1 : t2; - - if (max == 99) return false; // result is void? - - // special case for strin concatenation - if (op == ID_ADD && max >= CBotTypString) return true; - if (op == ID_ASSADD && max >= CBotTypString) return true; - if (op == ID_ASS && t1 == CBotTypString) return true; - - if (max >= CBotTypBoolean) - { - if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE) && - (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; - if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && - (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; - if (t2 != t1) return false; - if (t1 == CBotTypArrayPointer) return type1.Compare(type2); - if (t1 == CBotTypPointer || - t1 == CBotTypClass || - t1 == CBotTypIntrinsic ) - { - CBotClass* c1 = type1.GetClass(); - CBotClass* c2 = type2.GetClass(); - - return c1->IsChildOf(c2) || c2->IsChildOf(c1); - // accept the case in reverse - // the transaction will be denied at runtime if the pointer is not - // compatible - } - - return true; - } - - type1.SetType(max); - type2.SetType(max); - return true; -} - -// check if two variables are compatible for parameter passing - -bool TypesCompatibles(const CBotTypResult& type1, const CBotTypResult& type2) -{ - int t1 = type1.GetType(); - int t2 = type2.GetType(); - - if (t1 == CBotTypIntrinsic) t1 = CBotTypClass; - if (t2 == CBotTypIntrinsic) t2 = CBotTypClass; - - int max = (t1 > t2) ? t1 : t2; - - if (max == 99) return false; // result is void? - - if (max >= CBotTypBoolean) - { - if (t2 != t1) return false; - - if (max == CBotTypArrayPointer) - return TypesCompatibles(type1.GetTypElem(), type2.GetTypElem()); - - if (max == CBotTypClass || max == CBotTypPointer) - return type1.GetClass() == type2.GetClass() ; - - return true ; - } - return true; -} - - ///////////////////////////////////////////////////////////////////////////////////// // file management diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index a732d7ae..2f2bdffa 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -54,11 +54,6 @@ class CBotDefParam; // paramerer list of a function extern bool SaveVar(FILE* pf, CBotVar* pVar); -extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); - -extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 ); -extern bool TypesCompatibles( const CBotTypResult& type1, const CBotTypResult& type2 ); - extern bool WriteWord(FILE* pf, unsigned short w); extern bool ReadWord(FILE* pf, unsigned short& w); extern bool ReadLong(FILE* pf, long& w); diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index 5d2e0a66..d556dd77 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -19,6 +19,9 @@ // Modules inlcude #include "CBotClassInst.h" + +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotLeftExprVar.h" #include "CBotTwoOpExpr.h" #include "CBotInstArray.h" diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp index b6b97837..a3612426 100644 --- a/src/CBot/CBotInstr/CBotExpression.cpp +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -19,6 +19,9 @@ // Modules inlcude #include "CBotExpression.h" + +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotTwoOpExpr.h" #include "CBotStack.h" diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index d325020e..0a629c39 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -21,6 +21,8 @@ // Modules inlcude #include "CBotFunction.h" +#include "CBotInstr/CBotInstrUtils.h" + #include "CBot.h" #include "CBotInstr/CBotBlock.h" diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index 46adf723..26f188f8 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotInstrMethode.h" +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotStack.h" #include "CBotCStack.h" #include "CBotClass.h" diff --git a/src/CBot/CBotInstr/CBotInstrUtils.cpp b/src/CBot/CBotInstr/CBotInstrUtils.cpp new file mode 100644 index 00000000..576cfa30 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstrUtils.cpp @@ -0,0 +1,167 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotInstrUtils.h" + +#include "CBotToken.h" +#include "CBotCStack.h" +#include "CBotTypResult.h" +#include "CBotExpression.h" +#include "CBotClass.h" + +#include "CBotVar/CBotVar.h" + +#include "resource.h" + +// Local include + +// Global include + +//////////////////////////////////////////////////////////////////////////////// +CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars) +{ + bool first = true; + CBotInstr* ret = nullptr; // to return to the list + + CBotCStack* pile = pStack; + int i = 0; + + if (IsOfType(p, ID_OPENPAR)) + { + int start, end; + if (!IsOfType(p, ID_CLOSEPAR)) while (true) + { + start = p->GetStart(); + pile = pile->TokenStack(); // keeps the result on the stack + + if (first) pStack->SetStartError(start); + first = false; + + CBotInstr* param = CBotExpression::Compile(p, pile); + end = p->GetStart(); + + if (!pile->IsOk()) + { + return pStack->Return(nullptr, pile); + } + + if (ret == nullptr) ret = param; + else ret->AddNext(param); // construct the list + + if (param != nullptr) + { + if (pile->GetTypResult().Eq(99)) + { + delete pStack->TokenStack(); + pStack->SetError(TX_VOID, p->GetStart()); + return nullptr; + } + ppVars[i] = pile->GetVar(); + ppVars[i]->GetToken()->SetPos(start, end); + i++; + + if (IsOfType(p, ID_COMMA)) continue; // skips the comma + if (IsOfType(p, ID_CLOSEPAR)) break; + } + + pStack->SetError(TX_CLOSEPAR, p->GetStart()); + delete pStack->TokenStack(); + return nullptr; + } + } + ppVars[i] = nullptr; + return ret; +} + +//////////////////////////////////////////////////////////////////////////////// +bool TypeCompatible(CBotTypResult& type1, CBotTypResult& type2, int op) +{ + int t1 = type1.GetType(); + int t2 = type2.GetType(); + + int max = (t1 > t2) ? t1 : t2; + + if (max == 99) return false; // result is void? + + // special case for strin concatenation + if (op == ID_ADD && max >= CBotTypString) return true; + if (op == ID_ASSADD && max >= CBotTypString) return true; + if (op == ID_ASS && t1 == CBotTypString) return true; + + if (max >= CBotTypBoolean) + { + if ( (op == ID_EQ || op == ID_NE) && + (t1 == CBotTypPointer && t2 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && + (t2 == CBotTypPointer && t1 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE) && + (t1 == CBotTypArrayPointer && t2 == CBotTypNullPointer)) return true; + if ( (op == ID_EQ || op == ID_NE || op == ID_ASS) && + (t2 == CBotTypArrayPointer && t1 == CBotTypNullPointer)) return true; + if (t2 != t1) return false; + if (t1 == CBotTypArrayPointer) return type1.Compare(type2); + if (t1 == CBotTypPointer || + t1 == CBotTypClass || + t1 == CBotTypIntrinsic ) + { + CBotClass* c1 = type1.GetClass(); + CBotClass* c2 = type2.GetClass(); + + return c1->IsChildOf(c2) || c2->IsChildOf(c1); + // accept the case in reverse + // the transaction will be denied at runtime if the pointer is not + // compatible + } + + return true; + } + + type1.SetType(max); + type2.SetType(max); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool TypesCompatibles(const CBotTypResult& type1, const CBotTypResult& type2) +{ + int t1 = type1.GetType(); + int t2 = type2.GetType(); + + if (t1 == CBotTypIntrinsic) t1 = CBotTypClass; + if (t2 == CBotTypIntrinsic) t2 = CBotTypClass; + + int max = (t1 > t2) ? t1 : t2; + + if (max == 99) return false; // result is void? + + if (max >= CBotTypBoolean) + { + if (t2 != t1) return false; + + if (max == CBotTypArrayPointer) + return TypesCompatibles(type1.GetTypElem(), type2.GetTypElem()); + + if (max == CBotTypClass || max == CBotTypPointer) + return type1.GetClass() == type2.GetClass() ; + + return true ; + } + return true; +} diff --git a/src/CBot/CBotInstr/CBotInstrUtils.h b/src/CBot/CBotInstr/CBotInstrUtils.h new file mode 100644 index 00000000..4365aca6 --- /dev/null +++ b/src/CBot/CBotInstr/CBotInstrUtils.h @@ -0,0 +1,61 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude + +// Local include + +// Global include + +// Forward declaration +class CBotInstr; +class CBotToken; +class CBotCStack; +class CBotVar; +class CBotTypResult; + +/*! + * \brief CompileParams Compile a list of parameters. + * \param p + * \param pStack + * \param ppVars + * \return + */ +CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars); + +/*! + * \brief TypeCompatible Check if two results are consistent to make an + * operation. + * \param type1 + * \param type2 + * \param op + * \return + */ +bool TypeCompatible(CBotTypResult& type1, CBotTypResult& type2, int op = 0); + +/*! + * \brief TypesCompatibles Check if two variables are compatible for parameter + * passing. + * \param type1 + * \param type2 + * \return + */ +bool TypesCompatibles(const CBotTypResult& type1, const CBotTypResult& type2); diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp index 6ac0d6a0..ce1a5f8a 100644 --- a/src/CBot/CBotInstr/CBotListArray.cpp +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotListArray.h" +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotExprNull.h" #include "CBotTwoOpExpr.h" diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp index 6f7d8605..b7e36316 100644 --- a/src/CBot/CBotInstr/CBotNew.cpp +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -24,6 +24,8 @@ #include "CBotCStack.h" #include "CBotClass.h" +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotReturn.cpp b/src/CBot/CBotInstr/CBotReturn.cpp index 70c849d2..9f5d00f7 100644 --- a/src/CBot/CBotInstr/CBotReturn.cpp +++ b/src/CBot/CBotInstr/CBotReturn.cpp @@ -19,6 +19,9 @@ // Modules inlcude #include "CBotReturn.h" + +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotExpression.h" #include "CBotStack.h" diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index 6a7477f4..0d97f407 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -19,6 +19,9 @@ // Modules inlcude #include "CBotTwoOpExpr.h" + +#include "CBotInstr/CBotInstrUtils.h" + #include "CBotParExpr.h" #include "CBotLogicExpr.h" #include "CBotExpression.h" diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index d8bda9ff..e2fa17ce 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES CBotStringArray.cpp CBotTypResult.cpp CBotInstr/CBotInstr.cpp + CBotInstr/CBotInstrUtils.cpp CBotInstr/CBotWhile.cpp CBotInstr/CBotDo.cpp CBotInstr/CBotFor.cpp From 43ac0e35f2b3c5d66738e3dd035080927d0ecb23 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 16:04:38 +0100 Subject: [PATCH 71/91] Moving global files function from CBot.cpp to CBotFileUtils.cpp --- src/CBot/CBot.cpp | 40 ----------- src/CBot/CBotDll.h | 7 -- src/CBot/CBotFileUtils.cpp | 62 ++++++++++++++++ src/CBot/CBotFileUtils.h | 71 +++++++++++++++++++ src/CBot/CMakeLists.txt | 3 +- src/level/robotmain.cpp | 1 + .../implementation/programmable_impl.cpp | 2 + src/script/script.cpp | 1 + 8 files changed, 139 insertions(+), 48 deletions(-) create mode 100644 src/CBot/CBotFileUtils.cpp create mode 100644 src/CBot/CBotFileUtils.h diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp index da8109f4..52ea9c60 100644 --- a/src/CBot/CBot.cpp +++ b/src/CBot/CBot.cpp @@ -83,43 +83,3 @@ // Global include #include -///////////////////////////////////////////////////////////////////////////////////// -// file management - -// necessary because it is not possible to do the fopen in the main program -// fwrite and fread in a dll or using the FILE * returned. - - -FILE* fOpen(const char* name, const char* mode) -{ - return fopen(name, mode); -} - -int fClose(FILE* filehandle) -{ - return fclose(filehandle); -} - -size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle) -{ - return fwrite(buffer, elemsize, length, filehandle); -} - -size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle) -{ - return fread(buffer, elemsize, length, filehandle); -} - -size_t fWrite(const void *buffer, size_t length, FILE* filehandle) -{ - return fwrite(buffer, 1, length, filehandle); -} - -size_t fRead(void *buffer, size_t length, FILE* filehandle) -{ - return fread(buffer, 1, length, filehandle); -} - - -//////////////////////////////////////// - diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index fd5939d2..d1d1838e 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -147,13 +147,6 @@ class CBotCStack; // stack // for example exceptions returned by external routines // and " throw " with any number. -/////////////////////////////////////////////////////////////////////////////// -// routines for file management (* FILE) - FILE* fOpen(const char* name, const char* mode); - int fClose(FILE* filehandle); - size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle); - size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle); - #if 0 /* diff --git a/src/CBot/CBotFileUtils.cpp b/src/CBot/CBotFileUtils.cpp new file mode 100644 index 00000000..1c585713 --- /dev/null +++ b/src/CBot/CBotFileUtils.cpp @@ -0,0 +1,62 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +// Modules inlcude +#include "CBotFileUtils.h" + +// Local include + +// Global include + + + +// file management + +// necessary because it is not possible to do the fopen in the main program +// fwrite and fread in a dll or using the FILE * returned. + +//////////////////////////////////////////////////////////////////////////////// +FILE* fOpen(const char* name, const char* mode) +{ + return fopen(name, mode); +} + +//////////////////////////////////////////////////////////////////////////////// +int fClose(FILE* filehandle) +{ + return fclose(filehandle); +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t fWrite(const void *buffer, + std::size_t elemsize, + std::size_t length, + FILE* filehandle) +{ + return fwrite(buffer, elemsize, length, filehandle); +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t fRead(void *buffer, + std::size_t elemsize, + std::size_t length, + FILE* filehandle) +{ + return fread(buffer, elemsize, length, filehandle); +} diff --git a/src/CBot/CBotFileUtils.h b/src/CBot/CBotFileUtils.h new file mode 100644 index 00000000..8f76ec4d --- /dev/null +++ b/src/CBot/CBotFileUtils.h @@ -0,0 +1,71 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude + +// Local include + +// Global include + #include + +/////////////////////////////////////////////////////////////////////////////// +// routines for file management (* FILE) + +/*! + * \brief fOpen + * \param name + * \param mode + * \return + */ +FILE* fOpen(const char* name, const char* mode); + +/*! + * \brief fClose + * \param filehandle + * \return + */ +int fClose(FILE* filehandle); + +/*! + * \brief fWrite + * \param buffer + * \param elemsize + * \param length + * \param filehandle + * \return + */ +std::size_t fWrite(const void *buffer, + std::size_t elemsize, + std::size_t length, + FILE* filehandle); + +/*! + * \brief fRead + * \param buffer + * \param elemsize + * \param length + * \param filehandle + * \return + */ +std::size_t fRead(void *buffer, + std::size_t elemsize, + std::size_t length, + FILE* filehandle); diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index e2fa17ce..3bb1eb91 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,5 +1,7 @@ set(SOURCES CBot.cpp + CBotUtils.cpp + CBotFileUtils.cpp CBotClass.cpp CBotProgram.cpp CBotStack.cpp @@ -7,7 +9,6 @@ set(SOURCES CBotString.cpp CBotToken.cpp CBotCall.cpp - CBotUtils.cpp CBotDefParam.cpp CBotCallMethode.cpp CBotStringArray.cpp diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 8ff82eee..7cd1bdb0 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -20,6 +20,7 @@ #include "level/robotmain.h" #include "CBot/CBotDll.h" +#include "CBot/CBotFileUtils.h" // TODO must be replaced by CBot.h #include "CBot/CBotClass.h" diff --git a/src/object/implementation/programmable_impl.cpp b/src/object/implementation/programmable_impl.cpp index fe594e75..379929e5 100644 --- a/src/object/implementation/programmable_impl.cpp +++ b/src/object/implementation/programmable_impl.cpp @@ -40,6 +40,8 @@ #include "ui/controls/edit.h" +#include "CBot/CBotFileUtils.h" + #include #include diff --git a/src/script/script.cpp b/src/script/script.cpp index d87a41a6..92c658f6 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -44,6 +44,7 @@ #include "CBot/CBotToken.h" #include "CBot/CBotVar/CBotVar.h" +#include "CBot/CBotFileUtils.h" const int CBOT_IPF = 100; // CBOT: default number of instructions / frame From 2f7932ff69bfd7be0cb9e9f68998a27e05dd3304 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 16:21:30 +0100 Subject: [PATCH 72/91] Moving defines from CBotDll.h to CBotDefines.h. --- src/CBot/CBotDefines.h | 81 +++++++++++++++++++++++++++++++++++++++- src/CBot/CBotDll.h | 84 ------------------------------------------ 2 files changed, 80 insertions(+), 85 deletions(-) diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index ae72dbbb..9830fcbf 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -27,11 +27,90 @@ // Global include -#define MAXARRAYSIZE 9999 +#define MAXARRAYSIZE 9999 // variable type SetPrivate / IsPrivate #define PR_PUBLIC 0 // public variable #define PR_READ 1 // read only #define PR_PROTECT 2 // protected (inheritance) #define PR_PRIVATE 3 // strictly private + +//! Define the current CBot version +#define CBOTVERSION 104 + +// for SetUserPtr when deleting an object +// \TODO define own types to distinct between different states of objects +#define OBJECTDELETED (reinterpret_cast(-1)) +// value set before initialization +#define OBJECTCREATED (reinterpret_cast(-2)) + + +//////////////////////////////////////////////////////////////////////// +// Error Handling of compilation and execution +//////////////////////////////////////////////////////////////////////// + +// Here are the list of errors that can be returned by the module +// for compilation + +#define CBotErrOpenPar 5000 // missing the opening parenthesis +#define CBotErrClosePar 5001 // missing the closing parenthesis +#define CBotErrNotBoolean 5002 // expression must be a boolean +#define CBotErrUndefVar 5003 // undeclared variable +#define CBotErrBadLeft 5004 // assignment impossible ( 5 = ... ) +#define CBotErrNoTerminator 5005 // semicolon expected +#define CBotErrCaseOut 5006 // case outside a switch +#define CBotErrCloseBlock 5008 // missing " } " +#define CBotErrElseWhitoutIf 5009 // else without matching if +#define CBotErrOpenBlock 5010 // missing " { " +#define CBotErrBadType1 5011 // wrong type for the assignment +#define CBotErrRedefVar 5012 // redefinition of the variable +#define CBotErrBadType2 5013 // Two operands are incompatible +#define CBotErrUndefCall 5014 // routine undefined +#define CBotErrNoDoubleDots 5015 // " : " expected +#define CBotErrBreakOutside 5017 // break outside of a loop +#define CBotErrUndefLabel 5019 // label udnefined +#define CBotErrLabel 5018 // label ne peut se mettre ici (label can not get here) +#define CBotErrNoCase 5020 // missing " case " +#define CBotErrBadNum 5021 // expected number +#define CBotErrVoid 5022 // " void " not possible here +#define CBotErrNoType 5023 // type declaration expected +#define CBotErrNoVar 5024 // variable name expected +#define CBotErrNoFunc 5025 // expected function name +#define CBotErrOverParam 5026 // too many parameters +#define CBotErrRedefFunc 5027 // this function already exists +#define CBotErrLowParam 5028 // not enough parameters +#define CBotErrBadParam 5029 // wrong types of parameters +#define CBotErrNbParam 5030 // wrong number of parameters +#define CBotErrUndefItem 5031 // element does not exist in the class +#define CBotErrUndefClass 5032 // variable is not a class +#define CBotErrNoConstruct 5033 // no appropriate constructor +#define CBotErrRedefClass 5034 // class already exists +#define CBotErrCloseIndex 5035 // " ] " expected +#define CBotErrReserved 5036 // reserved word (for a DefineNum) +#define CBotErrBadNew 5037 // wrong setting for new +#define CBotErrOpenIndex 5038 // " [ " expected +#define CBotErrBadString 5039 // expected string +#define CBotErrBadIndex 5040 // wrong index type "[ false ]" +#define CBotErrPrivate 5041 // protected item +#define CBotErrNoPublic 5042 // missing word "public" + +// here is the list of errors that can be returned by the module +// for the execution + +#define CBotErrZeroDiv 6000 // division by zero +#define CBotErrNotInit 6001 // uninitialized variable +#define CBotErrBadThrow 6002 // throw a negative value +#define CBotErrNoRetVal 6003 // function did not return results +#define CBotErrNoRun 6004 // Run() without active function +#define CBotErrUndefFunc 6005 // calling a function that no longer exists +#define CBotErrNotClass 6006 // this class does not exist +#define CBotErrNull 6007 // null pointer +#define CBotErrNan 6008 // calculation with a NAN +#define CBotErrOutArray 6009 // index out of array +#define CBotErrStackOver 6010 // stack overflow +#define CBotErrDeletedPtr 6011 // pointer to an object destroyed +#define CBotErrFileOpen 6012 // cannot open the file +#define CBotErrNotOpen 6013 // channel not open +#define CBotErrRead 6014 // error while reading +#define CBotErrWrite 6015 // writing error diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h index d1d1838e..118263c6 100644 --- a/src/CBot/CBotDll.h +++ b/src/CBot/CBotDll.h @@ -37,9 +37,6 @@ #include #include - -#define CBOTVERSION 104 - //////////////////////////////////////////////////////////////////////// // forward declaration of needed classes @@ -62,87 +59,6 @@ class CBotCStack; // stack //////////////////////////////////////////////////////////////////////// //n = not implemented yet -// for SetUserPtr when deleting an object -// \TODO define own types to distinct between different states of objects -#define OBJECTDELETED (reinterpret_cast(-1)) -// value set before initialization -#define OBJECTCREATED (reinterpret_cast(-2)) - - - -//////////////////////////////////////////////////////////////////////// -// Error Handling of compilation and execution -//////////////////////////////////////////////////////////////////////// - -// Here are the list of errors that can be returned by the module -// for compilation - -#define CBotErrOpenPar 5000 // missing the opening parenthesis -#define CBotErrClosePar 5001 // missing the closing parenthesis -#define CBotErrNotBoolean 5002 // expression must be a boolean -#define CBotErrUndefVar 5003 // undeclared variable -#define CBotErrBadLeft 5004 // assignment impossible ( 5 = ... ) -#define CBotErrNoTerminator 5005 // semicolon expected -#define CBotErrCaseOut 5006 // case outside a switch -// CBotErrNoTerm 5007, plus utile -#define CBotErrCloseBlock 5008 // missing " } " -#define CBotErrElseWhitoutIf 5009 // else without matching if -#define CBotErrOpenBlock 5010 // missing " { " -#define CBotErrBadType1 5011 // wrong type for the assignment -#define CBotErrRedefVar 5012 // redefinition of the variable -#define CBotErrBadType2 5013 // Two operands are incompatible -#define CBotErrUndefCall 5014 // routine undefined -#define CBotErrNoDoubleDots 5015 // " : " expected -// CBotErrWhile 5016, plus utile -#define CBotErrBreakOutside 5017 // break outside of a loop -#define CBotErrUndefLabel 5019 // label udnefined -#define CBotErrLabel 5018 // label ne peut se mettre ici (label can not get here) -#define CBotErrNoCase 5020 // missing " case " -#define CBotErrBadNum 5021 // expected number -#define CBotErrVoid 5022 // " void " not possible here -#define CBotErrNoType 5023 // type declaration expected -#define CBotErrNoVar 5024 // variable name expected -#define CBotErrNoFunc 5025 // expected function name -#define CBotErrOverParam 5026 // too many parameters -#define CBotErrRedefFunc 5027 // this function already exists -#define CBotErrLowParam 5028 // not enough parameters -#define CBotErrBadParam 5029 // wrong types of parameters -#define CBotErrNbParam 5030 // wrong number of parameters -#define CBotErrUndefItem 5031 // element does not exist in the class -#define CBotErrUndefClass 5032 // variable is not a class -#define CBotErrNoConstruct 5033 // no appropriate constructor -#define CBotErrRedefClass 5034 // class already exists -#define CBotErrCloseIndex 5035 // " ] " expected -#define CBotErrReserved 5036 // reserved word (for a DefineNum) -#define CBotErrBadNew 5037 // wrong setting for new -#define CBotErrOpenIndex 5038 // " [ " expected -#define CBotErrBadString 5039 // expected string -#define CBotErrBadIndex 5040 // wrong index type "[ false ]" -#define CBotErrPrivate 5041 // protected item -#define CBotErrNoPublic 5042 // missing word "public" - -// here is the list of errors that can be returned by the module -// for the execution - -#define CBotErrZeroDiv 6000 // division by zero -#define CBotErrNotInit 6001 // uninitialized variable -#define CBotErrBadThrow 6002 // throw a negative value -#define CBotErrNoRetVal 6003 // function did not return results -#define CBotErrNoRun 6004 // Run() without active function -#define CBotErrUndefFunc 6005 // calling a function that no longer exists -#define CBotErrNotClass 6006 // this class does not exist -#define CBotErrNull 6007 // null pointer -#define CBotErrNan 6008 // calculation with a NAN -#define CBotErrOutArray 6009 // index out of array -#define CBotErrStackOver 6010 // stack overflow -#define CBotErrDeletedPtr 6011 // pointer to an object destroyed - -#define CBotErrFileOpen 6012 // cannot open the file -#define CBotErrNotOpen 6013 // channel not open -#define CBotErrRead 6014 // error while reading -#define CBotErrWrite 6015 // writing error - - // other values ​​may be returned // for example exceptions returned by external routines // and " throw " with any number. From 1a6b5ded640c1a606d85fd5b5d1b68254b855423 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 16:42:51 +0100 Subject: [PATCH 73/91] Delete CBotDll.h --- src/CBot/CBot.h | 1 - src/CBot/CBotCStack.h | 5 +- src/CBot/CBotCall.h | 7 +- src/CBot/CBotClass.h | 9 +- src/CBot/CBotDefParam.h | 1 - src/CBot/CBotDll.h | 207 ---------------------------- src/CBot/CBotProgram.h | 16 ++- src/CBot/CBotStack.h | 3 + src/CBot/CBotString.h | 1 + src/CBot/CBotStringArray.cpp | 2 + src/CBot/CBotToken.h | 2 - src/CBot/CBotUtils.cpp | 2 +- src/CBot/CBotUtils.h | 7 +- src/CBot/CBotVar/CBotVar.h | 5 +- src/CBot/CBotVar/CBotVarBoolean.cpp | 21 +-- src/CBot/CBotVar/CBotVarFloat.cpp | 2 + src/CBot/CBotVar/CBotVarFloat.h | 2 - src/CBot/CBotVar/CBotVarInt.cpp | 2 + src/CBot/CBotVar/CBotVarInt.h | 2 - src/CBot/CBotVar/CBotVarString.cpp | 2 + src/CBot/CBotVar/CBotVarString.h | 2 - src/level/robotmain.cpp | 1 - src/object/old_object.cpp | 2 - src/script/script.h | 1 - src/script/scriptfunc.h | 2 +- src/ui/studio.cpp | 2 - test/cbot/console/main.cpp | 5 +- test/unit/CBot/CBotString_test.cpp | 2 - 28 files changed, 62 insertions(+), 254 deletions(-) delete mode 100644 src/CBot/CBotDll.h diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 2f2bdffa..1e281109 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -26,7 +26,6 @@ #pragma once #include "resource.h" -#include "CBotDll.h" // public definitions #include "CBotToken.h" // token management #include "CBotProgram.h" diff --git a/src/CBot/CBotCStack.h b/src/CBot/CBotCStack.h index 28e9e886..70ce456a 100644 --- a/src/CBot/CBotCStack.h +++ b/src/CBot/CBotCStack.h @@ -20,14 +20,13 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotProgram.h" // Local include // Global include - +class CBotInstr; +class CBotDefParam; /*! * \brief The CBotCStack class Management of the stack of compilation. diff --git a/src/CBot/CBotCall.h b/src/CBot/CBotCall.h index 103d1fc4..b4dc4fe9 100644 --- a/src/CBot/CBotCall.h +++ b/src/CBot/CBotCall.h @@ -20,15 +20,16 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotString.h" // Local include // Global include -#define STACKRUN 1 /// \def return execution directly on a suspended routine +// Forward declaration +class CBotStack; + +#define STACKRUN 1 //! \def return execution directly on a suspended routine /*! * \brief The CBotCall class. Class for routine calls (external). diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index b776fb5a..ac9e418d 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotDefines.h" #include "CBotTypResult.h" @@ -32,7 +30,14 @@ // Global include +// Forward declaration +class CBotVar; +class CBotClass; +class CBotCallMethode; +class CBotFunction; class CBotProgram; +class CBotStack; +class CBotDefParam; /*! * \brief The CBotClass class Class to define new classes in the language CBOT diff --git a/src/CBot/CBotDefParam.h b/src/CBot/CBotDefParam.h index 705660e3..6da8e2cd 100644 --- a/src/CBot/CBotDefParam.h +++ b/src/CBot/CBotDefParam.h @@ -20,7 +20,6 @@ #pragma once // Modules inlcude -#include "CBotDll.h" #include "CBotToken.h" #include "CBotStack.h" diff --git a/src/CBot/CBotDll.h b/src/CBot/CBotDll.h deleted file mode 100644 index 118263c6..00000000 --- a/src/CBot/CBotDll.h +++ /dev/null @@ -1,207 +0,0 @@ -/* - * This file is part of the Colobot: Gold Edition source code - * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam - * http://epsitec.ch; http://colobot.info; http://github.com/colobot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://gnu.org/licenses - */ - -//////////////////////////////////////////////////////////////////////// - -/** - * \file CBotDll.h - * \brief Library for interpretation of CBOT language - */ - -#pragma once - -// Modules inlcude -#include "resource.h" -#include "CBotEnums.h" - -// Local include - -// Global include -#include -#include -#include - -//////////////////////////////////////////////////////////////////////// -// forward declaration of needed classes - -class CBotToken; // program turned into "tokens -class CBotStack; // for the execution stack -class CBotClass; // class of object -class CBotInstr; // instruction to be executed -class CBotFunction; // user functions -class CBotVar; // variables -class CBotVarClass; // instance of class -class CBotVarPointer; // pointer to an instance of class -class CBotCall; // functions -class CBotCallMethode; // methods -class CBotDefParam; // parameter list -class CBotCStack; // stack - - -//////////////////////////////////////////////////////////////////////// -// Variables management -//////////////////////////////////////////////////////////////////////// -//n = not implemented yet - -// other values ​​may be returned -// for example exceptions returned by external routines -// and " throw " with any number. - - -#if 0 -/* -(**) Note: - To define an external function, proceed as follows: - - a) define a routine for compilation - this routine receive list of parameters (no values) - and either returns a result type (CBotTyp... or 0 = void) - or an error number - b) define a routine for the execution - this routine receive list of parameters (with valeurs), - a variable to store the result (according to the given type at compile time) - - For example, a routine which calculates the mean of a parameter list */ - -int cMean(CBotVar* &pVar, CBotString& ClassName) -{ - if ( pVar == nullptr ) return 6001; // there is no parameter! - - while ( pVar != nullptr ) - { - if ( pVar->GetType() > CBotTypDouble ) return 6002; // this is not a number - pVar = pVar -> GetNext(); - } - - return CBotTypFloat; // the type of the result may depend on the parameters! -} - - -bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) -{ - float total = 0; - int nb = 0; - while (pVar != nullptr) - { - total += pVar->GetValFloat(); - pVar = pVar->GetNext(); - nb++; - } - pResult->SetValFloat(total/nb); // returns the mean value - - return true; // operation fully completed -} - -#endif - -/* -//////////////////////////////////////////////////////////////////////// -// Examples of use -// Definition classes and functions - - -// define the global class CPoint -// -------------------------------- - m_pClassPoint = new CBotClass("CPoint", nullptr); - // adds the component ".x" - m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); - // adds the component ".y" - m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); - // the player can then use the instructions - // CPoint position; position.x = 12; position.y = -13.6 - -// define class CColobotObject -// -------------------------------- -// This class manages all the objects in the world of COLOBOT -// the "main" user program belongs to this class - m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); - // adds the component ".position" - m_pClassObject->AddItem("position", m_pClassPoint); - // adds the component ".type" - m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); - // adds a definition of constant - m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT equivalent to the value 1 - // adds the FIND routine - m_pClassObject->AddFunction( rCompFind, rDoFind ); - // the player can now use the instructions - // CColobotObject chose; chose = FIND( ROBOT ) - - - -// define class CColobotRobot derived from CColobotObject -// --------------------------------------------------------- -// programs "main" associated with robots as a part of this class - m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); - // add routine GOTO - m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); - // the player can now use - // GOTO( FIND ( ROBOT ) ); - - -// creates an instance of the class Robot -// ------------------------------------ -// for example a new robot which has just been manufactured - CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); - -// compiles the program by hand for this robot -// ------------------------------------------ - CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); - if ( !m_pMonRobot->Compile( LeProgramme ) ) {error handling ...}; - -// build a stack for interpreter -// -------------------------------------- - CBotStack* pStack = new CBotStack(nullptr); - -// executes the main program -// ------------------------- - while( false = m_pMonRobot->Execute( "main", pStack )) - { - // program suspended - // could be pass a handle to another (safeguarding pstack for the robot one) - }; - // programme "main" finished ! - - - - -// routine that implements the GOTO (CPoint pos) -bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) -{ - if (pVar->GetType() != CBotTypeClass || - pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) - // the parameter is not the right class? - // in fact the control is done to the routine of compilation - - m_PosToGo.Copy( pVar ); // keeps the target position (object type CBotVar) - - // or so - CBotVar* temp; - temp = pVar->GetItem("x"); // is necessary for the object of type CPoint - ASSERT (temp != nullptr && temp->GetType() == CBotTypFloat); - m_PosToGo.x = temp->GetValFloat(); - - temp = pVar->GetItem("y"); // is necessary for the object of type CPoint - ASSERT (temp != nullptr && temp->GetType() == CBotTypFloat); - m_PosToGo.y = temp->GetValFloat(); - - return (m_CurentPos == m_PosToGo); // makes true if the position is reached - // returns false if one had wait yet -} - -*/ diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index f13a1af8..87dff3c2 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -24,10 +24,18 @@ #include "CBotString.h" #include "CBotStringArray.h" +#include "CBotEnums.h" + // Local include // Global include +// Forward declaration +class CBotFunction; +class CBotClass; +class CBotStack; +class CBotVar; + /*! * \brief The CBotProgram class Main class managing CBot program. */ @@ -225,9 +233,11 @@ public: * \param modestop * \return */ - bool GetPosition(const char* name, int& start, int& stop, - CBotGet modestart = GetPosExtern, - CBotGet modestop = GetPosBloc); + bool GetPosition(const char* name, + int& start, + int& stop, + CBotGet modestart = GetPosExtern, + CBotGet modestop = GetPosBloc); /*! * \brief GetFunctions diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h index df69a0d4..36f45b1e 100644 --- a/src/CBot/CBotStack.h +++ b/src/CBot/CBotStack.h @@ -26,6 +26,9 @@ // Global include +// Forward declaration +class CBotInstr; +class CBotCall; /*! * \class CBotStack diff --git a/src/CBot/CBotString.h b/src/CBot/CBotString.h index 3297c2b7..ea065b29 100644 --- a/src/CBot/CBotString.h +++ b/src/CBot/CBotString.h @@ -21,6 +21,7 @@ // Modules inlcude #include "CBotUtils.h" +#include "resource.h" // Local include diff --git a/src/CBot/CBotStringArray.cpp b/src/CBot/CBotStringArray.cpp index ecf53bf0..6d5d6617 100644 --- a/src/CBot/CBotStringArray.cpp +++ b/src/CBot/CBotStringArray.cpp @@ -26,6 +26,8 @@ // Global include +// Forward declaration +#include //////////////////////////////////////////////////////////////////////////////// CBotStringArray::CBotStringArray() diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index e8fd0432..85dc6988 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotStringArray.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 1a226dbc..c59f8745 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -30,7 +30,7 @@ // Local include // Global include - +#include //////////////////////////////////////////////////////////////////////////////// CBotVar* MakeListVars(CBotVar** ppVars, bool bSetVal) diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 739800eb..bf958bc8 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -20,16 +20,19 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotString.h" #include "CBotTypResult.h" // Local include // Global include +#include +// Forward declaration class CBotString; +class CBotVar; +class CBotToken; +class CBotCStack; /*! * \brief MakeListVars Transforms the array of pointers to variables in a diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index 68132ad2..1abedb33 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "../CBotDll.h" - #include "../CBotDefines.h" #include "../CBotString.h" @@ -30,6 +28,9 @@ // Global include +// Forward declaration +class CBotVarClass; +class CBotInstr; /*! * \brief The CBotVar class Class for managing variables. May be useful to the diff --git a/src/CBot/CBotVar/CBotVarBoolean.cpp b/src/CBot/CBotVar/CBotVarBoolean.cpp index f1c59347..b7680078 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.cpp +++ b/src/CBot/CBotVar/CBotVarBoolean.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotVarBoolean.h" +#include "CBotEnums.h" + #include "CBotUtils.h" // Local include @@ -30,18 +32,17 @@ //////////////////////////////////////////////////////////////////////////////// CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) { - m_token = new CBotToken(name); - m_next = nullptr; - m_pMyThis = nullptr; - m_pUserPtr = nullptr; + m_token = new CBotToken(name); + m_next = nullptr; + m_pMyThis = nullptr; + m_pUserPtr = nullptr; m_InitExpr = nullptr; - m_LimExpr = nullptr; - m_type = CBotTypBoolean; - m_binit = InitType::UNDEF; - m_bStatic = false; + m_LimExpr = nullptr; + m_type = CBotTypBoolean; + m_binit = InitType::UNDEF; + m_bStatic = false; m_mPrivate = 0; - - m_val = 0; + m_val = 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp index 465c2940..9f25fbb7 100644 --- a/src/CBot/CBotVar/CBotVarFloat.cpp +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotVarFloat.h" +#include "CBotEnums.h" + #include "CBotToken.h" #include "CBotUtils.h" diff --git a/src/CBot/CBotVar/CBotVarFloat.h b/src/CBot/CBotVar/CBotVarFloat.h index 07a3ecc6..e55378a7 100644 --- a/src/CBot/CBotVar/CBotVarFloat.h +++ b/src/CBot/CBotVar/CBotVarFloat.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp index 76f89c12..67220a1a 100644 --- a/src/CBot/CBotVar/CBotVarInt.cpp +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotVarInt.h" +#include "CBotEnums.h" + #include "CBotToken.h" #include "CBotUtils.h" diff --git a/src/CBot/CBotVar/CBotVarInt.h b/src/CBot/CBotVar/CBotVarInt.h index 65ebb8c3..524c2762 100644 --- a/src/CBot/CBotVar/CBotVarInt.h +++ b/src/CBot/CBotVar/CBotVarInt.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarString.cpp b/src/CBot/CBotVar/CBotVarString.cpp index 7b740147..09c4e761 100644 --- a/src/CBot/CBotVar/CBotVarString.cpp +++ b/src/CBot/CBotVar/CBotVarString.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBotVarString.h" +#include "CBotEnums.h" + #include "CBotToken.h" #include "CBotUtils.h" diff --git a/src/CBot/CBotVar/CBotVarString.h b/src/CBot/CBotVar/CBotVarString.h index 93007bee..75b4b1d0 100644 --- a/src/CBot/CBotVar/CBotVarString.h +++ b/src/CBot/CBotVar/CBotVarString.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBotDll.h" - #include "CBotVar/CBotVar.h" // Local include diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 7cd1bdb0..e2c82908 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -19,7 +19,6 @@ #include "level/robotmain.h" -#include "CBot/CBotDll.h" #include "CBot/CBotFileUtils.h" // TODO must be replaced by CBot.h #include "CBot/CBotClass.h" diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index 1939f655..b7a7929d 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -20,8 +20,6 @@ #include "object/old_object.h" -#include "CBot/CBotDll.h" - #include "app/app.h" #include "common/global.h" diff --git a/src/script/script.h b/src/script/script.h index d4a42eb7..be3f055a 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -25,7 +25,6 @@ #pragma once // TODO replace by CBot.h -#include "CBot/CBotDll.h" #include "CBot/CBotProgram.h" #include diff --git a/src/script/scriptfunc.h b/src/script/scriptfunc.h index 07b21cb9..13c48893 100644 --- a/src/script/scriptfunc.h +++ b/src/script/scriptfunc.h @@ -24,7 +24,6 @@ #pragma once -#include "CBot/CBotDll.h" #include "CBot/CBotTypResult.h" #include "common/error.h" @@ -36,6 +35,7 @@ class CObject; class CScript; class CExchangePost; +class CBotVar; class CScriptFunctions diff --git a/src/ui/studio.cpp b/src/ui/studio.cpp index 614a2888..511372fb 100644 --- a/src/ui/studio.cpp +++ b/src/ui/studio.cpp @@ -20,8 +20,6 @@ #include "ui/studio.h" -#include "CBot/CBotDll.h" - #include "app/app.h" #include "app/pausemanager.h" diff --git a/test/cbot/console/main.cpp b/test/cbot/console/main.cpp index 83e06dea..54ea1690 100644 --- a/test/cbot/console/main.cpp +++ b/test/cbot/console/main.cpp @@ -2,7 +2,8 @@ #include #include "common/restext.h" -#include "CBot/CBotDll.h" + +#include "CBot/CBot.h" CBotTypResult cMessage(CBotVar* &var, void* user) { @@ -91,4 +92,4 @@ int main(int argc, char* argv[]) } return runErrors ? 3 : 0; -} \ No newline at end of file +} diff --git a/test/unit/CBot/CBotString_test.cpp b/test/unit/CBot/CBotString_test.cpp index b0d1ede8..3e6b6da0 100644 --- a/test/unit/CBot/CBotString_test.cpp +++ b/test/unit/CBot/CBotString_test.cpp @@ -18,8 +18,6 @@ */ // Modules inlcude -#include "CBot/CBotDll.h" - #include "CBot/CBotString.h" // Local include From 889c0fbe8e96c2a76ab8cae9884b90241e351bd2 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 17:25:46 +0100 Subject: [PATCH 74/91] Split file StringFunctions.cpp into two files StringFunctions.h and StringFunctions.cpp. --- src/CBot/CBotInstr/CBotExprNum.cpp | 98 --------------------- src/CBot/CBotProgram.cpp | 4 +- src/CBot/CBotUtils.cpp | 94 ++++++++++++++++++++ src/CBot/CBotUtils.h | 15 ++++ src/CBot/CMakeLists.txt | 1 + src/CBot/StringFunctions.cpp | 70 +++++---------- src/CBot/StringFunctions.h | 137 +++++++++++++++++++++++++++++ 7 files changed, 273 insertions(+), 146 deletions(-) create mode 100644 src/CBot/StringFunctions.h diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp index 65dbd780..acf1e9a2 100644 --- a/src/CBot/CBotInstr/CBotExprNum.cpp +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -29,104 +29,6 @@ // Global include - -//////////////////////////////////////////////////////////////////////////////// -// converts a string into integer -// may be of the form 0xabc123 -long GetNumInt(const char* p) -{ - long num = 0; - while (*p >= '0' && *p <= '9') - { - num = num * 10 + *p - '0'; - p++; - } - if (*p == 'x' || *p == 'X') - { - while (*++p != 0) - { - if (*p >= '0' && *p <= '9') - { - num = num * 16 + *p - '0'; - continue; - } - if (*p >= 'A' && *p <= 'F') - { - num = num * 16 + *p - 'A' + 10; - continue; - } - if (*p >= 'a' && *p <= 'f') - { - num = num * 16 + *p - 'a' + 10; - continue; - } - break; - } - } - return num; -} - -//////////////////////////////////////////////////////////////////////////////// -// converts a string into a float number -extern float GetNumFloat(const char* p) -{ - double num = 0; - double div = 10; - bool bNeg = false; - - if (*p == '-') - { - bNeg = true; - p++; - } - while (*p >= '0' && *p <= '9') - { - num = num * 10. + (*p - '0'); - p++; - } - - if (*p == '.') - { - p++; - while (*p >= '0' && *p <= '9') - { - num = num + (*p - '0') / div; - div = div * 10; - p++; - } - } - - int exp = 0; - if (*p == 'e' || *p == 'E') - { - char neg = 0; - p++; - if (*p == '-' || *p == '+') neg = *p++; - - while (*p >= '0' && *p <= '9') - { - exp = exp * 10 + (*p - '0'); - p++; - } - if (neg == '-') exp = -exp; - } - - while (exp > 0) - { - num *= 10.0; - exp--; - } - - while (exp < 0) - { - num /= 10.0; - exp++; - } - - if (bNeg) num = -num; - return static_cast(num); -} - //////////////////////////////////////////////////////////////////////////////// CBotExprNum::CBotExprNum() { diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 4e3f2c49..4ee5bffb 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -20,6 +20,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotVar/CBotVar.h" + #include "CBotCall.h" #include "CBotStack.h" #include "CBotCStack.h" @@ -28,7 +30,7 @@ #include "CBotInstr/CBotFunction.h" -#include "StringFunctions.cpp" +#include "StringFunctions.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index c59f8745..5e38cb5f 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -178,3 +178,97 @@ void DestructElements(CBotString* pOldData, int nCount) pOldData++; } } + +//////////////////////////////////////////////////////////////////////////////// +long GetNumInt(const char* p) +{ + long num = 0; + while (*p >= '0' && *p <= '9') + { + num = num * 10 + *p - '0'; + p++; + } + if (*p == 'x' || *p == 'X') + { + while (*++p != 0) + { + if (*p >= '0' && *p <= '9') + { + num = num * 16 + *p - '0'; + continue; + } + if (*p >= 'A' && *p <= 'F') + { + num = num * 16 + *p - 'A' + 10; + continue; + } + if (*p >= 'a' && *p <= 'f') + { + num = num * 16 + *p - 'a' + 10; + continue; + } + break; + } + } + return num; +} + +//////////////////////////////////////////////////////////////////////////////// +float GetNumFloat(const char* p) +{ + double num = 0; + double div = 10; + bool bNeg = false; + + if (*p == '-') + { + bNeg = true; + p++; + } + while (*p >= '0' && *p <= '9') + { + num = num * 10. + (*p - '0'); + p++; + } + + if (*p == '.') + { + p++; + while (*p >= '0' && *p <= '9') + { + num = num + (*p - '0') / div; + div = div * 10; + p++; + } + } + + int exp = 0; + if (*p == 'e' || *p == 'E') + { + char neg = 0; + p++; + if (*p == '-' || *p == '+') neg = *p++; + + while (*p >= '0' && *p <= '9') + { + exp = exp * 10 + (*p - '0'); + p++; + } + if (neg == '-') exp = -exp; + } + + while (exp > 0) + { + num *= 10.0; + exp--; + } + + while (exp < 0) + { + num /= 10.0; + exp++; + } + + if (bNeg) num = -num; + return static_cast(num); +} diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index bf958bc8..0ce76414 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -116,3 +116,18 @@ void ConstructElements(CBotString* pNewData, int nCount); * \param nCount */ void DestructElements(CBotString* pOldData, int nCount); + +/*! + * \brief GetNumInt Converts a string into integer may be of the form 0xabc123. + * \param p + * \return + */ +long GetNumInt(const char* p); + +/*! + * \brief GetNumFloat Converts a string into a float number. + * \param p + * \return + */ +float GetNumFloat(const char* p); + diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 3bb1eb91..a2d530e1 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES CBotCallMethode.cpp CBotStringArray.cpp CBotTypResult.cpp + StringFunctions.cpp CBotInstr/CBotInstr.cpp CBotInstr/CBotInstrUtils.cpp CBotInstr/CBotWhile.cpp diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index 640efc19..c38af371 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -17,15 +17,20 @@ * along with this program. If not, see http://gnu.org/licenses */ +// Modules inlcude +#include "StringFunctions.h" + +#include "CBotProgram.h" +#include "CBotEnums.h" + #include "CBotVar/CBotVar.h" -// definition of string functions +// Local include -// gives the length of a chain -// execution - +// Global include +//////////////////////////////////////////////////////////////////////////////// bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -45,9 +50,7 @@ bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// int xxx ( string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -64,10 +67,7 @@ CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypInt ); } - -// gives the left side of a chain -// execution - +//////////////////////////////////////////////////////////////////////////////// bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -100,9 +100,7 @@ bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// string xxx ( string, int ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -127,9 +125,7 @@ CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypString ); } -// gives the right of a string -// execution - +//////////////////////////////////////////////////////////////////////////////// bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -162,9 +158,7 @@ bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// gives the central part of a chain -// execution - +//////////////////////////////////////////////////////////////////////////////// bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -214,9 +208,7 @@ bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// gives the central part of a chain -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -251,10 +243,7 @@ CBotTypResult cStrStrIntInt( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypString ); } - -// gives the number stored in a string -// execution - +//////////////////////////////////////////////////////////////////////////////// bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -276,9 +265,7 @@ bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// float xxx ( string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -295,10 +282,7 @@ CBotTypResult cFloatStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypFloat ); } - -// find string in other -// exécution - +//////////////////////////////////////////////////////////////////////////////// bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -330,9 +314,7 @@ bool rStrFind( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// int xxx ( string, string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -357,9 +339,7 @@ CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypInt ); } -// gives a string to uppercase -// exécution - +//////////////////////////////////////////////////////////////////////////////// bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -382,9 +362,7 @@ bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// gives a string to lowercase -// exécution - +//////////////////////////////////////////////////////////////////////////////// bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { // it takes a parameter @@ -407,9 +385,7 @@ bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) return true; } -// string xxx ( string ) -// compilation - +//////////////////////////////////////////////////////////////////////////////// CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) { // it takes a parameter @@ -426,7 +402,7 @@ CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ) return CBotTypResult( CBotTypString ); } - +//////////////////////////////////////////////////////////////////////////////// void InitStringFunctions() { CBotProgram::AddFunction("strlen", rStrLen, cIntStr ); diff --git a/src/CBot/StringFunctions.h b/src/CBot/StringFunctions.h new file mode 100644 index 00000000..da534aad --- /dev/null +++ b/src/CBot/StringFunctions.h @@ -0,0 +1,137 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#pragma once + +// Modules inlcude +#include "CBotTypResult.h" + +// Local include + +// Global include + +// Forward declaration +class CBotVar; + +/*! + * \brief rStrLen Gives the length of a chain execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrLen( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cIntStr int xxx ( string ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cIntStr( CBotVar* &pVar, void* pUser ); + +/*! + * \brief rStrLeft Gives the left side of a chain execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrLeft( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cStrStrInt string xxx ( string, int ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cStrStrInt( CBotVar* &pVar, void* pUser ); + +/*! + * \brief rStrRight Gives the right of a string execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrRight( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief rStrMid Gives the central part of a chain execution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrMid( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief rStrVal Gives the number stored in a string execution. + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrVal( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cIntStrStr int xxx ( string, string ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cIntStrStr( CBotVar* &pVar, void* pUser ); + +/*! + * \brief rStrUpper Gives a string to uppercase exécution + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrUpper( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief rStrLower Gives a string to lowercase exécution. + * \param pVar + * \param pResult + * \param ex + * \param pUser + * \return + */ +bool rStrLower( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ); + +/*! + * \brief cStrStr String xxx ( string ) compilation + * \param pVar + * \param pUser + * \return + */ +CBotTypResult cStrStr( CBotVar* &pVar, void* pUser ); + +/*! + * \brief InitStringFunctions + */ +void InitStringFunctions(); From 751999064bbb478e274b6b20a9fe96697b6e647b Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 17:36:01 +0100 Subject: [PATCH 75/91] Moving global files function from CBotProgram.cpp to CBotFileUtils.cpp. --- src/CBot/CBot.h | 15 ---- src/CBot/CBotClass.cpp | 1 + src/CBot/CBotFileUtils.cpp | 113 ++++++++++++++++++++++++++++ src/CBot/CBotFileUtils.h | 85 +++++++++++++++++++++ src/CBot/CBotProgram.cpp | 108 +------------------------- src/CBot/CBotStack.cpp | 2 + src/CBot/CBotVar/CBotVarArray.cpp | 2 + src/CBot/CBotVar/CBotVarClass.cpp | 2 + src/CBot/CBotVar/CBotVarPointer.cpp | 3 + 9 files changed, 209 insertions(+), 122 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 1e281109..458cfdd2 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -50,21 +50,6 @@ class CBotWhile; // while (...) {...}; class CBotIf; // if (...) {...} else {...} class CBotDefParam; // paramerer list of a function - -extern bool SaveVar(FILE* pf, CBotVar* pVar); - -extern bool WriteWord(FILE* pf, unsigned short w); -extern bool ReadWord(FILE* pf, unsigned short& w); -extern bool ReadLong(FILE* pf, long& w); -extern bool WriteFloat(FILE* pf, float w); -extern bool WriteLong(FILE* pf, long w); -extern bool ReadFloat(FILE* pf, float& w); -extern bool ReadString(FILE* pf, CBotString& s); -extern bool WriteType(FILE* pf, CBotTypResult type); -extern bool ReadType(FILE* pf, CBotTypResult& type); - -extern float GetNumFloat( const char* p ); - #if 0 extern void DEBUG( const char* text, int val, CBotStack* pile ); #endif diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 94a05346..172f2cd3 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -32,6 +32,7 @@ #include "CBotStack.h" #include "CBotCStack.h" #include "CBotUtils.h" +#include "CBotFileUtils.h" #include "CBotCallMethode.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotFileUtils.cpp b/src/CBot/CBotFileUtils.cpp index 1c585713..bb561bdf 100644 --- a/src/CBot/CBotFileUtils.cpp +++ b/src/CBot/CBotFileUtils.cpp @@ -20,6 +20,11 @@ // Modules inlcude #include "CBotFileUtils.h" +#include "CBotString.h" +#include "CBotClass.h" + +#include "CBotEnums.h" + // Local include // Global include @@ -60,3 +65,111 @@ std::size_t fRead(void *buffer, { return fread(buffer, elemsize, length, filehandle); } + + +//////////////////////////////////////////////////////////////////////////////// +bool ReadWord(FILE* pf, unsigned short& w) +{ + size_t lg; + + lg = fread(&w, sizeof( unsigned short ), 1, pf ); + + return (lg == 1); +} + +//////////////////////////////////////////////////////////////////////////////// +bool ReadFloat(FILE* pf, float& w) +{ + size_t lg; + + lg = fread(&w, sizeof( float ), 1, pf ); + + return (lg == 1); +} + +//////////////////////////////////////////////////////////////////////////////// +bool WriteLong(FILE* pf, long w) +{ + size_t lg; + + lg = fwrite(&w, sizeof( long ), 1, pf ); + + return (lg == 1); +} + +//////////////////////////////////////////////////////////////////////////////// +bool ReadLong(FILE* pf, long& w) +{ + size_t lg; + + lg = fread(&w, sizeof( long ), 1, pf ); + + return (lg == 1); +} + +//////////////////////////////////////////////////////////////////////////////// +bool ReadString(FILE* pf, CBotString& s) +{ + unsigned short w; + char buf[1000]; + size_t lg1, lg2; + + if (!ReadWord(pf, w)) return false; + lg1 = w; + lg2 = fread(buf, 1, lg1, pf ); + buf[lg2] = 0; + + s = buf; + return (lg1 == lg2); +} + +//////////////////////////////////////////////////////////////////////////////// +bool WriteType(FILE* pf, CBotTypResult type) +{ + int typ = type.GetType(); + if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; + if ( !WriteWord(pf, typ) ) return false; + if ( typ == CBotTypClass ) + { + CBotClass* p = type.GetClass(); + if ( !WriteString(pf, p->GetName()) ) return false; + } + if ( type.Eq( CBotTypArrayBody ) || + type.Eq( CBotTypArrayPointer ) ) + { + if ( !WriteWord(pf, type.GetLimite()) ) return false; + if ( !WriteType(pf, type.GetTypElem()) ) return false; + } + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool ReadType(FILE* pf, CBotTypResult& type) +{ + unsigned short w, ww; + if ( !ReadWord(pf, w) ) return false; + type.SetType(w); + + if ( type.Eq( CBotTypIntrinsic ) ) + { + type = CBotTypResult( w, "point" ); + } + + if ( type.Eq( CBotTypClass ) ) + { + CBotString s; + if ( !ReadString(pf, s) ) return false; + type = CBotTypResult( w, s ); + } + + if ( type.Eq( CBotTypArrayPointer ) || + type.Eq( CBotTypArrayBody ) ) + { + CBotTypResult r; + if ( !ReadWord(pf, ww) ) return false; + if ( !ReadType(pf, r) ) return false; + type = CBotTypResult( w, r ); + type.SetLimite(static_cast(ww)); + } + return true; +} diff --git a/src/CBot/CBotFileUtils.h b/src/CBot/CBotFileUtils.h index 8f76ec4d..1ac1664b 100644 --- a/src/CBot/CBotFileUtils.h +++ b/src/CBot/CBotFileUtils.h @@ -20,12 +20,17 @@ #pragma once // Modules inlcude +#include "CBotTypResult.h" // Local include // Global include #include +// Forward declaration +class CBotVar; +class CBotString; + /////////////////////////////////////////////////////////////////////////////// // routines for file management (* FILE) @@ -69,3 +74,83 @@ std::size_t fRead(void *buffer, std::size_t elemsize, std::size_t length, FILE* filehandle); + +/*! + * \brief SaveVar + * \param pf + * \param pVar + * \return + */ +bool SaveVar(FILE* pf, CBotVar* pVar); + +/*! + * \brief WriteWord + * \param pf + * \param w + * \return + */ +bool WriteWord(FILE* pf, unsigned short w); + +/*! + * \brief ReadWord + * \param pf + * \param w + * \return + */ +bool ReadWord(FILE* pf, unsigned short& w); + +/*! + * \brief ReadLong + * \param pf + * \param w + * \return + */ +bool ReadLong(FILE* pf, long& w); + +/*! + * \brief WriteFloat + * \param pf + * \param w + * \return + */ +bool WriteFloat(FILE* pf, float w); + +/*! + * \brief WriteLong + * \param pf + * \param w + * \return + */ +bool WriteLong(FILE* pf, long w); + +/*! + * \brief ReadFloat + * \param pf + * \param w + * \return + */ +bool ReadFloat(FILE* pf, float& w); + +/*! + * \brief ReadString + * \param pf + * \param s + * \return + */ +bool ReadString(FILE* pf, CBotString& s); + +/*! + * \brief WriteType + * \param pf + * \param type + * \return + */ +bool WriteType(FILE* pf, CBotTypResult type); + +/*! + * \brief ReadType + * \param pf + * \param type + * \return + */ +bool ReadType(FILE* pf, CBotTypResult& type); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 4ee5bffb..187f560e 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -27,6 +27,7 @@ #include "CBotCStack.h" #include "CBotClass.h" #include "CBotUtils.h" +#include "CBotFileUtils.h" #include "CBotInstr/CBotFunction.h" @@ -385,113 +386,6 @@ bool CBotProgram::AddFunction(const char* name, return CBotCall::AddFunction(name, rExec, rCompile); } -//////////////////////////////////////////////////////////////////////////////// -bool ReadWord(FILE* pf, unsigned short& w) -{ - size_t lg; - - lg = fread(&w, sizeof( unsigned short ), 1, pf ); - - return (lg == 1); -} - -//////////////////////////////////////////////////////////////////////////////// -bool ReadFloat(FILE* pf, float& w) -{ - size_t lg; - - lg = fread(&w, sizeof( float ), 1, pf ); - - return (lg == 1); -} - -//////////////////////////////////////////////////////////////////////////////// -bool WriteLong(FILE* pf, long w) -{ - size_t lg; - - lg = fwrite(&w, sizeof( long ), 1, pf ); - - return (lg == 1); -} - -//////////////////////////////////////////////////////////////////////////////// -bool ReadLong(FILE* pf, long& w) -{ - size_t lg; - - lg = fread(&w, sizeof( long ), 1, pf ); - - return (lg == 1); -} - -//////////////////////////////////////////////////////////////////////////////// -bool ReadString(FILE* pf, CBotString& s) -{ - unsigned short w; - char buf[1000]; - size_t lg1, lg2; - - if (!ReadWord(pf, w)) return false; - lg1 = w; - lg2 = fread(buf, 1, lg1, pf ); - buf[lg2] = 0; - - s = buf; - return (lg1 == lg2); -} - -//////////////////////////////////////////////////////////////////////////////// -bool WriteType(FILE* pf, CBotTypResult type) -{ - int typ = type.GetType(); - if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; - if ( !WriteWord(pf, typ) ) return false; - if ( typ == CBotTypClass ) - { - CBotClass* p = type.GetClass(); - if ( !WriteString(pf, p->GetName()) ) return false; - } - if ( type.Eq( CBotTypArrayBody ) || - type.Eq( CBotTypArrayPointer ) ) - { - if ( !WriteWord(pf, type.GetLimite()) ) return false; - if ( !WriteType(pf, type.GetTypElem()) ) return false; - } - return true; -} - -//////////////////////////////////////////////////////////////////////////////// -bool ReadType(FILE* pf, CBotTypResult& type) -{ - unsigned short w, ww; - if ( !ReadWord(pf, w) ) return false; - type.SetType(w); - - if ( type.Eq( CBotTypIntrinsic ) ) - { - type = CBotTypResult( w, "point" ); - } - - if ( type.Eq( CBotTypClass ) ) - { - CBotString s; - if ( !ReadString(pf, s) ) return false; - type = CBotTypResult( w, s ); - } - - if ( type.Eq( CBotTypArrayPointer ) || - type.Eq( CBotTypArrayBody ) ) - { - CBotTypResult r; - if ( !ReadWord(pf, ww) ) return false; - if ( !ReadType(pf, r) ) return false; - type = CBotTypResult( w, r ); - type.SetLimite(static_cast(ww)); - } - return true; -} - //////////////////////////////////////////////////////////////////////////////// bool rSizeOf( CBotVar* pVar, CBotVar* pResult, int& ex, void* pUser ) { diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 91f91a93..ac312c9a 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -26,6 +26,8 @@ #include "CBotVar/CBotVarPointer.h" #include "CBotVar/CBotVarClass.h" +#include "CBotFileUtils.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp index b030e7ae..59a3b97d 100644 --- a/src/CBot/CBotVar/CBotVarArray.cpp +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -21,6 +21,8 @@ #include "CBotVarArray.h" #include "CBotVarClass.h" +#include "CBotFileUtils.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index 4d6ee787..467255cb 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -24,6 +24,8 @@ #include "CBotStack.h" #include "CBotDefines.h" +#include "CBotFileUtils.h" + #include "CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp index df333221..9bbaee78 100644 --- a/src/CBot/CBotVar/CBotVarPointer.cpp +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -23,6 +23,9 @@ #include "CBot.h" #include "CBotClass.h" #include "CBotVarClass.h" + +#include "CBotFileUtils.h" + // Local include // Global include From b28e2aec0170930d7bdbec9288cb7318217bf06f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 17:38:16 +0100 Subject: [PATCH 76/91] Moving some define from CBot.h to CBotDefines.h. --- src/CBot/CBot.h | 5 ----- src/CBot/CBotDefines.h | 5 +++++ src/CBot/CBotStack.h | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 458cfdd2..97e2919f 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -29,11 +29,6 @@ #include "CBotToken.h" // token management #include "CBotProgram.h" -#define STACKMEM 1 /// \def preserve memory for the execution stack -#define MAXSTACK 990 /// \def stack size reserved - -#define EOX (reinterpret_cast(-1)) /// \def tag special condition - ///////////////////////////////////////////////////////////////////// // forward declaration diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index 9830fcbf..9cb70ab7 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -28,6 +28,11 @@ // Global include +#define STACKMEM 1 /// \def preserve memory for the execution stack +#define MAXSTACK 990 /// \def stack size reserved + +#define EOX (reinterpret_cast(-1)) /// \def tag special condition + #define MAXARRAYSIZE 9999 // variable type SetPrivate / IsPrivate diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h index 36f45b1e..283a8ae3 100644 --- a/src/CBot/CBotStack.h +++ b/src/CBot/CBotStack.h @@ -22,6 +22,8 @@ // Modules inlcude #include "CBot.h" +#include "CBotDefines.h" + // Local include // Global include From cedaaad45901d3973bfcd35b616294fccc764808 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 17:39:35 +0100 Subject: [PATCH 77/91] Deleted file CBot.cpp. --- src/CBot/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index a2d530e1..0d1cc7cd 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,5 +1,4 @@ set(SOURCES - CBot.cpp CBotUtils.cpp CBotFileUtils.cpp CBotClass.cpp From 575ff47c829c775eb92cac058115bda3d54149d1 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 17:54:40 +0100 Subject: [PATCH 78/91] Cleaning the CBot.h file. --- src/CBot/CBot.h | 32 +++---------------------- src/CBot/CBotCallMethode.h | 8 +++++-- src/CBot/CBotDefParam.cpp | 2 -- src/CBot/CBotInstr/CBotBlock.h | 2 -- src/CBot/CBotInstr/CBotBoolExpr.h | 2 -- src/CBot/CBotInstr/CBotBoolean.h | 2 -- src/CBot/CBotInstr/CBotBreak.h | 2 -- src/CBot/CBotInstr/CBotCase.h | 2 -- src/CBot/CBotInstr/CBotCatch.h | 2 -- src/CBot/CBotInstr/CBotClassInst.h | 2 -- src/CBot/CBotInstr/CBotCondition.h | 2 -- src/CBot/CBotInstr/CBotDo.h | 2 -- src/CBot/CBotInstr/CBotEmpty.h | 2 -- src/CBot/CBotInstr/CBotExprAlpha.h | 2 -- src/CBot/CBotInstr/CBotExprBool.h | 2 -- src/CBot/CBotInstr/CBotExprNan.h | 2 -- src/CBot/CBotInstr/CBotExprNull.h | 2 -- src/CBot/CBotInstr/CBotExprNum.h | 2 -- src/CBot/CBotInstr/CBotExprUnaire.h | 2 -- src/CBot/CBotInstr/CBotExprVar.h | 2 -- src/CBot/CBotInstr/CBotExpression.h | 2 -- src/CBot/CBotInstr/CBotFieldExpr.h | 2 -- src/CBot/CBotInstr/CBotFloat.h | 2 -- src/CBot/CBotInstr/CBotFor.h | 2 -- src/CBot/CBotInstr/CBotFunction.cpp | 2 -- src/CBot/CBotInstr/CBotFunction.h | 2 -- src/CBot/CBotInstr/CBotIString.h | 2 -- src/CBot/CBotInstr/CBotIf.h | 2 -- src/CBot/CBotInstr/CBotIndexExpr.h | 2 -- src/CBot/CBotInstr/CBotInstArray.h | 2 -- src/CBot/CBotInstr/CBotInstrCall.h | 2 -- src/CBot/CBotInstr/CBotInstrMethode.h | 2 -- src/CBot/CBotInstr/CBotInt.h | 2 -- src/CBot/CBotInstr/CBotLeftExpr.h | 2 -- src/CBot/CBotInstr/CBotLeftExprVar.h | 2 -- src/CBot/CBotInstr/CBotListArray.h | 2 -- src/CBot/CBotInstr/CBotListExpression.h | 2 -- src/CBot/CBotInstr/CBotListInstr.h | 2 -- src/CBot/CBotInstr/CBotLogicExpr.h | 2 -- src/CBot/CBotInstr/CBotNew.h | 2 -- src/CBot/CBotInstr/CBotParExpr.h | 2 -- src/CBot/CBotInstr/CBotPostIncExpr.h | 2 -- src/CBot/CBotInstr/CBotPreIncExpr.h | 2 -- src/CBot/CBotInstr/CBotReturn.h | 2 -- src/CBot/CBotInstr/CBotSwitch.h | 2 -- src/CBot/CBotInstr/CBotThrow.h | 2 -- src/CBot/CBotInstr/CBotTry.h | 1 - src/CBot/CBotInstr/CBotTwoOpExpr.h | 2 -- src/CBot/CBotInstr/CBotWhile.h | 2 -- src/CBot/CBotProgram.cpp | 2 -- src/CBot/CBotStack.h | 18 +++++++++----- src/CBot/CBotString.cpp | 5 ++-- src/CBot/CBotToken.cpp | 2 +- src/CBot/CBotVar/CBotVar.cpp | 2 ++ src/CBot/CBotVar/CBotVarArray.cpp | 4 +++- src/CBot/CBotVar/CBotVarArray.h | 1 - src/CBot/CBotVar/CBotVarClass.h | 2 -- src/CBot/CBotVar/CBotVarPointer.cpp | 3 ++- 58 files changed, 31 insertions(+), 141 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 97e2919f..868bf137 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -17,34 +17,8 @@ * along with this program. If not, see http://gnu.org/licenses */ -//////////////////////////////////////////////////////////////////// -/** - * \file CBot.h - * \brief Interpreter of the language CBot for COLOBOT game - */ +// Modules inlcude -#pragma once +// Local include -#include "resource.h" -#include "CBotToken.h" // token management -#include "CBotProgram.h" - - -///////////////////////////////////////////////////////////////////// -// forward declaration - -class CBotParExpr; // basic type or instruction in parenthesis - // Toto.truc - // 12.5 - // "string" - // ( expression ) -class CBotExprVar; // a variable name as - // Toto - // chose.truc.machin -class CBotWhile; // while (...) {...}; -class CBotIf; // if (...) {...} else {...} -class CBotDefParam; // paramerer list of a function - -#if 0 -extern void DEBUG( const char* text, int val, CBotStack* pile ); -#endif +// Global include diff --git a/src/CBot/CBotCallMethode.h b/src/CBot/CBotCallMethode.h index 8ea40254..d60a9b77 100644 --- a/src/CBot/CBotCallMethode.h +++ b/src/CBot/CBotCallMethode.h @@ -20,12 +20,16 @@ #pragma once // Modules inlcude -#include "CBot.h" +#include "CBotTypResult.h" +#include "CBotString.h" // Local include // Global include - +class CBotVar; +class CBotCStack; +class CBotStack; +class CBotToken; /*! * \brief The CBotCallMethode class Class managing the methods declared by diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp index 9af6043f..fe7ce8bf 100644 --- a/src/CBot/CBotDefParam.cpp +++ b/src/CBot/CBotDefParam.cpp @@ -20,8 +20,6 @@ // Modules inlcude #include "CBotDefParam.h" -#include "CBot.h" - #include "CBotUtils.h" #include "CBotCStack.h" diff --git a/src/CBot/CBotInstr/CBotBlock.h b/src/CBot/CBotInstr/CBotBlock.h index 6c7c6334..7df0bb5b 100644 --- a/src/CBot/CBotInstr/CBotBlock.h +++ b/src/CBot/CBotInstr/CBotBlock.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.h b/src/CBot/CBotInstr/CBotBoolExpr.h index e50d8a63..af6cd4f8 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.h +++ b/src/CBot/CBotInstr/CBotBoolExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolean.h b/src/CBot/CBotInstr/CBotBoolean.h index 217daadc..e589f508 100644 --- a/src/CBot/CBotInstr/CBotBoolean.h +++ b/src/CBot/CBotInstr/CBotBoolean.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBreak.h b/src/CBot/CBotInstr/CBotBreak.h index ae35a70f..78b39dd3 100644 --- a/src/CBot/CBotInstr/CBotBreak.h +++ b/src/CBot/CBotInstr/CBotBreak.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCase.h b/src/CBot/CBotInstr/CBotCase.h index e5043330..03ddddde 100644 --- a/src/CBot/CBotInstr/CBotCase.h +++ b/src/CBot/CBotInstr/CBotCase.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCatch.h b/src/CBot/CBotInstr/CBotCatch.h index d4450ec1..60ee00a9 100644 --- a/src/CBot/CBotInstr/CBotCatch.h +++ b/src/CBot/CBotInstr/CBotCatch.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotClassInst.h b/src/CBot/CBotInstr/CBotClassInst.h index 3ee791f0..ab70c0f2 100644 --- a/src/CBot/CBotInstr/CBotClassInst.h +++ b/src/CBot/CBotInstr/CBotClassInst.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCondition.h b/src/CBot/CBotInstr/CBotCondition.h index 3958cadb..f36c998a 100644 --- a/src/CBot/CBotInstr/CBotCondition.h +++ b/src/CBot/CBotInstr/CBotCondition.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotDo.h b/src/CBot/CBotInstr/CBotDo.h index 11fe40f4..30f1db9d 100644 --- a/src/CBot/CBotInstr/CBotDo.h +++ b/src/CBot/CBotInstr/CBotDo.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotEmpty.h b/src/CBot/CBotInstr/CBotEmpty.h index dc50b49b..782e87ca 100644 --- a/src/CBot/CBotInstr/CBotEmpty.h +++ b/src/CBot/CBotInstr/CBotEmpty.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.h b/src/CBot/CBotInstr/CBotExprAlpha.h index 3cadc322..01e97a8e 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.h +++ b/src/CBot/CBotInstr/CBotExprAlpha.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprBool.h b/src/CBot/CBotInstr/CBotExprBool.h index 90f53daa..a81ec2ee 100644 --- a/src/CBot/CBotInstr/CBotExprBool.h +++ b/src/CBot/CBotInstr/CBotExprBool.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNan.h b/src/CBot/CBotInstr/CBotExprNan.h index 1f0d58d8..c7c25963 100644 --- a/src/CBot/CBotInstr/CBotExprNan.h +++ b/src/CBot/CBotInstr/CBotExprNan.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNull.h b/src/CBot/CBotInstr/CBotExprNull.h index 90b3f89a..662ded3d 100644 --- a/src/CBot/CBotInstr/CBotExprNull.h +++ b/src/CBot/CBotInstr/CBotExprNull.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNum.h b/src/CBot/CBotInstr/CBotExprNum.h index 07fca7e9..e9ccf932 100644 --- a/src/CBot/CBotInstr/CBotExprNum.h +++ b/src/CBot/CBotInstr/CBotExprNum.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprUnaire.h b/src/CBot/CBotInstr/CBotExprUnaire.h index 75b30a94..660afdd7 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.h +++ b/src/CBot/CBotInstr/CBotExprUnaire.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprVar.h b/src/CBot/CBotInstr/CBotExprVar.h index 638cf8fe..9757b898 100644 --- a/src/CBot/CBotInstr/CBotExprVar.h +++ b/src/CBot/CBotInstr/CBotExprVar.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotDefines.h" #include "CBotInstr.h" diff --git a/src/CBot/CBotInstr/CBotExpression.h b/src/CBot/CBotInstr/CBotExpression.h index ddf2e257..637ff051 100644 --- a/src/CBot/CBotInstr/CBotExpression.h +++ b/src/CBot/CBotInstr/CBotExpression.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotLeftExpr.h" #include "CBotInstr.h" diff --git a/src/CBot/CBotInstr/CBotFieldExpr.h b/src/CBot/CBotInstr/CBotFieldExpr.h index 41e828bd..4a56a3f6 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.h +++ b/src/CBot/CBotInstr/CBotFieldExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFloat.h b/src/CBot/CBotInstr/CBotFloat.h index 309697b0..3e095fe5 100644 --- a/src/CBot/CBotInstr/CBotFloat.h +++ b/src/CBot/CBotInstr/CBotFloat.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFor.h b/src/CBot/CBotInstr/CBotFor.h index d7d4256d..56442b0c 100644 --- a/src/CBot/CBotInstr/CBotFor.h +++ b/src/CBot/CBotInstr/CBotFor.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 0a629c39..f8e4b6df 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -23,8 +23,6 @@ #include "CBotInstr/CBotInstrUtils.h" -#include "CBot.h" - #include "CBotInstr/CBotBlock.h" #include "CBotInstr/CBotTwoOpExpr.h" #include "CBotInstr/CBotExpression.h" diff --git a/src/CBot/CBotInstr/CBotFunction.h b/src/CBot/CBotInstr/CBotFunction.h index f1fc3c8d..7c44dc3e 100644 --- a/src/CBot/CBotInstr/CBotFunction.h +++ b/src/CBot/CBotInstr/CBotFunction.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIString.h b/src/CBot/CBotInstr/CBotIString.h index 2ea8e4ee..1435bb36 100644 --- a/src/CBot/CBotInstr/CBotIString.h +++ b/src/CBot/CBotInstr/CBotIString.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIf.h b/src/CBot/CBotInstr/CBotIf.h index e39e5226..de4c3672 100644 --- a/src/CBot/CBotInstr/CBotIf.h +++ b/src/CBot/CBotInstr/CBotIf.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.h b/src/CBot/CBotInstr/CBotIndexExpr.h index a8e7e07a..fe1ea555 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.h +++ b/src/CBot/CBotInstr/CBotIndexExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstArray.h b/src/CBot/CBotInstr/CBotInstArray.h index 4ad025d6..2b02c66d 100644 --- a/src/CBot/CBotInstr/CBotInstArray.h +++ b/src/CBot/CBotInstr/CBotInstArray.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrCall.h b/src/CBot/CBotInstr/CBotInstrCall.h index fa634fed..db259039 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.h +++ b/src/CBot/CBotInstr/CBotInstrCall.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.h b/src/CBot/CBotInstr/CBotInstrMethode.h index ea398115..f9447609 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.h +++ b/src/CBot/CBotInstr/CBotInstrMethode.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInt.h b/src/CBot/CBotInstr/CBotInt.h index 085e6265..113897f6 100644 --- a/src/CBot/CBotInstr/CBotInt.h +++ b/src/CBot/CBotInstr/CBotInt.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.h b/src/CBot/CBotInstr/CBotLeftExpr.h index aaf9ee47..5f09b507 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.h +++ b/src/CBot/CBotInstr/CBotLeftExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.h b/src/CBot/CBotInstr/CBotLeftExprVar.h index fd693727..0c9e0cc2 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.h +++ b/src/CBot/CBotInstr/CBotLeftExprVar.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListArray.h b/src/CBot/CBotInstr/CBotListArray.h index 586cc864..32a155c6 100644 --- a/src/CBot/CBotInstr/CBotListArray.h +++ b/src/CBot/CBotInstr/CBotListArray.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListExpression.h b/src/CBot/CBotInstr/CBotListExpression.h index 451b2181..a4ea3b2f 100644 --- a/src/CBot/CBotInstr/CBotListExpression.h +++ b/src/CBot/CBotInstr/CBotListExpression.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListInstr.h b/src/CBot/CBotInstr/CBotListInstr.h index 0aafaf59..b0612d86 100644 --- a/src/CBot/CBotInstr/CBotListInstr.h +++ b/src/CBot/CBotInstr/CBotListInstr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLogicExpr.h b/src/CBot/CBotInstr/CBotLogicExpr.h index db6c67f6..25e67854 100644 --- a/src/CBot/CBotInstr/CBotLogicExpr.h +++ b/src/CBot/CBotInstr/CBotLogicExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotNew.h b/src/CBot/CBotInstr/CBotNew.h index 02598b1a..f93bb6be 100644 --- a/src/CBot/CBotInstr/CBotNew.h +++ b/src/CBot/CBotInstr/CBotNew.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotParExpr.h b/src/CBot/CBotInstr/CBotParExpr.h index 690d6efb..3fdbb639 100644 --- a/src/CBot/CBotInstr/CBotParExpr.h +++ b/src/CBot/CBotInstr/CBotParExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.h b/src/CBot/CBotInstr/CBotPostIncExpr.h index 3a30fcef..79bc86e2 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.h +++ b/src/CBot/CBotInstr/CBotPostIncExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.h b/src/CBot/CBotInstr/CBotPreIncExpr.h index 896c0c9a..cf91a22f 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.h +++ b/src/CBot/CBotInstr/CBotPreIncExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotReturn.h b/src/CBot/CBotInstr/CBotReturn.h index 1816bf2d..7af52e52 100644 --- a/src/CBot/CBotInstr/CBotReturn.h +++ b/src/CBot/CBotInstr/CBotReturn.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.h b/src/CBot/CBotInstr/CBotSwitch.h index b4cff5e4..6edb38b8 100644 --- a/src/CBot/CBotInstr/CBotSwitch.h +++ b/src/CBot/CBotInstr/CBotSwitch.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotThrow.h b/src/CBot/CBotInstr/CBotThrow.h index b5227ad7..cbe06e50 100644 --- a/src/CBot/CBotInstr/CBotThrow.h +++ b/src/CBot/CBotInstr/CBotThrow.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTry.h b/src/CBot/CBotInstr/CBotTry.h index bdc3d4d8..9c79b3a4 100644 --- a/src/CBot/CBotInstr/CBotTry.h +++ b/src/CBot/CBotInstr/CBotTry.h @@ -18,7 +18,6 @@ */ // Modules inlcude -#include "CBot.h" #include "CBotCatch.h" #include "CBotInstr.h" diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.h b/src/CBot/CBotInstr/CBotTwoOpExpr.h index 720755fc..63e20ddc 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.h +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.h b/src/CBot/CBotInstr/CBotWhile.h index 528fe8f1..a6fac948 100644 --- a/src/CBot/CBotInstr/CBotWhile.h +++ b/src/CBot/CBotInstr/CBotWhile.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotInstr.h" // Local include diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 187f560e..1bdd89c6 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -18,8 +18,6 @@ */ // Modules inlcude -#include "CBot.h" - #include "CBotVar/CBotVar.h" #include "CBotCall.h" diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h index 283a8ae3..0e8ddd03 100644 --- a/src/CBot/CBotStack.h +++ b/src/CBot/CBotStack.h @@ -20,17 +20,21 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotDefines.h" +#include "CBotTypResult.h" +#include "CBotString.h" // Local include // Global include +#include // Forward declaration class CBotInstr; class CBotCall; +class CBotVar; +class CBotProgram; +class CBotToken; /*! * \class CBotStack @@ -122,8 +126,9 @@ public: * \param [in] bModif Not used. Probably need to be removed * \return Found variable */ - CBotVar* FindVar(CBotToken* &pToken, bool bUpdate = false, - bool bModif = false); + CBotVar* FindVar(CBotToken* &pToken, + bool bUpdate = false, + bool bModif = false); /** * \brief Fetch a variable by its token. @@ -133,8 +138,9 @@ public: * \param [in] bModif Not used. Probably need to be removed * \return Found variable */ - CBotVar* FindVar(CBotToken& pToken, bool bUpdate = false, - bool bModif = false); + CBotVar* FindVar(CBotToken& pToken, + bool bUpdate = false, + bool bModif = false); /** * \brief Fetch variable by its name diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index 12cc546b..d8fc07c7 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -19,10 +19,9 @@ ///////////////////////////////////////////////////// +#include "CBotString.h" + //strings management - -#include "CBot.h" - #include #include #include diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index 2b06c0ff..d5840f99 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -18,7 +18,7 @@ */ // Modules inlcude -#include "CBot.h" +#include "CBotToken.h" // Local include diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index e5ce073e..4f75249c 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -33,6 +33,8 @@ #include "CBotClass.h" +#include "CBotEnums.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp index 59a3b97d..cece8117 100644 --- a/src/CBot/CBotVar/CBotVarArray.cpp +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -20,9 +20,11 @@ // Modules inlcude #include "CBotVarArray.h" #include "CBotVarClass.h" - +#include "CBotToken.h" #include "CBotFileUtils.h" +#include "CBotEnums.h" + // Local include // Global include diff --git a/src/CBot/CBotVar/CBotVarArray.h b/src/CBot/CBotVar/CBotVarArray.h index 65222ac6..650437f3 100644 --- a/src/CBot/CBotVar/CBotVarArray.h +++ b/src/CBot/CBotVar/CBotVarArray.h @@ -20,7 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" #include "CBotDefines.h" #include "CBotVar/CBotVar.h" diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h index 5089c408..ff6f9248 100644 --- a/src/CBot/CBotVar/CBotVarClass.h +++ b/src/CBot/CBotVar/CBotVarClass.h @@ -20,8 +20,6 @@ #pragma once // Modules inlcude -#include "CBot.h" - #include "CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp index 9bbaee78..ff01eec7 100644 --- a/src/CBot/CBotVar/CBotVarPointer.cpp +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -20,12 +20,13 @@ // Modules inlcude #include "CBotVarPointer.h" #include "CBotToken.h" -#include "CBot.h" #include "CBotClass.h" #include "CBotVarClass.h" #include "CBotFileUtils.h" +#include "CBotEnums.h" + // Local include // Global include From ece0666954adae4e7b02bdec4ea8c9b16778740c Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 18:29:25 +0100 Subject: [PATCH 79/91] Create new file CBot.h. Included only useful interfaces. --- src/CBot/CBot.h | 8 ++++++++ src/common/restext.cpp | 2 +- src/level/robotmain.cpp | 4 +--- src/object/implementation/programmable_impl.cpp | 2 +- src/script/script.cpp | 4 +--- src/script/script.h | 3 +-- src/script/scriptfunc.cpp | 4 +--- src/script/scriptfunc.h | 2 +- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 868bf137..bea5ce75 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -18,6 +18,14 @@ */ // Modules inlcude +#include "CBot/resource.h" +#include "CBot/CBotFileUtils.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotProgram.h" +#include "CBot/CBotTypResult.h" + +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/common/restext.cpp b/src/common/restext.cpp index 67ac2158..8b2cb435 100644 --- a/src/common/restext.cpp +++ b/src/common/restext.cpp @@ -22,7 +22,7 @@ #include "common/config.h" -#include "CBot/resource.h" +#include "CBot/CBot.h" #include "app/input.h" diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index e2c82908..51107497 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -19,9 +19,7 @@ #include "level/robotmain.h" -#include "CBot/CBotFileUtils.h" -// TODO must be replaced by CBot.h -#include "CBot/CBotClass.h" +#include "CBot/CBot.h" #include "app/app.h" #include "app/input.h" diff --git a/src/object/implementation/programmable_impl.cpp b/src/object/implementation/programmable_impl.cpp index 379929e5..cea959ce 100644 --- a/src/object/implementation/programmable_impl.cpp +++ b/src/object/implementation/programmable_impl.cpp @@ -40,7 +40,7 @@ #include "ui/controls/edit.h" -#include "CBot/CBotFileUtils.h" +#include "CBot/CBot.h" #include #include diff --git a/src/script/script.cpp b/src/script/script.cpp index 92c658f6..5908d43b 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -42,9 +42,7 @@ #include "ui/controls/interface.h" #include "ui/controls/list.h" -#include "CBot/CBotToken.h" -#include "CBot/CBotVar/CBotVar.h" -#include "CBot/CBotFileUtils.h" +#include "CBot/CBot.h" const int CBOT_IPF = 100; // CBOT: default number of instructions / frame diff --git a/src/script/script.h b/src/script/script.h index be3f055a..7bf9b62f 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -24,8 +24,7 @@ #pragma once -// TODO replace by CBot.h -#include "CBot/CBotProgram.h" +#include "CBot/CBot.h" #include #include diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index 252f09df..b2fce1c1 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -19,9 +19,7 @@ #include "script/scriptfunc.h" -// TODO must be replaced by CBot.h -#include "CBot/CBotClass.h" -#include "CBot/CBotVar/CBotVar.h" +#include "CBot/CBot.h" #include "app/app.h" diff --git a/src/script/scriptfunc.h b/src/script/scriptfunc.h index 13c48893..e739ecdd 100644 --- a/src/script/scriptfunc.h +++ b/src/script/scriptfunc.h @@ -24,7 +24,7 @@ #pragma once -#include "CBot/CBotTypResult.h" +#include "CBot/CBot.h" #include "common/error.h" From 922082b360add4944a92e0860fc40d77ccaf318f Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 18:33:55 +0100 Subject: [PATCH 80/91] Move define from resource.h to CBotDefines.h. --- src/CBot/CBotDefines.h | 64 +++++++++++++++++++++++++++- src/CBot/CBotInstr/CBotBlock.cpp | 2 + src/CBot/CBotInstr/CBotBoolExpr.cpp | 2 + src/CBot/CBotInstr/CBotCondition.cpp | 2 + src/CBot/resource.h | 61 -------------------------- 5 files changed, 68 insertions(+), 63 deletions(-) diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index 9cb70ab7..b1c73b38 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -21,8 +21,6 @@ // Modules inlcude -#include "CBotInstr.h" - // Local include // Global include @@ -119,3 +117,65 @@ #define CBotErrNotOpen 6013 // channel not open #define CBotErrRead 6014 // error while reading #define CBotErrWrite 6015 // writing error + + +// TODO: refactor & change to enum! + +// Compile errors +#define TX_OPENPAR 5000 +#define TX_CLOSEPAR 5001 +#define TX_NOTBOOL 5002 +#define TX_UNDEFVAR 5003 +#define TX_BADLEFT 5004 +#define TX_ENDOF 5005 +#define TX_OUTCASE 5006 +#define TX_CLOSEBLK 5008 +#define TX_ELSEWITHOUTIF 5009 +#define TX_OPENBLK 5010 +#define TX_BADTYPE 5011 +#define TX_REDEFVAR 5012 +#define TX_BAD2TYPE 5013 +#define TX_UNDEFCALL 5014 +#define TX_MISDOTS 5015 +#define TX_WHILE 5016 +#define TX_BREAK 5017 +#define TX_LABEL 5018 +#define TX_NOLABEL 5019 +#define TX_NOCASE 5020 +#define TX_BADNUM 5021 +#define TX_VOID 5022 +#define TX_NOTYP 5023 +#define TX_NOVAR 5024 +#define TX_NOFONC 5025 +#define TX_OVERPARAM 5026 +#define TX_REDEF 5027 +#define TX_LOWPARAM 5028 +#define TX_BADPARAM 5029 +#define TX_NUMPARAM 5030 +#define TX_NOITEM 5031 +#define TX_DOT 5032 +#define TX_NOCONST 5033 +#define TX_REDEFCLASS 5034 +#define TX_CLBRK 5035 +#define TX_RESERVED 5036 +#define TX_BADNEW 5037 +#define TX_BADSTRING 5039 +#define TX_BADINDEX 5040 +#define TX_PRIVATE 5041 +#define TX_NOPUBLIC 5042 + +// Runtime errors +#define TX_DIVZERO 6000 +#define TX_NOTINIT 6001 +#define TX_BADTHROW 6002 +#define TX_NORUN 6004 +#define TX_NOCALL 6005 +#define TX_NOCLASS 6006 +#define TX_NULLPT 6007 +#define TX_OPNAN 6008 +#define TX_OUTARRAY 6009 +#define TX_STACKOVER 6010 +#define TX_DELETEDPT 6011 + +// Max errors +#define TX_MAX 6012 diff --git a/src/CBot/CBotInstr/CBotBlock.cpp b/src/CBot/CBotInstr/CBotBlock.cpp index ba2b5ce5..648c5ac3 100644 --- a/src/CBot/CBotInstr/CBotBlock.cpp +++ b/src/CBot/CBotInstr/CBotBlock.cpp @@ -23,6 +23,8 @@ #include "CBotCStack.h" #include "CBotListInstr.h" +#include "CBotDefines.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.cpp b/src/CBot/CBotInstr/CBotBoolExpr.cpp index db6b36bd..873a1a6c 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.cpp +++ b/src/CBot/CBotInstr/CBotBoolExpr.cpp @@ -23,6 +23,8 @@ #include "CBotCStack.h" +#include "CBotDefines.h" + // Local include // Global include diff --git a/src/CBot/CBotInstr/CBotCondition.cpp b/src/CBot/CBotInstr/CBotCondition.cpp index 453e26c8..6c5e53db 100644 --- a/src/CBot/CBotInstr/CBotCondition.cpp +++ b/src/CBot/CBotInstr/CBotCondition.cpp @@ -23,6 +23,8 @@ #include "CBotCStack.h" +#include "CBotDefines.h" + // Local include // Global include diff --git a/src/CBot/resource.h b/src/CBot/resource.h index 5c204f83..60d2c775 100644 --- a/src/CBot/resource.h +++ b/src/CBot/resource.h @@ -112,64 +112,3 @@ enum EID TX_UNDEF = 4000, TX_NAN }; - -// TODO: refactor & change to enum! - -// Compile errors -#define TX_OPENPAR 5000 -#define TX_CLOSEPAR 5001 -#define TX_NOTBOOL 5002 -#define TX_UNDEFVAR 5003 -#define TX_BADLEFT 5004 -#define TX_ENDOF 5005 -#define TX_OUTCASE 5006 -#define TX_CLOSEBLK 5008 -#define TX_ELSEWITHOUTIF 5009 -#define TX_OPENBLK 5010 -#define TX_BADTYPE 5011 -#define TX_REDEFVAR 5012 -#define TX_BAD2TYPE 5013 -#define TX_UNDEFCALL 5014 -#define TX_MISDOTS 5015 -#define TX_WHILE 5016 -#define TX_BREAK 5017 -#define TX_LABEL 5018 -#define TX_NOLABEL 5019 -#define TX_NOCASE 5020 -#define TX_BADNUM 5021 -#define TX_VOID 5022 -#define TX_NOTYP 5023 -#define TX_NOVAR 5024 -#define TX_NOFONC 5025 -#define TX_OVERPARAM 5026 -#define TX_REDEF 5027 -#define TX_LOWPARAM 5028 -#define TX_BADPARAM 5029 -#define TX_NUMPARAM 5030 -#define TX_NOITEM 5031 -#define TX_DOT 5032 -#define TX_NOCONST 5033 -#define TX_REDEFCLASS 5034 -#define TX_CLBRK 5035 -#define TX_RESERVED 5036 -#define TX_BADNEW 5037 -#define TX_BADSTRING 5039 -#define TX_BADINDEX 5040 -#define TX_PRIVATE 5041 -#define TX_NOPUBLIC 5042 - -// Runtime errors -#define TX_DIVZERO 6000 -#define TX_NOTINIT 6001 -#define TX_BADTHROW 6002 -#define TX_NORUN 6004 -#define TX_NOCALL 6005 -#define TX_NOCLASS 6006 -#define TX_NULLPT 6007 -#define TX_OPNAN 6008 -#define TX_OUTARRAY 6009 -#define TX_STACKOVER 6010 -#define TX_DELETEDPT 6011 - -// Max errors -#define TX_MAX 6012 From 4a9afdc525feaf4a8ea33363ddf30dcfb6891bde Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 18:38:43 +0100 Subject: [PATCH 81/91] Move enum from resource.h to CBotEnums.h. Delete file resource.h. --- src/CBot/CBot.h | 1 - src/CBot/CBotEnums.h | 94 +++++++++++++++++++++++++++ src/CBot/CBotInstr/CBotInstrUtils.cpp | 2 - src/CBot/CBotString.h | 3 +- 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index bea5ce75..11595183 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -18,7 +18,6 @@ */ // Modules inlcude -#include "CBot/resource.h" #include "CBot/CBotFileUtils.h" #include "CBot/CBotClass.h" #include "CBot/CBotToken.h" diff --git a/src/CBot/CBotEnums.h b/src/CBot/CBotEnums.h index 524f00c1..17dbef69 100644 --- a/src/CBot/CBotEnums.h +++ b/src/CBot/CBotEnums.h @@ -58,3 +58,97 @@ enum CBotGet GetPosParam = 3, GetPosBloc = 4 }; + + +enum EID +{ + ID_IF = 2000, + ID_ELSE, + ID_WHILE, + ID_DO, + ID_FOR, + ID_BREAK, + ID_CONTINUE, + ID_SWITCH, + ID_CASE, + ID_DEFAULT, + ID_TRY, + ID_THROW, + ID_CATCH, + ID_FINALLY, + ID_TXT_AND, + ID_TXT_OR, + ID_TXT_NOT, + ID_RETURN, + ID_CLASS, + ID_EXTENDS, + ID_SYNCHO, + ID_NEW, + ID_PUBLIC, + ID_EXTERN, + ID_STATIC, + ID_PROTECTED, + ID_PRIVATE, + ID_INT, + ID_FLOAT, + ID_BOOLEAN, + ID_STRING, + ID_VOID, + ID_BOOL, + + ID_TRUE = 2200, + ID_FALSE, + ID_NULL, + ID_NAN, + + ID_OPENPAR = 2300, + 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 = 4000, + TX_NAN +}; diff --git a/src/CBot/CBotInstr/CBotInstrUtils.cpp b/src/CBot/CBotInstr/CBotInstrUtils.cpp index 576cfa30..1b7e65c7 100644 --- a/src/CBot/CBotInstr/CBotInstrUtils.cpp +++ b/src/CBot/CBotInstr/CBotInstrUtils.cpp @@ -28,8 +28,6 @@ #include "CBotVar/CBotVar.h" -#include "resource.h" - // Local include // Global include diff --git a/src/CBot/CBotString.h b/src/CBot/CBotString.h index ea065b29..58630ea6 100644 --- a/src/CBot/CBotString.h +++ b/src/CBot/CBotString.h @@ -21,7 +21,8 @@ // Modules inlcude #include "CBotUtils.h" -#include "resource.h" + +#include "CBotEnums.h" // Local include From 5c7a665639dbeecde5affd09f992cc42de23283e Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 18:48:54 +0100 Subject: [PATCH 82/91] Add CBot.h, CBotEnums.h and CBotDefines.h in CMakeLists.txt. --- src/CBot/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 0d1cc7cd..91f08748 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,4 +1,7 @@ set(SOURCES + CBot.h + CBotEnums.h + CBotDefines.h CBotUtils.cpp CBotFileUtils.cpp CBotClass.cpp From fa9dc0dace1824661e8ef6199d457a50aacf4462 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 22 Nov 2015 18:59:01 +0100 Subject: [PATCH 83/91] Start cleaning of files CBotFileUtils.h and CBotFileUtils.cpp. Pass CBotTypResult parameter as reference. --- src/CBot/CBotFileUtils.cpp | 4 ++-- src/CBot/CBotFileUtils.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CBot/CBotFileUtils.cpp b/src/CBot/CBotFileUtils.cpp index bb561bdf..79a1240a 100644 --- a/src/CBot/CBotFileUtils.cpp +++ b/src/CBot/CBotFileUtils.cpp @@ -124,7 +124,7 @@ bool ReadString(FILE* pf, CBotString& s) } //////////////////////////////////////////////////////////////////////////////// -bool WriteType(FILE* pf, CBotTypResult type) +bool WriteType(FILE* pf, const CBotTypResult &type) { int typ = type.GetType(); if ( typ == CBotTypIntrinsic ) typ = CBotTypClass; @@ -144,7 +144,7 @@ bool WriteType(FILE* pf, CBotTypResult type) } //////////////////////////////////////////////////////////////////////////////// -bool ReadType(FILE* pf, CBotTypResult& type) +bool ReadType(FILE* pf, CBotTypResult &type) { unsigned short w, ww; if ( !ReadWord(pf, w) ) return false; diff --git a/src/CBot/CBotFileUtils.h b/src/CBot/CBotFileUtils.h index 1ac1664b..75bfa434 100644 --- a/src/CBot/CBotFileUtils.h +++ b/src/CBot/CBotFileUtils.h @@ -20,7 +20,6 @@ #pragma once // Modules inlcude -#include "CBotTypResult.h" // Local include @@ -30,6 +29,7 @@ // Forward declaration class CBotVar; class CBotString; +class CBotTypResult; /////////////////////////////////////////////////////////////////////////////// // routines for file management (* FILE) @@ -145,7 +145,7 @@ bool ReadString(FILE* pf, CBotString& s); * \param type * \return */ -bool WriteType(FILE* pf, CBotTypResult type); +bool WriteType(FILE* pf, const CBotTypResult &type); /*! * \brief ReadType @@ -153,4 +153,4 @@ bool WriteType(FILE* pf, CBotTypResult type); * \param type * \return */ -bool ReadType(FILE* pf, CBotTypResult& type); +bool ReadType(FILE* pf, CBotTypResult &type); From de5a57f793fb10a578775d6513f6f01637bb8df7 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Mon, 23 Nov 2015 21:59:56 +0100 Subject: [PATCH 84/91] Change CBot default include directory. Make all include directive absolute. --- src/CBot/CBotCStack.cpp | 10 +++--- src/CBot/CBotCStack.h | 2 +- src/CBot/CBotCall.cpp | 12 +++---- src/CBot/CBotCall.h | 2 +- src/CBot/CBotCallMethode.cpp | 10 +++--- src/CBot/CBotCallMethode.h | 4 +-- src/CBot/CBotClass.cpp | 30 ++++++++--------- src/CBot/CBotClass.h | 6 ++-- src/CBot/CBotDefParam.cpp | 8 ++--- src/CBot/CBotDefParam.h | 4 +-- src/CBot/CBotFileUtils.cpp | 8 ++--- src/CBot/CBotInstr/CBotBlock.cpp | 8 ++--- src/CBot/CBotInstr/CBotBlock.h | 2 +- src/CBot/CBotInstr/CBotBoolExpr.cpp | 8 ++--- src/CBot/CBotInstr/CBotBoolExpr.h | 2 +- src/CBot/CBotInstr/CBotBoolean.cpp | 14 ++++---- src/CBot/CBotInstr/CBotBoolean.h | 2 +- src/CBot/CBotInstr/CBotBreak.cpp | 6 ++-- src/CBot/CBotInstr/CBotBreak.h | 2 +- src/CBot/CBotInstr/CBotCase.cpp | 8 ++--- src/CBot/CBotInstr/CBotCase.h | 2 +- src/CBot/CBotInstr/CBotCatch.cpp | 12 +++---- src/CBot/CBotInstr/CBotCatch.h | 2 +- src/CBot/CBotInstr/CBotClassInst.cpp | 18 +++++----- src/CBot/CBotInstr/CBotClassInst.h | 2 +- src/CBot/CBotInstr/CBotCondition.cpp | 8 ++--- src/CBot/CBotInstr/CBotCondition.h | 2 +- src/CBot/CBotInstr/CBotDo.cpp | 10 +++--- src/CBot/CBotInstr/CBotDo.h | 2 +- src/CBot/CBotInstr/CBotEmpty.cpp | 6 ++-- src/CBot/CBotInstr/CBotEmpty.h | 2 +- src/CBot/CBotInstr/CBotExprAlpha.cpp | 8 ++--- src/CBot/CBotInstr/CBotExprAlpha.h | 2 +- src/CBot/CBotInstr/CBotExprBool.cpp | 8 ++--- src/CBot/CBotInstr/CBotExprBool.h | 2 +- src/CBot/CBotInstr/CBotExprNan.cpp | 6 ++-- src/CBot/CBotInstr/CBotExprNan.h | 2 +- src/CBot/CBotInstr/CBotExprNull.cpp | 6 ++-- src/CBot/CBotInstr/CBotExprNull.h | 2 +- src/CBot/CBotInstr/CBotExprNum.cpp | 8 ++--- src/CBot/CBotInstr/CBotExprNum.h | 2 +- src/CBot/CBotInstr/CBotExprUnaire.cpp | 10 +++--- src/CBot/CBotInstr/CBotExprUnaire.h | 2 +- src/CBot/CBotInstr/CBotExprVar.cpp | 16 ++++----- src/CBot/CBotInstr/CBotExprVar.h | 4 +-- src/CBot/CBotInstr/CBotExpression.cpp | 12 +++---- src/CBot/CBotInstr/CBotExpression.h | 4 +-- src/CBot/CBotInstr/CBotFieldExpr.cpp | 10 +++--- src/CBot/CBotInstr/CBotFieldExpr.h | 2 +- src/CBot/CBotInstr/CBotFloat.cpp | 14 ++++---- src/CBot/CBotInstr/CBotFloat.h | 2 +- src/CBot/CBotInstr/CBotFor.cpp | 12 +++---- src/CBot/CBotInstr/CBotFor.h | 2 +- src/CBot/CBotInstr/CBotFunction.cpp | 26 +++++++-------- src/CBot/CBotInstr/CBotFunction.h | 2 +- src/CBot/CBotInstr/CBotIString.cpp | 12 +++---- src/CBot/CBotInstr/CBotIString.h | 2 +- src/CBot/CBotInstr/CBotIf.cpp | 10 +++--- src/CBot/CBotInstr/CBotIf.h | 2 +- src/CBot/CBotInstr/CBotIndexExpr.cpp | 8 ++--- src/CBot/CBotInstr/CBotIndexExpr.h | 2 +- src/CBot/CBotInstr/CBotInstArray.cpp | 18 +++++----- src/CBot/CBotInstr/CBotInstArray.h | 2 +- src/CBot/CBotInstr/CBotInstr.cpp | 40 +++++++++++------------ src/CBot/CBotInstr/CBotInstr.h | 4 +-- src/CBot/CBotInstr/CBotInstrCall.cpp | 8 ++--- src/CBot/CBotInstr/CBotInstrCall.h | 2 +- src/CBot/CBotInstr/CBotInstrMethode.cpp | 12 +++---- src/CBot/CBotInstr/CBotInstrMethode.h | 2 +- src/CBot/CBotInstr/CBotInstrUtils.cpp | 14 ++++---- src/CBot/CBotInstr/CBotInt.cpp | 14 ++++---- src/CBot/CBotInstr/CBotInt.h | 2 +- src/CBot/CBotInstr/CBotLeftExpr.cpp | 16 ++++----- src/CBot/CBotInstr/CBotLeftExpr.h | 2 +- src/CBot/CBotInstr/CBotLeftExprVar.cpp | 8 ++--- src/CBot/CBotInstr/CBotLeftExprVar.h | 2 +- src/CBot/CBotInstr/CBotListArray.cpp | 14 ++++---- src/CBot/CBotInstr/CBotListArray.h | 2 +- src/CBot/CBotInstr/CBotListExpression.cpp | 14 ++++---- src/CBot/CBotInstr/CBotListExpression.h | 2 +- src/CBot/CBotInstr/CBotListInstr.cpp | 8 ++--- src/CBot/CBotInstr/CBotListInstr.h | 2 +- src/CBot/CBotInstr/CBotLogicExpr.cpp | 4 +-- src/CBot/CBotInstr/CBotLogicExpr.h | 2 +- src/CBot/CBotInstr/CBotNew.cpp | 12 +++---- src/CBot/CBotInstr/CBotNew.h | 2 +- src/CBot/CBotInstr/CBotParExpr.cpp | 30 ++++++++--------- src/CBot/CBotInstr/CBotParExpr.h | 2 +- src/CBot/CBotInstr/CBotPostIncExpr.cpp | 8 ++--- src/CBot/CBotInstr/CBotPostIncExpr.h | 2 +- src/CBot/CBotInstr/CBotPreIncExpr.cpp | 8 ++--- src/CBot/CBotInstr/CBotPreIncExpr.h | 2 +- src/CBot/CBotInstr/CBotReturn.cpp | 10 +++--- src/CBot/CBotInstr/CBotReturn.h | 2 +- src/CBot/CBotInstr/CBotSwitch.cpp | 12 +++---- src/CBot/CBotInstr/CBotSwitch.h | 2 +- src/CBot/CBotInstr/CBotThrow.cpp | 8 ++--- src/CBot/CBotInstr/CBotThrow.h | 2 +- src/CBot/CBotInstr/CBotTry.cpp | 6 ++-- src/CBot/CBotInstr/CBotTry.h | 4 +-- src/CBot/CBotInstr/CBotTwoOpExpr.cpp | 16 ++++----- src/CBot/CBotInstr/CBotTwoOpExpr.h | 2 +- src/CBot/CBotInstr/CBotWhile.cpp | 10 +++--- src/CBot/CBotInstr/CBotWhile.h | 2 +- src/CBot/CBotProgram.cpp | 16 ++++----- src/CBot/CBotProgram.h | 8 ++--- src/CBot/CBotStack.cpp | 12 +++---- src/CBot/CBotStack.h | 6 ++-- src/CBot/CBotString.cpp | 2 +- src/CBot/CBotString.h | 4 +-- src/CBot/CBotStringArray.cpp | 4 +-- src/CBot/CBotStringArray.h | 2 +- src/CBot/CBotToken.cpp | 2 +- src/CBot/CBotToken.h | 2 +- src/CBot/CBotTypResult.cpp | 6 ++-- src/CBot/CBotUtils.cpp | 12 +++---- src/CBot/CBotUtils.h | 4 +-- src/CBot/CBotVar/CBotVar.cpp | 22 ++++++------- src/CBot/CBotVar/CBotVar.h | 4 +-- src/CBot/CBotVar/CBotVarArray.cpp | 10 +++--- src/CBot/CBotVar/CBotVarArray.h | 4 +-- src/CBot/CBotVar/CBotVarBoolean.cpp | 6 ++-- src/CBot/CBotVar/CBotVarBoolean.h | 4 +-- src/CBot/CBotVar/CBotVarClass.cpp | 12 +++---- src/CBot/CBotVar/CBotVarClass.h | 2 +- src/CBot/CBotVar/CBotVarFloat.cpp | 8 ++--- src/CBot/CBotVar/CBotVarFloat.h | 2 +- src/CBot/CBotVar/CBotVarInt.cpp | 8 ++--- src/CBot/CBotVar/CBotVarInt.h | 2 +- src/CBot/CBotVar/CBotVarPointer.cpp | 12 +++---- src/CBot/CBotVar/CBotVarPointer.h | 2 +- src/CBot/CBotVar/CBotVarString.cpp | 8 ++--- src/CBot/CBotVar/CBotVarString.h | 2 +- src/CBot/CMakeLists.txt | 2 +- src/CBot/StringFunctions.cpp | 8 ++--- src/CBot/StringFunctions.h | 2 +- 136 files changed, 475 insertions(+), 475 deletions(-) diff --git a/src/CBot/CBotCStack.cpp b/src/CBot/CBotCStack.cpp index 6d9b820e..ed56e22a 100644 --- a/src/CBot/CBotCStack.cpp +++ b/src/CBot/CBotCStack.cpp @@ -19,14 +19,14 @@ // Modules inlcude -#include "CBotCStack.h" +#include "CBot/CBotCStack.h" -#include "CBotToken.h" -#include "CBotCall.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotCall.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" -#include "CBotInstr/CBotFunction.h" +#include "CBot/CBotInstr/CBotFunction.h" // Local include diff --git a/src/CBot/CBotCStack.h b/src/CBot/CBotCStack.h index 70ce456a..e6210f80 100644 --- a/src/CBot/CBotCStack.h +++ b/src/CBot/CBotCStack.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotProgram.h" +#include "CBot/CBotProgram.h" // Local include diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp index ee853d8a..13f1e857 100644 --- a/src/CBot/CBotCall.cpp +++ b/src/CBot/CBotCall.cpp @@ -18,14 +18,14 @@ */ // Modules inlcude -#include "CBotCall.h" +#include "CBot/CBotCall.h" -#include "CBotToken.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotUtils.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotUtils.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotCall.h b/src/CBot/CBotCall.h index b4dc4fe9..bb94c23e 100644 --- a/src/CBot/CBotCall.h +++ b/src/CBot/CBotCall.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotString.h" +#include "CBot/CBotString.h" // Local include diff --git a/src/CBot/CBotCallMethode.cpp b/src/CBot/CBotCallMethode.cpp index da915dfb..d38da859 100644 --- a/src/CBot/CBotCallMethode.cpp +++ b/src/CBot/CBotCallMethode.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotCallMethode.h" +#include "CBot/CBotCallMethode.h" -#include "CBotUtils.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotUtils.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotCallMethode.h b/src/CBot/CBotCallMethode.h index d60a9b77..445e259a 100644 --- a/src/CBot/CBotCallMethode.h +++ b/src/CBot/CBotCallMethode.h @@ -20,8 +20,8 @@ #pragma once // Modules inlcude -#include "CBotTypResult.h" -#include "CBotString.h" +#include "CBot/CBotTypResult.h" +#include "CBot/CBotString.h" // Local include diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 172f2cd3..8e580bb6 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -18,24 +18,24 @@ */ // Modules inlcude -#include "CBotClass.h" +#include "CBot/CBotClass.h" -#include "CBotInstr/CBotNew.h" -#include "CBotInstr/CBotLeftExprVar.h" -#include "CBotInstr/CBotTwoOpExpr.h" -#include "CBotInstr/CBotFunction.h" -#include "CBotInstr/CBotExpression.h" -#include "CBotInstr/CBotListArray.h" -#include "CBotInstr/CBotEmpty.h" +#include "CBot/CBotInstr/CBotNew.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotFunction.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotInstr/CBotListArray.h" +#include "CBot/CBotInstr/CBotEmpty.h" -#include "CBotCall.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotUtils.h" -#include "CBotFileUtils.h" -#include "CBotCallMethode.h" +#include "CBot/CBotCall.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotUtils.h" +#include "CBot/CBotFileUtils.h" +#include "CBot/CBotCallMethode.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index ac9e418d..2b151b0a 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -20,11 +20,11 @@ #pragma once // Modules inlcude -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" -#include "CBotTypResult.h" +#include "CBot/CBotTypResult.h" -#include "CBotString.h" +#include "CBot/CBotString.h" // Local include diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp index fe7ce8bf..7f13f89d 100644 --- a/src/CBot/CBotDefParam.cpp +++ b/src/CBot/CBotDefParam.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotDefParam.h" +#include "CBot/CBotDefParam.h" -#include "CBotUtils.h" -#include "CBotCStack.h" +#include "CBot/CBotUtils.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVarClass.h" +#include "CBot/CBotVar/CBotVarClass.h" // Local include diff --git a/src/CBot/CBotDefParam.h b/src/CBot/CBotDefParam.h index 6da8e2cd..433d18f4 100644 --- a/src/CBot/CBotDefParam.h +++ b/src/CBot/CBotDefParam.h @@ -20,8 +20,8 @@ #pragma once // Modules inlcude -#include "CBotToken.h" -#include "CBotStack.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotStack.h" // Local include diff --git a/src/CBot/CBotFileUtils.cpp b/src/CBot/CBotFileUtils.cpp index 79a1240a..c6e50e1a 100644 --- a/src/CBot/CBotFileUtils.cpp +++ b/src/CBot/CBotFileUtils.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotFileUtils.h" +#include "CBot/CBotFileUtils.h" -#include "CBotString.h" -#include "CBotClass.h" +#include "CBot/CBotString.h" +#include "CBot/CBotClass.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBlock.cpp b/src/CBot/CBotInstr/CBotBlock.cpp index 648c5ac3..9e6b2537 100644 --- a/src/CBot/CBotInstr/CBotBlock.cpp +++ b/src/CBot/CBotInstr/CBotBlock.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotBlock.h" +#include "CBot/CBotInstr/CBotBlock.h" -#include "CBotCStack.h" -#include "CBotListInstr.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotInstr/CBotListInstr.h" -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBlock.h b/src/CBot/CBotInstr/CBotBlock.h index 7df0bb5b..c88e5006 100644 --- a/src/CBot/CBotInstr/CBotBlock.h +++ b/src/CBot/CBotInstr/CBotBlock.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.cpp b/src/CBot/CBotInstr/CBotBoolExpr.cpp index 873a1a6c..e219d159 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.cpp +++ b/src/CBot/CBotInstr/CBotBoolExpr.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotBoolExpr.h" -#include "CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotBoolExpr.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" -#include "CBotCStack.h" +#include "CBot/CBotCStack.h" -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolExpr.h b/src/CBot/CBotInstr/CBotBoolExpr.h index af6cd4f8..31545029 100644 --- a/src/CBot/CBotInstr/CBotBoolExpr.h +++ b/src/CBot/CBotInstr/CBotBoolExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolean.cpp b/src/CBot/CBotInstr/CBotBoolean.cpp index 64c4d3d3..0b44d71e 100644 --- a/src/CBot/CBotInstr/CBotBoolean.cpp +++ b/src/CBot/CBotInstr/CBotBoolean.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotBoolean.h" -#include "CBotLeftExprVar.h" -#include "CBotTwoOpExpr.h" -#include "CBotInstArray.h" +#include "CBot/CBotInstr/CBotBoolean.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotInstArray.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBoolean.h b/src/CBot/CBotInstr/CBotBoolean.h index e589f508..78b49093 100644 --- a/src/CBot/CBotInstr/CBotBoolean.h +++ b/src/CBot/CBotInstr/CBotBoolean.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBreak.cpp b/src/CBot/CBotInstr/CBotBreak.cpp index 9d4100cb..d97d65ae 100644 --- a/src/CBot/CBotInstr/CBotBreak.cpp +++ b/src/CBot/CBotInstr/CBotBreak.cpp @@ -18,10 +18,10 @@ */ // Modules inlcude -#include "CBotBreak.h" +#include "CBot/CBotInstr/CBotBreak.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotBreak.h b/src/CBot/CBotInstr/CBotBreak.h index 78b39dd3..7961b0bf 100644 --- a/src/CBot/CBotInstr/CBotBreak.h +++ b/src/CBot/CBotInstr/CBotBreak.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCase.cpp b/src/CBot/CBotInstr/CBotCase.cpp index 3ae5b706..9387330f 100644 --- a/src/CBot/CBotInstr/CBotCase.cpp +++ b/src/CBot/CBotInstr/CBotCase.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotCase.h" -#include "CBotExprNum.h" +#include "CBot/CBotInstr/CBotCase.h" +#include "CBot/CBotInstr/CBotExprNum.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCase.h b/src/CBot/CBotInstr/CBotCase.h index 03ddddde..ebd67485 100644 --- a/src/CBot/CBotInstr/CBotCase.h +++ b/src/CBot/CBotInstr/CBotCase.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCatch.cpp b/src/CBot/CBotInstr/CBotCatch.cpp index 26f7d17d..f62f662e 100644 --- a/src/CBot/CBotInstr/CBotCatch.cpp +++ b/src/CBot/CBotInstr/CBotCatch.cpp @@ -18,14 +18,14 @@ */ // Modules inlcude -#include "CBotCatch.h" -#include "CBotBlock.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotCatch.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCatch.h b/src/CBot/CBotInstr/CBotCatch.h index 60ee00a9..eec4196f 100644 --- a/src/CBot/CBotInstr/CBotCatch.h +++ b/src/CBot/CBotInstr/CBotCatch.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotClassInst.cpp b/src/CBot/CBotInstr/CBotClassInst.cpp index d556dd77..d66ba3ec 100644 --- a/src/CBot/CBotInstr/CBotClassInst.cpp +++ b/src/CBot/CBotInstr/CBotClassInst.cpp @@ -20,18 +20,18 @@ // Modules inlcude #include "CBotClassInst.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotLeftExprVar.h" -#include "CBotTwoOpExpr.h" -#include "CBotInstArray.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotInstArray.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" -#include "CBotVar/CBotVarPointer.h" -#include "CBotVar/CBotVarClass.h" +#include "CBot/CBotVar/CBotVarPointer.h" +#include "CBot/CBotVar/CBotVarClass.h" // Local include diff --git a/src/CBot/CBotInstr/CBotClassInst.h b/src/CBot/CBotInstr/CBotClassInst.h index ab70c0f2..743e39e0 100644 --- a/src/CBot/CBotInstr/CBotClassInst.h +++ b/src/CBot/CBotInstr/CBotClassInst.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCondition.cpp b/src/CBot/CBotInstr/CBotCondition.cpp index 6c5e53db..6c88869e 100644 --- a/src/CBot/CBotInstr/CBotCondition.cpp +++ b/src/CBot/CBotInstr/CBotCondition.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotCondition.h" -#include "CBotBoolExpr.h" +#include "CBot/CBotInstr/CBotCondition.h" +#include "CBot/CBotInstr/CBotBoolExpr.h" -#include "CBotCStack.h" +#include "CBot/CBotCStack.h" -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" // Local include diff --git a/src/CBot/CBotInstr/CBotCondition.h b/src/CBot/CBotInstr/CBotCondition.h index f36c998a..334354f7 100644 --- a/src/CBot/CBotInstr/CBotCondition.h +++ b/src/CBot/CBotInstr/CBotCondition.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotDo.cpp b/src/CBot/CBotInstr/CBotDo.cpp index c24baff5..ef682b36 100644 --- a/src/CBot/CBotInstr/CBotDo.cpp +++ b/src/CBot/CBotInstr/CBotDo.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotDo.h" -#include "CBotBlock.h" -#include "CBotCondition.h" +#include "CBot/CBotInstr/CBotDo.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotCondition.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotDo.h b/src/CBot/CBotInstr/CBotDo.h index 30f1db9d..8a800400 100644 --- a/src/CBot/CBotInstr/CBotDo.h +++ b/src/CBot/CBotInstr/CBotDo.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotEmpty.cpp b/src/CBot/CBotInstr/CBotEmpty.cpp index 4318eadd..ba9a9fd3 100644 --- a/src/CBot/CBotInstr/CBotEmpty.cpp +++ b/src/CBot/CBotInstr/CBotEmpty.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotEmpty.h" +#include "CBot/CBotInstr/CBotEmpty.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotEmpty.h b/src/CBot/CBotInstr/CBotEmpty.h index 782e87ca..ebbee7b1 100644 --- a/src/CBot/CBotInstr/CBotEmpty.h +++ b/src/CBot/CBotInstr/CBotEmpty.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.cpp b/src/CBot/CBotInstr/CBotExprAlpha.cpp index 1f93b89a..da68497e 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.cpp +++ b/src/CBot/CBotInstr/CBotExprAlpha.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotExprAlpha.h" +#include "CBot/CBotInstr/CBotExprAlpha.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprAlpha.h b/src/CBot/CBotInstr/CBotExprAlpha.h index 01e97a8e..10f4e240 100644 --- a/src/CBot/CBotInstr/CBotExprAlpha.h +++ b/src/CBot/CBotInstr/CBotExprAlpha.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprBool.cpp b/src/CBot/CBotInstr/CBotExprBool.cpp index 3df5095f..94736934 100644 --- a/src/CBot/CBotInstr/CBotExprBool.cpp +++ b/src/CBot/CBotInstr/CBotExprBool.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotExprBool.h" +#include "CBot/CBotInstr/CBotExprBool.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprBool.h b/src/CBot/CBotInstr/CBotExprBool.h index a81ec2ee..ac1d84fd 100644 --- a/src/CBot/CBotInstr/CBotExprBool.h +++ b/src/CBot/CBotInstr/CBotExprBool.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNan.cpp b/src/CBot/CBotInstr/CBotExprNan.cpp index a2cb1c65..df0a296d 100644 --- a/src/CBot/CBotInstr/CBotExprNan.cpp +++ b/src/CBot/CBotInstr/CBotExprNan.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotExprNan.h" +#include "CBot/CBotInstr/CBotExprNan.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNan.h b/src/CBot/CBotInstr/CBotExprNan.h index c7c25963..623502a7 100644 --- a/src/CBot/CBotInstr/CBotExprNan.h +++ b/src/CBot/CBotInstr/CBotExprNan.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNull.cpp b/src/CBot/CBotInstr/CBotExprNull.cpp index cef5eca8..a36b4889 100644 --- a/src/CBot/CBotInstr/CBotExprNull.cpp +++ b/src/CBot/CBotInstr/CBotExprNull.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotExprNull.h" +#include "CBot/CBotInstr/CBotExprNull.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNull.h b/src/CBot/CBotInstr/CBotExprNull.h index 662ded3d..473ddb60 100644 --- a/src/CBot/CBotInstr/CBotExprNull.h +++ b/src/CBot/CBotInstr/CBotExprNull.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNum.cpp b/src/CBot/CBotInstr/CBotExprNum.cpp index acf1e9a2..e9a72dec 100644 --- a/src/CBot/CBotInstr/CBotExprNum.cpp +++ b/src/CBot/CBotInstr/CBotExprNum.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotExprNum.h" +#include "CBot/CBotInstr/CBotExprNum.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprNum.h b/src/CBot/CBotInstr/CBotExprNum.h index e9ccf932..7195f7bb 100644 --- a/src/CBot/CBotInstr/CBotExprNum.h +++ b/src/CBot/CBotInstr/CBotExprNum.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprUnaire.cpp b/src/CBot/CBotInstr/CBotExprUnaire.cpp index 31056bb3..50e39a78 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.cpp +++ b/src/CBot/CBotInstr/CBotExprUnaire.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotExprUnaire.h" -#include "CBotParExpr.h" +#include "CBot/CBotInstr/CBotExprUnaire.h" +#include "CBot/CBotInstr/CBotParExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprUnaire.h b/src/CBot/CBotInstr/CBotExprUnaire.h index 660afdd7..b015f341 100644 --- a/src/CBot/CBotInstr/CBotExprUnaire.h +++ b/src/CBot/CBotInstr/CBotExprUnaire.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index ee4a748e..4dc078dd 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -18,16 +18,16 @@ */ // Modules inlcude -#include "CBotExprVar.h" -#include "CBotInstrMethode.h" -#include "CBotExpression.h" -#include "CBotIndexExpr.h" -#include "CBotFieldExpr.h" +#include "CBot/CBotInstr/CBotExprVar.h" +#include "CBot/CBotInstr/CBotInstrMethode.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotInstr/CBotIndexExpr.h" +#include "CBot/CBotInstr/CBotFieldExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVarArray.h" +#include "CBot/CBotVar/CBotVarArray.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExprVar.h b/src/CBot/CBotInstr/CBotExprVar.h index 9757b898..ff5cf17d 100644 --- a/src/CBot/CBotInstr/CBotExprVar.h +++ b/src/CBot/CBotInstr/CBotExprVar.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExpression.cpp b/src/CBot/CBotInstr/CBotExpression.cpp index a3612426..d3821aab 100644 --- a/src/CBot/CBotInstr/CBotExpression.cpp +++ b/src/CBot/CBotInstr/CBotExpression.cpp @@ -18,16 +18,16 @@ */ // Modules inlcude -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotExpression.h b/src/CBot/CBotInstr/CBotExpression.h index 637ff051..a854d8bf 100644 --- a/src/CBot/CBotInstr/CBotExpression.h +++ b/src/CBot/CBotInstr/CBotExpression.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotLeftExpr.h" +#include "CBot/CBotInstr/CBotLeftExpr.h" -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.cpp b/src/CBot/CBotInstr/CBotFieldExpr.cpp index 7c4ff58c..c5817695 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.cpp +++ b/src/CBot/CBotInstr/CBotFieldExpr.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotFieldExpr.h" +#include "CBot/CBotInstr/CBotFieldExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" -#include "CBotVar/CBotVarClass.h" +#include "CBot/CBotVar/CBotVarClass.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFieldExpr.h b/src/CBot/CBotInstr/CBotFieldExpr.h index 4a56a3f6..84c014fb 100644 --- a/src/CBot/CBotInstr/CBotFieldExpr.h +++ b/src/CBot/CBotInstr/CBotFieldExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFloat.cpp b/src/CBot/CBotInstr/CBotFloat.cpp index df69b07f..f4a8ac31 100644 --- a/src/CBot/CBotInstr/CBotFloat.cpp +++ b/src/CBot/CBotInstr/CBotFloat.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotFloat.h" -#include "CBotLeftExprVar.h" -#include "CBotTwoOpExpr.h" -#include "CBotInstArray.h" +#include "CBot/CBotInstr/CBotFloat.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotInstArray.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFloat.h b/src/CBot/CBotInstr/CBotFloat.h index 3e095fe5..fa1d6f66 100644 --- a/src/CBot/CBotInstr/CBotFloat.h +++ b/src/CBot/CBotInstr/CBotFloat.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFor.cpp b/src/CBot/CBotInstr/CBotFor.cpp index 2a97b86b..e225b99b 100644 --- a/src/CBot/CBotInstr/CBotFor.cpp +++ b/src/CBot/CBotInstr/CBotFor.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotFor.h" -#include "CBotListExpression.h" -#include "CBotBlock.h" -#include "CBotBoolExpr.h" +#include "CBot/CBotInstr/CBotFor.h" +#include "CBot/CBotInstr/CBotListExpression.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotBoolExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFor.h b/src/CBot/CBotInstr/CBotFor.h index 56442b0c..5b9a93eb 100644 --- a/src/CBot/CBotInstr/CBotFor.h +++ b/src/CBot/CBotInstr/CBotFor.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index f8e4b6df..7277486e 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -19,23 +19,23 @@ // Modules inlcude -#include "CBotFunction.h" +#include "CBot/CBotInstr/CBotFunction.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotInstr/CBotBlock.h" -#include "CBotInstr/CBotTwoOpExpr.h" -#include "CBotInstr/CBotExpression.h" -#include "CBotInstr/CBotEmpty.h" -#include "CBotInstr/CBotListArray.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotInstr/CBotEmpty.h" +#include "CBot/CBotInstr/CBotListArray.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" -#include "CBotDefParam.h" -#include "CBotUtils.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotDefParam.h" +#include "CBot/CBotUtils.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotFunction.h b/src/CBot/CBotInstr/CBotFunction.h index 7c44dc3e..f11015f5 100644 --- a/src/CBot/CBotInstr/CBotFunction.h +++ b/src/CBot/CBotInstr/CBotFunction.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIString.cpp b/src/CBot/CBotInstr/CBotIString.cpp index 6feefc41..be03c6e8 100644 --- a/src/CBot/CBotInstr/CBotIString.cpp +++ b/src/CBot/CBotInstr/CBotIString.cpp @@ -18,14 +18,14 @@ */ // Modules inlcude -#include "CBotIString.h" -#include "CBotLeftExprVar.h" -#include "CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotIString.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIString.h b/src/CBot/CBotInstr/CBotIString.h index 1435bb36..93fcfbdb 100644 --- a/src/CBot/CBotInstr/CBotIString.h +++ b/src/CBot/CBotInstr/CBotIString.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIf.cpp b/src/CBot/CBotInstr/CBotIf.cpp index 1075f65f..784b88f9 100644 --- a/src/CBot/CBotInstr/CBotIf.cpp +++ b/src/CBot/CBotInstr/CBotIf.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotIf.h" -#include "CBotInstr/CBotBlock.h" -#include "CBotInstr/CBotCondition.h" +#include "CBot/CBotInstr/CBotIf.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotCondition.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIf.h b/src/CBot/CBotInstr/CBotIf.h index de4c3672..41c533d9 100644 --- a/src/CBot/CBotInstr/CBotIf.h +++ b/src/CBot/CBotInstr/CBotIf.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.cpp b/src/CBot/CBotInstr/CBotIndexExpr.cpp index 471089c3..27784c53 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.cpp +++ b/src/CBot/CBotInstr/CBotIndexExpr.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotIndexExpr.h" +#include "CBot/CBotInstr/CBotIndexExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVarArray.h" +#include "CBot/CBotVar/CBotVarArray.h" // Local include diff --git a/src/CBot/CBotInstr/CBotIndexExpr.h b/src/CBot/CBotInstr/CBotIndexExpr.h index fe1ea555..89f39649 100644 --- a/src/CBot/CBotInstr/CBotIndexExpr.h +++ b/src/CBot/CBotInstr/CBotIndexExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstArray.cpp b/src/CBot/CBotInstr/CBotInstArray.cpp index 7d2a441e..b5497f3a 100644 --- a/src/CBot/CBotInstr/CBotInstArray.cpp +++ b/src/CBot/CBotInstr/CBotInstArray.cpp @@ -18,19 +18,19 @@ */ // Modules inlcude -#include "CBotInstArray.h" +#include "CBot/CBotInstr/CBotInstArray.h" -#include "CBotLeftExprVar.h" -#include "CBotExpression.h" -#include "CBotListArray.h" -#include "CBotEmpty.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotInstr/CBotListArray.h" +#include "CBot/CBotInstr/CBotEmpty.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstArray.h b/src/CBot/CBotInstr/CBotInstArray.h index 2b02c66d..217cab69 100644 --- a/src/CBot/CBotInstr/CBotInstArray.h +++ b/src/CBot/CBotInstr/CBotInstArray.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstr.cpp b/src/CBot/CBotInstr/CBotInstr.cpp index 7d376f7c..9d6aac32 100644 --- a/src/CBot/CBotInstr/CBotInstr.cpp +++ b/src/CBot/CBotInstr/CBotInstr.cpp @@ -18,29 +18,29 @@ */ // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" -#include "CBotInstr/CBotFor.h" -#include "CBotInstr/CBotDo.h" -#include "CBotInstr/CBotBreak.h" -#include "CBotInstr/CBotSwitch.h" -#include "CBotInstr/CBotTry.h" -#include "CBotInstr/CBotThrow.h" -#include "CBotInstr/CBotInt.h" -#include "CBotInstr/CBotFloat.h" -#include "CBotInstr/CBotWhile.h" -#include "CBotInstr/CBotIString.h" -#include "CBotInstr/CBotBoolean.h" -#include "CBotInstr/CBotIf.h" -#include "CBotInstr/CBotReturn.h" -#include "CBotInstr/CBotClassInst.h" -#include "CBotInstr/CBotExpression.h" -#include "CBotInstr/CBotInstArray.h" +#include "CBot/CBotInstr/CBotFor.h" +#include "CBot/CBotInstr/CBotDo.h" +#include "CBot/CBotInstr/CBotBreak.h" +#include "CBot/CBotInstr/CBotSwitch.h" +#include "CBot/CBotInstr/CBotTry.h" +#include "CBot/CBotInstr/CBotThrow.h" +#include "CBot/CBotInstr/CBotInt.h" +#include "CBot/CBotInstr/CBotFloat.h" +#include "CBot/CBotInstr/CBotWhile.h" +#include "CBot/CBotInstr/CBotIString.h" +#include "CBot/CBotInstr/CBotBoolean.h" +#include "CBot/CBotInstr/CBotIf.h" +#include "CBot/CBotInstr/CBotReturn.h" +#include "CBot/CBotInstr/CBotClassInst.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotInstr/CBotInstArray.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" -#include "CBotClass.h" -#include "CBotStack.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstr.h b/src/CBot/CBotInstr/CBotInstr.h index 1f25115e..acf36781 100644 --- a/src/CBot/CBotInstr/CBotInstr.h +++ b/src/CBot/CBotInstr/CBotInstr.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotToken.h" +#include "CBot/CBotToken.h" -#include "CBotCStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrCall.cpp b/src/CBot/CBotInstr/CBotInstrCall.cpp index ca21b87d..d83f2a8a 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.cpp +++ b/src/CBot/CBotInstr/CBotInstrCall.cpp @@ -19,12 +19,12 @@ // Modules inlcude #include "CBotInstrCall.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrCall.h b/src/CBot/CBotInstr/CBotInstrCall.h index db259039..2a35eeba 100644 --- a/src/CBot/CBotInstr/CBotInstrCall.h +++ b/src/CBot/CBotInstr/CBotInstrCall.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.cpp b/src/CBot/CBotInstr/CBotInstrMethode.cpp index 26f188f8..3778a9c2 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.cpp +++ b/src/CBot/CBotInstr/CBotInstrMethode.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotInstrMethode.h" +#include "CBot/CBotInstr/CBotInstrMethode.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrMethode.h b/src/CBot/CBotInstr/CBotInstrMethode.h index f9447609..1c8512d8 100644 --- a/src/CBot/CBotInstr/CBotInstrMethode.h +++ b/src/CBot/CBotInstr/CBotInstrMethode.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInstrUtils.cpp b/src/CBot/CBotInstr/CBotInstrUtils.cpp index 1b7e65c7..a7fc8c9d 100644 --- a/src/CBot/CBotInstr/CBotInstrUtils.cpp +++ b/src/CBot/CBotInstr/CBotInstrUtils.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotToken.h" -#include "CBotCStack.h" -#include "CBotTypResult.h" -#include "CBotExpression.h" -#include "CBotClass.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotTypResult.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotClass.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInt.cpp b/src/CBot/CBotInstr/CBotInt.cpp index 7ea8cbf6..93cc0f68 100644 --- a/src/CBot/CBotInstr/CBotInt.cpp +++ b/src/CBot/CBotInstr/CBotInt.cpp @@ -18,16 +18,16 @@ */ // Modules inlcude -#include "CBotInt.h" +#include "CBot/CBotInstr/CBotInt.h" -#include "CBotLeftExprVar.h" -#include "CBotInstArray.h" -#include "CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotInstArray.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotInt.h b/src/CBot/CBotInstr/CBotInt.h index 113897f6..4489b8b7 100644 --- a/src/CBot/CBotInstr/CBotInt.h +++ b/src/CBot/CBotInstr/CBotInt.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp index e09a2263..28badcaa 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.cpp +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -18,16 +18,16 @@ */ // Modules inlcude -#include "CBotLeftExpr.h" -#include "CBotFieldExpr.h" -#include "CBotIndexExpr.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotLeftExpr.h" +#include "CBot/CBotInstr/CBotFieldExpr.h" +#include "CBot/CBotInstr/CBotIndexExpr.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" -#include "CBotVar/CBotVarArray.h" +#include "CBot/CBotVar/CBotVarArray.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExpr.h b/src/CBot/CBotInstr/CBotLeftExpr.h index 5f09b507..9ac75d95 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.h +++ b/src/CBot/CBotInstr/CBotLeftExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.cpp b/src/CBot/CBotInstr/CBotLeftExprVar.cpp index 18af17c7..517d8b71 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.cpp +++ b/src/CBot/CBotInstr/CBotLeftExprVar.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotLeftExprVar.h" +#include "CBot/CBotInstr/CBotLeftExprVar.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLeftExprVar.h b/src/CBot/CBotInstr/CBotLeftExprVar.h index 0c9e0cc2..a9526e84 100644 --- a/src/CBot/CBotInstr/CBotLeftExprVar.h +++ b/src/CBot/CBotInstr/CBotLeftExprVar.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListArray.cpp b/src/CBot/CBotInstr/CBotListArray.cpp index ce1a5f8a..f19cfa5e 100644 --- a/src/CBot/CBotInstr/CBotListArray.cpp +++ b/src/CBot/CBotInstr/CBotListArray.cpp @@ -18,17 +18,17 @@ */ // Modules inlcude -#include "CBotListArray.h" +#include "CBot/CBotInstr/CBotListArray.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotExprNull.h" -#include "CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotExprNull.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListArray.h b/src/CBot/CBotInstr/CBotListArray.h index 32a155c6..a5069a9e 100644 --- a/src/CBot/CBotInstr/CBotListArray.h +++ b/src/CBot/CBotInstr/CBotListArray.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListExpression.cpp b/src/CBot/CBotInstr/CBotListExpression.cpp index 670f3fb5..5f5d9141 100644 --- a/src/CBot/CBotInstr/CBotListExpression.cpp +++ b/src/CBot/CBotInstr/CBotListExpression.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotListExpression.h" -#include "CBotExpression.h" -#include "CBotIString.h" -#include "CBotFloat.h" +#include "CBot/CBotInstr/CBotListExpression.h" +#include "CBot/CBotInstr/CBotExpression.h" +#include "CBot/CBotInstr/CBotIString.h" +#include "CBot/CBotInstr/CBotFloat.h" #include "CBotBoolean.h" -#include "CBotInt.h" +#include "CBot/CBotInstr/CBotInt.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListExpression.h b/src/CBot/CBotInstr/CBotListExpression.h index a4ea3b2f..1b4ccae3 100644 --- a/src/CBot/CBotInstr/CBotListExpression.h +++ b/src/CBot/CBotInstr/CBotListExpression.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListInstr.cpp b/src/CBot/CBotInstr/CBotListInstr.cpp index 576012b8..13b4ded7 100644 --- a/src/CBot/CBotInstr/CBotListInstr.cpp +++ b/src/CBot/CBotInstr/CBotListInstr.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotListInstr.h" -#include "CBotBlock.h" +#include "CBot/CBotInstr/CBotListInstr.h" +#include "CBot/CBotInstr/CBotBlock.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotListInstr.h b/src/CBot/CBotInstr/CBotListInstr.h index b0612d86..b3d43622 100644 --- a/src/CBot/CBotInstr/CBotListInstr.h +++ b/src/CBot/CBotInstr/CBotListInstr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLogicExpr.cpp b/src/CBot/CBotInstr/CBotLogicExpr.cpp index d332d18f..9e1eb4c4 100644 --- a/src/CBot/CBotInstr/CBotLogicExpr.cpp +++ b/src/CBot/CBotInstr/CBotLogicExpr.cpp @@ -18,9 +18,9 @@ */ // Modules inlcude -#include "CBotLogicExpr.h" +#include "CBot/CBotInstr/CBotLogicExpr.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotLogicExpr.h b/src/CBot/CBotInstr/CBotLogicExpr.h index 25e67854..33b65033 100644 --- a/src/CBot/CBotInstr/CBotLogicExpr.h +++ b/src/CBot/CBotInstr/CBotLogicExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotNew.cpp b/src/CBot/CBotInstr/CBotNew.cpp index b7e36316..2351835c 100644 --- a/src/CBot/CBotInstr/CBotNew.cpp +++ b/src/CBot/CBotInstr/CBotNew.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotNew.h" +#include "CBot/CBotInstr/CBotNew.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotNew.h b/src/CBot/CBotInstr/CBotNew.h index f93bb6be..8a69e61e 100644 --- a/src/CBot/CBotInstr/CBotNew.h +++ b/src/CBot/CBotInstr/CBotNew.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index 21b40d18..e4e34796 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -18,23 +18,23 @@ */ // Modules inlcude -#include "CBotParExpr.h" -#include "CBotExprUnaire.h" -#include "CBotExprVar.h" -#include "CBotInstrCall.h" -#include "CBotPostIncExpr.h" -#include "CBotPreIncExpr.h" -#include "CBotExprNum.h" -#include "CBotExprAlpha.h" -#include "CBotExprBool.h" -#include "CBotNew.h" -#include "CBotExprNull.h" -#include "CBotExprNan.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotParExpr.h" +#include "CBot/CBotInstr/CBotExprUnaire.h" +#include "CBot/CBotInstr/CBotExprVar.h" +#include "CBot/CBotInstr/CBotInstrCall.h" +#include "CBot/CBotInstr/CBotPostIncExpr.h" +#include "CBot/CBotInstr/CBotPreIncExpr.h" +#include "CBot/CBotInstr/CBotExprNum.h" +#include "CBot/CBotInstr/CBotExprAlpha.h" +#include "CBot/CBotInstr/CBotExprBool.h" +#include "CBot/CBotInstr/CBotNew.h" +#include "CBot/CBotInstr/CBotExprNull.h" +#include "CBot/CBotInstr/CBotExprNan.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" -#include "CBotCStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotParExpr.h b/src/CBot/CBotInstr/CBotParExpr.h index 3fdbb639..992b7028 100644 --- a/src/CBot/CBotInstr/CBotParExpr.h +++ b/src/CBot/CBotInstr/CBotParExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.cpp b/src/CBot/CBotInstr/CBotPostIncExpr.cpp index 938274e6..99531023 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPostIncExpr.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotPostIncExpr.h" -#include "CBotExprVar.h" +#include "CBot/CBotInstr/CBotPostIncExpr.h" +#include "CBot/CBotInstr/CBotExprVar.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPostIncExpr.h b/src/CBot/CBotInstr/CBotPostIncExpr.h index 79bc86e2..cdc45b34 100644 --- a/src/CBot/CBotInstr/CBotPostIncExpr.h +++ b/src/CBot/CBotInstr/CBotPostIncExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.cpp b/src/CBot/CBotInstr/CBotPreIncExpr.cpp index 30ee9a97..6e8b7440 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.cpp +++ b/src/CBot/CBotInstr/CBotPreIncExpr.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotPreIncExpr.h" -#include "CBotExprVar.h" +#include "CBot/CBotInstr/CBotPreIncExpr.h" +#include "CBot/CBotInstr/CBotExprVar.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotPreIncExpr.h b/src/CBot/CBotInstr/CBotPreIncExpr.h index cf91a22f..eb5d2a98 100644 --- a/src/CBot/CBotInstr/CBotPreIncExpr.h +++ b/src/CBot/CBotInstr/CBotPreIncExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotReturn.cpp b/src/CBot/CBotInstr/CBotReturn.cpp index 9f5d00f7..c89e959c 100644 --- a/src/CBot/CBotInstr/CBotReturn.cpp +++ b/src/CBot/CBotInstr/CBotReturn.cpp @@ -18,14 +18,14 @@ */ // Modules inlcude -#include "CBotReturn.h" +#include "CBot/CBotInstr/CBotReturn.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotReturn.h b/src/CBot/CBotInstr/CBotReturn.h index 7af52e52..e41240ec 100644 --- a/src/CBot/CBotInstr/CBotReturn.h +++ b/src/CBot/CBotInstr/CBotReturn.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.cpp b/src/CBot/CBotInstr/CBotSwitch.cpp index ef53fd77..0104d5da 100644 --- a/src/CBot/CBotInstr/CBotSwitch.cpp +++ b/src/CBot/CBotInstr/CBotSwitch.cpp @@ -20,13 +20,13 @@ */ // Modules inlcude -#include "CBotSwitch.h" -#include "CBotCase.h" -#include "CBotBlock.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotSwitch.h" +#include "CBot/CBotInstr/CBotCase.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotSwitch.h b/src/CBot/CBotInstr/CBotSwitch.h index 6edb38b8..f174161c 100644 --- a/src/CBot/CBotInstr/CBotSwitch.h +++ b/src/CBot/CBotInstr/CBotSwitch.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotThrow.cpp b/src/CBot/CBotInstr/CBotThrow.cpp index 4fc8cddd..4525dbbb 100644 --- a/src/CBot/CBotInstr/CBotThrow.cpp +++ b/src/CBot/CBotInstr/CBotThrow.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotThrow.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotThrow.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotThrow.h b/src/CBot/CBotInstr/CBotThrow.h index cbe06e50..62e11fc4 100644 --- a/src/CBot/CBotInstr/CBotThrow.h +++ b/src/CBot/CBotInstr/CBotThrow.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTry.cpp b/src/CBot/CBotInstr/CBotTry.cpp index d3aecf85..5cf57904 100644 --- a/src/CBot/CBotInstr/CBotTry.cpp +++ b/src/CBot/CBotInstr/CBotTry.cpp @@ -19,10 +19,10 @@ // Modules inlcude #include "CBotTry.h" -#include "CBotBlock.h" +#include "CBot/CBotInstr/CBotBlock.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTry.h b/src/CBot/CBotInstr/CBotTry.h index 9c79b3a4..6777a710 100644 --- a/src/CBot/CBotInstr/CBotTry.h +++ b/src/CBot/CBotInstr/CBotTry.h @@ -18,9 +18,9 @@ */ // Modules inlcude -#include "CBotCatch.h" +#include "CBot/CBotInstr/CBotCatch.h" -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp index 0d97f407..26342f94 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.cpp +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.cpp @@ -18,18 +18,18 @@ */ // Modules inlcude -#include "CBotTwoOpExpr.h" +#include "CBot/CBotInstr/CBotTwoOpExpr.h" -#include "CBotInstr/CBotInstrUtils.h" +#include "CBot/CBotInstr/CBotInstrUtils.h" -#include "CBotParExpr.h" -#include "CBotLogicExpr.h" -#include "CBotExpression.h" +#include "CBot/CBotInstr/CBotParExpr.h" +#include "CBot/CBotInstr/CBotLogicExpr.h" +#include "CBot/CBotInstr/CBotExpression.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotInstr/CBotTwoOpExpr.h b/src/CBot/CBotInstr/CBotTwoOpExpr.h index 63e20ddc..e9829647 100644 --- a/src/CBot/CBotInstr/CBotTwoOpExpr.h +++ b/src/CBot/CBotInstr/CBotTwoOpExpr.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.cpp b/src/CBot/CBotInstr/CBotWhile.cpp index eb38aea1..99e120e6 100644 --- a/src/CBot/CBotInstr/CBotWhile.cpp +++ b/src/CBot/CBotInstr/CBotWhile.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotWhile.h" -#include "CBotBlock.h" -#include "CBotCondition.h" +#include "CBot/CBotInstr/CBotWhile.h" +#include "CBot/CBotInstr/CBotBlock.h" +#include "CBot/CBotInstr/CBotCondition.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" // Local include diff --git a/src/CBot/CBotInstr/CBotWhile.h b/src/CBot/CBotInstr/CBotWhile.h index a6fac948..6b91c741 100644 --- a/src/CBot/CBotInstr/CBotWhile.h +++ b/src/CBot/CBotInstr/CBotWhile.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 1bdd89c6..e2058a80 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -18,16 +18,16 @@ */ // Modules inlcude -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" -#include "CBotCall.h" -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" -#include "CBotUtils.h" -#include "CBotFileUtils.h" +#include "CBot/CBotCall.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotUtils.h" +#include "CBot/CBotFileUtils.h" -#include "CBotInstr/CBotFunction.h" +#include "CBot/CBotInstr/CBotFunction.h" #include "StringFunctions.h" diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index 87dff3c2..88dd0741 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -20,11 +20,11 @@ #pragma once // Modules inlcude -#include "CBotTypResult.h" -#include "CBotString.h" -#include "CBotStringArray.h" +#include "CBot/CBotTypResult.h" +#include "CBot/CBotString.h" +#include "CBot/CBotStringArray.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" // Local include diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index ac312c9a..8577c451 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotStack.h" -#include "CBotCall.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCall.h" -#include "CBotInstr/CBotFunction.h" +#include "CBot/CBotInstr/CBotFunction.h" -#include "CBotVar/CBotVarPointer.h" -#include "CBotVar/CBotVarClass.h" +#include "CBot/CBotVar/CBotVarPointer.h" +#include "CBot/CBotVar/CBotVarClass.h" -#include "CBotFileUtils.h" +#include "CBot/CBotFileUtils.h" // Local include diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h index 0e8ddd03..3b58116f 100644 --- a/src/CBot/CBotStack.h +++ b/src/CBot/CBotStack.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotDefines.h" -#include "CBotTypResult.h" -#include "CBotString.h" +#include "CBot/CBotDefines.h" +#include "CBot/CBotTypResult.h" +#include "CBot/CBotString.h" // Local include diff --git a/src/CBot/CBotString.cpp b/src/CBot/CBotString.cpp index d8fc07c7..fafcf77a 100644 --- a/src/CBot/CBotString.cpp +++ b/src/CBot/CBotString.cpp @@ -19,7 +19,7 @@ ///////////////////////////////////////////////////// -#include "CBotString.h" +#include "CBot/CBotString.h" //strings management #include diff --git a/src/CBot/CBotString.h b/src/CBot/CBotString.h index 58630ea6..3d87d2c0 100644 --- a/src/CBot/CBotString.h +++ b/src/CBot/CBotString.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" // Local include diff --git a/src/CBot/CBotStringArray.cpp b/src/CBot/CBotStringArray.cpp index 6d5d6617..add32492 100644 --- a/src/CBot/CBotStringArray.cpp +++ b/src/CBot/CBotStringArray.cpp @@ -18,9 +18,9 @@ */ // Modules inlcude -#include "CBotStringArray.h" +#include "CBot/CBotStringArray.h" -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" // Local include diff --git a/src/CBot/CBotStringArray.h b/src/CBot/CBotStringArray.h index bc13233c..af4e9eff 100644 --- a/src/CBot/CBotStringArray.h +++ b/src/CBot/CBotStringArray.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotString.h" +#include "CBot/CBotString.h" // Local include diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index d5840f99..6b34d688 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -18,7 +18,7 @@ */ // Modules inlcude -#include "CBotToken.h" +#include "CBot/CBotToken.h" // Local include diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index 85dc6988..12be45cd 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotStringArray.h" +#include "CBot/CBotStringArray.h" // Local include diff --git a/src/CBot/CBotTypResult.cpp b/src/CBot/CBotTypResult.cpp index 0ba8ff55..bde4f748 100644 --- a/src/CBot/CBotTypResult.cpp +++ b/src/CBot/CBotTypResult.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotTypResult.h" +#include "CBot/CBotTypResult.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" -#include "CBotClass.h" +#include "CBot/CBotClass.h" // Local include diff --git a/src/CBot/CBotUtils.cpp b/src/CBot/CBotUtils.cpp index 5e38cb5f..715a84c3 100644 --- a/src/CBot/CBotUtils.cpp +++ b/src/CBot/CBotUtils.cpp @@ -18,14 +18,14 @@ */ // Modules inlcude -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" -#include "CBotToken.h" -#include "CBotClass.h" -#include "CBotStack.h" -#include "CBotCStack.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotCStack.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h index 0ce76414..819331cd 100644 --- a/src/CBot/CBotUtils.h +++ b/src/CBot/CBotUtils.h @@ -20,8 +20,8 @@ #pragma once // Modules inlcude -#include "CBotString.h" -#include "CBotTypResult.h" +#include "CBot/CBotString.h" +#include "CBot/CBotTypResult.h" // Local include diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index 4f75249c..43a6a34f 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -19,21 +19,21 @@ // Modules inlcude -#include "CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" -#include "CBotStack.h" +#include "CBot/CBotStack.h" -#include "CBotVar/CBotVarArray.h" -#include "CBotVar/CBotVarPointer.h" -#include "CBotVar/CBotVarClass.h" -#include "CBotVar/CBotVarBoolean.h" -#include "CBotVar/CBotVarString.h" -#include "CBotVar/CBotVarFloat.h" -#include "CBotVar/CBotVarInt.h" +#include "CBot/CBotVar/CBotVarArray.h" +#include "CBot/CBotVar/CBotVarPointer.h" +#include "CBot/CBotVar/CBotVarClass.h" +#include "CBot/CBotVar/CBotVarBoolean.h" +#include "CBot/CBotVar/CBotVarString.h" +#include "CBot/CBotVar/CBotVarFloat.h" +#include "CBot/CBotVar/CBotVarInt.h" -#include "CBotClass.h" +#include "CBot/CBotClass.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" // Local include diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index 1abedb33..56a5d5da 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "../CBotDefines.h" +#include "CBot/CBotDefines.h" -#include "../CBotString.h" +#include "CBot/CBotString.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarArray.cpp b/src/CBot/CBotVar/CBotVarArray.cpp index cece8117..f24dd207 100644 --- a/src/CBot/CBotVar/CBotVarArray.cpp +++ b/src/CBot/CBotVar/CBotVarArray.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "CBotVarArray.h" -#include "CBotVarClass.h" -#include "CBotToken.h" -#include "CBotFileUtils.h" +#include "CBot/CBotVar/CBotVarArray.h" +#include "CBot/CBotVar/CBotVarClass.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotFileUtils.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarArray.h b/src/CBot/CBotVar/CBotVarArray.h index 650437f3..542984d0 100644 --- a/src/CBot/CBotVar/CBotVarArray.h +++ b/src/CBot/CBotVar/CBotVarArray.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotDefines.h" +#include "CBot/CBotDefines.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarBoolean.cpp b/src/CBot/CBotVar/CBotVarBoolean.cpp index b7680078..2ae7e6b3 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.cpp +++ b/src/CBot/CBotVar/CBotVarBoolean.cpp @@ -18,11 +18,11 @@ */ // Modules inlcude -#include "CBotVarBoolean.h" +#include "CBot/CBotVar/CBotVarBoolean.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarBoolean.h b/src/CBot/CBotVar/CBotVarBoolean.h index f21cf285..0451f6d2 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.h +++ b/src/CBot/CBotVar/CBotVarBoolean.h @@ -20,9 +20,9 @@ #pragma once // Modules inlcude -#include "CBotToken.h" +#include "CBot/CBotToken.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index 467255cb..894ff039 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -18,15 +18,15 @@ */ // Modules inlcude -#include "CBotVarClass.h" +#include "CBot/CBotVar/CBotVarClass.h" -#include "CBotClass.h" -#include "CBotStack.h" -#include "CBotDefines.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotStack.h" +#include "CBot/CBotDefines.h" -#include "CBotFileUtils.h" +#include "CBot/CBotFileUtils.h" -#include "CBotInstr/CBotInstr.h" +#include "CBot/CBotInstr/CBotInstr.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h index ff6f9248..a04fccc7 100644 --- a/src/CBot/CBotVar/CBotVarClass.h +++ b/src/CBot/CBotVar/CBotVarClass.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp index 9f25fbb7..d23fca46 100644 --- a/src/CBot/CBotVar/CBotVarFloat.cpp +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotVarFloat.h" +#include "CBot/CBotVar/CBotVarFloat.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" -#include "CBotToken.h" +#include "CBot/CBotToken.h" -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarFloat.h b/src/CBot/CBotVar/CBotVarFloat.h index e55378a7..37a121be 100644 --- a/src/CBot/CBotVar/CBotVarFloat.h +++ b/src/CBot/CBotVar/CBotVarFloat.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp index 67220a1a..f584997f 100644 --- a/src/CBot/CBotVar/CBotVarInt.cpp +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotVarInt.h" +#include "CBot/CBotVar/CBotVarInt.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" -#include "CBotToken.h" +#include "CBot/CBotToken.h" -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarInt.h b/src/CBot/CBotVar/CBotVarInt.h index 524c2762..0d8372e9 100644 --- a/src/CBot/CBotVar/CBotVarInt.h +++ b/src/CBot/CBotVar/CBotVarInt.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarPointer.cpp b/src/CBot/CBotVar/CBotVarPointer.cpp index ff01eec7..de0f1665 100644 --- a/src/CBot/CBotVar/CBotVarPointer.cpp +++ b/src/CBot/CBotVar/CBotVarPointer.cpp @@ -18,14 +18,14 @@ */ // Modules inlcude -#include "CBotVarPointer.h" -#include "CBotToken.h" -#include "CBotClass.h" -#include "CBotVarClass.h" +#include "CBot/CBotVar/CBotVarPointer.h" +#include "CBot/CBotToken.h" +#include "CBot/CBotClass.h" +#include "CBot/CBotVar/CBotVarClass.h" -#include "CBotFileUtils.h" +#include "CBot/CBotFileUtils.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarPointer.h b/src/CBot/CBotVar/CBotVarPointer.h index 278fbd8d..8f54f58c 100644 --- a/src/CBot/CBotVar/CBotVarPointer.h +++ b/src/CBot/CBotVar/CBotVarPointer.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarString.cpp b/src/CBot/CBotVar/CBotVarString.cpp index 09c4e761..25f39a6c 100644 --- a/src/CBot/CBotVar/CBotVarString.cpp +++ b/src/CBot/CBotVar/CBotVarString.cpp @@ -18,13 +18,13 @@ */ // Modules inlcude -#include "CBotVarString.h" +#include "CBot/CBotVar/CBotVarString.h" -#include "CBotEnums.h" +#include "CBot/CBotEnums.h" -#include "CBotToken.h" +#include "CBot/CBotToken.h" -#include "CBotUtils.h" +#include "CBot/CBotUtils.h" // Local include diff --git a/src/CBot/CBotVar/CBotVarString.h b/src/CBot/CBotVar/CBotVarString.h index 75b4b1d0..662e1d96 100644 --- a/src/CBot/CBotVar/CBotVarString.h +++ b/src/CBot/CBotVar/CBotVarString.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 91f08748..e9c602e3 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -75,7 +75,7 @@ set(SOURCES # Includes set(LOCAL_INCLUDES - ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/.. ) include_directories(${LOCAL_INCLUDES}) diff --git a/src/CBot/StringFunctions.cpp b/src/CBot/StringFunctions.cpp index c38af371..e323fe34 100644 --- a/src/CBot/StringFunctions.cpp +++ b/src/CBot/StringFunctions.cpp @@ -18,12 +18,12 @@ */ // Modules inlcude -#include "StringFunctions.h" +#include "CBot/StringFunctions.h" -#include "CBotProgram.h" -#include "CBotEnums.h" +#include "CBot/CBotProgram.h" +#include "CBot/CBotEnums.h" -#include "CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVar.h" // Local include diff --git a/src/CBot/StringFunctions.h b/src/CBot/StringFunctions.h index da534aad..fd3618f4 100644 --- a/src/CBot/StringFunctions.h +++ b/src/CBot/StringFunctions.h @@ -20,7 +20,7 @@ #pragma once // Modules inlcude -#include "CBotTypResult.h" +#include "CBot/CBotTypResult.h" // Local include From 2048ecb14816989d7bbfb63ad0912b6d46538b5e Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 14:45:31 +0100 Subject: [PATCH 85/91] Delete CBot.cpp. --- src/CBot/CBot.cpp | 85 ----------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 src/CBot/CBot.cpp diff --git a/src/CBot/CBot.cpp b/src/CBot/CBot.cpp deleted file mode 100644 index 52ea9c60..00000000 --- a/src/CBot/CBot.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * This file is part of the Colobot: Gold Edition source code - * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam - * http://epsitec.ch; http://colobot.info; http://github.com/colobot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://gnu.org/licenses - */ - -/////////////////////////////////////////////////////////////////////// - -// compilation of various instructions -// compile all routines as static -// and return an object according to what was found as instruction - -// compiler principle: -// compile the routines return an object of the class corresponding to the operation found -// this is always a subclass of CBotInstr. -// (CBotInstr objects are never used directly) - - -// compiles if the routine returns nullptr is that the statement is false -// or misunderstood. -// the error is then on the stack CBotCStack :: Isok () is false - - -// Modules inlcude -#include "CBot.h" -#include "CBotInstr/CBotDo.h" -#include "CBotInstr/CBotFor.h" -#include "CBotInstr/CBotSwitch.h" -#include "CBotInstr/CBotBreak.h" -#include "CBotInstr/CBotTry.h" -#include "CBotInstr/CBotThrow.h" -#include "CBotInstr/CBotWhile.h" -#include "CBotInstr/CBotExprAlpha.h" -#include "CBotInstr/CBotExprNum.h" -#include "CBotInstr/CBotNew.h" -#include "CBotInstr/CBotExprNan.h" -#include "CBotInstr/CBotExprNull.h" -#include "CBotInstr/CBotExprBool.h" -#include "CBotInstr/CBotLeftExprVar.h" -#include "CBotInstr/CBotPreIncExpr.h" -#include "CBotInstr/CBotPostIncExpr.h" -#include "CBotInstr/CBotExprVar.h" -#include "CBotInstr/CBotInstrCall.h" -#include "CBotInstr/CBotListInstr.h" -#include "CBotInstr/CBotExprUnaire.h" -#include "CBotInstr/CBotBoolExpr.h" -#include "CBotInstr/CBotTwoOpExpr.h" -#include "CBotInstr/CBotExpression.h" -#include "CBotInstr/CBotIndexExpr.h" -#include "CBotInstr/CBotFieldExpr.h" -#include "CBotInstr/CBotClassInst.h" -#include "CBotInstr/CBotIString.h" -#include "CBotInstr/CBotFloat.h" -#include "CBotInstr/CBotBoolean.h" -#include "CBotInstr/CBotEmpty.h" -#include "CBotInstr/CBotReturn.h" -#include "CBotInstr/CBotIf.h" -#include "CBotInstr/CBotListArray.h" -#include "CBotInstr/CBotInstArray.h" -#include "CBotInstr/CBotInt.h" - -#include "CBotStack.h" -#include "CBotCStack.h" -#include "CBotClass.h" - -#include "CBotVar/CBotVar.h" - -// Local include - -// Global include -#include - From 17704c4d547f4d4e4d9f17adf30f0727642ee9c9 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 14:46:38 +0100 Subject: [PATCH 86/91] Delete resource.h. --- src/CBot/resource.h | 114 -------------------------------------------- 1 file changed, 114 deletions(-) delete mode 100644 src/CBot/resource.h diff --git a/src/CBot/resource.h b/src/CBot/resource.h deleted file mode 100644 index 60d2c775..00000000 --- a/src/CBot/resource.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * This file is part of the Colobot: Gold Edition source code - * Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam - * http://epsitec.ch; http://colobot.info; http://github.com/colobot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://gnu.org/licenses - */ - -#pragma once - - -enum EID -{ - ID_IF = 2000, - ID_ELSE, - ID_WHILE, - ID_DO, - ID_FOR, - ID_BREAK, - ID_CONTINUE, - ID_SWITCH, - ID_CASE, - ID_DEFAULT, - ID_TRY, - ID_THROW, - ID_CATCH, - ID_FINALLY, - ID_TXT_AND, - ID_TXT_OR, - ID_TXT_NOT, - ID_RETURN, - ID_CLASS, - ID_EXTENDS, - ID_SYNCHO, - ID_NEW, - ID_PUBLIC, - ID_EXTERN, - ID_STATIC, - ID_PROTECTED, - ID_PRIVATE, - ID_INT, - ID_FLOAT, - ID_BOOLEAN, - ID_STRING, - ID_VOID, - ID_BOOL, - - ID_TRUE = 2200, - ID_FALSE, - ID_NULL, - ID_NAN, - - ID_OPENPAR = 2300, - 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 = 4000, - TX_NAN -}; From f9ab37fd1401c1924598398012a489c5034ff55c Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 14:49:27 +0100 Subject: [PATCH 87/91] Add comment deleted by commit 573e1152e339f5d018464a473340980a3d8dbb8b. --- src/CBot/CBotEnums.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CBot/CBotEnums.h b/src/CBot/CBotEnums.h index 17dbef69..0db5aff0 100644 --- a/src/CBot/CBotEnums.h +++ b/src/CBot/CBotEnums.h @@ -49,6 +49,7 @@ enum CBotType CBotTypClass = 15, CBotTypIntrinsic = 16 // instance of a class intrinsic }; +//n = not implemented yet //! \brief CBotGet Different modes for GetPosition. enum CBotGet From 0f491ce433e39b49e329f5920779a5e50950e1e4 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 15:05:22 +0100 Subject: [PATCH 88/91] Add commments in CBot.h --- src/CBot/CBot.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 11595183..4ba45b0e 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -17,6 +17,12 @@ * along with this program. If not, see http://gnu.org/licenses */ +/*! + * \file CBot.h + * \brief Public interface of CBot language interpreter. CBot.h is the only file + * that should be included by any Colobot files outside of the CBot module. + */ + // Modules inlcude #include "CBot/CBotFileUtils.h" #include "CBot/CBotClass.h" From aa3f8c0cd1884950487e6164f20b7f21e669d3bd Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 15:37:18 +0100 Subject: [PATCH 89/91] Add missing include in CBot.h. --- src/CBot/CBot.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CBot/CBot.h b/src/CBot/CBot.h index 4ba45b0e..d0f42f1f 100644 --- a/src/CBot/CBot.h +++ b/src/CBot/CBot.h @@ -25,6 +25,8 @@ // Modules inlcude #include "CBot/CBotFileUtils.h" +#include "CBot/CBotString.h" +#include "CBot/CBotStringArray.h" #include "CBot/CBotClass.h" #include "CBot/CBotToken.h" #include "CBot/CBotProgram.h" From f45d657ab02ac21d8087e3addeca1e8ceb90e61e Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 15:42:09 +0100 Subject: [PATCH 90/91] Add comments delete by commit 1a6b5ded640c1a606d85fd5b5d1b68254b855423. --- src/CBot/CBotClass.h | 140 ++++++++++++++++++++++++++++++++++++++++- src/CBot/CBotDefines.h | 3 + src/CBot/CBotProgram.h | 51 +++++++++++++-- 3 files changed, 187 insertions(+), 7 deletions(-) diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index 2b151b0a..69f3605e 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -41,7 +41,8 @@ class CBotDefParam; /*! * \brief The CBotClass class Class to define new classes in the language CBOT - * for example to define the class CPoint (x, y). + * For example to define the class CPoint (x, y) see comments at end of this + * file. */ class CBotClass { @@ -77,8 +78,9 @@ public: bool intrinsic = false); /*! - * \brief AddFunction This call allows to add as external (**) new method - * used by the objects of this class. + * \brief AddFunction This call allows to add as external new method + * used by the objects of this class. See (**) at end of this file for + * more details. * \param name * \param rExec * \param rCompile @@ -339,3 +341,135 @@ private: //! Processes waiting for sync. CBotProgram* m_ProgInLock[5]; }; + +/* +(**) Note: + + To define an external function, proceed as follows: + + a) define a routine for compilation this routine receive list of parameters + (no values) and either returns a result type (CBotTyp... or 0 = void) + or an error number. + + b) define a routine for the execution this routine receive list of + parameters (with valeurs), a variable to store the result + (according to the given type at compile time) + + For example, a routine which calculates the mean of a parameter list + +int cMean(CBotVar* &pVar, CBotString& ClassName) +{ + if ( pVar == nullptr ) return 6001; // there is no parameter! + while ( pVar != nullptr ) + { + if ( pVar->GetType() > CBotTypDouble ) return 6002; // this is not a number + pVar = pVar -> GetNext(); + } + return CBotTypFloat; // the type of the result may depend on the parameters! +} + + +bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + float total = 0; + int nb = 0; + while (pVar != nullptr) + { + total += pVar->GetValFloat(); + pVar = pVar->GetNext(); + nb++; + } + pResult->SetValFloat(total/nb); // returns the mean value + return true; // operation fully completed +} + +//////////////////////////////////////////////////////////////////////////////// +// Examples of use +// Definition classes and functions + + +// define the global class CPoint +// -------------------------------- + m_pClassPoint = new CBotClass("CPoint", nullptr); + // adds the component ".x" + m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat)); + // adds the component ".y" + m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat)); + // the player can then use the instructions + // CPoint position; position.x = 12; position.y = -13.6 + +// define class CColobotObject +// -------------------------------- + // This class manages all the objects in the world of COLOBOT + // the "main" user program belongs to this class + m_pClassObject = new CBotClass("CColobotObject", m_pClassBase); + // adds the component ".position" + m_pClassObject->AddItem("position", m_pClassPoint); + // adds the component ".type" + m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort)); + // adds a definition of constant + m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT equivalent to the value 1 + // adds the FIND routine + m_pClassObject->AddFunction( rCompFind, rDoFind ); + // the player can now use the instructions + // CColobotObject chose; chose = FIND( ROBOT ) + +// define class CColobotRobot derived from CColobotObject +// --------------------------------------------------------- + // programs "main" associated with robots as a part of this class + m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject); + // add routine GOTO + m_pClassRobot->AddFunction( rCompGoto, rDoGoto ); + // the player can now use + // GOTO( FIND ( ROBOT ) ); + + +// creates an instance of the class Robot +// ------------------------------------ + // for example a new robot which has just been manufactured + CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot); + + +// compiles the program by hand for this robot +// ------------------------------------------ + CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" ); + if ( !m_pMonRobot->Compile( LeProgramme ) ) {error handling ...}; + +// build a stack for interpreter +// -------------------------------------- + CBotStack* pStack = new CBotStack(nullptr); + +// executes the main program +// ------------------------- + while( false = m_pMonRobot->Execute( "main", pStack )) + { + // program suspended + // could be pass a handle to another (safeguarding pstack for the robot one) + }; + // programme "main" finished ! + +// routine that implements the GOTO (CPoint pos) +bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception ) +{ + if (pVar->GetType() != CBotTypeClass || + pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; ) + // the parameter is not the right class? + // in fact the control is done to the routine of compilation + + m_PosToGo.Copy( pVar ); // keeps the target position (object type CBotVar) + + // or so + CBotVar* temp; + temp = pVar->GetItem("x"); // is necessary for the object of type CPoint + ASSERT (temp != nullptr && temp->GetType() == CBotTypFloat); + m_PosToGo.x = temp->GetValFloat(); + + temp = pVar->GetItem("y"); // is necessary for the object of type CPoint + ASSERT (temp != nullptr && temp->GetType() == CBotTypFloat); + m_PosToGo.y = temp->GetValFloat(); + + return (m_CurentPos == m_PosToGo); // makes true if the position is reached + // returns false if one had wait yet +} + +*/ diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index b1c73b38..93be12bb 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -118,6 +118,9 @@ #define CBotErrRead 6014 // error while reading #define CBotErrWrite 6015 // writing error +// other values ​​may be returned +// for example exceptions returned by external routines +// and " throw " with any number. // TODO: refactor & change to enum! diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index 88dd0741..98854b33 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -186,16 +186,16 @@ public: // /*! - * \brief AddFunction Call this to add externally (**) a new function used - * by the program CBoT. + * \brief AddFunction Call this to add externally a new function used + * by the program CBoT. See (**) at end of this file for more details. * \param name * \param rExec * \param rCompile * \return */ static bool AddFunction(const char* name, - bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), - CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); + bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), + CBotTypResult rCompile (CBotVar* &pVar, void* pUser)); /*! * \brief DefineNum @@ -270,3 +270,46 @@ private: //! Associated identifier. long m_Ident; }; + +/* +(**) Note: + + To define an external function, proceed as follows: + + a) define a routine for compilation this routine receive list of parameters + (no values) and either returns a result type (CBotTyp... or 0 = void) + or an error number. + + b) define a routine for the execution this routine receive list of + parameters (with valeurs), a variable to store the result + (according to the given type at compile time) + + For example, a routine which calculates the mean of a parameter list + +int cMean(CBotVar* &pVar, CBotString& ClassName) +{ + if ( pVar == nullptr ) return 6001; // there is no parameter! + while ( pVar != nullptr ) + { + if ( pVar->GetType() > CBotTypDouble ) return 6002; // this is not a number + pVar = pVar -> GetNext(); + } + return CBotTypFloat; // the type of the result may depend on the parameters! +} + + +bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception) +{ + float total = 0; + int nb = 0; + while (pVar != nullptr) + { + total += pVar->GetValFloat(); + pVar = pVar->GetNext(); + nb++; + } + pResult->SetValFloat(total/nb); // returns the mean value + return true; // operation fully completed +} + +*/ From 30c3d031410aa8f4936807d2e72638b864de83d3 Mon Sep 17 00:00:00 2001 From: Grunaka Date: Sun, 6 Dec 2015 16:30:13 +0100 Subject: [PATCH 91/91] Remove header files from to the list of .cpp files in CBot CMakeLists.txt. --- src/CBot/CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index e9c602e3..b38ff615 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -1,7 +1,4 @@ set(SOURCES - CBot.h - CBotEnums.h - CBotDefines.h CBotUtils.cpp CBotFileUtils.cpp CBotClass.cpp