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

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