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

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