3 * - Script AST Manipulator
12 extern void SyntaxError(tParser *Parser, int bFatal, const char *Message, ...);
16 * \brief Append a function to a script
18 int AST_AppendFunction(tSpiderScript *Script, const char *Name, int ReturnType, tAST_Node *Args, tAST_Node *Code)
20 tScript_Function *fcn;
21 int arg_count = 0, arg_bytes = 0;
24 // Count and size arguments
25 for(arg = Args; arg; arg = arg->NextSibling)
28 arg_bytes += sizeof(fcn->Arguments[0]) + strlen(Args->DefVar.Name) + 1;
31 // Allocate information
32 fcn = malloc( sizeof(tScript_Function) + arg_bytes + strlen(Name) + 1 );
35 fcn->Name = (char*)&fcn->Arguments[arg_count];
36 strcpy(fcn->Name, Name);
37 fcn->ReturnType = ReturnType;
38 fcn->ArgumentCount = arg_count;
43 arg_bytes = strlen(Name) + 1; // Used as an offset into fcn->Name
45 for(arg = Args; arg; arg = arg->NextSibling)
47 fcn->Arguments[arg_count].Name = fcn->Name + arg_bytes;
48 strcpy(fcn->Arguments[arg_count].Name, arg->DefVar.Name);
49 fcn->Arguments[arg_count].Type = arg->DefVar.DataType;
50 arg_bytes += strlen(arg->DefVar.Name) + 1;
54 if(Script->LastFunction == NULL) {
55 Script->Functions = Script->LastFunction = fcn;
58 Script->LastFunction->Next = fcn;
59 Script->LastFunction = fcn;
66 * \name Node Manipulation
69 #define WRITE_N(_buffer, _offset, _len, _dataptr) do { \
70 if(_buffer) memcpy((char*)_buffer + _offset, _dataptr, _len);\
74 #define WRITE_8(_buffer, _offset, _val) do {\
76 WRITE_N(_buffer, _offset, 1, &v);\
78 #define WRITE_16(_buffer, _offset, _val) do {\
80 WRITE_N(_buffer, _offset, 2, &v);\
82 #define WRITE_32(_buffer, _offset, _val) do {\
84 WRITE_N(_buffer, _offset, 4, &v);\
86 #define WRITE_64(_buffer, _offset, _val) do {\
88 WRITE_N(_buffer, _offset, 8, &v);\
90 #define WRITE_REAL(_buffer, _offset, _val) do {\
92 WRITE_N(_buffer, _offset, sizeof(double), &v);\
95 #define WRITE_STR(_buffer, _offset, _string) do {\
96 int len = strlen(_string);\
97 WRITE_16(_buffer, _offset, len);\
98 WRITE_N(_buffer, _offset, len, _string);\
99 if((_offset & 1) == 1)WRITE_8(_buffer, _offset, 0); \
100 if((_offset & 3) == 2)WRITE_16(_buffer, _offset, 0); \
102 #define WRITE_NODELIST(_buffer, _offset, _listHead) do {\
105 for(node=(_listHead); node; node = node->NextSibling) {\
107 _offset += AST_WriteNode(_buffer, _offset, node); \
108 WRITE_32(_buffer, ptr, ptr); \
110 if(ptr != -1){ptr -= 4; WRITE_32(_buffer, ptr, 0);} \
114 * \brief Writes a script dump to a buffer
115 * \return Size of encoded data
116 * \note If \a Buffer is NULL, no write is done, but the size is still returned
118 size_t AST_WriteScript(void *Buffer, tSpiderScript *Script)
120 tScript_Function *fcn;
121 size_t ret = 0, ptr = 0;
124 for( fcn = Script->Functions; fcn; fcn = fcn->Next )
126 // printf("fcn = %p, fcn->Name = %p\n", fcn, fcn->Name);
128 WRITE_32(Buffer, ret, 0); // Next
129 WRITE_STR(Buffer, ret, fcn->Name);
130 WRITE_32(Buffer, ret, fcn->ArgumentCount);
131 for( i = 0; i < fcn->ArgumentCount; i ++ )
133 WRITE_16(Buffer, ret, fcn->Arguments[i].Type);
134 WRITE_STR(Buffer, ret, fcn->Arguments[i].Name);
136 ret += AST_WriteNode(Buffer, ret, fcn->ASTFcn);
137 WRITE_32(Buffer, ptr, ret); // Actually set `Next`
142 WRITE_32(Buffer, ptr, 0); // Clear next for final
149 * \brief Write a node to a file
151 size_t AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node)
153 size_t baseOfs = Offset;
156 //fprintf(stderr, "Possible Bug - NULL passed to AST_WriteNode\n");
157 WRITE_32(Buffer, Offset, 0);
158 WRITE_16(Buffer, Offset, NODETYPE_NOP);
159 WRITE_16(Buffer, Offset, 0); // Line (0)
163 WRITE_32(Buffer, Offset, 0); // Next
164 WRITE_16(Buffer, Offset, Node->Type);
165 // TODO: Scan the buffer for the location of the filename (with NULL byte)
166 // else, write the string at the end of the node
167 WRITE_16(Buffer, Offset, Node->Line); // Line
168 //WRITE_32(Buffer, Offset, 0); // File
174 WRITE_NODELIST(Buffer, Offset, Node->Block.FirstChild);
178 case NODETYPE_METHODCALL:
179 Offset += AST_WriteNode(Buffer, Offset, Node->FunctionCall.Object);
180 case NODETYPE_FUNCTIONCALL:
181 case NODETYPE_CREATEOBJECT:
182 // TODO: Search for the same function name and add a pointer
183 WRITE_STR(Buffer, Offset, Node->FunctionCall.Name);
184 WRITE_NODELIST(Buffer, Offset, Node->FunctionCall.FirstArg);
189 Offset += AST_WriteNode(Buffer, Offset, Node->If.Condition);
190 Offset += AST_WriteNode(Buffer, Offset, Node->If.True);
191 Offset += AST_WriteNode(Buffer, Offset, Node->If.False);
194 // Looping Construct (For loop node)
196 WRITE_8(Buffer, Offset, Node->For.bCheckAfter);
197 WRITE_STR(Buffer, Offset, Node->For.Tag);
198 Offset += AST_WriteNode(Buffer, Offset, Node->For.Init);
199 Offset += AST_WriteNode(Buffer, Offset, Node->For.Condition);
200 Offset += AST_WriteNode(Buffer, Offset, Node->For.Increment);
201 Offset += AST_WriteNode(Buffer, Offset, Node->For.Code);
205 case NODETYPE_ASSIGN:
206 WRITE_8(Buffer, Offset, Node->Assign.Operation);
207 Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Dest);
208 Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Value);
213 WRITE_8(Buffer, Offset, Node->Cast.DataType);
214 Offset += AST_WriteNode(Buffer, Offset, Node->Cast.Value);
218 case NODETYPE_DEFVAR:
219 WRITE_8(Buffer, Offset, Node->DefVar.DataType);
220 // TODO: Duplicate compress the strings
221 WRITE_STR(Buffer, Offset, Node->DefVar.Name);
223 WRITE_NODELIST(Buffer, Offset, Node->DefVar.LevelSizes);
224 Offset += AST_WriteNode(Buffer, Offset, Node->DefVar.InitialValue);
229 case NODETYPE_ELEMENT:
230 WRITE_STR(Buffer, Offset, Node->Scope.Name);
231 Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
235 case NODETYPE_RETURN:
237 case NODETYPE_LOGICALNOT:
238 case NODETYPE_NEGATE:
239 case NODETYPE_POSTINC:
240 case NODETYPE_POSTDEC:
241 Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
247 case NODETYPE_SUBTRACT:
248 case NODETYPE_MULTIPLY:
249 case NODETYPE_DIVIDE:
250 case NODETYPE_MODULO:
251 case NODETYPE_BITSHIFTLEFT:
252 case NODETYPE_BITSHIFTRIGHT:
253 case NODETYPE_BITROTATELEFT:
254 case NODETYPE_BWAND: case NODETYPE_LOGICALAND:
255 case NODETYPE_BWOR: case NODETYPE_LOGICALOR:
256 case NODETYPE_BWXOR: case NODETYPE_LOGICALXOR:
257 case NODETYPE_EQUALS:
258 case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
259 case NODETYPE_GREATERTHAN: case NODETYPE_GREATERTHANEQUAL:
260 Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Left);
261 Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Right);
264 // Node types with no children
267 case NODETYPE_VARIABLE:
268 case NODETYPE_CONSTANT:
270 case NODETYPE_CONTINUE:
271 // TODO: De-Duplicate the strings
272 WRITE_STR(Buffer, Offset, Node->Variable.Name);
274 case NODETYPE_STRING:
275 WRITE_32(Buffer, Offset, Node->Constant.String.Length);
276 WRITE_N(Buffer, Offset, Node->Constant.String.Length, Node->Constant.String.Data);
278 case NODETYPE_INTEGER:
279 WRITE_64(Buffer, Offset, Node->Constant.Integer);
282 WRITE_REAL(Buffer, Offset, Node->Constant.Real);
286 // fprintf(stderr, "AST_WriteNode: Unknown node type %i\n", Node->Type);
290 return Offset - baseOfs;
294 * \brief Free a node and all subnodes
296 void AST_FreeNode(tAST_Node *Node)
302 // Referenced counted file name
303 (*(int*)(Node->File - sizeof(int))) -= 1;
304 if( *(int*)(Node->File - sizeof(int)) == 0 )
305 free( (void*)(Node->File - sizeof(int)) );
311 for( node = Node->Block.FirstChild; node; )
313 tAST_Node *savedNext = node->NextSibling;
320 case NODETYPE_METHODCALL:
321 AST_FreeNode(Node->FunctionCall.Object);
322 case NODETYPE_FUNCTIONCALL:
323 case NODETYPE_CREATEOBJECT:
324 for( node = Node->FunctionCall.FirstArg; node; )
326 tAST_Node *savedNext = node->NextSibling;
334 AST_FreeNode(Node->If.Condition);
335 AST_FreeNode(Node->If.True);
336 AST_FreeNode(Node->If.False);
339 // Looping Construct (For loop node)
341 AST_FreeNode(Node->For.Init);
342 AST_FreeNode(Node->For.Condition);
343 AST_FreeNode(Node->For.Increment);
344 AST_FreeNode(Node->For.Code);
348 case NODETYPE_ASSIGN:
349 AST_FreeNode(Node->Assign.Dest);
350 AST_FreeNode(Node->Assign.Value);
355 AST_FreeNode(Node->Cast.Value);
359 case NODETYPE_ELEMENT:
360 AST_FreeNode(Node->Scope.Element);
364 case NODETYPE_DEFVAR:
365 for( node = Node->DefVar.LevelSizes; node; )
367 tAST_Node *savedNext = node->NextSibling;
371 AST_FreeNode(Node->DefVar.InitialValue);
375 case NODETYPE_RETURN:
377 case NODETYPE_LOGICALNOT:
378 case NODETYPE_NEGATE:
379 case NODETYPE_POSTINC:
380 case NODETYPE_POSTDEC:
381 AST_FreeNode(Node->UniOp.Value);
387 case NODETYPE_SUBTRACT:
388 case NODETYPE_MULTIPLY:
389 case NODETYPE_DIVIDE:
390 case NODETYPE_MODULO:
391 case NODETYPE_BITSHIFTLEFT:
392 case NODETYPE_BITSHIFTRIGHT:
393 case NODETYPE_BITROTATELEFT:
394 case NODETYPE_BWAND: case NODETYPE_LOGICALAND:
395 case NODETYPE_BWOR: case NODETYPE_LOGICALOR:
396 case NODETYPE_BWXOR: case NODETYPE_LOGICALXOR:
397 case NODETYPE_EQUALS:
398 case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
399 case NODETYPE_GREATERTHAN: case NODETYPE_GREATERTHANEQUAL:
400 AST_FreeNode( Node->BinOp.Left );
401 AST_FreeNode( Node->BinOp.Right );
404 // Node types with no children
405 case NODETYPE_NOP: break;
406 case NODETYPE_VARIABLE: break;
407 case NODETYPE_CONSTANT: break;
409 case NODETYPE_CONTINUE: break;
411 case NODETYPE_STRING:
412 case NODETYPE_INTEGER:
414 if( Node->ValueCache )
415 SpiderScript_DereferenceValue(Node->ValueCache);
416 Node->ValueCache = NULL;
422 tAST_Node *AST_int_AllocateNode(tParser *Parser, int Type, int ExtraSize)
424 tAST_Node *ret = malloc( sizeof(tAST_Node) + ExtraSize );
425 ret->NextSibling = NULL;
426 ret->File = Parser->Filename; *(int*)(Parser->Filename - sizeof(int)) += 1;
427 ret->Line = Parser->CurLine;
431 ret->BlockState = NULL;
433 ret->ValueCache = NULL;
438 tAST_Node *AST_NewCodeBlock(tParser *Parser)
440 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_BLOCK, 0 );
442 ret->Block.FirstChild = NULL;
443 ret->Block.LastChild = NULL;
448 void AST_AppendNode(tAST_Node *Parent, tAST_Node *Child)
450 // Ignore NULL children
451 if( !Child ) return ;
453 Child->NextSibling = NULL;
454 switch( Parent->Type )
457 if(Parent->Block.FirstChild == NULL) {
458 Parent->Block.FirstChild = Parent->Block.LastChild = Child;
461 Parent->Block.LastChild->NextSibling = Child;
462 Parent->Block.LastChild = Child;
465 case NODETYPE_DEFVAR:
466 if(Parent->DefVar.LevelSizes == NULL) {
467 Parent->DefVar.LevelSizes = Parent->DefVar.LevelSizes_Last = Child;
470 Parent->DefVar.LevelSizes_Last->NextSibling = Child;
471 Parent->DefVar.LevelSizes_Last = Child;
475 fprintf(stderr, "BUG REPORT: AST_AppendNode on an invalid node type (%i)\n", Parent->Type);
480 tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False)
482 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_IF, 0);
483 ret->If.Condition = Condition;
485 ret->If.False = False;
489 tAST_Node *AST_NewLoop(tParser *Parser, const char *Tag, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code)
493 // NOTE: The +3) & ~3 is to align the size to 4 bytes, and shut valgrind up
494 // - GCC sometimes inlines strlen as a loop of dword reads, triggering valgrind
495 ret = AST_int_AllocateNode(Parser, NODETYPE_LOOP, (strlen(Tag) + 1 + 3) & ~3);
496 ret->For.Init = Init;
497 ret->For.bCheckAfter = !!bPostCheck;
498 ret->For.Condition = Condition;
499 ret->For.Increment = Increment;
500 ret->For.Code = Code;
501 strcpy(ret->For.Tag, Tag);
505 tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value)
507 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_ASSIGN, 0);
509 if( Dest->Type != NODETYPE_VARIABLE && Dest->Type != NODETYPE_ELEMENT ) {
511 SyntaxError(Parser, 1, "Assign target is not a variable or attribute (instead %i)",
518 ret->Assign.Operation = Operation;
519 ret->Assign.Dest = Dest;
520 ret->Assign.Value = Value;
525 tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value)
527 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CAST, 0);
529 ret->Cast.DataType = Target;
530 ret->Cast.Value = Value;
535 tAST_Node *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_Node *Right)
537 tAST_Node *ret = AST_int_AllocateNode(Parser, Operation, 0);
539 ret->BinOp.Left = Left;
540 ret->BinOp.Right = Right;
547 tAST_Node *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value)
549 tAST_Node *ret = AST_int_AllocateNode(Parser, Operation, 0);
551 ret->UniOp.Value = Value;
556 tAST_Node *AST_NewBreakout(tParser *Parser, int Type, const char *DestTag)
558 int len = (DestTag ? strlen(DestTag) : 0);
559 tAST_Node *ret = AST_int_AllocateNode(Parser, Type, len + 1);
562 strcpy(ret->Variable.Name, DestTag);
564 ret->Variable.Name[0] = '\0';
569 tAST_Node *AST_NewNop(tParser *Parser)
571 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_NOP, 0);
577 * \brief Create a new string node
579 tAST_Node *AST_NewString(tParser *Parser, const char *String, int Length)
581 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_STRING, Length + 1);
583 ret->Constant.Type = SS_DATATYPE_STRING;
584 ret->Constant.ReferenceCount = 1;
585 ret->Constant.String.Length = Length;
586 memcpy(ret->Constant.String.Data, String, Length);
587 ret->Constant.String.Data[Length] = '\0';
593 * \brief Create a new integer node
595 tAST_Node *AST_NewInteger(tParser *Parser, int64_t Value)
597 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_INTEGER, 0);
598 ret->Constant.Type = SS_DATATYPE_INTEGER;
599 ret->Constant.ReferenceCount = 1;
600 ret->Constant.Integer = Value;
605 * \brief Create a new real number node
607 tAST_Node *AST_NewReal(tParser *Parser, double Value)
609 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_REAL, 0);
610 ret->Constant.Type = SS_DATATYPE_REAL;
611 ret->Constant.ReferenceCount = 1;
612 ret->Constant.Real = Value;
617 * \brief Create a new variable reference node
619 tAST_Node *AST_NewVariable(tParser *Parser, const char *Name)
621 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_VARIABLE, strlen(Name) + 1 );
622 strcpy(ret->Variable.Name, Name);
627 * \brief Create a new variable definition node
629 tAST_Node *AST_NewDefineVar(tParser *Parser, int Type, const char *Name)
631 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_DEFVAR, strlen(Name) + 1 );
633 ret->DefVar.DataType = Type;
634 ret->DefVar.LevelSizes = NULL;
635 ret->DefVar.LevelSizes_Last = NULL;
636 ret->DefVar.InitialValue = NULL;
637 strcpy(ret->DefVar.Name, Name);
643 * \brief Create a new runtime constant reference node
645 tAST_Node *AST_NewConstant(tParser *Parser, const char *Name)
647 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CONSTANT, strlen(Name) + 1 );
649 strcpy(ret->Variable.Name, Name);
655 * \brief Create a function call node
656 * \note Argument list is manipulated using AST_AppendFunctionCallArg
658 tAST_Node *AST_NewFunctionCall(tParser *Parser, const char *Name)
660 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_FUNCTIONCALL, strlen(Name) + 1 );
662 ret->FunctionCall.Object = NULL;
663 ret->FunctionCall.FirstArg = NULL;
664 ret->FunctionCall.LastArg = NULL;
665 ret->FunctionCall.NumArgs = 0;
666 strcpy(ret->FunctionCall.Name, Name);
670 tAST_Node *AST_NewMethodCall(tParser *Parser, tAST_Node *Object, const char *Name)
672 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_METHODCALL, strlen(Name) + 1 );
674 ret->FunctionCall.Object = Object;
675 ret->FunctionCall.FirstArg = NULL;
676 ret->FunctionCall.LastArg = NULL;
677 ret->FunctionCall.NumArgs = 0;
678 strcpy(ret->FunctionCall.Name, Name);
683 tAST_Node *AST_NewCreateObject(tParser *Parser, const char *Name)
685 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_CREATEOBJECT, strlen(Name) + 1 );
687 ret->FunctionCall.Object = NULL;
688 ret->FunctionCall.FirstArg = NULL;
689 ret->FunctionCall.LastArg = NULL;
690 ret->FunctionCall.NumArgs = 0;
691 strcpy(ret->FunctionCall.Name, Name);
697 * \brief Append an argument to a function call
699 void AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg)
701 if( Node->Type != NODETYPE_FUNCTIONCALL
702 && Node->Type != NODETYPE_CREATEOBJECT
703 && Node->Type != NODETYPE_METHODCALL)
705 fprintf(stderr, "BUG REPORT: AST_AppendFunctionCallArg on an invalid node type (%i)\n", Node->Type);
709 if(Node->FunctionCall.LastArg) {
710 Node->FunctionCall.LastArg->NextSibling = Arg;
711 Node->FunctionCall.LastArg = Arg;
714 Node->FunctionCall.FirstArg = Arg;
715 Node->FunctionCall.LastArg = Arg;
717 Node->FunctionCall.NumArgs ++;
721 * \brief Add a scope node
723 tAST_Node *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node *Child)
725 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_SCOPE, strlen(Name) + 1 );
726 ret->Scope.Element = Child;
727 strcpy(ret->Scope.Name, Name);
732 * \brief Add a scope node
734 tAST_Node *AST_NewClassElement(tParser *Parser, tAST_Node *Object, const char *Name)
736 tAST_Node *ret = AST_int_AllocateNode(Parser, NODETYPE_ELEMENT, strlen(Name) + 1 );
737 ret->Scope.Element = Object;
738 strcpy(ret->Scope.Name, Name);