Usermode/libspiderscript - Fixing a multitude of bugs
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / ast.h
1 /*
2  */
3 #ifndef _AST_H_
4 #define _AST_H_
5
6 #include <spiderscript.h>
7 #include "tokens.h"
8
9 typedef enum eAST_NodeTypes     tAST_NodeType;
10 typedef struct sAST_Script      tAST_Script;
11 typedef struct sAST_Function    tAST_Function;
12 typedef struct sAST_Node        tAST_Node;
13 typedef struct sAST_BlockState  tAST_BlockState;
14 typedef struct sAST_Variable    tAST_Variable;
15
16 /**
17  * \brief Node Types
18  */
19 enum eAST_NodeTypes
20 {
21         NODETYPE_NOP,
22         
23         NODETYPE_BLOCK, //!< Node Block
24         
25         NODETYPE_VARIABLE,      //!< Variable
26         NODETYPE_CONSTANT,      //!< Runtime Constant
27         NODETYPE_STRING,        //!< String Constant
28         NODETYPE_INTEGER,       //!< Integer Constant
29         NODETYPE_REAL,  //!< Real Constant
30         
31         NODETYPE_DEFVAR,        //!< Define a variable (Variable)
32         NODETYPE_SCOPE, //!< Dereference a Namespace/Class static
33         NODETYPE_ELEMENT,       //!< Reference a class attribute
34         NODETYPE_CAST,  //!< Cast a value to another (Uniop)
35         
36         NODETYPE_RETURN,        //!< Return from a function (reserved word)
37         NODETYPE_BREAK,         //!< Break out of a loop
38         NODETYPE_CONTINUE,      //!< Next loop iteration
39         NODETYPE_ASSIGN,        //!< Variable assignment operator
40         NODETYPE_POSTINC,       //!< Post-increment (i++) - Uniop
41         NODETYPE_POSTDEC,       //!< Post-decrement (i--) - Uniop
42         NODETYPE_FUNCTIONCALL,  //!< Call a function
43         NODETYPE_METHODCALL,    //!< Call a class method
44         NODETYPE_CREATEOBJECT,  //!< Create an object
45         
46         NODETYPE_IF,    //!< Conditional
47         NODETYPE_LOOP,  //!< Looping Construct
48         
49         NODETYPE_INDEX, //!< Index into an array
50         
51         NODETYPE_LOGICALNOT,    //!< Logical NOT operator
52         NODETYPE_LOGICALAND,    //!< Logical AND operator
53         NODETYPE_LOGICALOR,     //!< Logical OR operator
54         NODETYPE_LOGICALXOR,    //!< Logical XOR operator
55         
56         NODETYPE_EQUALS,        //!< Comparison Equals
57         NODETYPE_NOTEQUALS,     //!< Comparison Not Equals
58         NODETYPE_LESSTHAN,      //!< Comparison Less Than
59         NODETYPE_LESSTHANEQUAL, //!< Comparison Less Than or Equal
60         NODETYPE_GREATERTHAN,   //!< Comparison Greater Than
61         NODETYPE_GREATERTHANEQUAL,      //!< Comparison Greater Than or Equal
62         
63         NODETYPE_BWNOT, //!< Bitwise NOT
64         NODETYPE_BWAND, //!< Bitwise AND
65         NODETYPE_BWOR,  //!< Bitwise OR
66         NODETYPE_BWXOR, //!< Bitwise XOR
67         
68         NODETYPE_BITSHIFTLEFT,  //!< Bitwise Shift Left (Grow)
69         NODETYPE_BITSHIFTRIGHT, //!< Bitwise Shift Right (Shrink)
70         NODETYPE_BITROTATELEFT, //!< Bitwise Rotate Left (Grow)
71         
72         NODETYPE_NEGATE,        //!< Negagte
73         NODETYPE_ADD,   //!< Add
74         NODETYPE_SUBTRACT,      //!< Subtract
75         NODETYPE_MULTIPLY,      //!< Multiply
76         NODETYPE_DIVIDE,        //!< Divide
77         NODETYPE_MODULO,        //!< Modulus
78 };
79
80 struct sAST_Node
81 {
82         tAST_Node       *NextSibling;
83         tAST_NodeType   Type;
84
85         const char      *File;
86          int    Line;
87         
88         void    *BlockState;    //!< BlockState pointer (for cache integrity)
89          int    BlockIdent;     //!< Ident (same as above)
90         void    *ValueCache;    //!< Cached value / pointer
91         
92         union
93         {
94                 struct {
95                         tAST_Node       *FirstChild;
96                         tAST_Node       *LastChild;
97                 }       Block;
98                 
99                 struct {
100                          int    Operation;
101                         tAST_Node       *Dest;
102                         tAST_Node       *Value;
103                 }       Assign;
104                 
105                 struct {
106                         tAST_Node       *Value;
107                 }       UniOp;
108                 
109                 struct {
110                         tAST_Node       *Left;
111                         tAST_Node       *Right;
112                 }       BinOp;
113                 
114                 struct {
115                         tAST_Node       *Object;
116                         tAST_Node       *FirstArg;
117                         tAST_Node       *LastArg;
118                          int    NumArgs;
119                         char    Name[];
120                 }       FunctionCall;
121                 
122                 struct {
123                         tAST_Node       *Condition;
124                         tAST_Node       *True;
125                         tAST_Node       *False;
126                 }       If;
127                 
128                 struct {
129                         tAST_Node       *Init;
130                          int    bCheckAfter;
131                         tAST_Node       *Condition;
132                         tAST_Node       *Increment;
133                         tAST_Node       *Code;
134                         char    Tag[];
135                 }       For;
136                 
137                 /**
138                  * \note Used for \a NODETYPE_VARIABLE and \a NODETYPE_CONSTANT
139                  */
140                 struct {
141                         char    _unused;        // Shut GCC up
142                         char    Name[];
143                 }       Variable;
144                 
145                 struct {
146                         tAST_Node       *Element;
147                         char    Name[];
148                 }       Scope;  // Used by NODETYPE_SCOPE and NODETYPE_ELEMENT
149                 
150                 struct {
151                          int    DataType;
152                         tAST_Node       *LevelSizes;
153                         tAST_Node       *LevelSizes_Last;
154                         tAST_Node       *InitialValue;
155                         char    Name[];
156                 }       DefVar;
157                 
158                 struct {
159                          int    DataType;
160                          tAST_Node      *Value;
161                 }       Cast;
162                 
163                 // Used for NODETYPE_REAL, NODETYPE_INTEGER and NODETYPE_STRING
164                 tSpiderValue    Constant;
165         };
166 };
167
168 /**
169  * \brief Code Block state (stores local variables)
170  */
171 struct sAST_BlockState
172 {
173         tAST_BlockState *Parent;
174         tSpiderScript   *Script;        //!< Script
175         tAST_Variable   *FirstVar;      //!< First variable in the list
176         tSpiderValue    *RetVal;
177         tSpiderNamespace        *BaseNamespace; //!< Base namespace (for entire block)
178         tSpiderNamespace        *CurNamespace;  //!< Currently selected namespace
179          int    Ident;  //!< ID number used for variable lookup caching
180         const char      *BreakTarget;
181          int    BreakType;
182 };
183
184 struct sAST_Variable
185 {
186         tAST_Variable   *Next;
187          int    Type;   // Only used for static typing
188         tSpiderValue    *Object;
189         char    Name[];
190 };
191
192 // === FUNCTIONS ===
193 extern tAST_Script      *AST_NewScript(void);
194 extern size_t   AST_WriteScript(void *Buffer, tSpiderScript *Script);
195 extern size_t   AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node);
196
197 extern int      AST_AppendFunction(tSpiderScript *Script, const char *Name, int ReturnType, tAST_Node *FirstArg, tAST_Node *Code);
198
199 extern tAST_Node        *AST_NewNop(tParser *Parser);
200
201 extern tAST_Node        *AST_NewString(tParser *Parser, const char *String, int Length);
202 extern tAST_Node        *AST_NewInteger(tParser *Parser, int64_t Value);
203 extern tAST_Node        *AST_NewReal(tParser *Parser, double Value);
204 extern tAST_Node        *AST_NewVariable(tParser *Parser, const char *Name);
205 extern tAST_Node        *AST_NewDefineVar(tParser *Parser, int Type, const char *Name);
206 extern tAST_Node        *AST_NewConstant(tParser *Parser, const char *Name);
207 extern tAST_Node        *AST_NewClassElement(tParser *Parser, tAST_Node *Object, const char *Name);
208
209 extern tAST_Node        *AST_NewFunctionCall(tParser *Parser, const char *Name);
210 extern tAST_Node        *AST_NewCreateObject(tParser *Parser, const char *Name);
211 extern tAST_Node        *AST_NewMethodCall(tParser *Parser, tAST_Node *Object, const char *Name);
212 extern void     AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg);
213
214 extern tAST_Node        *AST_NewCodeBlock(tParser *Parser);
215 extern void     AST_AppendNode(tAST_Node *Parent, tAST_Node *Child);
216
217 extern tAST_Node        *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False);
218 extern tAST_Node        *AST_NewLoop(tParser *Parser, const char *Tag, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code);
219
220 extern tAST_Node        *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value);
221 extern tAST_Node        *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value);
222 extern tAST_Node        *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_Node *Right);
223 extern tAST_Node        *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value);
224 extern tAST_Node        *AST_NewBreakout(tParser *Parser, int Type, const char *DestTag);
225 extern tAST_Node        *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node *Child);
226
227 extern void     AST_FreeNode(tAST_Node *Node);
228
229 // exec_ast.h
230 extern void     Object_Dereference(tSpiderValue *Object);
231 extern void     Object_Reference(tSpiderValue *Object);
232 extern tSpiderValue     *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
233 extern tSpiderValue     *AST_ExecuteNode_BinOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Left, tSpiderValue *Right);
234 extern tSpiderValue     *AST_ExecuteNode_UniOp(tSpiderScript *Script, tAST_Node *Node, int Operation, tSpiderValue *Value);
235
236 #endif

UCC git Repository :: git.ucc.asn.au