ast.h

Engine/source/console/ast.h

More...

Classes:

Public Defines

define
DBG_STMT_TYPE(s) virtual  dbgStmtType()  { return (#s); }

Helper macro.

Public Enumerations

enum
TypeReq {
  TypeReqNone 
  TypeReqUInt 
  TypeReqFloat 
  TypeReqString 
  TypeReqVar 
}

Enable this #define if you are seeing the message "precompile size mismatch" in the console.

Detailed Description

Public Defines

DBG_STMT_TYPE(s) virtual  dbgStmtType()  { return (#s); }

Helper macro.

Public Enumerations

TypeReq

Enumerator

TypeReqNone
TypeReqUInt
TypeReqFloat
TypeReqString
TypeReqVar

Enable this #define if you are seeing the message "precompile size mismatch" in the console.

This will help track down which node type is causing the error. It could be due to incorrect compiler optimization.

Public Variables

U32 gAnonFunctionID 
StmtNode * gAnonFunctionList 
ExprEvalState gEvalState 
StmtNode * gStatementList 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _AST_H_
 25#define _AST_H_
 26
 27class ExprEvalState;
 28class Namespace;
 29class SimObject;
 30class SimGroup;
 31class CodeStream;
 32
 33/// Enable this #define if you are seeing the message "precompile size mismatch" in the console.
 34/// This will help track down which node type is causing the error. It could be
 35/// due to incorrect compiler optimization.
 36//#define DEBUG_AST_NODES
 37
 38enum TypeReq
 39{
 40   TypeReqNone,
 41   TypeReqUInt,
 42   TypeReqFloat,
 43   TypeReqString,
 44   TypeReqVar
 45};
 46
 47/// Representation of a node for the scripting language parser.
 48///
 49/// When the scripting language is evaluated, it is turned from a string representation,
 50/// into a parse tree, thence into byte code, which is ultimately interpreted by the VM.
 51///
 52/// This is the base class for the nodes in the parse tree. There are a great many subclasses,
 53/// each representing a different language construct.
 54struct StmtNode
 55{
 56   StmtNode *next;   ///< Next entry in parse tree.
 57
 58   StmtNode();
 59   virtual ~StmtNode() {}
 60   
 61   /// @name next Accessors
 62   /// @{
 63
 64   ///
 65   void append(StmtNode *next);
 66   StmtNode *getNext() const { return next; }
 67
 68   /// @}
 69
 70   /// @name Debug Info
 71   /// @{
 72
 73   StringTableEntry dbgFileName; ///< Name of file this node is associated with.
 74   S32 dbgLineNumber;            ///< Line number this node is associated with.
 75#ifdef DEBUG_AST_NODES
 76   virtual String dbgStmtType() const = 0;
 77#endif
 78   /// @}
 79
 80   /// @name Breaking
 81   /// @{
 82
 83   void addBreakLine(CodeStream &codeStream);
 84   /// @}
 85
 86   /// @name Compilation
 87   /// @{
 88
 89   virtual U32 compileStmt(CodeStream &codeStream, U32 ip) = 0;
 90   virtual void setPackage(StringTableEntry packageName);
 91   /// @}
 92};
 93
 94/// Helper macro
 95#ifndef DEBUG_AST_NODES
 96#  define DBG_STMT_TYPE(s) virtual String dbgStmtType() const { return String(#s); }
 97#else
 98#  define DBG_STMT_TYPE(s) 
 99#endif
100
101struct BreakStmtNode : StmtNode
102{
103   static BreakStmtNode *alloc( S32 lineNumber );
104
105   
106   U32 compileStmt(CodeStream &codeStream, U32 ip);
107   DBG_STMT_TYPE(BreakStmtNode);
108};
109
110struct ContinueStmtNode : StmtNode
111{
112   static ContinueStmtNode *alloc( S32 lineNumber );
113   
114   U32 compileStmt(CodeStream &codeStream, U32 ip);
115   DBG_STMT_TYPE(ContinueStmtNode);
116};
117
118/// A mathematical expression.
119struct ExprNode : StmtNode
120{
121   
122   U32 compileStmt(CodeStream &codeStream, U32 ip);
123
124   virtual U32 compile(CodeStream &codeStream, U32 ip, TypeReq type) = 0;
125   virtual TypeReq getPreferredType() = 0;
126};
127
128struct ReturnStmtNode : StmtNode
129{
130   ExprNode *expr;
131
132   static ReturnStmtNode *alloc( S32 lineNumber, ExprNode *expr );
133   
134   U32 compileStmt(CodeStream &codeStream, U32 ip);
135   DBG_STMT_TYPE(ReturnStmtNode);
136};
137
138struct IfStmtNode : StmtNode
139{
140   ExprNode *testExpr;
141   StmtNode *ifBlock, *elseBlock;
142   U32 endifOffset;
143   U32 elseOffset;
144   bool integer;
145   bool propagate;
146
147   static IfStmtNode *alloc( S32 lineNumber, ExprNode *testExpr, StmtNode *ifBlock, StmtNode *elseBlock, bool propagateThrough );
148   void propagateSwitchExpr(ExprNode *left, bool string);
149   ExprNode *getSwitchOR(ExprNode *left, ExprNode *list, bool string);
150   
151   U32 compileStmt(CodeStream &codeStream, U32 ip);
152   DBG_STMT_TYPE(IfStmtNode);
153};
154
155struct LoopStmtNode : StmtNode
156{
157   ExprNode *testExpr;
158   ExprNode *initExpr;
159   ExprNode *endLoopExpr;
160   StmtNode *loopBlock;
161   bool isDoLoop;
162   U32 breakOffset;
163   U32 continueOffset;
164   U32 loopBlockStartOffset;
165   bool integer;
166
167   static LoopStmtNode *alloc( S32 lineNumber, ExprNode *testExpr, ExprNode *initExpr, ExprNode *endLoopExpr, StmtNode *loopBlock, bool isDoLoop );
168   
169   U32 compileStmt(CodeStream &codeStream, U32 ip);
170   DBG_STMT_TYPE(LoopStmtNode);
171};
172
173/// A "foreach" statement.
174struct IterStmtNode : StmtNode
175{
176   /// Local variable name to use for the container element.
177   StringTableEntry varName;
178   
179   /// Expression evaluating to a SimSet object.
180   ExprNode* containerExpr;
181   
182   /// The statement body.
183   StmtNode* body;
184   
185   /// If true, this is a 'foreach$'.
186   bool isStringIter;
187   
188   /// Bytecode size of body statement.  Set by precompileStmt.
189   U32 bodySize;
190   
191   static IterStmtNode* alloc( S32 lineNumber, StringTableEntry varName, ExprNode* containerExpr, StmtNode* body, bool isStringIter );
192   
193   U32 compileStmt( CodeStream &codeStream, U32 ip );
194};
195
196/// A binary mathematical expression (ie, left op right).
197struct BinaryExprNode : ExprNode
198{
199   S32 op;
200   ExprNode *left;
201   ExprNode *right;
202};
203
204struct FloatBinaryExprNode : BinaryExprNode
205{
206   static FloatBinaryExprNode *alloc( S32 lineNumber, S32 op, ExprNode *left, ExprNode *right );
207  
208   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
209   TypeReq getPreferredType();
210   DBG_STMT_TYPE(FloatBinaryExprNode);
211};
212
213struct ConditionalExprNode : ExprNode
214{
215   ExprNode *testExpr;
216   ExprNode *trueExpr;
217   ExprNode *falseExpr;
218   bool integer;
219   static ConditionalExprNode *alloc( S32 lineNumber, ExprNode *testExpr, ExprNode *trueExpr, ExprNode *falseExpr );
220   
221   virtual U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
222   virtual TypeReq getPreferredType();
223   DBG_STMT_TYPE(ConditionalExprNode);
224};
225
226struct IntBinaryExprNode : BinaryExprNode
227{
228   TypeReq subType;
229   U32 operand;
230
231   static IntBinaryExprNode *alloc( S32 lineNumber, S32 op, ExprNode *left, ExprNode *right );
232
233   void getSubTypeOperand();
234   
235   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
236   TypeReq getPreferredType();
237   DBG_STMT_TYPE(IntBinaryExprNode);
238};
239
240struct StreqExprNode : BinaryExprNode
241{
242   bool eq;
243   static StreqExprNode *alloc( S32 lineNumber, ExprNode *left, ExprNode *right, bool eq );
244   
245   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
246   TypeReq getPreferredType();
247   DBG_STMT_TYPE(StreqExprNode);
248};
249
250struct StrcatExprNode : BinaryExprNode
251{
252   S32 appendChar;
253   static StrcatExprNode *alloc( S32 lineNumber, ExprNode *left, ExprNode *right, S32 appendChar );
254  
255   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
256   TypeReq getPreferredType();
257   DBG_STMT_TYPE(StrcatExprNode);
258};
259
260struct CommaCatExprNode : BinaryExprNode
261{
262   static CommaCatExprNode *alloc( S32 lineNumber, ExprNode *left, ExprNode *right );
263
264  
265   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
266   TypeReq getPreferredType();
267   DBG_STMT_TYPE(CommaCatExprNode);
268};
269
270struct IntUnaryExprNode : ExprNode
271{
272   S32 op;
273   ExprNode *expr;
274   bool integer;
275
276   static IntUnaryExprNode *alloc( S32 lineNumber, S32 op, ExprNode *expr );
277  
278   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
279   TypeReq getPreferredType();
280   DBG_STMT_TYPE(IntUnaryExprNode);
281};
282
283struct FloatUnaryExprNode : ExprNode
284{
285   S32 op;
286   ExprNode *expr;
287
288   static FloatUnaryExprNode *alloc( S32 lineNumber, S32 op, ExprNode *expr );
289  
290   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
291   TypeReq getPreferredType();
292   DBG_STMT_TYPE(FloatUnaryExprNode);
293};
294
295struct VarNode : ExprNode
296{
297   StringTableEntry varName;
298   ExprNode *arrayIndex;
299
300   static VarNode *alloc( S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex );
301  
302   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
303   TypeReq getPreferredType();
304   DBG_STMT_TYPE(VarNode);
305};
306
307struct IntNode : ExprNode
308{
309   S32 value;
310   U32 index; // if it's converted to float/string
311
312   static IntNode *alloc( S32 lineNumber, S32 value );
313  
314   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
315   TypeReq getPreferredType();
316   DBG_STMT_TYPE(IntNode);
317};
318
319struct FloatNode : ExprNode
320{
321   F64 value;
322   U32 index;
323
324   static FloatNode *alloc( S32 lineNumber, F64 value );
325  
326   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
327   TypeReq getPreferredType();
328   DBG_STMT_TYPE(FloatNode);
329};
330
331struct StrConstNode : ExprNode
332{
333   char *str;
334   F64 fVal;
335   U32 index;
336   bool tag;
337   bool doc; // Specifies that this string is a documentation block.
338
339   static StrConstNode *alloc( S32 lineNumber, char *str, bool tag, bool doc = false );
340  
341   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
342   TypeReq getPreferredType();
343   DBG_STMT_TYPE(StrConstNode);
344};
345
346struct ConstantNode : ExprNode
347{
348   StringTableEntry value;
349   F64 fVal;
350   U32 index;
351
352   static ConstantNode *alloc( S32 lineNumber, StringTableEntry value );
353  
354   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
355   TypeReq getPreferredType();
356   DBG_STMT_TYPE(ConstantNode);
357};
358
359struct AssignExprNode : ExprNode
360{
361   StringTableEntry varName;
362   ExprNode *expr;
363   ExprNode *arrayIndex;
364   TypeReq subType;
365
366   static AssignExprNode *alloc( S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr );
367  
368   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
369   TypeReq getPreferredType();
370   DBG_STMT_TYPE(AssignExprNode);
371};
372
373struct AssignDecl
374{
375   S32 lineNumber;
376   S32 token;
377   ExprNode *expr;
378   bool integer;
379};
380
381struct AssignOpExprNode : ExprNode
382{
383   StringTableEntry varName;
384   ExprNode *expr;
385   ExprNode *arrayIndex;
386   S32 op;
387   U32 operand;
388   TypeReq subType;
389
390   static AssignOpExprNode *alloc( S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr, S32 op );
391  
392   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
393   TypeReq getPreferredType();
394   DBG_STMT_TYPE(AssignOpExprNode);
395};
396
397struct TTagSetStmtNode : StmtNode
398{
399   StringTableEntry tag;
400   ExprNode *valueExpr;
401   ExprNode *stringExpr;
402
403   static TTagSetStmtNode *alloc( S32 lineNumber, StringTableEntry tag, ExprNode *valueExpr, ExprNode *stringExpr );
404   
405   U32 compileStmt(CodeStream &codeStream, U32 ip);
406   DBG_STMT_TYPE(TTagSetStmtNode);
407};
408
409struct TTagDerefNode : ExprNode
410{
411   ExprNode *expr;
412
413   static TTagDerefNode *alloc( S32 lineNumber, ExprNode *expr );
414  
415   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
416   TypeReq getPreferredType();
417   DBG_STMT_TYPE(TTagDerefNode);
418};
419
420struct TTagExprNode : ExprNode
421{
422   StringTableEntry tag;
423
424   static TTagExprNode *alloc( S32 lineNumber, StringTableEntry tag );
425  
426   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
427   TypeReq getPreferredType();
428   DBG_STMT_TYPE(TTagExprNode);
429};
430
431struct FuncCallExprNode : ExprNode
432{
433   StringTableEntry funcName;
434   StringTableEntry nameSpace;
435   ExprNode *args;
436   U32 callType;
437   enum {
438      FunctionCall,
439      MethodCall,
440      ParentCall
441   };
442
443   static FuncCallExprNode *alloc( S32 lineNumber, StringTableEntry funcName, StringTableEntry nameSpace, ExprNode *args, bool dot );
444  
445   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
446   TypeReq getPreferredType();
447   DBG_STMT_TYPE(FuncCallExprNode);
448};
449
450struct AssertCallExprNode : ExprNode
451{
452   ExprNode *testExpr;
453   const char *message;
454   U32 messageIndex;
455
456   static AssertCallExprNode *alloc( S32 lineNumber, ExprNode *testExpr, const char *message );
457  
458   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
459   TypeReq getPreferredType();
460   DBG_STMT_TYPE(AssertCallExprNode);
461};
462
463struct SlotDecl
464{
465   S32              lineNumber;
466   ExprNode         *object;
467   StringTableEntry slotName;
468   ExprNode         *array;
469};
470
471struct SlotAccessNode : ExprNode
472{
473   ExprNode *objectExpr, *arrayExpr;
474   StringTableEntry slotName;
475
476   static SlotAccessNode *alloc( S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName );
477  
478   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
479   TypeReq getPreferredType();
480   DBG_STMT_TYPE(SlotAccessNode);
481};
482
483struct InternalSlotDecl
484{
485   S32              lineNumber;
486   ExprNode         *object;
487   ExprNode         *slotExpr;
488   bool             recurse;
489};
490
491struct InternalSlotAccessNode : ExprNode
492{
493   ExprNode *objectExpr, *slotExpr;
494   bool recurse;
495
496   static InternalSlotAccessNode *alloc( S32 lineNumber, ExprNode *objectExpr, ExprNode *slotExpr, bool recurse );
497  
498   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
499   TypeReq getPreferredType();
500   DBG_STMT_TYPE(InternalSlotAccessNode);
501};
502
503struct SlotAssignNode : ExprNode
504{
505   ExprNode *objectExpr, *arrayExpr;
506   StringTableEntry slotName;
507   ExprNode *valueExpr;
508   U32 typeID;
509
510   static SlotAssignNode *alloc( S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName, ExprNode *valueExpr, U32 typeID = -1 );
511  
512   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
513   TypeReq getPreferredType();
514   DBG_STMT_TYPE(SlotAssignNode);
515};
516
517struct SlotAssignOpNode : ExprNode
518{
519   ExprNode *objectExpr, *arrayExpr;
520   StringTableEntry slotName;
521   S32 op;
522   ExprNode *valueExpr;
523   U32 operand;
524   TypeReq subType;
525
526   static SlotAssignOpNode *alloc( S32 lineNumber, ExprNode *objectExpr, StringTableEntry slotName, ExprNode *arrayExpr, S32 op, ExprNode *valueExpr );
527  
528   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
529   TypeReq getPreferredType();
530   DBG_STMT_TYPE(SlotAssignOpNode);
531};
532
533struct ObjectDeclNode : ExprNode
534{
535   ExprNode *classNameExpr;
536   StringTableEntry parentObject;
537   ExprNode *objectNameExpr;
538   ExprNode *argList;
539   SlotAssignNode *slotDecls;
540   ObjectDeclNode *subObjects;
541   bool isDatablock;
542   U32 failOffset;
543   bool isClassNameInternal;
544   bool isSingleton;
545
546   static ObjectDeclNode *alloc( S32 lineNumber, ExprNode *classNameExpr, ExprNode *objectNameExpr, ExprNode *argList, StringTableEntry parentObject, SlotAssignNode *slotDecls, ObjectDeclNode *subObjects, bool isDatablock, bool classNameInternal, bool isSingleton );
547  
548   U32 precompileSubObject(bool);
549   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
550   U32 compileSubObject(CodeStream &codeStream, U32 ip, bool);
551   TypeReq getPreferredType();
552   DBG_STMT_TYPE(ObjectDeclNode);
553};
554
555struct ObjectBlockDecl
556{
557   SlotAssignNode *slots;
558   ObjectDeclNode *decls;
559};
560
561struct FunctionDeclStmtNode : StmtNode
562{
563   StringTableEntry fnName;
564   VarNode *args;
565   StmtNode *stmts;
566   StringTableEntry nameSpace;
567   StringTableEntry package;
568   U32 endOffset;
569   U32 argc;
570
571   static FunctionDeclStmtNode *alloc( S32 lineNumber, StringTableEntry fnName, StringTableEntry nameSpace, VarNode *args, StmtNode *stmts );
572   
573   U32 compileStmt(CodeStream &codeStream, U32 ip);
574   void setPackage(StringTableEntry packageName);
575   DBG_STMT_TYPE(FunctionDeclStmtNode);
576};
577
578extern StmtNode *gStatementList;
579extern StmtNode *gAnonFunctionList;
580extern U32 gAnonFunctionID;
581extern ExprEvalState gEvalState;
582
583#endif
584