3 * - Script AST Manipulator
11 tAST_Script *AST_NewScript(void)
13 tAST_Script *ret = malloc( sizeof(tAST_Script) );
15 ret->Functions = NULL;
16 ret->LastFunction = NULL;
22 * \brief Append a function to a script
24 tAST_Function *AST_AppendFunction(tAST_Script *Script, const char *Name)
28 ret = malloc( sizeof(tAST_Function) + strlen(Name) + 1 );
30 strcpy(ret->Name, Name);
32 ret->Arguments = NULL;
34 if(Script->LastFunction == NULL) {
35 Script->Functions = Script->LastFunction = ret;
38 Script->LastFunction->Next = ret;
39 Script->LastFunction = ret;
45 void AST_AppendFunctionArg(tAST_Function *Function, tAST_Node *Node)
47 if( !Function->Arguments ) {
48 Function->Arguments_Last = Function->Arguments = Node;
51 Function->Arguments_Last->NextSibling = Node;
52 Function->Arguments_Last = Node;
57 * \brief Set the code for a function
59 void AST_SetFunctionCode(tAST_Function *Function, tAST_Node *Root)
61 Function->Code = Root;
65 * \name Node Manipulation
68 #define WRITE_N(_buffer, _offset, _len, _dataptr) do { \
69 if(_buffer) memcpy((char*)_buffer + _offset, _dataptr, _len);\
73 #define WRITE_8(_buffer, _offset, _val) do {\
75 WRITE_N(_buffer, _offset, 1, &v);\
77 #define WRITE_16(_buffer, _offset, _val) do {\
79 WRITE_N(_buffer, _offset, 2, &v);\
81 #define WRITE_32(_buffer, _offset, _val) do {\
83 WRITE_N(_buffer, _offset, 4, &v);\
85 #define WRITE_64(_buffer, _offset, _val) do {\
87 WRITE_N(_buffer, _offset, 8, &v);\
89 #define WRITE_REAL(_buffer, _offset, _val) do {\
91 WRITE_N(_buffer, _offset, sizeof(double), &v);\
94 #define WRITE_STR(_buffer, _offset, _string) do {\
95 int len = strlen(_string);\
96 WRITE_16(_buffer, _offset, len);\
97 WRITE_N(_buffer, _offset, len, _string);\
98 if((_offset & 1) == 1)WRITE_8(_buffer, _offset, 0); \
99 if((_offset & 3) == 2)WRITE_16(_buffer, _offset, 0); \
101 #define WRITE_NODELIST(_buffer, _offset, _listHead) do {\
104 for(node=(_listHead); node; node = node->NextSibling) {\
106 _offset += AST_WriteNode(_buffer, _offset, node); \
107 WRITE_32(_buffer, ptr, ptr); \
109 if(ptr != -1){ptr -= 4; WRITE_32(_buffer, ptr, 0);} \
113 * \brief Writes a script dump to a buffer
114 * \return Size of encoded data
115 * \note If \a Buffer is NULL, no write is done, but the size is still returned
117 size_t AST_WriteScript(void *Buffer, tAST_Script *Script)
120 size_t ret = 0, ptr = 0;
122 for( fcn = Script->Functions; fcn; fcn = fcn->Next )
125 WRITE_32(Buffer, ret, 0); // Next
126 WRITE_STR(Buffer, ret, fcn->Name);
127 WRITE_NODELIST(Buffer, ret, fcn->Arguments); // TODO: Cheaper way
128 ret += AST_WriteNode(Buffer, ret, fcn->Code);
129 WRITE_32(Buffer, ptr, ret); // Actually set next
134 WRITE_32(Buffer, ptr, 0); // Clear next for final
141 * \brief Write a node to a file
143 size_t AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node)
145 size_t baseOfs = Offset;
148 fprintf(stderr, "Possible Bug - NULL passed to AST_WriteNode\n");
152 WRITE_32(Buffer, Offset, 0); // Next
153 WRITE_16(Buffer, Offset, Node->Type);
154 // TODO: Scan the buffer for the location of the filename (with NULL byte)
155 // else, write the string at the end of the node
156 WRITE_16(Buffer, Offset, Node->Line); // Line
157 //WRITE_32(Buffer, Offset, 0); // File
163 WRITE_NODELIST(Buffer, Offset, Node->Block.FirstChild);
167 case NODETYPE_METHODCALL:
168 Offset += AST_WriteNode(Buffer, Offset, Node->FunctionCall.Object);
169 case NODETYPE_FUNCTIONCALL:
170 case NODETYPE_CREATEOBJECT:
171 // TODO: Search for the same function name and add a pointer
172 WRITE_STR(Buffer, Offset, Node->FunctionCall.Name);
173 WRITE_NODELIST(Buffer, Offset, Node->FunctionCall.FirstArg);
178 Offset += AST_WriteNode(Buffer, Offset, Node->If.Condition);
179 Offset += AST_WriteNode(Buffer, Offset, Node->If.True);
180 Offset += AST_WriteNode(Buffer, Offset, Node->If.False);
183 // Looping Construct (For loop node)
185 WRITE_8(Buffer, Offset, Node->For.bCheckAfter);
187 Offset += AST_WriteNode(Buffer, Offset, Node->For.Init);
188 Offset += AST_WriteNode(Buffer, Offset, Node->For.Condition);
189 Offset += AST_WriteNode(Buffer, Offset, Node->For.Increment);
190 Offset += AST_WriteNode(Buffer, Offset, Node->For.Code);
194 case NODETYPE_ASSIGN:
195 WRITE_8(Buffer, Offset, Node->Assign.Operation);
196 Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Dest);
197 Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Value);
202 WRITE_8(Buffer, Offset, Node->Cast.DataType);
203 Offset += AST_WriteNode(Buffer, Offset, Node->Cast.Value);
207 case NODETYPE_DEFVAR:
208 WRITE_8(Buffer, Offset, Node->DefVar.DataType);
209 // TODO: Duplicate compress the strings
210 WRITE_STR(Buffer, Offset, Node->DefVar.Name);
212 WRITE_NODELIST(Buffer, Offset, Node->DefVar.LevelSizes);
217 case NODETYPE_ELEMENT:
218 WRITE_STR(Buffer, Offset, Node->Scope.Name);
219 Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
223 case NODETYPE_RETURN:
225 case NODETYPE_LOGICALNOT:
226 case NODETYPE_NEGATE:
227 case NODETYPE_POSTINC:
228 case NODETYPE_POSTDEC:
229 Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
235 case NODETYPE_SUBTRACT:
236 case NODETYPE_MULTIPLY:
237 case NODETYPE_DIVIDE:
238 case NODETYPE_MODULO:
239 case NODETYPE_BITSHIFTLEFT:
240 case NODETYPE_BITSHIFTRIGHT:
241 case NODETYPE_BITROTATELEFT:
242 case NODETYPE_BWAND: case NODETYPE_LOGICALAND:
243 case NODETYPE_BWOR: case NODETYPE_LOGICALOR:
244 case NODETYPE_BWXOR: case NODETYPE_LOGICALXOR:
245 case NODETYPE_EQUALS:
246 case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
247 case NODETYPE_GREATERTHAN: case NODETYPE_GREATERTHANEQUAL:
248 Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Left);
249 Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Right);
252 // Node types with no children
255 case NODETYPE_VARIABLE:
256 case NODETYPE_CONSTANT:
257 // TODO: De-Duplicate the strings
258 WRITE_STR(Buffer, Offset, Node->Variable.Name);
260 case NODETYPE_STRING:
261 WRITE_32(Buffer, Offset, Node->Constant.String.Length);
262 WRITE_N(Buffer, Offset, Node->Constant.String.Length, Node->Constant.String.Data);
264 case NODETYPE_INTEGER:
265 WRITE_64(Buffer, Offset, Node->Constant.Integer);
268 WRITE_REAL(Buffer, Offset, Node->Constant.Real);
272 // fprintf(stderr, "AST_WriteNode: Unknown node type %i\n", Node->Type);
276 return Offset - baseOfs;
280 * \brief Free a node and all subnodes
282 void AST_FreeNode(tAST_Node *Node)
292 for( node = Node->Block.FirstChild; node; )
294 tAST_Node *savedNext = node->NextSibling;
301 case NODETYPE_METHODCALL:
302 AST_FreeNode(Node->FunctionCall.Object);
303 case NODETYPE_FUNCTIONCALL:
304 case NODETYPE_CREATEOBJECT:
305 for( node = Node->FunctionCall.FirstArg; node; )
307 tAST_Node *savedNext = node->NextSibling;
315 AST_FreeNode(Node->If.Condition);
316 AST_FreeNode(Node->If.True);
317 AST_FreeNode(Node->If.False);
320 // Looping Construct (For loop node)
322 AST_FreeNode(Node->For.Init);
323 AST_FreeNode(Node->For.Condition);
324 AST_FreeNode(Node->For.Increment);
325 AST_FreeNode(Node->For.Code);
329 case NODETYPE_ASSIGN:
330 AST_FreeNode(Node->Assign.Dest);
331 AST_FreeNode(Node->Assign.Value);
336 AST_FreeNode(Node->Cast.Value);
340 case NODETYPE_ELEMENT:
341 AST_FreeNode(Node->Scope.Element);
345 case NODETYPE_DEFVAR:
346 for( node = Node->DefVar.LevelSizes; node; )
348 tAST_Node *savedNext = node->NextSibling;
355 case NODETYPE_RETURN:
357 case NODETYPE_LOGICALNOT:
358 case NODETYPE_NEGATE:
359 case NODETYPE_POSTINC:
360 case NODETYPE_POSTDEC:
361 AST_FreeNode(Node->UniOp.Value);
367 case NODETYPE_SUBTRACT:
368 case NODETYPE_MULTIPLY:
369 case NODETYPE_DIVIDE:
370 case NODETYPE_MODULO:
371 case NODETYPE_BITSHIFTLEFT:
372 case NODETYPE_BITSHIFTRIGHT:
373 case NODETYPE_BITROTATELEFT:
374 case NODETYPE_BWAND: case NODETYPE_LOGICALAND:
375 case NODETYPE_BWOR: case NODETYPE_LOGICALOR:
376 case NODETYPE_BWXOR: case NODETYPE_LOGICALXOR:
377 case NODETYPE_EQUALS:
378 case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
379 case NODETYPE_GREATERTHAN: case NODETYPE_GREATERTHANEQUAL:
380 AST_FreeNode( Node->BinOp.Left );
381 AST_FreeNode( Node->BinOp.Right );
384 // Node types with no children
385 case NODETYPE_NOP: break;
386 case NODETYPE_VARIABLE: break;
387 case NODETYPE_CONSTANT: break;
389 case NODETYPE_STRING:
390 case NODETYPE_INTEGER:
392 if( Node->ValueCache )
393 Object_Dereference(Node->ValueCache);
394 Node->ValueCache = NULL;
400 tAST_Node *AST_int_AllocateNode(tParser *Parser, int Type, int ExtraSize)
402 tAST_Node *ret = malloc( sizeof(tAST_Node) + ExtraSize );
403 ret->NextSibling = NULL;
405 ret->Line = Parser->CurLine;
409 ret->BlockState = NULL;
411 ret->ValueCache = NULL;
416 tAST_Node *AST_NewCodeBlock(tParser *Parser)
418 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_BLOCK, 0 );
420 ret->Block.FirstChild = NULL;
421 ret->Block.LastChild = NULL;
426 void AST_AppendNode(tAST_Node *Parent, tAST_Node *Child)
428 Child->NextSibling = NULL;
429 switch( Parent->Type )
432 if(Parent->Block.FirstChild == NULL) {
433 Parent->Block.FirstChild = Parent->Block.LastChild = Child;
436 Parent->Block.LastChild->NextSibling = Child;
437 Parent->Block.LastChild = Child;
440 case NODETYPE_DEFVAR:
441 if(Parent->DefVar.LevelSizes == NULL) {
442 Parent->DefVar.LevelSizes = Parent->DefVar.LevelSizes_Last = Child;
445 Parent->DefVar.LevelSizes_Last->NextSibling = Child;
446 Parent->DefVar.LevelSizes_Last = Child;
450 fprintf(stderr, "BUG REPORT: AST_AppendNode on an invalid node type (%i)\n", Parent->Type);
455 tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False)
457 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_IF, 0);
458 ret->If.Condition = Condition;
460 ret->If.False = False;
464 tAST_Node *AST_NewLoop(tParser *Parser, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code)
466 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_LOOP, 0);
467 ret->For.Init = Init;
468 ret->For.bCheckAfter = !!bPostCheck;
469 ret->For.Condition = Condition;
470 ret->For.Increment = Increment;
471 ret->For.Code = Code;
475 tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value)
477 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_ASSIGN, 0);
479 ret->Assign.Operation = Operation;
480 ret->Assign.Dest = Dest;
481 ret->Assign.Value = Value;
486 tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value)
488 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CAST, 0);
490 ret->Cast.DataType = Target;
491 ret->Cast.Value = Value;
496 tAST_Node *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_Node *Right)
498 tAST_Node *ret = AST_int_AllocateNode(Parser, Operation, 0);
500 ret->BinOp.Left = Left;
501 ret->BinOp.Right = Right;
508 tAST_Node *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value)
510 tAST_Node *ret = AST_int_AllocateNode(Parser, Operation, 0);
512 ret->UniOp.Value = Value;
517 tAST_Node *AST_NewNop(tParser *Parser)
519 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_NOP, 0);
525 * \brief Create a new string node
527 tAST_Node *AST_NewString(tParser *Parser, const char *String, int Length)
529 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_STRING, Length + 1);
531 ret->Constant.Type = SS_DATATYPE_STRING;
532 ret->Constant.ReferenceCount = 1;
533 ret->Constant.String.Length = Length;
534 memcpy(ret->Constant.String.Data, String, Length);
535 ret->Constant.String.Data[Length] = '\0';
541 * \brief Create a new integer node
543 tAST_Node *AST_NewInteger(tParser *Parser, int64_t Value)
545 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_INTEGER, 0);
546 ret->Constant.Type = SS_DATATYPE_INTEGER;
547 ret->Constant.ReferenceCount = 1;
548 ret->Constant.Integer = Value;
553 * \brief Create a new real number node
555 tAST_Node *AST_NewReal(tParser *Parser, double Value)
557 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_REAL, 0);
558 ret->Constant.Type = SS_DATATYPE_REAL;
559 ret->Constant.ReferenceCount = 1;
560 ret->Constant.Real = Value;
565 * \brief Create a new variable reference node
567 tAST_Node *AST_NewVariable(tParser *Parser, const char *Name)
569 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_VARIABLE, strlen(Name) + 1 );
570 strcpy(ret->Variable.Name, Name);
575 * \brief Create a new variable definition node
577 tAST_Node *AST_NewDefineVar(tParser *Parser, int Type, const char *Name)
579 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_DEFVAR, strlen(Name) + 1 );
581 ret->DefVar.DataType = Type;
582 ret->DefVar.LevelSizes = NULL;
583 strcpy(ret->DefVar.Name, Name);
589 * \brief Create a new runtime constant reference node
591 tAST_Node *AST_NewConstant(tParser *Parser, const char *Name)
593 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CONSTANT, strlen(Name) + 1 );
595 strcpy(ret->Variable.Name, Name);
601 * \brief Create a function call node
602 * \note Argument list is manipulated using AST_AppendFunctionCallArg
604 tAST_Node *AST_NewFunctionCall(tParser *Parser, const char *Name)
606 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_FUNCTIONCALL, strlen(Name) + 1 );
608 ret->FunctionCall.Object = NULL;
609 ret->FunctionCall.FirstArg = NULL;
610 ret->FunctionCall.LastArg = NULL;
611 ret->FunctionCall.NumArgs = 0;
612 strcpy(ret->FunctionCall.Name, Name);
616 tAST_Node *AST_NewMethodCall(tParser *Parser, tAST_Node *Object, const char *Name)
618 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_METHODCALL, strlen(Name) + 1 );
620 ret->FunctionCall.Object = Object;
621 ret->FunctionCall.FirstArg = NULL;
622 ret->FunctionCall.LastArg = NULL;
623 ret->FunctionCall.NumArgs = 0;
624 strcpy(ret->FunctionCall.Name, Name);
629 tAST_Node *AST_NewCreateObject(tParser *Parser, const char *Name)
631 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CREATEOBJECT, strlen(Name) + 1 );
633 ret->FunctionCall.Object = NULL;
634 ret->FunctionCall.FirstArg = NULL;
635 ret->FunctionCall.LastArg = NULL;
636 ret->FunctionCall.NumArgs = 0;
637 strcpy(ret->FunctionCall.Name, Name);
643 * \brief Append an argument to a function call
645 void AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg)
647 if( Node->Type != NODETYPE_FUNCTIONCALL
648 && Node->Type != NODETYPE_CREATEOBJECT
649 && Node->Type != NODETYPE_METHODCALL)
651 fprintf(stderr, "BUG REPORT: AST_AppendFunctionCallArg on an invalid node type (%i)\n", Node->Type);
655 if(Node->FunctionCall.LastArg) {
656 Node->FunctionCall.LastArg->NextSibling = Arg;
657 Node->FunctionCall.LastArg = Arg;
660 Node->FunctionCall.FirstArg = Arg;
661 Node->FunctionCall.LastArg = Arg;
663 Node->FunctionCall.NumArgs ++;
667 * \brief Add a scope node
669 tAST_Node *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node *Child)
671 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_SCOPE, strlen(Name) + 1 );
672 ret->Scope.Element = Child;
673 strcpy(ret->Scope.Name, Name);
678 * \brief Add a scope node
680 tAST_Node *AST_NewClassElement(tParser *Parser, tAST_Node *Object, const char *Name)
682 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_ELEMENT, strlen(Name) + 1 );
683 ret->Scope.Element = Object;
684 strcpy(ret->Scope.Name, Name);