6 #include <spiderscript.h>
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;
23 NODETYPE_BLOCK, //!< Node Block
25 NODETYPE_VARIABLE, //!< Variable
26 NODETYPE_CONSTANT, //!< Runtime Constant
27 NODETYPE_STRING, //!< String Constant
28 NODETYPE_INTEGER, //!< Integer Constant
29 NODETYPE_REAL, //!< Real Constant
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)
36 NODETYPE_RETURN, //!< Return from a function (reserved word)
37 NODETYPE_ASSIGN, //!< Variable assignment operator
38 NODETYPE_FUNCTIONCALL, //!< Call a function
39 NODETYPE_METHODCALL, //!< Call a class method
40 NODETYPE_CREATEOBJECT, //!< Create an object
42 NODETYPE_IF, //!< Conditional
43 NODETYPE_LOOP, //!< Looping Construct
45 NODETYPE_INDEX, //!< Index into an array
47 NODETYPE_LOGICALAND, //!< Logical AND operator
48 NODETYPE_LOGICALOR, //!< Logical OR operator
49 NODETYPE_LOGICALXOR, //!< Logical XOR operator
51 NODETYPE_EQUALS, //!< Comparison Equals
52 NODETYPE_LESSTHAN, //!< Comparison Less Than
53 NODETYPE_GREATERTHAN, //!< Comparison Greater Than
55 NODETYPE_BWAND, //!< Bitwise AND
56 NODETYPE_BWOR, //!< Bitwise OR
57 NODETYPE_BWXOR, //!< Bitwise XOR
59 NODETYPE_BITSHIFTLEFT, //!< Bitwise Shift Left (Grow)
60 NODETYPE_BITSHIFTRIGHT, //!< Bitwise Shift Right (Shrink)
61 NODETYPE_BITROTATELEFT, //!< Bitwise Rotate Left (Grow)
63 NODETYPE_ADD, //!< Add
64 NODETYPE_SUBTRACT, //!< Subtract
65 NODETYPE_MULTIPLY, //!< Multiply
66 NODETYPE_DIVIDE, //!< Divide
67 NODETYPE_MODULO, //!< Modulus
72 tSpiderVariant *Variant;
74 char *CurNamespace; //!< Current namespace prefix (NULL = Root) - No trailing .
79 // TODO: Namespaces and Classes
80 tAST_Function *Functions;
81 tAST_Function *LastFunction;
86 tAST_Function *Next; //!< Next function in list
87 tAST_Node *Code; //!< Function Code
88 tAST_Node *Arguments; // HACKJOB (Only NODETYPE_DEFVAR is allowed)
89 tAST_Node *Arguments_Last;
90 char Name[]; //!< Function Name
95 tAST_Node *NextSibling;
104 tAST_Node *FirstChild;
105 tAST_Node *LastChild;
131 tAST_Node *Condition;
139 tAST_Node *Condition;
140 tAST_Node *Increment;
145 * \note Used for \a NODETYPE_VARIABLE, \a NODETYPE_CONSTANT and
149 char _unused; // Shut GCC up
156 } Scope; // Used by NODETYPE_SCOPE and NODETYPE_ELEMENT
160 tAST_Node *LevelSizes;
161 tAST_Node *LevelSizes_Last;
181 * \brief Code Block state (stores local variables)
183 struct sAST_BlockState
185 tAST_BlockState *Parent;
186 tSpiderScript *Script; //!< Script
187 tAST_Variable *FirstVar; //!< First variable in the list
188 tSpiderValue *RetVal;
189 tSpiderNamespace *BaseNamespace; //!< Base namespace (for entire block)
190 tSpiderNamespace *CurNamespace; //!< Currently selected namespace
196 int Type; // Only used for static typing
197 tSpiderValue *Object;
202 extern tAST_Script *AST_NewScript(void);
203 extern size_t AST_WriteScript(void *Buffer, tAST_Script *Script);
204 extern size_t AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node);
206 extern tAST_Function *AST_AppendFunction(tAST_Script *Script, const char *Name);
207 extern void AST_AppendFunctionArg(tAST_Function *Function, tAST_Node *Arg);
208 extern void AST_SetFunctionCode(tAST_Function *Function, tAST_Node *Root);
210 extern tAST_Node *AST_NewString(tParser *Parser, const char *String, int Length);
211 extern tAST_Node *AST_NewInteger(tParser *Parser, uint64_t Value);
212 extern tAST_Node *AST_NewVariable(tParser *Parser, const char *Name);
213 extern tAST_Node *AST_NewDefineVar(tParser *Parser, int Type, const char *Name);
214 extern tAST_Node *AST_NewConstant(tParser *Parser, const char *Name);
215 extern tAST_Node *AST_NewClassElement(tParser *Parser, tAST_Node *Object, const char *Name);
217 extern tAST_Node *AST_NewFunctionCall(tParser *Parser, const char *Name);
218 extern tAST_Node *AST_NewCreateObject(tParser *Parser, const char *Name);
219 extern tAST_Node *AST_NewMethodCall(tParser *Parser, tAST_Node *Object, const char *Name);
220 extern void AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg);
222 extern tAST_Node *AST_NewCodeBlock(tParser *Parser);
223 extern void AST_AppendNode(tAST_Node *Parent, tAST_Node *Child);
225 extern tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False);
226 extern tAST_Node *AST_NewLoop(tParser *Parser, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code);
228 extern tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value);
229 extern tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value);
230 extern tAST_Node *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_Node *Right);
231 extern tAST_Node *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value);
232 extern tAST_Node *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node *Child);
234 extern void AST_FreeNode(tAST_Node *Node);
237 extern void Object_Dereference(tSpiderValue *Object);
238 extern void Object_Reference(tSpiderValue *Object);
239 extern tSpiderValue *AST_ExecuteNode(tAST_BlockState *Block, tAST_Node *Node);