Script_Compiler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code 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. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SCRIPT_COMPILER_H__
  21. #define __SCRIPT_COMPILER_H__
  22. const char * const RESULT_STRING = "<RESULT>";
  23. typedef struct opcode_s {
  24. char *name;
  25. char *opname;
  26. int priority;
  27. bool rightAssociative;
  28. idVarDef *type_a;
  29. idVarDef *type_b;
  30. idVarDef *type_c;
  31. } opcode_t;
  32. // These opcodes are no longer necessary:
  33. // OP_PUSH_OBJ:
  34. // OP_PUSH_OBJENT:
  35. enum {
  36. OP_RETURN,
  37. OP_UINC_F,
  38. OP_UINCP_F,
  39. OP_UDEC_F,
  40. OP_UDECP_F,
  41. OP_COMP_F,
  42. OP_MUL_F,
  43. OP_MUL_V,
  44. OP_MUL_FV,
  45. OP_MUL_VF,
  46. OP_DIV_F,
  47. OP_MOD_F,
  48. OP_ADD_F,
  49. OP_ADD_V,
  50. OP_ADD_S,
  51. OP_ADD_FS,
  52. OP_ADD_SF,
  53. OP_ADD_VS,
  54. OP_ADD_SV,
  55. OP_SUB_F,
  56. OP_SUB_V,
  57. OP_EQ_F,
  58. OP_EQ_V,
  59. OP_EQ_S,
  60. OP_EQ_E,
  61. OP_EQ_EO,
  62. OP_EQ_OE,
  63. OP_EQ_OO,
  64. OP_NE_F,
  65. OP_NE_V,
  66. OP_NE_S,
  67. OP_NE_E,
  68. OP_NE_EO,
  69. OP_NE_OE,
  70. OP_NE_OO,
  71. OP_LE,
  72. OP_GE,
  73. OP_LT,
  74. OP_GT,
  75. OP_INDIRECT_F,
  76. OP_INDIRECT_V,
  77. OP_INDIRECT_S,
  78. OP_INDIRECT_ENT,
  79. OP_INDIRECT_BOOL,
  80. OP_INDIRECT_OBJ,
  81. OP_ADDRESS,
  82. OP_EVENTCALL,
  83. OP_OBJECTCALL,
  84. OP_SYSCALL,
  85. OP_STORE_F,
  86. OP_STORE_V,
  87. OP_STORE_S,
  88. OP_STORE_ENT,
  89. OP_STORE_BOOL,
  90. OP_STORE_OBJENT,
  91. OP_STORE_OBJ,
  92. OP_STORE_ENTOBJ,
  93. OP_STORE_FTOS,
  94. OP_STORE_BTOS,
  95. OP_STORE_VTOS,
  96. OP_STORE_FTOBOOL,
  97. OP_STORE_BOOLTOF,
  98. OP_STOREP_F,
  99. OP_STOREP_V,
  100. OP_STOREP_S,
  101. OP_STOREP_ENT,
  102. OP_STOREP_FLD,
  103. OP_STOREP_BOOL,
  104. OP_STOREP_OBJ,
  105. OP_STOREP_OBJENT,
  106. OP_STOREP_FTOS,
  107. OP_STOREP_BTOS,
  108. OP_STOREP_VTOS,
  109. OP_STOREP_FTOBOOL,
  110. OP_STOREP_BOOLTOF,
  111. OP_UMUL_F,
  112. OP_UMUL_V,
  113. OP_UDIV_F,
  114. OP_UDIV_V,
  115. OP_UMOD_F,
  116. OP_UADD_F,
  117. OP_UADD_V,
  118. OP_USUB_F,
  119. OP_USUB_V,
  120. OP_UAND_F,
  121. OP_UOR_F,
  122. OP_NOT_BOOL,
  123. OP_NOT_F,
  124. OP_NOT_V,
  125. OP_NOT_S,
  126. OP_NOT_ENT,
  127. OP_NEG_F,
  128. OP_NEG_V,
  129. OP_INT_F,
  130. OP_IF,
  131. OP_IFNOT,
  132. OP_CALL,
  133. OP_THREAD,
  134. OP_OBJTHREAD,
  135. OP_PUSH_F,
  136. OP_PUSH_V,
  137. OP_PUSH_S,
  138. OP_PUSH_ENT,
  139. OP_PUSH_OBJ,
  140. OP_PUSH_OBJENT,
  141. OP_PUSH_FTOS,
  142. OP_PUSH_BTOF,
  143. OP_PUSH_FTOB,
  144. OP_PUSH_VTOS,
  145. OP_PUSH_BTOS,
  146. OP_GOTO,
  147. OP_AND,
  148. OP_AND_BOOLF,
  149. OP_AND_FBOOL,
  150. OP_AND_BOOLBOOL,
  151. OP_OR,
  152. OP_OR_BOOLF,
  153. OP_OR_FBOOL,
  154. OP_OR_BOOLBOOL,
  155. OP_BITAND,
  156. OP_BITOR,
  157. OP_BREAK, // placeholder op. not used in final code
  158. OP_CONTINUE, // placeholder op. not used in final code
  159. NUM_OPCODES
  160. };
  161. class idCompiler {
  162. private:
  163. static bool punctuationValid[ 256 ];
  164. static char *punctuation[];
  165. idParser parser;
  166. idParser *parserPtr;
  167. idToken token;
  168. idTypeDef *immediateType;
  169. eval_t immediate;
  170. bool eof;
  171. bool console;
  172. bool callthread;
  173. int braceDepth;
  174. int loopDepth;
  175. int currentLineNumber;
  176. int currentFileNumber;
  177. int errorCount;
  178. idVarDef *scope; // the function being parsed, or NULL
  179. const idVarDef *basetype; // for accessing fields
  180. float Divide( float numerator, float denominator );
  181. void Error( VERIFY_FORMAT_STRING const char *error, ... ) const;
  182. void Warning( VERIFY_FORMAT_STRING const char *message, ... ) const;
  183. idVarDef *OptimizeOpcode( const opcode_t *op, idVarDef *var_a, idVarDef *var_b );
  184. idVarDef *EmitOpcode( const opcode_t *op, idVarDef *var_a, idVarDef *var_b );
  185. idVarDef *EmitOpcode( int op, idVarDef *var_a, idVarDef *var_b );
  186. bool EmitPush( idVarDef *expression, const idTypeDef *funcArg );
  187. void NextToken();
  188. void ExpectToken( const char *string );
  189. bool CheckToken( const char *string );
  190. void ParseName( idStr &name );
  191. void SkipOutOfFunction();
  192. void SkipToSemicolon();
  193. idTypeDef *CheckType();
  194. idTypeDef *ParseType();
  195. idVarDef *FindImmediate( const idTypeDef *type, const eval_t *eval, const char *string ) const;
  196. idVarDef *GetImmediate( idTypeDef *type, const eval_t *eval, const char *string );
  197. idVarDef *VirtualFunctionConstant( idVarDef *func );
  198. idVarDef *SizeConstant( int size );
  199. idVarDef *JumpConstant( int value );
  200. idVarDef *JumpDef( int jumpfrom, int jumpto );
  201. idVarDef *JumpTo( int jumpto );
  202. idVarDef *JumpFrom( int jumpfrom );
  203. idVarDef *ParseImmediate();
  204. idVarDef *EmitFunctionParms( int op, idVarDef *func, int startarg, int startsize, idVarDef *object );
  205. idVarDef *ParseFunctionCall( idVarDef *func );
  206. idVarDef *ParseObjectCall( idVarDef *object, idVarDef *func );
  207. idVarDef *ParseEventCall( idVarDef *object, idVarDef *func );
  208. idVarDef *ParseSysObjectCall( idVarDef *func );
  209. idVarDef *LookupDef( const char *name, const idVarDef *baseobj );
  210. idVarDef *ParseValue();
  211. idVarDef *GetTerm();
  212. bool TypeMatches( etype_t type1, etype_t type2 ) const;
  213. idVarDef *GetExpression( int priority );
  214. idTypeDef *GetTypeForEventArg( char argType );
  215. void PatchLoop( int start, int continuePos );
  216. void ParseReturnStatement();
  217. void ParseWhileStatement();
  218. void ParseForStatement();
  219. void ParseDoWhileStatement();
  220. void ParseIfStatement();
  221. void ParseStatement();
  222. void ParseObjectDef( const char *objname );
  223. idTypeDef *ParseFunction( idTypeDef *returnType, const char *name );
  224. void ParseFunctionDef( idTypeDef *returnType, const char *name );
  225. void ParseVariableDef( idTypeDef *type, const char *name );
  226. void ParseEventDef( idTypeDef *type, const char *name );
  227. void ParseDefs();
  228. void ParseNamespace( idVarDef *newScope );
  229. public :
  230. static opcode_t opcodes[];
  231. idCompiler();
  232. void CompileFile( const char *text, const char *filename, bool console );
  233. };
  234. #endif /* !__SCRIPT_COMPILER_H__ */