DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Script_Compiler.h (6787B)


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