Colobot
CBotProgram.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/CBotTypResult.h"
23 #include "CBot/CBotEnums.h"
24 
25 #include <vector>
26 
27 namespace CBot
28 {
29 
30 class CBotFunction;
31 class CBotClass;
32 class CBotStack;
33 class CBotVar;
34 class CBotExternalCallList;
35 
84 {
85 public:
89  CBotProgram();
90 
95  CBotProgram(CBotVar* thisVar);
96 
100  ~CBotProgram();
101 
105  static void Init();
106 
110  static void Free();
111 
116  static int GetVersion();
117 
132  bool Compile(const std::string& program, std::vector<std::string>& functions, void* pUser = nullptr);
133 
140 
148  bool GetError(CBotError& code, int& start, int& end);
149 
158  bool GetError(CBotError& code, int& start, int& end, CBotProgram*& pProg);
159 
166  bool Start(const std::string& name);
167 
178  bool Run(void* pUser = nullptr, int timer = -1);
179 
187  bool GetRunPos(std::string& functionName, int& start, int& end);
188 
195  CBotVar* GetStackVars(std::string& functionName, int level);
196 
200  void Stop();
201 
208  static void SetTimer(int n);
209 
272  static bool AddFunction(const std::string& name,
273  bool rExec(CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser),
274  CBotTypResult rCompile(CBotVar*& pVar, void* pUser));
275 
280  static bool DefineNum(const std::string& name, long val);
281 
294  bool SaveState(FILE* pf);
295 
304  bool RestoreState(FILE* pf);
305 
320  bool GetPosition(const std::string& name,
321  int& start,
322  int& stop,
323  CBotGet modestart = GetPosExtern,
324  CBotGet modestop = GetPosBloc);
325 
334 
341 
346 
347 private:
349  static CBotExternalCallList* m_externalCalls;
351  CBotFunction* m_functions = nullptr;
353  CBotFunction* m_entryPoint = nullptr;
355  CBotClass* m_classes = nullptr;
357  CBotStack* m_stack = nullptr;
359  CBotVar* m_thisVar = nullptr;
360  friend class CBotFunction;
361  friend class CBotDebug;
362 
363  CBotError m_error = CBotNoErr;
364  int m_errorStart = 0;
365  int m_errorEnd = 0;
366 };
367 
368 } // namespace CBot
Class for mangaging CBot external calls.
Definition: CBotExternalCall.h:114
~CBotProgram()
Destructor.
Definition: CBotProgram.cpp:47
bool GetPosition(const std::string &name, int &start, int &stop, CBotGet modestart=GetPosExtern, CBotGet modestop=GetPosBloc)
GetPosition Gives the position of a routine in the original text the user can select the item to find...
Definition: CBotProgram.cpp:172
A function declaration in the code.
Definition: CBotFunction.h:42
bool m_bCompileClass
true while compiling class
Definition: CBotProgram.h:340
static void Init()
Initializes the module, should be done once (and only once) at the beginning.
Definition: CBotProgram.cpp:385
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
bool GetRunPos(std::string &functionName, int &start, int &end)
Gives the current position in the executing program.
Definition: CBotProgram.cpp:234
static CBotExternalCallList * GetExternalCalls()
Returns static list of all registered external calls.
Definition: CBotProgram.cpp:414
bool Start(const std::string &name)
Starts the program using given function as an entry point. The function must be declared as "extern"...
Definition: CBotProgram.cpp:149
A CBot class definition.
Definition: CBotClass.h:105
CBotVar * GetStackVars(std::string &functionName, int level)
Provides the pointer to the variables on the execution stack.
Definition: CBotProgram.cpp:245
static void Free()
Frees the static memory areas.
Definition: CBotProgram.cpp:407
Various utilities used for debugging.
Definition: CBotDebug.h:29
A CBot variable.
Definition: CBotVar.h:42
Some enum values used across the CBot engine.
CBotProgram()
Constructor.
Definition: CBotProgram.cpp:38
CBotError
This enum contains possible CBot error values. Values in range 5000-5999 are compile errors...
Definition: CBotEnums.h:190
bool SaveState(FILE *pf)
Save the current execution status into a file.
Definition: CBotProgram.cpp:331
CBotFunction * GetFunctions()
Returns the list of all user-defined functions in this program as instances of CBotFunction.
Definition: CBotProgram.cpp:285
CBotError GetError()
Returns the last error.
Definition: CBotProgram.cpp:260
void Stop()
Stops execution of the program.
Definition: CBotProgram.cpp:225
static bool DefineNum(const std::string &name, long val)
Define a new constant.
Definition: CBotProgram.cpp:324
static int GetVersion()
Returns version of the CBot library.
Definition: CBotProgram.cpp:380
CBotGet
Different modes for CBotProgram::GetPosition.
Definition: CBotEnums.h:62
bool RestoreState(FILE *pf)
Restore the execution state from a file.
Definition: CBotProgram.cpp:349
Class to define the complete type of a variable.
Definition: CBotTypResult.h:47
CBot engine.
Definition: CBotCallMethode.cpp:28
static bool AddFunction(const std::string &name, bool rExec(CBotVar *pVar, CBotVar *pResult, int &Exception, void *pUser), CBotTypResult rCompile(CBotVar *&pVar, void *pUser))
Add a function that can be called from CBot.
Definition: CBotProgram.cpp:317
bool Run(void *pUser=nullptr, int timer=-1)
Executes the program.
Definition: CBotProgram.cpp:187
bool Compile(const std::string &program, std::vector< std::string > &functions, void *pUser=nullptr)
Compile compiles the program given as string.
Definition: CBotProgram.cpp:59
static void SetTimer(int n)
Sets the number of steps (parts of instructions) to execute in Run() before suspending the program ex...
Definition: CBotProgram.cpp:254