Colobot
CBotFunction.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 #include "CBot/CBotInstr/CBotInstr.h"
23 
24 #include <set>
25 
26 namespace CBot
27 {
28 
42 class CBotFunction : public CBotInstr
43 {
44 public:
45  CBotFunction();
46  ~CBotFunction();
47 
57  static CBotFunction* Compile(CBotToken* &p,
58  CBotCStack* pStack,
59  CBotFunction* pFunc,
60  bool bLocal = true);
61 
69  static CBotFunction* Compile1(CBotToken* &p,
70  CBotCStack* pStack,
71  CBotClass* pClass);
72 
80  bool Execute(CBotVar** ppVars,
81  CBotStack* &pj,
82  CBotVar* pInstance = nullptr);
83 
90  void RestoreState(CBotVar** ppVars,
91  CBotStack* &pj,
92  CBotVar* pInstance = nullptr);
93 
98  void AddNext(CBotFunction* p);
99 
107  CBotTypResult CompileCall(const std::string& name,
108  CBotVar** ppVars,
109  long& nIdent);
110 
121  CBotFunction* FindLocalOrPublic(long& nIdent, const std::string& name,
122  CBotVar** ppVars,
123  CBotTypResult& TypeOrError,
124  bool bPublic = true);
125 
136  int DoCall(long& nIdent,
137  const std::string& name,
138  CBotVar** ppVars,
139  CBotStack* pStack,
140  CBotToken* pToken);
141 
149  void RestoreCall(long& nIdent,
150  const std::string& name,
151  CBotVar** ppVars,
152  CBotStack* pStack);
153 
166  int DoCall(long& nIdent,
167  const std::string& name,
168  CBotVar* pThis,
169  CBotVar** ppVars,
170  CBotStack* pStack,
171  CBotToken* pToken,
172  CBotClass* pClass);
173 
183  void RestoreCall(long& nIdent,
184  const std::string& name,
185  CBotVar* pThis,
186  CBotVar** ppVars,
187  CBotStack* pStack,
188  CBotClass* pClass);
189 
195  bool CheckParam(CBotDefParam* pParam);
196 
201  static void AddPublic(CBotFunction* pfunc);
202 
207  std::string GetName();
208 
213  std::string GetParams();
214 
219  bool IsPublic();
220 
225  bool IsExtern();
226 
231  CBotFunction* Next();
232 
241  bool GetPosition(int& start, int& stop,
242  CBotGet modestart,
243  CBotGet modestop);
244 
245 protected:
246  virtual const std::string GetDebugName() override { return "CBotFunction"; }
247  virtual std::string GetDebugData() override;
248  virtual std::map<std::string, CBotInstr*> GetDebugLinks() override;
249 
250 private:
251  friend class CBotDebug;
252  long m_nFuncIdent;
254  bool m_bSynchro;
255 
257  CBotDefParam* m_param;
259  CBotInstr* m_block;
260  CBotFunction* m_next;
262  CBotToken m_retToken;
264  CBotTypResult m_retTyp;
266  bool m_bPublic;
268  bool m_bExtern;
270  std::string m_MasterClass;
272  CBotToken m_classToken;
273  CBotProgram* m_pProg;
275  CBotToken m_extern;
276  CBotToken m_openpar;
277  CBotToken m_closepar;
278  CBotToken m_openblk;
279  CBotToken m_closeblk;
280 
282  static std::set<CBotFunction*> m_publicFunctions;
283 
284  friend class CBotProgram;
285  friend class CBotClass;
286  friend class CBotCStack;
287 
288 };
289 
290 } // namespace CBot
Class for one CBot instruction.
Definition: CBotInstr.h:86
A function declaration in the code.
Definition: CBotFunction.h:42
bool GetPosition(int &start, int &stop, CBotGet modestart, CBotGet modestop)
GetPosition.
Definition: CBotFunction.cpp:88
The CBotDefParam class A list of parameters.
Definition: CBotDefParam.h:36
Class that manages a CBot program. This is the main entry point into the CBot engine.
Definition: CBotProgram.h:83
The execution stack.
Definition: CBotStack.h:44
int DoCall(long &nIdent, const std::string &name, CBotVar **ppVars, CBotStack *pStack, CBotToken *pToken)
DoCall Fait un appel à une fonction.
Definition: CBotFunction.cpp:592
void AddNext(CBotFunction *p)
AddNext.
Definition: CBotFunction.cpp:419
bool Execute(CBotVar **ppVars, CBotStack *&pj, CBotVar *pInstance=nullptr)
Execute.
Definition: CBotFunction.cpp:334
A CBot class definition.
Definition: CBotClass.h:105
static CBotFunction * Compile1(CBotToken *&p, CBotCStack *pStack, CBotClass *pClass)
Compile1 Pre-compile a new function.
Definition: CBotFunction.cpp:244
CBotFunction * FindLocalOrPublic(long &nIdent, const std::string &name, CBotVar **ppVars, CBotTypResult &TypeOrError, bool bPublic=true)
FindLocalOrPublic Is a function according to its unique identifier if the identifier is not found...
Definition: CBotFunction.cpp:439
Various utilities used for debugging.
Definition: CBotDebug.h:29
A CBot variable.
Definition: CBotVar.h:42
std::string GetParams()
GetParams.
Definition: CBotFunction.cpp:859
virtual const std::string GetDebugName() override
Returns the name of this class.
Definition: CBotFunction.h:246
The CBotCStack class Management of the stack of compilation.
Definition: CBotCStack.h:35
CBotTypResult CompileCall(const std::string &name, CBotVar **ppVars, long &nIdent)
CompileCall.
Definition: CBotFunction.cpp:428
virtual std::string GetDebugData() override
Returns additional data associated with this instruction for debugging purposes.
Definition: CBotFunction.cpp:889
bool IsExtern()
IsExtern.
Definition: CBotFunction.cpp:82
virtual std::map< std::string, CBotInstr * > GetDebugLinks() override
Definition: CBotFunction.cpp:899
bool CheckParam(CBotDefParam *pParam)
CheckParam See if the "signature" of parameters is identical.
Definition: CBotFunction.cpp:838
void RestoreCall(long &nIdent, const std::string &name, CBotVar **ppVars, CBotStack *pStack)
RestoreCall.
Definition: CBotFunction.cpp:665
CBotGet
Different modes for CBotProgram::GetPosition.
Definition: CBotEnums.h:62
Class to define the complete type of a variable.
Definition: CBotTypResult.h:47
static CBotFunction * Compile(CBotToken *&p, CBotCStack *pStack, CBotFunction *pFunc, bool bLocal=true)
Compile Compiles a new function.
Definition: CBotFunction.cpp:130
CBot engine.
Definition: CBotCallMethode.cpp:28
CBotFunction * Next()
Next.
Definition: CBotFunction.cpp:878
void RestoreState(CBotVar **ppVars, CBotStack *&pj, CBotVar *pInstance=nullptr)
RestoreState.
Definition: CBotFunction.cpp:390
static void AddPublic(CBotFunction *pfunc)
AddPublic.
Definition: CBotFunction.cpp:884
std::string GetName()
GetName.
Definition: CBotFunction.cpp:853
bool IsPublic()
IsPublic.
Definition: CBotFunction.cpp:76
Class representing one token of a program.
Definition: CBotToken.h:80