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

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