Colobot
CBotDll.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2015, 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 
21 
27 #pragma once
28 
29 #include <stdio.h>
30 #include "resource.h"
31 #include <map>
32 #include <cstring>
33 
34 
35 #define CBOTVERSION 104
36 
38 // forward declaration of needed classes
39 
40 class CBotToken; // program turned into "tokens
41 class CBotStack; // for the execution stack
42 class CBotClass; // class of object
43 class CBotInstr; // instruction to be executed
44 class CBotFunction; // user functions
45 class CBotVar; // variables
46 class CBotVarClass; // instance of class
47 class CBotVarPointer; // pointer to an instance of class
48 class CBotCall; // functions
49 class CBotCallMethode; // methods
50 class CBotDefParam; // parameter list
51 class CBotCStack; // stack
52 
53 
55 // Variables management
57 
60 {
61  CBotTypVoid = 0,
62  CBotTypByte = 1, //n
63  CBotTypShort = 2, //n
64  CBotTypChar = 3, //n
65  CBotTypInt = 4,
66  CBotTypLong = 5, //n
67  CBotTypFloat = 6,
68  CBotTypDouble = 7, //n
69  CBotTypBoolean = 8,
70  CBotTypString = 9,
71 
72  CBotTypArrayPointer = 10, // array of variables
73  CBotTypArrayBody = 11, // same but creates an instance
74 
75  CBotTypPointer = 12, // pointer to an instance
76  CBotTypNullPointer = 13, // null pointer is special
77  CBotTypClass = 15,
78  CBotTypIntrinsic = 16 // instance of a class intrinsic
79 };
80 //n = not implemented yet
81 
82 // for SetUserPtr when deleting an object
83 // \TODO define own types to distinct between different states of objects
84 #define OBJECTDELETED (reinterpret_cast<void*>(-1))
85 // value set before initialization
86 #define OBJECTCREATED (reinterpret_cast<void*>(-2))
87 
88 
91 {
92 public:
97  CBotTypResult(int type);
98  // for simple types (CBotTypInt à CBotTypString)
99 
100 
101  CBotTypResult(int type, const char* name);
102  // for pointer types and intrinsic classes
103 
104  CBotTypResult(int type, CBotClass* pClass);
105  // for the instance of a class
106 
107  CBotTypResult(int type, CBotTypResult elem);
108  // for arrays of variables
109 
110  CBotTypResult(const CBotTypResult& typ);
111  // for assignments
112 
113  CBotTypResult();
114  // for default
115 
116  ~CBotTypResult();
117 
118  int GetType(int mode = 0) const;
119  // returns type CBotType* as a result
120 
121  void SetType(int n);
122  // modifies a type
123 
124  CBotClass* GetClass() const;
125  // makes the pointer to the class (for CBotTypClass, CBotTypPointer)
126 
127  int GetLimite() const;
128  // returns limit size of table (CBotTypArray)
129 
130  void SetLimite(int n);
131  // set limit to the table
132 
133  void SetArray(int* max );
134  // set limits for a list of dimensions (arrays of arrays)
135 
136  CBotTypResult& GetTypElem() const;
137  // returns type of array elements (CBotTypArray)
138  // rend le type des éléments du tableau (CBotTypArray)
139 
140  bool Compare(const CBotTypResult& typ) const;
141  // compares whether the types are compatible
142  bool Eq(int type) const;
143  // compare type
144 
145  CBotTypResult& operator=(const CBotTypResult& src);
146  // copy a complete type in another
147 
148 private:
149  int m_type;
150  CBotTypResult* m_pNext; // for the types of type
151  CBotClass* m_pClass; // for the derivatives of class
152  int m_limite; // limits of tables
153  friend class CBotVarClass;
154  friend class CBotVarPointer;
155 };
156 
157 /*
158 // to define a result as output, using for example
159 
160  // to return a simple Float
161  return CBotTypResult( CBotTypFloat );
162 
163 
164  // to return a string array
165  return CBotTypResult( CBotTypArray, CBotTypResult( CBotTypString ) );
166 
167  // to return un array of array of "point" class
168  CBotTypResult typPoint( CBotTypIntrinsic, "point" );
169  CBotTypResult arrPoint( CBotTypArray, typPoint );
170  return CBotTypResult( CBotTypArray, arrPoint );
171 */
172 
173 
175 // Error Handling of compilation and execution
177 
178 // Here are the list of errors that can be returned by the module
179 // for compilation
180 
181 #define CBotErrOpenPar 5000 // missing the opening parenthesis
182 #define CBotErrClosePar 5001 // missing the closing parenthesis
183 #define CBotErrNotBoolean 5002 // expression must be a boolean
184 #define CBotErrUndefVar 5003 // undeclared variable
185 #define CBotErrBadLeft 5004 // assignment impossible ( 5 = ... )
186 #define CBotErrNoTerminator 5005 // semicolon expected
187 #define CBotErrCaseOut 5006 // case outside a switch
188 // CBotErrNoTerm 5007, plus utile
189 #define CBotErrCloseBlock 5008 // missing " } "
190 #define CBotErrElseWhitoutIf 5009 // else without matching if
191 #define CBotErrOpenBlock 5010 // missing " { "
192 #define CBotErrBadType1 5011 // wrong type for the assignment
193 #define CBotErrRedefVar 5012 // redefinition of the variable
194 #define CBotErrBadType2 5013 // Two operands are incompatible
195 #define CBotErrUndefCall 5014 // routine undefined
196 #define CBotErrNoDoubleDots 5015 // " : " expected
197 // CBotErrWhile 5016, plus utile
198 #define CBotErrBreakOutside 5017 // break outside of a loop
199 #define CBotErrUndefLabel 5019 // label udnefined
200 #define CBotErrLabel 5018 // label ne peut se mettre ici (label can not get here)
201 #define CBotErrNoCase 5020 // missing " case "
202 #define CBotErrBadNum 5021 // expected number
203 #define CBotErrVoid 5022 // " void " not possible here
204 #define CBotErrNoType 5023 // type declaration expected
205 #define CBotErrNoVar 5024 // variable name expected
206 #define CBotErrNoFunc 5025 // expected function name
207 #define CBotErrOverParam 5026 // too many parameters
208 #define CBotErrRedefFunc 5027 // this function already exists
209 #define CBotErrLowParam 5028 // not enough parameters
210 #define CBotErrBadParam 5029 // wrong types of parameters
211 #define CBotErrNbParam 5030 // wrong number of parameters
212 #define CBotErrUndefItem 5031 // element does not exist in the class
213 #define CBotErrUndefClass 5032 // variable is not a class
214 #define CBotErrNoConstruct 5033 // no appropriate constructor
215 #define CBotErrRedefClass 5034 // class already exists
216 #define CBotErrCloseIndex 5035 // " ] " expected
217 #define CBotErrReserved 5036 // reserved word (for a DefineNum)
218 #define CBotErrBadNew 5037 // wrong setting for new
219 #define CBotErrOpenIndex 5038 // " [ " expected
220 #define CBotErrBadString 5039 // expected string
221 #define CBotErrBadIndex 5040 // wrong index type "[ false ]"
222 #define CBotErrPrivate 5041 // protected item
223 #define CBotErrNoPublic 5042 // missing word "public"
224 
225 // here is the list of errors that can be returned by the module
226 // for the execution
227 
228 #define CBotErrZeroDiv 6000 // division by zero
229 #define CBotErrNotInit 6001 // uninitialized variable
230 #define CBotErrBadThrow 6002 // throw a negative value
231 #define CBotErrNoRetVal 6003 // function did not return results
232 #define CBotErrNoRun 6004 // Run() without active function
233 #define CBotErrUndefFunc 6005 // calling a function that no longer exists
234 #define CBotErrNotClass 6006 // this class does not exist
235 #define CBotErrNull 6007 // null pointer
236 #define CBotErrNan 6008 // calculation with a NAN
237 #define CBotErrOutArray 6009 // index out of array
238 #define CBotErrStackOver 6010 // stack overflow
239 #define CBotErrDeletedPtr 6011 // pointer to an object destroyed
240 
241 #define CBotErrFileOpen 6012 // cannot open the file
242 #define CBotErrNotOpen 6013 // channel not open
243 #define CBotErrRead 6014 // error while reading
244 #define CBotErrWrite 6015 // writing error
245 
246 
247 // other values ​​may be returned
248 // for example exceptions returned by external routines
249 // and " throw " with any number.
250 
251 
253 //
254 // as part of MFC CString not used here.
255 //
256 // ( all functions are not implemented yet )
257 
260 {
261 public:
262  CBotString();
263  CBotString(const char* p);
264  CBotString(const CBotString& p);
265  ~CBotString();
266 
267  void Empty();
268  bool IsEmpty() const;
269  int GetLength();
270  int Find(const char c);
271  int Find(const char* lpsz);
272  int ReverseFind(const char c);
273  int ReverseFind(const char* lpsz);
274  bool LoadString(unsigned int id);
275  CBotString Mid(int nFirst, int nCount) const;
276  CBotString Mid(int nFirst) const;
277  CBotString Mid(int start, int lg=-1);
278  CBotString Left(int nCount) const;
279  CBotString Right(int nCount) const;
280  int Compare(const char* lpsz) const;
281  void MakeUpper();
282  void MakeLower();
283 
284 
288  const CBotString& operator=(const CBotString& stringSrc);
289  const CBotString& operator=(const char ch);
290  const CBotString& operator=(const char* pString);
291  const CBotString& operator+(const CBotString& str);
292  friend CBotString operator+(const CBotString& string, const char* lpsz);
293 
294  const CBotString& operator+=(const char ch);
295  const CBotString& operator+=(const CBotString& str);
296  bool operator==(const CBotString& str);
297  bool operator==(const char* p);
298  bool operator!=(const CBotString& str);
299  bool operator!=(const char* p);
300  bool operator>(const CBotString& str);
301  bool operator>(const char* p);
302  bool operator>=(const CBotString& str);
303  bool operator>=(const char* p);
304  bool operator<(const CBotString& str);
305  bool operator<(const char* p);
306  bool operator<=(const CBotString& str);
307  bool operator<=(const char* p);
308 
309  operator const char*() const; // as a C string
310 
311 
312 private:
313 
315  char* m_ptr;
316 
318  int m_lg;
319 
321  static const std::map<EID,const char *> s_keywordString;
322 
328  static const char * MapIdToString(EID id);
329 };
330 
331 
332 // Class used to array management
333 
335 {
336 private:
337  int m_nSize; // number of elements
338  int m_nMaxSize; // reserved size
339  CBotString* m_pData; // ^data
340 
341 public:
342  CBotStringArray();
343  ~CBotStringArray();
344  void SetSize(int nb);
345  int GetSize();
346  void Add(const CBotString& str);
347  CBotString& operator[](int nIndex);
348 
349  CBotString& ElementAt(int nIndex);
350 };
351 
352 // different modes for GetPosition
353 enum CBotGet
354 {
355  GetPosExtern = 1,
356  GetPosNom = 2,
357  GetPosParam = 3,
358  GetPosBloc = 4
359 };
360 
362 // main class managing CBot program
363 //
364 
366 {
367 private:
368  CBotFunction* m_Prog; // the user-defined functions
369  CBotFunction* m_pRun; // the basic function for the execution
370  CBotClass* m_pClass; // classes defined in this part
371  CBotStack* m_pStack; // execution stack
372  CBotVar* m_pInstance; // instance of the parent class
373  friend class CBotFunction;
374 
375  int m_ErrorCode;
376  int m_ErrorStart;
377  int m_ErrorEnd;
378 
379  long m_Ident; // associated identifier
380 
381 public:
382  static CBotString m_DebugVarStr; // end of a debug
383  bool m_bDebugDD; // idem déclanchable par robot \TODO ???
384  bool m_bCompileClass;
385 
386 public:
387  static void Init();
388  // initializes the module (defined keywords for errors)
389  // should be done once (and only one) at the beginning
390  static
391  void Free();
392  // frees the static memory areas
393 
394  static
395  int GetVersion();
396  // gives the version of the library CBOT
397 
398 
399  CBotProgram();
400  CBotProgram(CBotVar* pInstance);
401  ~CBotProgram();
402 
403  bool Compile( const char* program, CBotStringArray& ListFonctions, void* pUser = nullptr);
404  // compiles the program given in text
405  // returns false if an error at compile
406  // see GetCompileError () to retrieve the error
407  // ListFonctions returns the names of functions declared as extern
408  // pUser can pass a pointer to routines defined by AddFunction
409 
410  void SetIdent(long n);
411  // associates an identifier with the instance CBotProgram
412 
413  long GetIdent();
414  // gives the identifier
415 
416  int GetError();
417  bool GetError(int& code, int& start, int& end);
418  bool GetError(int& code, int& start, int& end, CBotProgram* &pProg);
419  // if true
420  // gives the error found in the compilation
421  // or execution
422  // delimits the start and end block where the error
423  // pProg lets you know what "module" has produced runtime error
424  static CBotString GetErrorText(int code);
425 
426 
427  bool Start(const char* name);
428  // defines what function should be executed
429  // returns false if the funtion name is not found
430  // the program does nothing, we must call Run () for this
431 
432  bool Run(void* pUser = nullptr, int timer = -1);
433  // executes the program
434  // returns false if the program was suspended
435  // returns true if the program ended with or without error
436  // timer = 0 allows to advance step by step
437 
438  bool GetRunPos(const char* &FunctionName, int &start, int &end);
439  // gives the position in the executing program
440  // returns false if it is not running (program completion)
441  // FunctionName is a pointer made to the name of the function
442  // start and end position in the text of the token processing
443 
444  CBotVar* GetStackVars(const char* &FunctionName, int level);
445  // provides the pointer to the variables on the execution stack
446  // level is an input parameter, 0 for the last level, -1, -2, etc. for the other levels
447  // the return value (CBotVar *) is a variable list (or nullptr)
448  // that can be processed as the list of parameters received by a routine
449  // FunctionName gives the name of the function where are these variables
450  // FunctionName == nullptr means that is more in a program (depending on level)
451 
452  void Stop();
453  // stops execution of the program
454  // therefore quits "suspend" mode
455 
456  static
457  void SetTimer(int n);
458  // defines the number of steps (parts of instructions) to done
459  // in Run() before rendering hand "false" \TODO avant de rendre la main "false"
460 
461  static
462  bool AddFunction(const char* name,
463  bool rExec (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser),
464  CBotTypResult rCompile (CBotVar* &pVar, void* pUser));
465  // call this to add externally (**)
466  // a new function used by the program CBoT
467 
468  static
469  bool DefineNum(const char* name, long val);
470 
471  bool SaveState(FILE* pf);
472  // backup the execution status in the file
473  // the file must have been opened with the fopen call this dll (\TODO this library??)
474  // if the system crashes
475  bool RestoreState(FILE* pf);
476  // restores the state of execution from file
477  // the compiled program must obviously be the same
478 
479  bool GetPosition(const char* name, int& start, int& stop,
480  CBotGet modestart = GetPosExtern,
481  CBotGet modestop = GetPosBloc);
482  // gives the position of a routine in the original text
483  // the user can select the item to find from the beginning to the end
484  // see the above modes in CBotGet
485 
486 
487  CBotFunction* GetFunctions();
488 };
489 
490 
492 // routines for file management (* FILE)
493  FILE* fOpen(const char* name, const char* mode);
494  int fClose(FILE* filehandle);
495  size_t fWrite(const void *buffer, size_t elemsize, size_t length, FILE* filehandle);
496  size_t fRead(void *buffer, size_t elemsize, size_t length, FILE* filehandle);
497 
498 
499 #if 0
500 /*
501 (**) Note:
502  To define an external function, proceed as follows:
503 
504  a) define a routine for compilation
505  this routine receive list of parameters (no values)
506  and either returns a result type (CBotTyp... or 0 = void)
507  or an error number
508  b) define a routine for the execution
509  this routine receive list of parameters (with valeurs),
510  a variable to store the result (according to the given type at compile time)
511 
512  For example, a routine which calculates the mean of a parameter list */
513 
514 int cMean(CBotVar* &pVar, CBotString& ClassName)
515 {
516  if ( pVar == nullptr ) return 6001; // there is no parameter!
517 
518  while ( pVar != nullptr )
519  {
520  if ( pVar->GetType() > CBotTypDouble ) return 6002; // this is not a number
521  pVar = pVar -> GetNext();
522  }
523 
524  return CBotTypFloat; // the type of the result may depend on the parameters!
525 }
526 
527 
528 bool rMean(CBotVar* pVar, CBotVar* pResult, int& Exception)
529 {
530  float total = 0;
531  int nb = 0;
532  while (pVar != nullptr)
533  {
534  total += pVar->GetValFloat();
535  pVar = pVar->GetNext();
536  nb++;
537  }
538  pResult->SetValFloat(total/nb); // returns the mean value
539 
540  return true; // operation fully completed
541 }
542 
543 #endif
544 
546 // Class for managing variables
547 
548 // may be useful to the outside of the module
549 // ( it is currently not expected to be able to create these objects in outer )
550 
551 // variable type SetPrivate / IsPrivate
552 #define PR_PUBLIC 0 // public variable
553 #define PR_READ 1 // read only
554 #define PR_PROTECT 2 // protected (inheritance)
555 #define PR_PRIVATE 3 // strictly private
556 
557 class CBotVar
558 {
559 public:
560  // results of GetInit()
561  enum class InitType : int { UNDEF = 0, DEF = 1, IS_POINTER = 2, IS_NAN = 999 };
562 
563 protected:
564  CBotToken* m_token; // the corresponding token
565 
566  CBotVar* m_next; // list of variables
567  friend class CBotStack;
568  friend class CBotCStack;
569  friend class CBotInstrCall;
570  friend class CBotProgram;
571 
572  CBotTypResult m_type; // type of value
573 
574  InitType m_binit; // not initialized?
575  CBotVarClass* m_pMyThis; // ^ corresponding this element
576  void* m_pUserPtr; // ^user data if necessary
577  bool m_bStatic; // static element (in class)
578  int m_mPrivate; // element public, protected or private?
579 
580  CBotInstr* m_InitExpr; // expression for the original content
581  CBotInstr* m_LimExpr; // list of limits for a table
582  friend class CBotClass;
583  friend class CBotVarClass;
584  friend class CBotVarPointer;
585  friend class CBotVarArray;
586 
587  long m_ident; // unique identifier
588  static long m_identcpt; // counter
589 
590 public:
591  CBotVar();
592 virtual ~CBotVar( ); // destructor
593 
594  static
595  CBotVar* Create( const char* name, CBotTypResult type);
596  // creates from a complete type
597 
598  static
599  CBotVar* Create( const char* name, CBotClass* pClass);
600  // creates from one instance of a known class
601 
602  static
603  CBotVar* Create( const CBotToken* name, int type );
604  static
605  CBotVar* Create( const CBotToken* name, CBotTypResult type );
606 
607  static
608  CBotVar* Create( const char* name, int type, CBotClass* pClass);
609 
610  static
611  CBotVar* Create( CBotVar* pVar );
612 
613 
614  void SetUserPtr(void* pUser);
615  // associate a user pointer to an instance
616 
617  virtual void SetIdent(long UniqId);
618  // associates a unique identifier to an instance
619  // ( it is used to ensure that the id is unique)
620 
621  void* GetUserPtr();
622  // makes the pointer associated with the variable
623 
624  CBotString GetName(); // the name of the variable, if known
626  void SetName(const char* name); // changes the name of the variable
627 
628  int GetType(int mode = 0); // returns the base type (int) of the variable
629  // TODO check it
631 
632  CBotTypResult GetTypResult(int mode = 0); // returns the complete type of the variable
633 
634 
635  CBotToken* GetToken();
636  void SetType(CBotTypResult& type);
637 
638  void SetInit(InitType initType); // is the variable in the state UNDEF, DEF, NAN
639  InitType GetInit() const; // gives the state of the variable
640  bool IsUndefined() const { return GetInit() == InitType::UNDEF; }
641  bool IsDefined() const { return GetInit() == InitType::DEF; }
642  bool IsNAN() const { return GetInit() == InitType::IS_NAN; }
643 
644  void SetStatic(bool bStatic);
645  bool IsStatic();
646 
647  void SetPrivate(int mPrivate);
648  bool IsPrivate(int mode = PR_PROTECT);
649  int GetPrivate();
650 
651  virtual
652  void ConstructorSet();
653 
654  void SetVal(CBotVar* var); // remprend une valeur
655  // TODO remprend value
656  virtual
657  CBotVar* GetItem(const char* name); // returns an element of a class according to its name (*)
658  virtual
659  CBotVar* GetItemRef(int nIdent); // idem à partir du n° ref
660  // TODO ditto from ref no.
661  virtual
662  CBotVar* GetItem(int row, bool bGrow = false);
663 
664  virtual
665  CBotVar* GetItemList(); // lists the elements
666 
667  CBotVar* GetStaticVar(); // makes the pointer to the variable if it is static
668 
669  bool IsElemOfClass(const char* name);
670  // said if the element belongs to the class "name"
671  // makes true if the object is a subclass
672 
673  CBotVar* GetNext(); // next variable in the list (parameters)
675 
676  void AddNext(CBotVar* pVar); // added to a list
677 
678  virtual
679  void Copy(CBotVar* pSrc, bool bName = true); // makes a copy of the variable
680 
681  virtual void SetValInt(int val, const char* name = nullptr);
682  // initialized with an integer value (#)
684 
685  virtual void SetValFloat(float val); // initialized with a real value (#)
687 
688  virtual void SetValString(const char* p);// initialized with a string value (#)
690 
691  virtual int GetValInt(); // request the full value (#)
693 
694  virtual float GetValFloat(); // gets real value (#)
696 
697  virtual
698  CBotString GetValString(); // request the string value (#)
700 
701  virtual void SetClass(CBotClass* pClass);
702  virtual
703  CBotClass* GetClass();
704 
705  virtual void SetPointer(CBotVar* p);
706  virtual
707  CBotVarClass* GetPointer();
708 // virtual void SetIndirection(CBotVar* pVar);
709 
710  virtual void Add(CBotVar* left, CBotVar* right); // addition
711  virtual void Sub(CBotVar* left, CBotVar* right); // subtraction
712  virtual void Mul(CBotVar* left, CBotVar* right); // multiplication
713  virtual int Div(CBotVar* left, CBotVar* right); // division
714  virtual int Modulo(CBotVar* left, CBotVar* right); // remainder of division
715  virtual void Power(CBotVar* left, CBotVar* right); // power
716 
717  virtual bool Lo(CBotVar* left, CBotVar* right);
718  virtual bool Hi(CBotVar* left, CBotVar* right);
719  virtual bool Ls(CBotVar* left, CBotVar* right);
720  virtual bool Hs(CBotVar* left, CBotVar* right);
721  virtual bool Eq(CBotVar* left, CBotVar* right);
722  virtual bool Ne(CBotVar* left, CBotVar* right);
723 
724  virtual void And(CBotVar* left, CBotVar* right);
725  virtual void Or(CBotVar* left, CBotVar* right);
726  virtual void XOr(CBotVar* left, CBotVar* right);
727  virtual void ASR(CBotVar* left, CBotVar* right);
728  virtual void SR(CBotVar* left, CBotVar* right);
729  virtual void SL(CBotVar* left, CBotVar* right);
730 
731  virtual void Neg();
732  virtual void Not();
733  virtual void Inc();
734  virtual void Dec();
735 
736 
737  virtual bool Save0State(FILE* pf);
738  virtual bool Save1State(FILE* pf);
739  static bool RestoreState(FILE* pf, CBotVar* &pVar);
740 
741  void debug();
742 
743 // virtual
744 // CBotVar* GetMyThis();
745 
746  virtual
747  void Maj(void* pUser = nullptr, bool bContinue = true);
748 
749  void SetUniqNum(long n);
750  long GetUniqNum();
751  static long NextUniqNum();
752 };
753 
754 /* NOTE (#)
755  methods SetValInt() SetValFloat() et SetValString()
756  can be called with objects which are respectively integer, real or string
757  Always be sure of the type of the variable before calling these methods
758 
759  if ( pVar->GetType() == CBotInt() ) pVar->SetValFloat( 3.3 ); // plante !!
760 
761  methods GetValInt(), GetValFloat() et GetValString()
762  use value conversions,
763  GetValString() works on numbers (makes the corresponding string)
764  but do not make GetValInt () with a string variable!
765 */
766 
767 
768 
770 // management of classes
772 
773 // class to define new classes in the language CBOT
774 // for example to define the class CPoint (x, y)
775 
777 {
778 private:
779  static
780  CBotClass* m_ExClass; // list of classes existing at a given time
781  CBotClass* m_ExNext; // for this general list
782  CBotClass* m_ExPrev; // for this general list
783 
784 private:
785  CBotClass* m_pParent; // parent class
786  CBotString m_name; // name of this class
787  int m_nbVar; // number of variables in the chain
788  CBotVar* m_pVar; // content of the class
789  bool m_bIntrinsic; // intrinsic class
790  CBotClass* m_next; // the string class
791  CBotCallMethode* m_pCalls; // list of methods defined in external
792  CBotFunction* m_pMethod; // compiled list of methods
793  void (*m_rMaj) ( CBotVar* pThis, void* pUser );
794  friend class CBotVarClass;
795  int m_cptLock; // for Lock / UnLock
796  int m_cptOne; // Lock for reentrancy
797  CBotProgram* m_ProgInLock[5];// processes waiting for sync
798 
799 public:
800  bool m_IsDef; // mark if is set or not
801 
802  CBotClass( const char* name,
803  CBotClass* pParent, bool bIntrinsic = false ); // constructor
804  // Once a class is created, it is known
805  // around CBoT
806  // intrinsic mode gives a class that is not managed by pointers
807 
808  ~CBotClass( ); // destructor
809 
810  bool AddFunction(const char* name,
811  bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
812  CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar));
813  // this call allows to add as external (**)
814  // new method used by the objects of this class
815 
816  bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) );
817  // defines routine to be called to update the elements of the class
818 
819  bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC);
820  // adds an element to the class
821 // bool AddItem(CBotString name, CBotClass* pClass);
822  // the same for elements belonging to pClass
823  bool AddItem(CBotVar* pVar);
824  // adds an item by passing the pointer to an instance of a variable
825  // the object is taken as is, so do not destroyed
826 
827 
828 
829  // adds an element by giving an element of type CBotVar
830  void AddNext(CBotClass* pClass);
831 
832  CBotString GetName(); // gives the name of the class
833  CBotClass* GetParent(); // gives the parent class (or nullptr)
834 
835  // true if a class is derived (Extends) of another
836  // return true also if the classes are identical
837  bool IsChildOf(CBotClass* pClass);
838 
839  static
840  CBotClass* Find(CBotToken* &pToken); // trouve une classe d'après son nom
841  // return a class by it's its name
842  static
843  CBotClass* Find(const char* name);
844 
845  CBotVar* GetVar(); // return the list of variables
846  CBotVar* GetItem(const char* name); // one of the variables according to its name
847  CBotVar* GetItemRef(int nIdent);
848 
849  CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams,
850  CBotCStack* pStack, long& nIdent);
851 
852  bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken);
853  void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack);
854 
855  // compiles a class declared by the user
856  static
857  CBotClass* Compile(CBotToken* &p, CBotCStack* pStack);
858  static
859  CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack);
860 
861  bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond);
862 
863  bool IsIntrinsic();
864  void Purge();
865  static
866  void Free();
867 
868  static
869  bool SaveStaticState(FILE* pf);
870 
871  static
872  bool RestoreStaticState(FILE* pf);
873 
874  bool Lock(CBotProgram* p);
875  void Unlock();
876  static
877  void FreeLock(CBotProgram* p);
878 
879  bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam);
880 
881 };
882 
883 #define MAXDEFNUM 1000 // limited number of DefineNum
884 
886 // Token management (tokens)
887 
888 #define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord)
889 #define TokenTypNum 2 // number
890 #define TokenTypString 3 // string
891 #define TokenTypVar 4 // a variable name
892 #define TokenTypDef 5 // value according DefineNum
893 
894 #define TokenKeyWord 2000 // keywords of the language
895 #define TokenKeyDeclare 2100 // keywords of declarations (int, float,..)
896 #define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan)
897 #define TokenKeyOp 2300 // operators
898 
904 {
905 private:
906  static
907  CBotStringArray m_ListKeyWords; // list of keywords of language
908  static
909  int m_ListIdKeyWords[200]; // the corresponding codes
910 
911  static
912  CBotStringArray m_ListKeyDefine; // names defined by a DefineNum
913  static
914  long m_ListKeyNums[MAXDEFNUM]; // the ​​associated values
915 
916 private:
917  CBotToken* m_next; // following in the list
918  CBotToken* m_prev;
919  int m_type; // type of Token
920  long m_IdKeyWord; // number of the keyword if it is a
921  // or value of the "define"
922 
923  CBotString m_Text; // word found as token
924  CBotString m_Sep; // following separators
925 
926  int m_start; // position in the original text (program)
927  int m_end; // the same for the end of the token
928 
932  static
933  int GetKeyWords(const char* w); // is it a keyword?
934  static
935  bool GetKeyDefNum(const char* w, CBotToken* &token);
936 
940  static
941  void LoadKeyWords();
942 
943 public:
947  CBotToken();
948  CBotToken(const CBotToken* pSrc);
949  CBotToken(const CBotString& mot, const CBotString& sep, int start=0, int end=0);
950  CBotToken(const char* mot, const char* sep = nullptr);
951 
955  ~CBotToken();
959  int GetType();
960 
965 
969  CBotString& GetSep();
970 
974  int GetStart();
978  int GetEnd();
979 
983  CBotToken* GetNext();
987  CBotToken* GetPrev();
988 
992  static
993  CBotToken* CompileTokens(const char* p, int& error);
994 
998  static
999  void Delete(CBotToken* pToken); // libère la liste
1000 
1001 
1002  // fonctions non utiles en export
1003  static
1004  bool DefineNum(const char* name, long val);
1005  void SetString(const char* name);
1006 
1007  void SetPos(int start, int end);
1008  long GetIdKey();
1012  void AddNext(CBotToken* p);
1013 
1017  static
1018  CBotToken* NextToken(char* &program, int& error, bool first = false);
1019 
1020  const CBotToken&
1021  operator=(const CBotToken& src);
1022 
1023  static
1024  void Free();
1025 };
1026 
1027 
1028 
1029 /*
1031 // Examples of use
1032 // Definition classes and functions
1033 
1034 
1035 // define the global class CPoint
1036 // --------------------------------
1037  m_pClassPoint = new CBotClass("CPoint", nullptr);
1038  // adds the component ".x"
1039  m_pClassPoint->AddItem("x", CBotTypResult(CBotTypFloat));
1040  // adds the component ".y"
1041  m_pClassPoint->AddItem("y", CBotTypResult(CBotTypFloat));
1042  // the player can then use the instructions
1043  // CPoint position; position.x = 12; position.y = -13.6
1044 
1045 // define class CColobotObject
1046 // --------------------------------
1047 // This class manages all the objects in the world of COLOBOT
1048 // the "main" user program belongs to this class
1049  m_pClassObject = new CBotClass("CColobotObject", m_pClassBase);
1050  // adds the component ".position"
1051  m_pClassObject->AddItem("position", m_pClassPoint);
1052  // adds the component ".type"
1053  m_pClassObject->AddItem("type", CBotTypResult(CBotTypShort));
1054  // adds a definition of constant
1055  m_pClassObject->AddConst("ROBOT", CBotTypShort, 1); // ROBOT equivalent to the value 1
1056  // adds the FIND routine
1057  m_pClassObject->AddFunction( rCompFind, rDoFind );
1058  // the player can now use the instructions
1059  // CColobotObject chose; chose = FIND( ROBOT )
1060 
1061 
1062 
1063 // define class CColobotRobot derived from CColobotObject
1064 // ---------------------------------------------------------
1065 // programs "main" associated with robots as a part of this class
1066  m_pClassRobot = new CBotClass("CColobotRobot", m_pClassObject);
1067  // add routine GOTO
1068  m_pClassRobot->AddFunction( rCompGoto, rDoGoto );
1069  // the player can now use
1070  // GOTO( FIND ( ROBOT ) );
1071 
1072 
1073 // creates an instance of the class Robot
1074 // ------------------------------------
1075 // for example a new robot which has just been manufactured
1076  CBotVar* m_pMonRobot = new CBotVar("MonRobot", m_pClassRobot);
1077 
1078 // compiles the program by hand for this robot
1079 // ------------------------------------------
1080  CString LeProgramme( "void main() {GOTO(0, 0); return 0;}" );
1081  if ( !m_pMonRobot->Compile( LeProgramme ) ) {error handling ...};
1082 
1083 // build a stack for interpreter
1084 // --------------------------------------
1085  CBotStack* pStack = new CBotStack(nullptr);
1086 
1087 // executes the main program
1088 // -------------------------
1089  while( false = m_pMonRobot->Execute( "main", pStack ))
1090  {
1091  // program suspended
1092  // could be pass a handle to another (safeguarding pstack for the robot one)
1093  };
1094  // programme "main" finished !
1095 
1096 
1097 
1098 
1099 // routine that implements the GOTO (CPoint pos)
1100 bool rDoGoto( CBotVar* pVar, CBotVar* pResult, int& exception )
1101 {
1102  if (pVar->GetType() != CBotTypeClass ||
1103  pVar->IsElemOfClas("CPoint") ) { exception = 6522; return false; )
1104  // the parameter is not the right class?
1105  // in fact the control is done to the routine of compilation
1106 
1107  m_PosToGo.Copy( pVar ); // keeps the target position (object type CBotVar)
1108 
1109  // or so
1110  CBotVar* temp;
1111  temp = pVar->GetItem("x"); // is necessary for the object of type CPoint
1112  ASSERT (temp != nullptr && temp->GetType() == CBotTypFloat);
1113  m_PosToGo.x = temp->GetValFloat();
1114 
1115  temp = pVar->GetItem("y"); // is necessary for the object of type CPoint
1116  ASSERT (temp != nullptr && temp->GetType() == CBotTypFloat);
1117  m_PosToGo.y = temp->GetValFloat();
1118 
1119  return (m_CurentPos == m_PosToGo); // makes true if the position is reached
1120  // returns false if one had wait yet
1121 }
1122 
1123 */
static void Delete(CBotToken *pToken)
releases the list
Definition: CBotToken.cpp:434
Definition: CBot.h:388
Definition: CBotDll.h:903
Definition: CBot.h:1611
~CBotToken()
Destructor.
Definition: CBotToken.cpp:98
Definition: CBot.h:1527
Definition: CBot.h:1639
int GetType()
Returns the type of token.
Definition: CBotToken.cpp:127
bool AddItem(CBotString name, CBotTypResult type, int mPrivate=PR_PUBLIC)
Definition: CBotClass.cpp:178
CBotString & GetString()
makes the string corresponding to this token
Definition: CBotToken.cpp:163
Definition: CBot.h:1579
Definition: CBot.h:1434
Definition: CBot.h:315
Definition: CBot.h:1009
const CBotString & operator=(const CBotString &stringSrc)
Overloaded oprators to work on CBotString classes.
Definition: CBotString.cpp:366
Definition: CBotDll.h:365
CBotToken()
Constructors.
Definition: CBotToken.cpp:38
static CBotToken * NextToken(char *&program, int &error, bool first=false)
Definition: CBotToken.cpp:233
CBotType
CBotType Defines known types. This types are modeled on Java types. Do not change the order of elemen...
Definition: CBotDll.h:59
CBotString Class used to work on strings.
Definition: CBotDll.h:259
Management of the execution stack.
Definition: CBot.h:73
Definition: CBot.h:1374
CBotTypResult class to define the complete type of a result.
Definition: CBotDll.h:90
int GetEnd()
end position in the text
Definition: CBotToken.cpp:185
Definition: CBotDll.h:334
CBotString & GetSep()
makes the following separator token
Definition: CBotToken.cpp:168
Definition: CBotDll.h:557
Definition: CBotDll.h:776
Definition: CBot.h:1474
CBotToken * GetNext()
gives the next token in the list
Definition: CBotToken.cpp:139
int GetStart()
position of the beginning in the text
Definition: CBotToken.cpp:179
void AddNext(CBotToken *p)
adds a token (a copy)
Definition: CBotToken.cpp:151
static CBotToken * CompileTokens(const char *p, int &error)
transforms the entire program
Definition: CBotToken.cpp:392
CBotToken * GetPrev()
gives the previous token in a list
Definition: CBotToken.cpp:145