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

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