Added if() statement (and internal support for for,while and do{}while())
[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
8 typedef enum eAST_NodeTypes     tAST_NodeType;
9 typedef struct sAST_Script      tAST_Script;
10 typedef struct sAST_Function    tAST_Function;
11 typedef struct sAST_Node        tAST_Node;
12 typedef struct sAST_BlockState  tAST_BlockState;
13 typedef struct sAST_Variable    tAST_Variable;
14
15 /**
16  * \brief Node Types
17  */
18 enum eAST_NodeTypes
19 {
20         NODETYPE_NOP,
21         
22         NODETYPE_BLOCK, //!< Node Block
23         
24         NODETYPE_VARIABLE,      //!< Variable
25         NODETYPE_CONSTANT,      //!< Runtime Constant
26         NODETYPE_STRING,        //!< String Constant
27         NODETYPE_INTEGER,       //!< Integer Constant
28         NODETYPE_REAL,  //!< Real Constant
29         
30         NODETYPE_DEFVAR,        //!< Define a variable (Variable)
31         NODETYPE_CAST,  //!< Cast a value to another (Uniop)
32         
33         NODETYPE_RETURN,        //!< Return from a function (reserved word)
34         NODETYPE_ASSIGN,        //!< Variable assignment operator
35         NODETYPE_FUNCTIONCALL,  //!< Call a function
36         
37         NODETYPE_IF,    //!< Conditional
38         NODETYPE_LOOP,  //!< Looping Construct
39         
40         NODETYPE_INDEX, //!< Index into an array
41         
42         NODETYPE_LOGICALAND,    //!< Logical AND operator
43         NODETYPE_LOGICALOR,     //!< Logical OR operator
44         NODETYPE_LOGICALXOR,    //!< Logical XOR operator
45         
46         NODETYPE_EQUALS,        //!< Comparison Equals
47         NODETYPE_LESSTHAN,      //!< Comparison Less Than
48         NODETYPE_GREATERTHAN,   //!< Comparison Greater Than
49         
50         NODETYPE_BWAND, //!< Bitwise AND
51         NODETYPE_BWOR,  //!< Bitwise OR
52         NODETYPE_BWXOR, //!< Bitwise XOR
53         
54         NODETYPE_BITSHIFTLEFT,  //!< Bitwise Shift Left (Grow)
55         NODETYPE_BITSHIFTRIGHT, //!< Bitwise Shift Right (Shrink)
56         NODETYPE_BITROTATELEFT, //!< Bitwise Rotate Left (Grow)
57         
58         NODETYPE_ADD,   //!< Add
59         NODETYPE_SUBTRACT,      //!< Subtract
60         NODETYPE_MULTIPLY,      //!< Multiply
61         NODETYPE_DIVIDE,        //!< Divide
62         NODETYPE_MODULO,        //!< Modulus
63 };
64
65 struct sSpiderScript
66 {
67         tSpiderVariant  *Variant;
68         tAST_Script     *Script;
69         char    *CurNamespace;  //!< Current namespace prefix (NULL = Root) - No trailing .
70 };
71
72 struct sAST_Script
73 {
74         tAST_Function   *Functions;
75         tAST_Function   *LastFunction;
76 };
77
78 struct sAST_Function
79 {
80         tAST_Function   *Next;  //!< Next function in list
81         tAST_Node       *Code;  //!< Function Code
82         tAST_Node       *Arguments;     // HACKJOB (Only NODETYPE_DEFVAR is allowed)
83         tAST_Node       *Arguments_Last;
84         char    Name[]; //!< Function Name
85 };
86
87 struct sAST_Node
88 {
89         tAST_Node       *NextSibling;
90         tAST_NodeType   Type;
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                          int    Length;
116                         char    Data[];
117                 }       String;
118                 
119                 struct {
120                         tAST_Node       *FirstArg;
121                         tAST_Node       *LastArg;
122                         char    Name[];
123                 }       FunctionCall;
124                 
125                 struct {
126                         tAST_Node       *Condition;
127                         tAST_Node       *True;
128                         tAST_Node       *False;
129                 }       If;
130                 
131                 struct {
132                         tAST_Node       *Init;
133                          int    bCheckAfter;
134                         tAST_Node       *Condition;
135                         tAST_Node       *Increment;
136                         tAST_Node       *Code;
137                 }       For;
138                 
139                 /**
140                  * \note Used for \a NODETYPE_VARIABLE and \a NODETYPE_CONSTANT
141                  */
142                 struct {
143                         char    _unused;        // Shut GCC up
144                         char    Name[];
145                 }       Variable;
146                 
147                 struct {
148                          int    DataType;
149                          int    Depth;
150                         tAST_Node       *LevelSizes;
151                         tAST_Node       *LevelSizes_Last;
152                         char    Name[];
153                 }       DefVar;
154                 
155                 struct {
156                          int    DataType;
157                          tAST_Node      *Value;
158                 }       Cast;
159                 
160                 uint64_t        Integer;
161                 double  Real;
162         };
163 };
164
165 /**
166  * \brief Code Block state (stores local variables)
167  */
168 struct sAST_BlockState
169 {
170         tAST_BlockState *Parent;
171         tSpiderScript   *Script;        //!< Script
172         tAST_Variable   *FirstVar;      //!< First variable in the list
173         tSpiderValue    *RetVal;
174 };
175
176 struct sAST_Variable
177 {
178         tAST_Variable   *Next;
179          int    Type;   // Only used for static typing
180         tSpiderValue    *Object;
181         char    Name[];
182 };
183
184 // === FUNCTIONS ===
185 extern tAST_Script      *AST_NewScript(void);
186
187 extern tAST_Function    *AST_AppendFunction(tAST_Script *Script, const char *Name);
188 extern void     AST_AppendFunctionArg(tAST_Function *Function, tAST_Node *Arg);
189 extern void     AST_SetFunctionCode(tAST_Function *Function, tAST_Node *Root);
190
191 extern tAST_Node        *AST_NewString(const char *String, int Length);
192 extern tAST_Node        *AST_NewInteger(uint64_t Value);
193 extern tAST_Node        *AST_NewVariable(const char *Name);
194 extern tAST_Node        *AST_NewDefineVar(int Type, const char *Name);
195 extern tAST_Node        *AST_NewConstant(const char *Name);
196 extern tAST_Node        *AST_NewFunctionCall(const char *Name);
197 extern void     AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg);
198
199 extern tAST_Node        *AST_NewCodeBlock(void);
200 extern void     AST_AppendNode(tAST_Node *Parent, tAST_Node *Child);
201
202 extern tAST_Node        *AST_NewIf(tAST_Node *Condition, tAST_Node *True, tAST_Node *False);
203
204 extern tAST_Node        *AST_NewAssign(int Operation, tAST_Node *Dest, tAST_Node *Value);
205 extern tAST_Node        *AST_NewBinOp(int Operation, tAST_Node *Left, tAST_Node *Right);
206 extern tAST_Node        *AST_NewUniOp(int Operation, tAST_Node *Value);
207
208 extern void     AST_FreeNode(tAST_Node *Node);
209
210 // exec_ast.h
211 extern tSpiderValue     *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
212
213 #endif

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