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

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