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