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

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