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

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