c1d7a46e0a1e7cb7f00d07b391ffe87f86fe3867
[tpg/acess2.git] / Usermode / Libraries / libspiderscript.so_src / ast.c
1 /*
2  * Acess2 Init
3  * - Script AST Manipulator
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "ast.h"
9
10 // === IMPORTS ===
11 extern void     SyntaxError(tParser *Parser, int bFatal, const char *Message, ...);
12
13 // === CODE ===
14 tAST_Script *AST_NewScript(void)
15 {
16         tAST_Script     *ret = malloc( sizeof(tAST_Script) );
17         
18         ret->Functions = NULL;
19         ret->LastFunction = NULL;
20         
21         return ret;
22 }
23
24 /**
25  * \brief Append a function to a script
26  */
27 tAST_Function *AST_AppendFunction(tAST_Script *Script, const char *Name, int ReturnType)
28 {
29         tAST_Function   *ret;
30         
31         ret = malloc( sizeof(tAST_Function) + strlen(Name) + 1 );
32         ret->Next = NULL;
33         strcpy(ret->Name, Name);
34         ret->Code = NULL;
35         ret->Arguments = NULL;
36         ret->ReturnType = ReturnType;
37         
38         if(Script->LastFunction == NULL) {
39                 Script->Functions = Script->LastFunction = ret;
40         }
41         else {
42                 Script->LastFunction->Next = ret;
43                 Script->LastFunction = ret;
44         }
45         
46         return ret;
47 }
48
49 void AST_AppendFunctionArg(tAST_Function *Function, tAST_Node *Node)
50 {
51         if( !Function->Arguments ) {
52                 Function->Arguments_Last = Function->Arguments = Node;
53         }
54         else {
55                 Function->Arguments_Last->NextSibling = Node;
56                 Function->Arguments_Last = Node;
57         }
58 }
59
60 /**
61  * \brief Set the code for a function
62  */
63 void AST_SetFunctionCode(tAST_Function *Function, tAST_Node *Root)
64 {
65         Function->Code = Root;
66 }
67
68 /**
69  * \name Node Manipulation
70  * \{
71  */
72 #define WRITE_N(_buffer, _offset, _len, _dataptr) do { \
73         if(_buffer)     memcpy((char*)_buffer + _offset, _dataptr, _len);\
74         _offset += _len; \
75 } while(0)
76
77 #define WRITE_8(_buffer, _offset, _val) do {\
78         uint8_t v = (_val);\
79         WRITE_N(_buffer, _offset, 1, &v);\
80 } while(0)
81 #define WRITE_16(_buffer, _offset, _val) do {\
82         uint16_t        v = (_val);\
83         WRITE_N(_buffer, _offset, 2, &v);\
84 } while(0)
85 #define WRITE_32(_buffer, _offset, _val) do {\
86         uint32_t        v = (_val);\
87         WRITE_N(_buffer, _offset, 4, &v);\
88 } while(0)
89 #define WRITE_64(_buffer, _offset, _val) do {\
90         uint64_t        v = (_val);\
91         WRITE_N(_buffer, _offset, 8, &v);\
92 } while(0)
93 #define WRITE_REAL(_buffer, _offset, _val) do {\
94         double  v = (_val);\
95         WRITE_N(_buffer, _offset, sizeof(double), &v);\
96 } while(0)
97
98 #define WRITE_STR(_buffer, _offset, _string) do {\
99         int len = strlen(_string);\
100         WRITE_16(_buffer, _offset, len);\
101         WRITE_N(_buffer, _offset, len, _string);\
102         if((_offset & 1) == 1)WRITE_8(_buffer, _offset, 0); \
103         if((_offset & 3) == 2)WRITE_16(_buffer, _offset, 0); \
104 } while(0)
105 #define WRITE_NODELIST(_buffer, _offset, _listHead)     do {\
106         tAST_Node *node; \
107         size_t ptr = -1;\
108         for(node=(_listHead); node; node = node->NextSibling) {\
109                 ptr = _offset;\
110                 _offset += AST_WriteNode(_buffer, _offset, node); \
111                 WRITE_32(_buffer, ptr, ptr); \
112         } \
113         if(ptr != -1){ptr -= 4; WRITE_32(_buffer, ptr, 0);} \
114 } while(0)
115
116 /**
117  * \brief Writes a script dump to a buffer
118  * \return Size of encoded data
119  * \note If \a Buffer is NULL, no write is done, but the size is still returned
120  */
121 size_t AST_WriteScript(void *Buffer, tAST_Script *Script)
122 {
123         tAST_Function   *fcn;
124         size_t  ret = 0, ptr = 0;
125         
126         for( fcn = Script->Functions; fcn; fcn = fcn->Next )
127         {
128 //              printf("fcn = %p, fcn->Name = %p\n", fcn, fcn->Name);
129                 ptr = ret;
130                 WRITE_32(Buffer, ret, 0);       // Next
131                 WRITE_STR(Buffer, ret, fcn->Name);
132                 WRITE_NODELIST(Buffer, ret, fcn->Arguments);    // TODO: Cheaper way
133                 ret += AST_WriteNode(Buffer, ret, fcn->Code);
134                 WRITE_32(Buffer, ptr, ret);     // Actually set next
135         }
136         if( ptr )
137         {
138                 ptr -= 4;
139                 WRITE_32(Buffer, ptr, 0);       // Clear next for final
140         }
141         
142         return ret;
143 }
144
145 /**
146  * \brief Write a node to a file
147  */
148 size_t AST_WriteNode(void *Buffer, size_t Offset, tAST_Node *Node)
149 {
150         size_t  baseOfs = Offset;
151         
152         if(!Node) {
153                 //fprintf(stderr, "Possible Bug - NULL passed to AST_WriteNode\n");
154                 WRITE_32(Buffer, Offset, 0);
155                 WRITE_16(Buffer, Offset, NODETYPE_NOP);
156                 WRITE_16(Buffer, Offset, 0);    // Line (0)
157                 return 0;
158         }
159         
160         WRITE_32(Buffer, Offset, 0);    // Next
161         WRITE_16(Buffer, Offset, Node->Type);
162         // TODO: Scan the buffer for the location of the filename (with NULL byte)
163         //       else, write the string at the end of the node
164         WRITE_16(Buffer, Offset, Node->Line);   // Line
165         //WRITE_32(Buffer, Offset, 0);  // File
166         
167         switch(Node->Type)
168         {
169         // Block of code
170         case NODETYPE_BLOCK:
171                 WRITE_NODELIST(Buffer, Offset, Node->Block.FirstChild);
172                 break;
173         
174         // Function Call
175         case NODETYPE_METHODCALL:
176                 Offset += AST_WriteNode(Buffer, Offset, Node->FunctionCall.Object);
177         case NODETYPE_FUNCTIONCALL:
178         case NODETYPE_CREATEOBJECT:
179                 // TODO: Search for the same function name and add a pointer
180                 WRITE_STR(Buffer, Offset, Node->FunctionCall.Name);
181                 WRITE_NODELIST(Buffer, Offset, Node->FunctionCall.FirstArg);
182                 break;
183         
184         // If node
185         case NODETYPE_IF:
186                 Offset += AST_WriteNode(Buffer, Offset, Node->If.Condition);
187                 Offset += AST_WriteNode(Buffer, Offset, Node->If.True);
188                 Offset += AST_WriteNode(Buffer, Offset, Node->If.False);
189                 break;
190         
191         // Looping Construct (For loop node)
192         case NODETYPE_LOOP:
193                 WRITE_8(Buffer, Offset, Node->For.bCheckAfter);
194                 
195                 Offset += AST_WriteNode(Buffer, Offset, Node->For.Init);
196                 Offset += AST_WriteNode(Buffer, Offset, Node->For.Condition);
197                 Offset += AST_WriteNode(Buffer, Offset, Node->For.Increment);
198                 Offset += AST_WriteNode(Buffer, Offset, Node->For.Code);
199                 break;
200         
201         // Asignment
202         case NODETYPE_ASSIGN:
203                 WRITE_8(Buffer, Offset, Node->Assign.Operation);
204                 Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Dest);
205                 Offset += AST_WriteNode(Buffer, Offset, Node->Assign.Value);
206                 break;
207         
208         // Casting
209         case NODETYPE_CAST:
210                 WRITE_8(Buffer, Offset, Node->Cast.DataType);
211                 Offset += AST_WriteNode(Buffer, Offset, Node->Cast.Value);
212                 break;
213         
214         // Define a variable
215         case NODETYPE_DEFVAR:
216                 WRITE_8(Buffer, Offset, Node->DefVar.DataType);
217                 // TODO: Duplicate compress the strings
218                 WRITE_STR(Buffer, Offset, Node->DefVar.Name);
219                 
220                 WRITE_NODELIST(Buffer, Offset, Node->DefVar.LevelSizes);
221                 Offset += AST_WriteNode(Buffer, Offset, Node->DefVar.InitialValue);
222                 break;
223         
224         // Scope Reference
225         case NODETYPE_SCOPE:
226         case NODETYPE_ELEMENT:
227                 WRITE_STR(Buffer, Offset, Node->Scope.Name);
228                 Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
229                 break;
230         
231         // Unary Operations
232         case NODETYPE_RETURN:
233         case NODETYPE_BWNOT:
234         case NODETYPE_LOGICALNOT:
235         case NODETYPE_NEGATE:
236         case NODETYPE_POSTINC:
237         case NODETYPE_POSTDEC:
238                 Offset += AST_WriteNode(Buffer, Offset, Node->UniOp.Value);
239                 break;
240         
241         // Binary Operations
242         case NODETYPE_INDEX:
243         case NODETYPE_ADD:
244         case NODETYPE_SUBTRACT:
245         case NODETYPE_MULTIPLY:
246         case NODETYPE_DIVIDE:
247         case NODETYPE_MODULO:
248         case NODETYPE_BITSHIFTLEFT:
249         case NODETYPE_BITSHIFTRIGHT:
250         case NODETYPE_BITROTATELEFT:
251         case NODETYPE_BWAND:    case NODETYPE_LOGICALAND:
252         case NODETYPE_BWOR:     case NODETYPE_LOGICALOR:
253         case NODETYPE_BWXOR:    case NODETYPE_LOGICALXOR:
254         case NODETYPE_EQUALS:
255         case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
256         case NODETYPE_GREATERTHAN:      case NODETYPE_GREATERTHANEQUAL:
257                 Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Left);
258                 Offset += AST_WriteNode(Buffer, Offset, Node->BinOp.Right);
259                 break;
260         
261         // Node types with no children
262         case NODETYPE_NOP:
263                 break;
264         case NODETYPE_VARIABLE:
265         case NODETYPE_CONSTANT:
266                 // TODO: De-Duplicate the strings
267                 WRITE_STR(Buffer, Offset, Node->Variable.Name);
268                 break;
269         case NODETYPE_STRING:
270                 WRITE_32(Buffer, Offset, Node->Constant.String.Length);
271                 WRITE_N(Buffer, Offset, Node->Constant.String.Length, Node->Constant.String.Data);
272                 break;
273         case NODETYPE_INTEGER:
274                 WRITE_64(Buffer, Offset, Node->Constant.Integer);
275                 break;
276         case NODETYPE_REAL:
277                 WRITE_REAL(Buffer, Offset, Node->Constant.Real);
278                 break;
279         
280         //default:
281         //      fprintf(stderr, "AST_WriteNode: Unknown node type %i\n", Node->Type);
282         //      break;
283         }
284         
285         return Offset - baseOfs;
286 }
287
288 /**
289  * \brief Free a node and all subnodes
290  */
291 void AST_FreeNode(tAST_Node *Node)
292 {
293         tAST_Node       *node;
294         
295         if(!Node)       return ;
296         
297         switch(Node->Type)
298         {
299         // Block of code
300         case NODETYPE_BLOCK:
301                 for( node = Node->Block.FirstChild; node; )
302                 {
303                         tAST_Node       *savedNext = node->NextSibling;
304                         AST_FreeNode(node);
305                         node = savedNext;
306                 }
307                 break;
308         
309         // Function Call
310         case NODETYPE_METHODCALL:
311                 AST_FreeNode(Node->FunctionCall.Object);
312         case NODETYPE_FUNCTIONCALL:
313         case NODETYPE_CREATEOBJECT:
314                 for( node = Node->FunctionCall.FirstArg; node; )
315                 {
316                         tAST_Node       *savedNext = node->NextSibling;
317                         AST_FreeNode(node);
318                         node = savedNext;
319                 }
320                 break;
321         
322         // If node
323         case NODETYPE_IF:
324                 AST_FreeNode(Node->If.Condition);
325                 AST_FreeNode(Node->If.True);
326                 AST_FreeNode(Node->If.False);
327                 break;
328         
329         // Looping Construct (For loop node)
330         case NODETYPE_LOOP:
331                 AST_FreeNode(Node->For.Init);
332                 AST_FreeNode(Node->For.Condition);
333                 AST_FreeNode(Node->For.Increment);
334                 AST_FreeNode(Node->For.Code);
335                 break;
336         
337         // Asignment
338         case NODETYPE_ASSIGN:
339                 AST_FreeNode(Node->Assign.Dest);
340                 AST_FreeNode(Node->Assign.Value);
341                 break;
342         
343         // Casting
344         case NODETYPE_CAST:
345                 AST_FreeNode(Node->Cast.Value);
346                 break;
347         
348         case NODETYPE_SCOPE:
349         case NODETYPE_ELEMENT:
350                 AST_FreeNode(Node->Scope.Element);
351                 break;
352         
353         // Define a variable
354         case NODETYPE_DEFVAR:
355                 for( node = Node->DefVar.LevelSizes; node; )
356                 {
357                         tAST_Node       *savedNext = node->NextSibling;
358                         AST_FreeNode(node);
359                         node = savedNext;
360                 }
361                 AST_FreeNode(Node->DefVar.InitialValue);
362                 break;
363         
364         // Unary Operations
365         case NODETYPE_RETURN:
366         case NODETYPE_BWNOT:
367         case NODETYPE_LOGICALNOT:
368         case NODETYPE_NEGATE:
369         case NODETYPE_POSTINC:
370         case NODETYPE_POSTDEC:
371                 AST_FreeNode(Node->UniOp.Value);
372                 break;
373         
374         // Binary Operations
375         case NODETYPE_INDEX:
376         case NODETYPE_ADD:
377         case NODETYPE_SUBTRACT:
378         case NODETYPE_MULTIPLY:
379         case NODETYPE_DIVIDE:
380         case NODETYPE_MODULO:
381         case NODETYPE_BITSHIFTLEFT:
382         case NODETYPE_BITSHIFTRIGHT:
383         case NODETYPE_BITROTATELEFT:
384         case NODETYPE_BWAND:    case NODETYPE_LOGICALAND:
385         case NODETYPE_BWOR:     case NODETYPE_LOGICALOR:
386         case NODETYPE_BWXOR:    case NODETYPE_LOGICALXOR:
387         case NODETYPE_EQUALS:
388         case NODETYPE_LESSTHAN: case NODETYPE_LESSTHANEQUAL:
389         case NODETYPE_GREATERTHAN:      case NODETYPE_GREATERTHANEQUAL:
390                 AST_FreeNode( Node->BinOp.Left );
391                 AST_FreeNode( Node->BinOp.Right );
392                 break;
393         
394         // Node types with no children
395         case NODETYPE_NOP:      break;
396         case NODETYPE_VARIABLE: break;
397         case NODETYPE_CONSTANT: break;
398         
399         case NODETYPE_STRING:
400         case NODETYPE_INTEGER:
401         case NODETYPE_REAL:
402                 if( Node->ValueCache )
403                         Object_Dereference(Node->ValueCache);
404                 Node->ValueCache = NULL;
405                 break;
406         }
407         free( Node );
408 }
409
410 tAST_Node *AST_int_AllocateNode(tParser *Parser, int Type, int ExtraSize)
411 {
412         tAST_Node       *ret = malloc( sizeof(tAST_Node) + ExtraSize );
413         ret->NextSibling = NULL;
414         ret->File = "<unk>";
415         ret->Line = Parser->CurLine;
416         ret->Type = Type;
417         
418         // Runtime Caching
419         ret->BlockState = NULL;
420         ret->BlockIdent = 0;
421         ret->ValueCache = NULL;
422         
423         return ret;
424 }
425
426 tAST_Node *AST_NewCodeBlock(tParser *Parser)
427 {
428         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_BLOCK, 0 );
429         
430         ret->Block.FirstChild = NULL;
431         ret->Block.LastChild = NULL;
432         
433         return ret;
434 }
435
436 void AST_AppendNode(tAST_Node *Parent, tAST_Node *Child)
437 {
438         // Ignore NULL children
439         if( !Child )    return ;
440         
441         Child->NextSibling = NULL;
442         switch( Parent->Type )
443         {
444         case NODETYPE_BLOCK:
445                 if(Parent->Block.FirstChild == NULL) {
446                         Parent->Block.FirstChild = Parent->Block.LastChild = Child;
447                 }
448                 else {
449                         Parent->Block.LastChild->NextSibling = Child;
450                         Parent->Block.LastChild = Child;
451                 }
452                 break;
453         case NODETYPE_DEFVAR:
454                 if(Parent->DefVar.LevelSizes == NULL) {
455                         Parent->DefVar.LevelSizes = Parent->DefVar.LevelSizes_Last = Child;
456                 }
457                 else {
458                         Parent->DefVar.LevelSizes_Last->NextSibling = Child;
459                         Parent->DefVar.LevelSizes_Last = Child;
460                 }
461                 break;
462         default:
463                 fprintf(stderr, "BUG REPORT: AST_AppendNode on an invalid node type (%i)\n", Parent->Type);
464                 break;
465         }
466 }
467
468 tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False)
469 {
470         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_IF, 0);
471         ret->If.Condition = Condition;
472         ret->If.True = True;
473         ret->If.False = False;
474         return ret;
475 }
476
477 tAST_Node *AST_NewLoop(tParser *Parser, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code)
478 {
479         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_LOOP, 0);
480         ret->For.Init = Init;
481         ret->For.bCheckAfter = !!bPostCheck;
482         ret->For.Condition = Condition;
483         ret->For.Increment = Increment;
484         ret->For.Code = Code;
485         return ret;
486 }
487
488 tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value)
489 {
490         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_ASSIGN, 0);
491         
492         if( Dest->Type != NODETYPE_VARIABLE && Dest->Type != NODETYPE_ELEMENT ) {
493                 free(ret);
494                 SyntaxError(Parser, 1, "Assign target is not a variable or attribute (instead %i)",
495                         Dest->Type);
496                 AST_FreeNode(Dest);
497                 AST_FreeNode(Value);
498                 return NULL;
499         }
500         
501         ret->Assign.Operation = Operation;
502         ret->Assign.Dest = Dest;
503         ret->Assign.Value = Value;
504         
505         return ret;
506 }
507
508 tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value)
509 {
510         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_CAST, 0);
511         
512         ret->Cast.DataType = Target;
513         ret->Cast.Value = Value;
514         
515         return ret;
516 }
517
518 tAST_Node *AST_NewBinOp(tParser *Parser, int Operation, tAST_Node *Left, tAST_Node *Right)
519 {
520         tAST_Node       *ret = AST_int_AllocateNode(Parser, Operation, 0);
521         
522         ret->BinOp.Left = Left;
523         ret->BinOp.Right = Right;
524         
525         return ret;
526 }
527
528 /**
529  */
530 tAST_Node *AST_NewUniOp(tParser *Parser, int Operation, tAST_Node *Value)
531 {
532         tAST_Node       *ret = AST_int_AllocateNode(Parser, Operation, 0);
533         
534         ret->UniOp.Value = Value;
535         
536         return ret;
537 }
538
539 tAST_Node *AST_NewNop(tParser *Parser)
540 {
541         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_NOP, 0);
542         
543         return ret;
544 }
545
546 /**
547  * \brief Create a new string node
548  */
549 tAST_Node *AST_NewString(tParser *Parser, const char *String, int Length)
550 {
551         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_STRING, Length + 1);
552         
553         ret->Constant.Type = SS_DATATYPE_STRING;
554         ret->Constant.ReferenceCount = 1;
555         ret->Constant.String.Length = Length;
556         memcpy(ret->Constant.String.Data, String, Length);
557         ret->Constant.String.Data[Length] = '\0';
558         
559         return ret;
560 }
561
562 /**
563  * \brief Create a new integer node
564  */
565 tAST_Node *AST_NewInteger(tParser *Parser, int64_t Value)
566 {
567         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_INTEGER, 0);
568         ret->Constant.Type = SS_DATATYPE_INTEGER;
569         ret->Constant.ReferenceCount = 1;
570         ret->Constant.Integer = Value;
571         return ret;
572 }
573
574 /**
575  * \brief Create a new real number node
576  */
577 tAST_Node *AST_NewReal(tParser *Parser, double Value)
578 {
579         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_REAL, 0);
580         ret->Constant.Type = SS_DATATYPE_REAL;
581         ret->Constant.ReferenceCount = 1;
582         ret->Constant.Real = Value;
583         return ret;
584 }
585
586 /**
587  * \brief Create a new variable reference node
588  */
589 tAST_Node *AST_NewVariable(tParser *Parser, const char *Name)
590 {
591         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_VARIABLE, strlen(Name) + 1 );
592         strcpy(ret->Variable.Name, Name);
593         return ret;
594 }
595
596 /**
597  * \brief Create a new variable definition node
598  */
599 tAST_Node *AST_NewDefineVar(tParser *Parser, int Type, const char *Name)
600 {
601         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_DEFVAR, strlen(Name) + 1 );
602         
603         ret->DefVar.DataType = Type;
604         ret->DefVar.LevelSizes = NULL;
605         ret->DefVar.LevelSizes_Last = NULL;
606         ret->DefVar.InitialValue = NULL;
607         strcpy(ret->DefVar.Name, Name);
608         
609         return ret;
610 }
611
612 /**
613  * \brief Create a new runtime constant reference node
614  */
615 tAST_Node *AST_NewConstant(tParser *Parser, const char *Name)
616 {
617         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_CONSTANT, strlen(Name) + 1 );
618         
619         strcpy(ret->Variable.Name, Name);
620         
621         return ret;
622 }
623
624 /**
625  * \brief Create a function call node
626  * \note Argument list is manipulated using AST_AppendFunctionCallArg
627  */
628 tAST_Node *AST_NewFunctionCall(tParser *Parser, const char *Name)
629 {
630         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_FUNCTIONCALL, strlen(Name) + 1 );
631         
632         ret->FunctionCall.Object = NULL;
633         ret->FunctionCall.FirstArg = NULL;
634         ret->FunctionCall.LastArg = NULL;
635         ret->FunctionCall.NumArgs = 0;
636         strcpy(ret->FunctionCall.Name, Name);
637         
638         return ret;
639 }
640 tAST_Node *AST_NewMethodCall(tParser *Parser, tAST_Node *Object, const char *Name)
641 {
642         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_METHODCALL, strlen(Name) + 1 );
643         
644         ret->FunctionCall.Object = Object;
645         ret->FunctionCall.FirstArg = NULL;
646         ret->FunctionCall.LastArg = NULL;
647         ret->FunctionCall.NumArgs = 0;
648         strcpy(ret->FunctionCall.Name, Name);
649         
650         return ret;
651 }
652
653 tAST_Node *AST_NewCreateObject(tParser *Parser, const char *Name)
654 {
655         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_CREATEOBJECT, strlen(Name) + 1 );
656         
657         ret->FunctionCall.Object = NULL;
658         ret->FunctionCall.FirstArg = NULL;
659         ret->FunctionCall.LastArg = NULL;
660         ret->FunctionCall.NumArgs = 0;
661         strcpy(ret->FunctionCall.Name, Name);
662         
663         return ret;
664 }
665
666 /**
667  * \brief Append an argument to a function call
668  */
669 void AST_AppendFunctionCallArg(tAST_Node *Node, tAST_Node *Arg)
670 {
671         if( Node->Type != NODETYPE_FUNCTIONCALL
672          && Node->Type != NODETYPE_CREATEOBJECT
673          && Node->Type != NODETYPE_METHODCALL)
674         {
675                 fprintf(stderr, "BUG REPORT: AST_AppendFunctionCallArg on an invalid node type (%i)\n", Node->Type);
676                 return ;
677         }
678         
679         if(Node->FunctionCall.LastArg) {
680                 Node->FunctionCall.LastArg->NextSibling = Arg;
681                 Node->FunctionCall.LastArg = Arg;
682         }
683         else {
684                 Node->FunctionCall.FirstArg = Arg;
685                 Node->FunctionCall.LastArg = Arg;
686         }
687         Node->FunctionCall.NumArgs ++;
688 }
689
690 /**
691  * \brief Add a scope node
692  */
693 tAST_Node *AST_NewScopeDereference(tParser *Parser, const char *Name, tAST_Node *Child)
694 {
695         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_SCOPE, strlen(Name) + 1 );
696         ret->Scope.Element = Child;
697         strcpy(ret->Scope.Name, Name);
698         return ret;
699 }
700
701 /**
702  * \brief Add a scope node
703  */
704 tAST_Node *AST_NewClassElement(tParser *Parser, tAST_Node *Object, const char *Name)
705 {
706         tAST_Node       *ret = AST_int_AllocateNode(Parser, NODETYPE_ELEMENT, strlen(Name) + 1 );
707         ret->Scope.Element = Object;
708         strcpy(ret->Scope.Name, Name);
709         return ret;
710 }
711
712 /**
713  * \}
714  */

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