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

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