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

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