SpiderScript: String Casts, Escape Codes, Size for Opaque data types
[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          int    Line;
93         
94         union
95         {
96                 struct {
97                         tAST_Node       *FirstChild;
98                         tAST_Node       *LastChild;
99                 }       Block;
100                 
101                 struct {
102                          int    Operation;
103                         tAST_Node       *Dest;
104                         tAST_Node       *Value;
105                 }       Assign;
106                 
107                 struct {
108                         tAST_Node       *Value;
109                 }       UniOp;
110                 
111                 struct {
112                         tAST_Node       *Left;
113                         tAST_Node       *Right;
114                 }       BinOp;
115                 
116                 struct {
117                          int    Length;
118                         char    Data[];
119                 }       String;
120                 
121                 struct {
122                         tAST_Node       *FirstArg;
123                         tAST_Node       *LastArg;
124                         char    Name[];
125                 }       FunctionCall;
126                 
127                 struct {
128                         tAST_Node       *Condition;
129                         tAST_Node       *True;
130                         tAST_Node       *False;
131                 }       If;
132                 
133                 struct {
134                         tAST_Node       *Init;
135                          int    bCheckAfter;
136                         tAST_Node       *Condition;
137                         tAST_Node       *Increment;
138                         tAST_Node       *Code;
139                 }       For;
140                 
141                 /**
142                  * \note Used for \a NODETYPE_VARIABLE and \a NODETYPE_CONSTANT
143                  */
144                 struct {
145                         char    _unused;        // Shut GCC up
146                         char    Name[];
147                 }       Variable;
148                 
149                 struct {
150                          int    DataType;
151                          int    Depth;
152                         tAST_Node       *LevelSizes;
153                         tAST_Node       *LevelSizes_Last;
154                         char    Name[];
155                 }       DefVar;
156                 
157                 struct {
158                          int    DataType;
159                          tAST_Node      *Value;
160                 }       Cast;
161                 
162                 uint64_t        Integer;
163                 double  Real;
164         };
165 };
166
167 /**
168  * \brief Code Block state (stores local variables)
169  */
170 struct sAST_BlockState
171 {
172         tAST_BlockState *Parent;
173         tSpiderScript   *Script;        //!< Script
174         tAST_Variable   *FirstVar;      //!< First variable in the list
175         tSpiderValue    *RetVal;
176 };
177
178 struct sAST_Variable
179 {
180         tAST_Variable   *Next;
181          int    Type;   // Only used for static typing
182         tSpiderValue    *Object;
183         char    Name[];
184 };
185
186 // === FUNCTIONS ===
187 extern tAST_Script      *AST_NewScript(void);
188
189 extern tAST_Function    *AST_AppendFunction(tAST_Script *Script, const char *Name);
190 extern void     AST_AppendFunctionArg(tAST_Function *Function, tAST_Node *Arg);
191 extern void     AST_SetFunctionCode(tAST_Function *Function, tAST_Node *Root);
192 extern tAST_Node        *AST_NewString(const char *String, int Length);
193 extern tAST_Node        *AST_NewInteger(uint64_t Value);
194 extern tAST_Node        *AST_NewVariable(const char *Name);
195 extern tAST_Node        *AST_NewDefineVar(int Type, const char *Name);
196 extern tAST_Node        *AST_NewConstant(const char *Name);
197 extern tAST_Node        *AST_NewFunctionCall(const char *Name);
198 extern void     AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg);
199
200 extern tAST_Node        *AST_NewCodeBlock(void);
201 extern void     AST_AppendNode(tAST_Node *Parent, tAST_Node *Child);
202
203 extern tAST_Node        *AST_NewIf(tAST_Node *Condition, tAST_Node *True, tAST_Node *False);
204
205 extern tAST_Node        *AST_NewAssign(int Operation, tAST_Node *Dest, tAST_Node *Value);
206 extern tAST_Node        *AST_NewCast(int Target, tAST_Node *Value);
207 extern tAST_Node        *AST_NewBinOp(int Operation, tAST_Node *Left, tAST_Node *Right);
208 extern tAST_Node        *AST_NewUniOp(int Operation, tAST_Node *Value);
209
210 extern void     AST_FreeNode(tAST_Node *Node);
211
212 // exec_ast.h
213 extern void     Object_Dereference(tSpiderValue *Object);
214 extern void     Object_Reference(tSpiderValue *Object);
215 extern tSpiderValue     *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);
216
217 #endif

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