Colobot
CBotStack.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/CBotDefines.h"
23 #include "CBot/CBotTypResult.h"
24 #include "CBot/CBotEnums.h"
25 #include "CBot/CBotVar/CBotVar.h"
26 
27 #include <cstdio>
28 #include <string>
29 
30 namespace CBot
31 {
32 
33 class CBotInstr;
34 class CBotExternalCall;
35 class CBotVar;
36 class CBotProgram;
37 class CBotToken;
38 
44 class CBotStack
45 {
46 public:
50  enum class BlockVisibilityType : unsigned short
51  {
52  INSTRUCTION = 0,
53  BLOCK = 1,
54  FUNCTION = 2
55  };
56 
57  enum class IsFunction : unsigned short { NO = 0, YES = 1, EXTERNAL_CALL = 2 };
58 
61 
62 
67  static CBotStack* AllocateStack();
68 
70  void Delete();
71 
72  CBotStack() = delete;
73  ~CBotStack() = delete;
74 
79  bool StackOver();
80 
82 
84 
90 
97  CBotError GetError(int& start, int& end) { start = m_start; end = m_end; return m_error; }
98 
104  CBotError GetError() { return m_error; }
105 
111  bool IsOk()
112  {
113  return m_error == CBotNoErr;
114  }
115 
123  void SetError(CBotError n, CBotToken* token = nullptr);
129  void SetPosError(CBotToken* token);
135  void ResetError(CBotError n, int start, int end);
141  void SetBreak(int val, const std::string& name);
142 
144 
146 
150  void Reset();
151 
154 
155 
160  void AddVar(CBotVar* var);
161 
168  CBotVar* FindVar(CBotToken*& pToken, bool bUpdate);
169 
173  CBotVar* FindVar(CBotToken& pToken, bool bUpdate);
174 
180  CBotVar* FindVar(const std::string& name);
181 
191  CBotVar* FindVar(long ident, bool bUpdate);
192 
200  CBotVar* CopyVar(CBotToken& pToken, bool bUpdate = false);
201 
203 
205 
211 
222 
234 
245 
250  CBotStack* RestoreStack(CBotInstr* instr = nullptr);
255  CBotStack* RestoreStackEOX(CBotExternalCall* instr = nullptr);
256 
265  bool Return(CBotStack* pFils);
272  bool ReturnKeep(CBotStack* pFils);
273 
279  bool BreakReturn(CBotStack* pfils, const std::string& name = "");
285  bool IfContinue(int state, const std::string& name);
286 
288 
290 
299 
306  bool SetState(int n, int lim = -10);
314  int GetState() { return m_state; }
320  bool IncState(int lim = -10);
321 
326  bool IfStep();
327 
332  bool Execute();
333 
335 
338 
339 
345  void SetVar(CBotVar* var);
350  void SetCopyVar(CBotVar* var);
355  CBotVar* GetVar();
356 
362  bool GetRetVar(bool bRet);
363 
370  long GetVal();
371 
373 
375 
382  void SetProgram(CBotProgram* p);
387  CBotProgram* GetProgram(bool bFirst = false);
388 
396  void SetUserPtr(void* user);
402  void* GetUserPtr();
403 
409 
410 
413 
414 
422  bool ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, const CBotTypResult& rettype);
429  void RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar);
430 
432 
435 
436 
437  bool SaveState(FILE* pf);
438  bool RestoreState(FILE* pf, CBotStack* &pStack);
439 
441 
449  static void SetTimer(int n);
453  static int GetTimer();
454 
461  void GetRunPos(std::string& functionName, int& start, int& end);
462 
468  CBotVar* GetStackVars(std::string& functionName, int level);
469 
470  bool IsCallFinished();
471 
472 private:
473  CBotStack* m_next;
474  CBotStack* m_next2;
475  CBotStack* m_prev;
476 
477  int m_state;
478  int m_step;
479  static CBotError m_error;
480  static int m_start;
481  static int m_end;
482  static CBotVar* m_retvar; // result of a return
483 
484  CBotVar* m_var; // result of the operations
485  CBotVar* m_listVar; // variables declared at this level
486 
487  BlockVisibilityType m_block; // is part of a block (variables are local to this block)
488  bool m_bOver; // stack limits?
490  CBotProgram* m_prog;
491 
492  static int m_initimer;
493  static int m_timer;
494  static std::string m_labelBreak;
495  static void* m_pUser;
496 
498  CBotInstr* m_instr;
500  IsFunction m_func;
502  CBotExternalCall* m_call;
503 
504  bool m_callFinished;
505 };
506 
507 } // namespace CBot
bool Return(CBotStack *pFils)
Return to this point - copy the result from given stack, and destroy all child stacks from here...
Definition: CBotStack.cpp:185
void RestoreCall(long &nIdent, CBotToken *token, CBotVar **ppVar)
Restore a function call after the program state has been restored from a file.
Definition: CBotStack.cpp:586
void * GetUserPtr()
Get user pointer for external calls.
Definition: CBotStack.cpp:549
bool IfStep()
Check if we are in step by step execution mode.
Definition: CBotStack.cpp:251
Class for one CBot instruction.
Definition: CBotInstr.h:86
int GetState()
Return current execution state.
Definition: CBotStack.h:314
BlockVisibilityType GetBlock()
Get the block type this stack represents - instruction, code block or function.
Definition: CBotStack.cpp:179
void SetPosError(CBotToken *token)
Set error position to position of given token.
Definition: CBotStack.cpp:439
CBotVar * FindVar(CBotToken *&pToken, bool bUpdate)
Fetch a variable by its token.
Definition: CBotStack.cpp:314
bool Execute()
Resumes execution of interrupted external call.
Definition: CBotStack.cpp:457
bool IncState(int lim=-10)
Increase the execution state by one.
Definition: CBotStack.cpp:410
void SetBreak(int val, const std::string &name)
Definition: CBotStack.cpp:287
bool BreakReturn(CBotStack *pfils, const std::string &name="")
Definition: CBotStack.cpp:258
void Reset()
Reset the stack for execution resume - resets the error and timer.
Definition: CBotStack.cpp:220
bool IfContinue(int state, const std::string &name)
Definition: CBotStack.cpp:272
void GetRunPos(std::string &functionName, int &start, int &end)
Get current position in the program.
Definition: CBotStack.cpp:597
Class that manages a CBot program. This is the main entry point into the CBot engine.
Definition: CBotProgram.h:83
bool IsOk()
Check if there was an error.
Definition: CBotStack.h:111
The execution stack.
Definition: CBotStack.h:44
static int GetTimer()
Get the current configured maximum number of "timer ticks" (parts of instructions) to execute...
Definition: CBotStack.cpp:451
static CBotStack * AllocateStack()
Allocate the stack.
Definition: CBotStack.cpp:51
void AddVar(CBotVar *var)
Adds a local variable.
Definition: CBotStack.cpp:517
bool GetRetVar(bool bRet)
Definition: CBotStack.cpp:300
CBotError GetError(int &start, int &end)
Get last error.
Definition: CBotStack.h:97
Interface for external CBot calls.
Definition: CBotExternalCall.h:45
bool ReturnKeep(CBotStack *pFils)
Like Return() but doesn&#39;t destroy the stacks.
Definition: CBotStack.cpp:200
A CBot variable.
Definition: CBotVar.h:42
void SetUserPtr(void *user)
Set user pointer for external calls.
Definition: CBotStack.cpp:554
Some enum values used across the CBot engine.
void SetProgram(CBotProgram *p)
Set program this stack level is in. Additionally marks this block as function block. Note: for public functions different stack levels might be in different programs.
Definition: CBotStack.cpp:533
CBotStack * RestoreStack(CBotInstr *instr=nullptr)
Restore CBotInstr pointer after loading stack from file.
Definition: CBotStack.cpp:230
CBotStack * AddStack2(BlockVisibilityType bBlock=BlockVisibilityType::INSTRUCTION)
Creates or gets the secondary child stack.
Definition: CBotStack.cpp:155
static void SetTimer(int n)
Set the maximum number of "timer ticks" (parts of instructions) to execute.
Definition: CBotStack.cpp:446
CBotError
This enum contains possible CBot error values. Values in range 5000-5999 are compile errors...
Definition: CBotEnums.h:190
long GetVal()
Return the result variable as int.
Definition: CBotStack.cpp:510
CBotStack * AddStackExternalCall(CBotExternalCall *instr=nullptr, BlockVisibilityType bBlock=BlockVisibilityType::INSTRUCTION)
Adds special EOX stack marker.
Definition: CBotStack.cpp:141
void SetCopyVar(CBotVar *var)
Set the result variable to copy of given variable.
Definition: CBotStack.cpp:495
void SetError(CBotError n, CBotToken *token=nullptr)
Set execution error unless it&#39;s already set unless you are trying to reset it.
Definition: CBotStack.cpp:419
CBotError GetError()
Get last error.
Definition: CBotStack.h:104
BlockVisibilityType
Block type this stack represents. This determines local variable visibility (you can only see up to t...
Definition: CBotStack.h:50
CBotStack * AddStack(CBotInstr *instr=nullptr, BlockVisibilityType bBlock=BlockVisibilityType::INSTRUCTION)
Creates or gets the primary child stack.
Definition: CBotStack.cpp:113
CBotVar * GetStackVars(std::string &functionName, int level)
Get local variables at the given stack level.
Definition: CBotStack.cpp:632
bool StackOver()
Check for stack overflow and set error status as needed.
Definition: CBotStack.cpp:212
bool SetState(int n, int lim=-10)
Set execution state.
Definition: CBotStack.cpp:401
void ResetError(CBotError n, int start, int end)
Set execution error even if it is already set.
Definition: CBotStack.cpp:431
CBotVar * GetVar()
Return result variable.
Definition: CBotStack.cpp:504
bool ExecuteCall(long &nIdent, CBotToken *token, CBotVar **ppVar, const CBotTypResult &rettype)
Execute a function call, either external or user-defined.
Definition: CBotStack.cpp:560
CBotStack * RestoreStackEOX(CBotExternalCall *instr=nullptr)
Restores CBotExternalCall in the EOX marker after loading stack from file.
Definition: CBotStack.cpp:242
Class to define the complete type of a variable.
Definition: CBotTypResult.h:47
CBot engine.
Definition: CBotCallMethode.cpp:28
CBotProgram * GetProgram(bool bFirst=false)
Get program we are currently in.
Definition: CBotStack.cpp:540
void Delete()
Remove the current stack.
Definition: CBotStack.cpp:81
CBotVar * CopyVar(CBotToken &pToken, bool bUpdate=false)
Find variable by its token and returns a copy of it.
Definition: CBotStack.cpp:389
void SetVar(CBotVar *var)
Set the result variable.
Definition: CBotStack.cpp:487
Class representing one token of a program.
Definition: CBotToken.h:80
Function - variable visibility limit.