gmCodeTree.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. _____ __ ___ __ ____ _ __
  3. / ___/__ ___ _ ___ / |/ /__ ___ / /_____ __ __/ __/_______(_)__ / /_
  4. / (_ / _ `/ ' \/ -_) /|_/ / _ \/ _ \/ '_/ -_) // /\ \/ __/ __/ / _ \/ __/
  5. \___/\_,_/_/_/_/\__/_/ /_/\___/_//_/_/\_\\__/\_, /___/\__/_/ /_/ .__/\__/
  6. /___/ /_/
  7. See Copyright Notice in gmMachine.h
  8. */
  9. #ifndef _GMCODETREE_H_
  10. #define _GMCODETREE_H_
  11. #include "gmConfig.h"
  12. #include "gmMem.h"
  13. #include "gmMemChain.h"
  14. #include "gmLog.h"
  15. #include "gmScanner.h"
  16. #define GMCODETREE_NUMCHILDREN 4
  17. // fwd decl
  18. struct gmCodeTreeNode;
  19. /// \class gmCodeTree
  20. /// \brief gmCodeTree is a singleton class for creating code trees.
  21. class gmCodeTree
  22. {
  23. protected:
  24. gmCodeTree();
  25. public:
  26. ~gmCodeTree();
  27. /// \brief Get() will return the singlton parser.
  28. static gmCodeTree &Get();
  29. /// \brief FreeMemory() will free all memory allocated by the code tree. must be unlocked
  30. void FreeMemory();
  31. /// \brief Lock() will create a code tree for the passed script. Note that the code tree is valid until
  32. /// Unlock() is called.
  33. /// \param a_script is a null terminated script string.
  34. /// \return the number of errors encounted when parsing.
  35. /// \sa Unlock()
  36. int Lock(const char * a_script, gmLog * a_log = NULL);
  37. /// \brief Unlock() will unlock the singleton code tree such that it may be used again.
  38. /// \return 0 on success
  39. /// \sa Lock()
  40. int Unlock();
  41. /// \brief GetCodeTree() will return the code tree resulting from a Lock() operation.
  42. /// \return NULL on failure
  43. /// \sa Lock()
  44. const gmCodeTreeNode * GetCodeTree() const;
  45. inline gmLog * GetLog() const { return m_log; }
  46. /// \brief Alloc() will return memory from the code tree memory pool. This method is used by the
  47. /// parser when building the code tree.
  48. /// \return NULL on failure
  49. void * Alloc(int a_size, int a_align = GM_DEFAULT_ALLOC_ALIGNMENT);
  50. #if GM_COMPILE_DEBUG
  51. /// \brief Print() will write the tree to the given file. this is purely for debugging.
  52. /// \param a_fp is an open file for writing.
  53. void Print(FILE * a_fp);
  54. #endif // GM_COMPILE_DEBUG
  55. private:
  56. bool m_locked;
  57. int m_errors;
  58. gmLog * m_log;
  59. gmMemChain m_mem;
  60. };
  61. /// \enum gmCodeTreeNodeType
  62. /// \brief gmCodeTreeNodeType indicates the type of a gmCodeTreeNode.
  63. enum gmCodeTreeNodeType
  64. {
  65. CTNT_INVALID = 0,
  66. CTNT_DECLARATION,
  67. CTNT_STATEMENT,
  68. CTNT_EXPRESSION,
  69. };
  70. /// \enum gmCodeTreeNodeDeclarationType
  71. /// \brief if a tree node is of type CTNT_DECLARATION, gmCodeTreeNodeDeclarationType are the sub types
  72. enum gmCodeTreeNodeDeclarationType
  73. {
  74. CTNDT_PARAMETER = 0,
  75. CTNDT_VARIABLE,
  76. };
  77. /// \enum gmCodeTreeVariableType
  78. /// \brief if a treenode is CTNT_DECLARATION, CTNDT_VARIABLE, this is the type of variable decl.
  79. enum gmCodeTreeVariableType
  80. {
  81. CTVT_LOCAL = 0,
  82. CTVT_GLOBAL,
  83. CTVT_MEMBER,
  84. };
  85. /// \enum gmCodeTreeNodeStatementType
  86. /// \brief if a tree node is of type CTNT_STATEMENT, gmCodeTreeNodeStatementType are the sub types
  87. enum gmCodeTreeNodeStatementType
  88. {
  89. CTNST_INVALID = 0,
  90. CTNST_RETURN,
  91. CTNST_BREAK,
  92. CTNST_CONTINUE,
  93. CTNST_FOR,
  94. CTNST_FOREACH,
  95. CTNST_WHILE,
  96. CTNST_DOWHILE,
  97. CTNST_IF,
  98. CTNST_COMPOUND,
  99. CTNST_FORK, // If GM_USE_FORK
  100. };
  101. /// \enum gmCodeTreeNodeExpressionType
  102. /// \brief
  103. enum gmCodeTreeNodeExpressionType
  104. {
  105. CTNET_INVALID = 0,
  106. CTNET_OPERATION,
  107. CTNET_CONSTANT,
  108. CTNET_IDENTIFIER,
  109. CTNET_THIS,
  110. CTNET_CALL,
  111. CTNET_FUNCTION,
  112. CTNET_TABLE,
  113. };
  114. /// \enum gmCodeTreeNodeOperationType
  115. /// \brief
  116. enum gmCodeTreeNodeOperationType
  117. {
  118. CTNOT_INVALID = 0,
  119. CTNOT_DOT,
  120. CTNOT_UNARY_PLUS,
  121. CTNOT_UNARY_MINUS,
  122. CTNOT_UNARY_COMPLEMENT,
  123. CTNOT_UNARY_NOT,
  124. #if GM_USE_INCDECOPERATORS
  125. CTNOT_PRE_DEC,
  126. CTNOT_PRE_INC,
  127. #endif //GM_USE_INCDECOPERATORS
  128. CTNOT_ARRAY_INDEX,
  129. CTNOT_TIMES,
  130. CTNOT_DIVIDE,
  131. CTNOT_REM,
  132. CTNOT_ADD,
  133. CTNOT_MINUS,
  134. CTNOT_LT,
  135. CTNOT_GT,
  136. CTNOT_LTE,
  137. CTNOT_GTE,
  138. CTNOT_EQ,
  139. CTNOT_NEQ,
  140. CTNOT_AND,
  141. CTNOT_OR,
  142. CTNOT_BIT_OR,
  143. CTNOT_BIT_XOR,
  144. CTNOT_BIT_AND,
  145. CTNOT_SHIFT_LEFT,
  146. CTNOT_SHIFT_RIGHT,
  147. CTNOT_ASSIGN,
  148. CTNOT_ASSIGN_FIELD,
  149. CTNOT_MAX,
  150. };
  151. /// \enum gmCodeTreeNodeConstantType
  152. enum gmCodeTreeNodeConstantType
  153. {
  154. CTNCT_INVALID = 0,
  155. CTNCT_INT,
  156. CTNCT_FLOAT,
  157. CTNCT_STRING,
  158. CTNCT_NULL,
  159. };
  160. /// \union gmCodeTreeNodeUnion
  161. /// \brief
  162. union gmCodeTreeNodeData
  163. {
  164. char * m_string;
  165. gmint m_iValue;
  166. gmfloat m_fValue;
  167. };
  168. /// \struct gmCodeTreeNode
  169. /// \brief gmCodeTreeNode is the tree node structure used to represent the game monkey script syntax tree.
  170. struct gmCodeTreeNode
  171. {
  172. // flags
  173. enum
  174. {
  175. CTN_POP = (1 << 0),
  176. CTN_MEMBER = (1 << 1),
  177. };
  178. /// \brief Create() will create a tree node. the singleton gmCodeTree must be locked.
  179. /// \return a tree node
  180. static gmCodeTreeNode * Create(gmCodeTreeNodeType a_type, int a_subType, int a_lineNumber, int a_subTypeType = 0);
  181. /// \brief SetChild() will set the child at the given index.
  182. /// \param a_node is the child node, whose parent pointer will be assigned to this.
  183. void SetChild(int a_index, gmCodeTreeNode * a_node);
  184. /// \brief ConstantFold() will pull child nodes into this node, and make this node a constant if possible
  185. bool ConstantFold();
  186. gmCodeTreeNodeType m_type;
  187. int m_subType;
  188. int m_subTypeType;
  189. int m_flags;
  190. gmCodeTreeNode * m_children[GMCODETREE_NUMCHILDREN];
  191. gmCodeTreeNode * m_sibling;
  192. gmCodeTreeNode * m_parent;
  193. int m_lineNumber;
  194. gmCodeTreeNodeData m_data;
  195. };
  196. //
  197. // misc lexing and parsing functions.
  198. //
  199. void gmProcessSingleQuoteString(char * a_string);
  200. void gmProcessDoubleQuoteString(char * a_string);
  201. int gmerror(char * a_message);
  202. int gmparse(void);
  203. #endif // _GMCODETREE_H_