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 *mNext;   ///< 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 mNext; }
 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 FuncPointerCallExprNode : ExprNode
451{
452   ExprNode *funcPointer;
453   ExprNode *args;
454
455   static FuncPointerCallExprNode *alloc(S32 lineNumber, ExprNode *stmt, ExprNode *args);
456
457   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
458   TypeReq getPreferredType();
459   DBG_STMT_TYPE(FuncPointerCallExprNode);
460};
461
462struct AssertCallExprNode : ExprNode
463{
464   ExprNode *testExpr;
465   const char *message;
466   U32 messageIndex;
467
468   static AssertCallExprNode *alloc(S32 lineNumber, ExprNode *testExpr, const char *message);
469
470   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
471   TypeReq getPreferredType();
472   DBG_STMT_TYPE(AssertCallExprNode);
473};
474
475struct SlotDecl
476{
477   S32              lineNumber;
478   ExprNode         *object;
479   StringTableEntry slotName;
480   ExprNode         *array;
481};
482
483struct SlotAccessNode : ExprNode
484{
485   ExprNode *objectExpr, *arrayExpr;
486   StringTableEntry slotName;
487
488   static SlotAccessNode *alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName);
489
490   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
491   TypeReq getPreferredType();
492   DBG_STMT_TYPE(SlotAccessNode);
493};
494
495struct InternalSlotDecl
496{
497   S32              lineNumber;
498   ExprNode         *object;
499   ExprNode         *slotExpr;
500   bool             recurse;
501};
502
503struct InternalSlotAccessNode : ExprNode
504{
505   ExprNode *objectExpr, *slotExpr;
506   bool recurse;
507
508   static InternalSlotAccessNode *alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *slotExpr, bool recurse);
509
510   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
511   TypeReq getPreferredType();
512   DBG_STMT_TYPE(InternalSlotAccessNode);
513};
514
515struct SlotAssignNode : ExprNode
516{
517   ExprNode *objectExpr, *arrayExpr;
518   StringTableEntry slotName;
519   ExprNode *valueExpr;
520   U32 typeID;
521
522   static SlotAssignNode *alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName, ExprNode *valueExpr, U32 typeID = -1);
523
524   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
525   TypeReq getPreferredType();
526   DBG_STMT_TYPE(SlotAssignNode);
527};
528
529struct SlotAssignOpNode : ExprNode
530{
531   ExprNode *objectExpr, *arrayExpr;
532   StringTableEntry slotName;
533   S32 op;
534   ExprNode *valueExpr;
535   U32 operand;
536   TypeReq subType;
537
538   static SlotAssignOpNode *alloc(S32 lineNumber, ExprNode *objectExpr, StringTableEntry slotName, ExprNode *arrayExpr, S32 op, ExprNode *valueExpr);
539
540   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
541   TypeReq getPreferredType();
542   DBG_STMT_TYPE(SlotAssignOpNode);
543};
544
545struct ObjectDeclNode : ExprNode
546{
547   ExprNode *classNameExpr;
548   StringTableEntry parentObject;
549   ExprNode *objectNameExpr;
550   ExprNode *argList;
551   SlotAssignNode *slotDecls;
552   ObjectDeclNode *subObjects;
553   bool isDatablock;
554   U32 failOffset;
555   bool isClassNameInternal;
556   bool isSingleton;
557
558   static ObjectDeclNode *alloc(S32 lineNumber, ExprNode *classNameExpr, ExprNode *objectNameExpr, ExprNode *argList, StringTableEntry parentObject, SlotAssignNode *slotDecls, ObjectDeclNode *subObjects, bool isDatablock, bool classNameInternal, bool isSingleton);
559
560   U32 precompileSubObject(bool);
561   U32 compile(CodeStream &codeStream, U32 ip, TypeReq type);
562   U32 compileSubObject(CodeStream &codeStream, U32 ip, bool);
563   TypeReq getPreferredType();
564   DBG_STMT_TYPE(ObjectDeclNode);
565};
566
567struct ObjectBlockDecl
568{
569   SlotAssignNode *slots;
570   ObjectDeclNode *decls;
571};
572
573struct FunctionDeclStmtNode : StmtNode
574{
575   StringTableEntry fnName;
576   VarNode *args;
577   StmtNode *stmts;
578   StringTableEntry nameSpace;
579   StringTableEntry package;
580   U32 endOffset;
581   U32 argc;
582
583   static FunctionDeclStmtNode *alloc(S32 lineNumber, StringTableEntry fnName, StringTableEntry nameSpace, VarNode *args, StmtNode *stmts);
584
585   U32 compileStmt(CodeStream &codeStream, U32 ip);
586   void setPackage(StringTableEntry packageName);
587   DBG_STMT_TYPE(FunctionDeclStmtNode);
588};
589
590extern StmtNode *gStatementList;
591extern StmtNode *gAnonFunctionList;
592extern U32 gAnonFunctionID;
593extern ExprEvalState gEvalState;
594
595#endif
596