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