forked from justinmeza/lci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
3076 lines (3004 loc) · 89.4 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "parser.h"
#ifdef DEBUG
static unsigned int shiftwidth = 0;
void shiftout(void) { shiftwidth += 4; }
void shiftin(void) { shiftwidth -= 4; }
void debug(const char *info)
{
int n;
for (n = 0; n < shiftwidth; n++)
fprintf(stderr, " ");
fprintf(stderr, "%s\n", info);
}
#endif
/** Creates a MainNode structure.
*
* \pre \a block was created by createBlockNode(StmtNodeList *).
* \pre \a functab was created by createFunctionTable(void) and contains
* items added by addFuncDefStmtNode(FunctionTable *, FuncDefStmtNode *).
*
* \return A pointer to a MainNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteMainNode(MainNode *) */
MainNode *createMainNode(BlockNode *block, /**< [in] A pointer to the block of code to execute first. */
FunctionTable *functab) /**< [in] A pointer to the function table associated with this block of code. */
{
MainNode *p = malloc(sizeof(MainNode));
if (!p) {
perror("malloc");
return NULL;
}
p->block = block;
p->functab = functab;
return p;
}
/** Deletes a MainNode structure.
*
* \pre \a node was created by createMainNode(BlockNode *, FunctionTable *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createMainNode(BlockNode *, FunctionTable *) */
void deleteMainNode(MainNode *node) /**< [in,out] A pointer to the MainNode structure to be deleted. */
{
if (!node) return;
deleteBlockNode(node->block);
deleteFunctionTable(node->functab);
free(node);
}
/** Creates a BlockNode structure.
*
* \pre \a stmts was created by createStmtNodeList(void) and contains contents
* added by addStmtNode(StmtNodeList *, StmtNode *).
*
* \return A pointer to a BlockNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteBlockNode(BlockNode *) */
BlockNode *createBlockNode(StmtNodeList *stmts) /**< [in] A pointer to the list of statements which comprise the block of code. */
{
BlockNode *p = malloc(sizeof(BlockNode));
if (!p) {
perror("malloc");
return NULL;
}
p->stmts = stmts;
return p;
}
/** Deletes a BlockNode structure.
*
* \pre \a node was created by createBlockNode(StmtNodeList *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createBlockNode(StmtNodeList *) */
void deleteBlockNode(BlockNode *node) /**< [in,out] A pointer to the BlockNode structure to be deleted. */
{
if (!node) return;
deleteStmtNodeList(node->stmts);
free(node);
}
/** Creates a BlockNodeList structure.
*
* \return A pointer to a BlockNodeList structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteBlockNodeList(BlockNodeList *) */
BlockNodeList *createBlockNodeList(void)
{
BlockNodeList *p = malloc(sizeof(BlockNodeList));
if (!p) {
perror("malloc");
return NULL;
}
p->num = 0;
p->blocks = NULL;
return p;
}
/** Adds a BlockNode structure to a BlockNodeList structure.
*
* \pre \a list was created by createBlockNodeList(void).
* \pre \a node was created by createBlockNode(StmtNodeList *).
*
* \post \a node will be added on to the end of \a list and the size of
* \a list will be updated accordingly.
*
* \return A pointer to the added BlockNode structure (will be the same as \a node).
*
* \retval NULL realloc was unable to allocate memory. */
BlockNode *addBlockNode(BlockNodeList *list, /**< [in,out] A pointer to the BlockNodeList structure to add \a node to. */
BlockNode *node) /**< [in] A pointer to the BlockNode structure to add to \a list. */
{
unsigned int newsize = list->num + 1;
void *mem = realloc(list->blocks, sizeof(BlockNode *) * newsize);
if (!mem) {
perror("realloc");
return NULL;
}
list->blocks = mem;
list->blocks[list->num] = node;
list->num = newsize;
return node;
}
/** Deletes a BlockNodeList structure.
*
* \pre \a list was created by createBlockNodeList(void) and contains items
* added by addBlockNode(BlockNodeList *, BlockNode *).
*
* \post The memory at \a list and any of its associated members will be
* freed.
*
* \see createBlockNodeList(void) */
void deleteBlockNodeList(BlockNodeList *list) /**< [in,out] A pointer to the BlockNodeList structure to delete. */
{
unsigned int n;
if (!list) return;
for (n = 0; n < list->num; n++)
deleteBlockNode(list->blocks[n]);
free(list->blocks);
free(list);
}
/** Creates a boolean type ConstantNode structure.
*
* \return A pointer to a boolean type ConstantNode structure with value
* \c 0 if \a data equals 0 and \c 1 otherwise.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteConstantNode(ConstantNode *) */
ConstantNode *createBooleanConstantNode(int data) /**< [in] The constant boolean data. */
{
ConstantNode *p = malloc(sizeof(ConstantNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = CT_BOOLEAN;
p->data.i = (data != 0);
return p;
}
/** Creates an integer type ConstantNode structure.
*
* \return A pointer to an integer type ConstantNode storing the desired value.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteConstantNode(ConstantNode *) */
ConstantNode *createIntegerConstantNode(int data) /**< [in] The constant integer data. */
{
ConstantNode *p = malloc(sizeof(ConstantNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = CT_INTEGER;
p->data.i = data;
return p;
}
/** Creates a floating point decimal type ConstantNode structure.
*
* \return A pointer to a floating point decimal type ConstantNode storing the
* desired value.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteConstantNode(ConstantNode *) */
ConstantNode *createFloatConstantNode(float data) /**< [in] The constant floating point decimal data. */
{
ConstantNode *p = malloc(sizeof(ConstantNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = CT_FLOAT;
p->data.f = data;
return p;
}
/** Creates a string type ConstantNode structure.
*
* \return A pointer to a string type ConstantNode storing the desired value.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteConstantNode(ConstantNode *) */
ConstantNode *createStringConstantNode(char *data) /**< [in] The constant character string data. */
{
ConstantNode *p = malloc(sizeof(ConstantNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = CT_STRING;
p->data.s = data;
return p;
}
/** Deletes a ConstantNode structure.
*
* \pre \a node was created by either createBooleanConstantNode(int), createIntegerConstantNode(int),
* createFloatConstantNode(float), or createStringConstantNode(char *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createBooleanConstantNode(int)
* \see createIntegerConstantNode(int)
* \see createFloatConstantNode(float)
* \see createStringConstantNode(char *) */
void deleteConstantNode(ConstantNode *node) /**< [in,out] A pointer to the ConstantNode structure to be deleted. */
{
if (!node) return;
if (node->type == CT_STRING) free(node->data.s);
free(node);
}
/** Creates an IdentifierNode structure.
*
* \note \a image is \b copied for use within the structure so it must be freed
* by the caller.
*
* \return A pointer to an IdentifierNode structure with the desired
* properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteIdentifierNode(IdentifierNode *) */
IdentifierNode *createIdentifierNode(char *image, /**< [in] An array of characters that name the identifier. */
const char *fname, /**< [in] A pointer to the name of the file containing the identifier. */
unsigned int line) /**< [in] The line number from the source file that the identifier occurred on. */
{
IdentifierNode *p = malloc(sizeof(IdentifierNode));
if (!p) {
perror("malloc");
return NULL;
}
p->image = malloc(sizeof(char) * (strlen(image) + 1));
if (!p->image) {
free(p);
perror("malloc");
return NULL;
}
strcpy(p->image, image);
p->fname = fname;
p->line = line;
return p;
}
/** Deletes an IdentifierNode structure.
*
* \pre \a node was created by createIdentifierNode(char *, const char *, unsigned int).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createIdentifierNode(char *, const char *, unsigned int) */
void deleteIdentifierNode(IdentifierNode *node) /**< [in,out] A pointer to the IdentifierNode structure to be deleted. */
{
if (!node) return;
free(node->image);
free(node);
}
/** Creates an IdentifierNodeList structure.
*
* \return A pointer to a IdentifierNodeList structure with the desired
* properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteIdentifierNodeList(IdentifierNodeList *) */
IdentifierNodeList *createIdentifierNodeList(void)
{
IdentifierNodeList *p = malloc(sizeof(IdentifierNodeList));
if (!p) {
perror("malloc");
return NULL;
}
p->num = 0;
p->ids = NULL;
return p;
}
/** Adds an IdentifierNode structure to an IdentifierNodeList structure.
*
* \pre \a list was created by createIdentifierNodeList(void).
* \pre \a node was created by createIdentifierNode(char *, const char *, unsigned int).
*
* \post \a node will be added on to the end of \a list and the size of
* \a list will be updated accordingly.
*
* \return A pointer to the added IdentifierNode structure (will be the same as
* \a node).
*
* \retval NULL realloc was unable to allocate memory. */
IdentifierNode *addIdentifierNode(IdentifierNodeList *list, /**< [in,out] A pointer to the IdentifierNodeList structure to add \a node to. */
IdentifierNode *node) /**< [in] A pointer to the IdentifierNode structure to add to \a list. */
{
unsigned int newsize = list->num + 1;
void *mem = realloc(list->ids, sizeof(IdentifierNode *) * newsize);
if (!mem) {
perror("realloc");
return NULL;
}
list->ids = mem;
list->ids[list->num] = node;
list->num = newsize;
return node;
}
/** Deletes an IdentifierNodeList structure.
*
* \pre \a list was created by createIdentifierNodeList(void) and contains
* items added by addIdentifierNode(IdentifierNodeList *, IdentifierNode *).
*
* \post The memory at \a list and any of its associated members will be
* freed.
*
* \see createIdentifierNodeList(void) */
void deleteIdentifierNodeList(IdentifierNodeList *list) /**< [in,out] A pointer to the IdentifierNodeList structure to delete. */
{
unsigned int n;
if (!list) return;
for (n = 0; n < list->num; n++)
deleteIdentifierNode(list->ids[n]);
free(list->ids);
free(list);
}
/** Creates a TypeNode structure.
*
* \return A pointer to a TypeNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteTypeNode(TypeNode *) */
TypeNode *createTypeNode(ConstantType type) /**< [in] The type of value. */
{
TypeNode *p = malloc(sizeof(TypeNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = type;
return p;
}
/** Deletes a TypeNode structure.
*
* \pre \a node was created by createTypeNode(ConstantType).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createTypeNode(ConstantType) */
void deleteTypeNode(TypeNode *node) /**< [in,out] A pointer to the TypeNode structure to be deleted. */
{
if (!node) return;
free(node);
}
/** Creates a StmtNode structure.
*
* \pre \a stmt contains a structure created corresponding to \a type:
* - ST_CAST: createCastStmtNode(IdentifierNode *, TypeNode *)
* - ST_PRINT: createPrintStmtNode(ExprNodeList *, int)
* - ST_INPUT: createInputStmtNode(IdentifierNode *)
* - ST_ASSIGNMENT: createAssignmentStmtNode(IdentifierNode *, ExprNode *)
* - ST_DECLARATION: createDeclarationStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *)
* - ST_IFTHENELSE: createIfThenElseStmtNode(BlockNode *, BlockNode *, ExprNodeList *, BlockNodeList *)
* - ST_SWITCH: createSwitchStmtNode(ExprNodeList *, BlockNodeList *, BlockNode *)
* - ST_BREAK: no structure needed, use \c NULL
* - ST_RETURN: createReturnStmtNode(ExprNode *)
* - ST_LOOP: createLoopStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *, ExprNode *, BlockNode *)
* - ST_FUNCDEF: createFuncDefStmtNode(IdentifierNode *, IdentifierNode *, IdentifierNodeList *, BlockNode *)
* - ST_EXPR: createExprNode(ExprType, void *)
*
* \return A pointer to a StmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteStmtNode(StmtNode *) */
StmtNode *createStmtNode(StmtType type, /**< [in] The type of statement stored in \a node. */
void *stmt) /**< [in] A pointer to the particular statement structure. */
{
StmtNode *p = malloc(sizeof(StmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = type;
p->stmt = stmt;
return p;
}
/** Deletes a StmtNode structure.
*
* \pre \a node was created by createStmtNode(StmtType, void *).
*
* \post The memory at \a stmt and any of its associated members will be
* freed.
*
* \see createStmtNode(StmtType, void *) */
void deleteStmtNode(StmtNode *node) /**< [in, out] A pointer to the StmtNode structure to be deleted. */
{
if (!node) return;
switch (node->type) {
case ST_CAST: {
CastStmtNode *stmt = (CastStmtNode *)node->stmt;
deleteCastStmtNode(stmt);
break;
}
case ST_PRINT: {
PrintStmtNode *stmt = (PrintStmtNode *)node->stmt;
deletePrintStmtNode(stmt);
break;
}
case ST_INPUT: {
InputStmtNode *stmt = (InputStmtNode *)node->stmt;
deleteInputStmtNode(stmt);
break;
}
case ST_ASSIGNMENT: {
AssignmentStmtNode *stmt = (AssignmentStmtNode *)node->stmt;
deleteAssignmentStmtNode(stmt);
break;
}
case ST_DECLARATION: {
DeclarationStmtNode *stmt = (DeclarationStmtNode *)node->stmt;
deleteDeclarationStmtNode(stmt);
break;
}
case ST_IFTHENELSE: {
IfThenElseStmtNode *stmt = (IfThenElseStmtNode *)node->stmt;
deleteIfThenElseStmtNode(stmt);
break;
}
case ST_SWITCH: {
SwitchStmtNode *stmt = (SwitchStmtNode *)node->stmt;
deleteSwitchStmtNode(stmt);
break;
}
case ST_BREAK:
break; /* This statement type does not have any content */
case ST_RETURN: {
ReturnStmtNode *stmt = (ReturnStmtNode *)node->stmt;
deleteReturnStmtNode(stmt);
break;
}
case ST_LOOP: {
LoopStmtNode *stmt = (LoopStmtNode *)node->stmt;
deleteLoopStmtNode(stmt);
break;
}
case ST_EXPR: {
ExprNode *expr = (ExprNode *)node->stmt;
deleteExprNode(expr);
break;
}
case ST_FUNCDEF: {
FuncDefStmtNode *stmt = (FuncDefStmtNode *)node->stmt;
deleteFuncDefStmtNode(stmt);
break;
}
default:
fprintf(stderr, "Unable to delete unknown statement type\n");
break;
}
free(node);
}
/** Creates a StmtNodeList structure.
*
* \return A pointer to a StmtNodeList structure with no elements.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteStmtNodeList(StmtNodeList *) */
StmtNodeList *createStmtNodeList(void)
{
StmtNodeList *p = malloc(sizeof(StmtNodeList));
if (!p) {
perror("malloc");
return NULL;
}
p->num = 0;
p->stmts = NULL;
return p;
}
/** Adds a StmtNode to a StmtNodeList structure.
*
* \pre \a list was created by createStmtNodeList(void).
* \pre \a node was created by createStmtNode(StmtType, void *).
*
* \post \a node will be added on to the end of \a list and the size of
* \a list will be updated accordingly.
*
* \return A pointer to the added StmtNode (will be the same as \a node).
*
* \retval NULL malloc was unable to allocate memory. */
StmtNode *addStmtNode(StmtNodeList *list, /**< [in,out] A pointer to the StmtNodeList structure to add \a node to. */
StmtNode *node) /**< [in] A pointer to the StmtNode structure to add to \a list. */
{
unsigned int newsize = list->num + 1;
void *mem = realloc(list->stmts, sizeof(StmtNode *) * newsize);
if (!mem) {
perror("realloc");
return NULL;
}
list->stmts = mem;
list->stmts[list->num] = node;
list->num = newsize;
return node;
}
/** Deletes a StmtNodeList structure.
*
* \pre \a list was created by createStmtNodeList(void) and contains items added
* by addStmtNode(StmtNodeList *, StmtNode *).
*
* \post The memory at \a list and any of its associated members will be
* freed.
*
* \see createStmtNodeList(void) */
void deleteStmtNodeList(StmtNodeList *list) /**< [in,out] A pointer to the StmtNodeList structure to delete. */
{
unsigned int n;
if (!list) return;
for (n = 0; n < list->num; n++)
deleteStmtNode(list->stmts[n]);
free(list->stmts);
free(list);
}
/** Creates a CastStmtNode structure.
*
* \pre \a target was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a newtype was created by createTypeNode(ConstantType).
*
* \return A pointer to a CastStmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteCastStmtNode(CastStmtNode *) */
CastStmtNode *createCastStmtNode(IdentifierNode *target, /**< [in] A pointer to the name of the variable whose type is to be changed to \a newtype. */
TypeNode *newtype) /**< [in] A pointer to the type to change \a target to. */
{
CastStmtNode *p = malloc(sizeof(CastStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;
p->newtype = newtype;
return p;
}
/** Deletes a CastStmtNode structure.
*
* \pre \a node was created by createCastStmtNode(IdentifierNode *, TypeNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createCastStmtNode(IdentifierNode *, TypeNode *) */
void deleteCastStmtNode(CastStmtNode *node) /**< [in,out] A pointer to the CastStmtNode structure to be deleted. */
{
if (!node) return;
deleteIdentifierNode(node->target);
deleteTypeNode(node->newtype);
free(node);
}
/** Creates a PrintStmtNode structure.
*
* \pre \a args was created by createExprNodeList(void) and contains items
* added by addExprNode(ExprNodeList *, ExprNode *).
*
* \return A pointer to a PrintStmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deletePrintStmtNode(PrintStmtNode *) */
PrintStmtNode *createPrintStmtNode(ExprNodeList *args, /**< [in] A pointer to the list of expressions to evaluate and print. */
int nonl) /**< [in] Denotes an ending newline should be surpressed if not \c 0 and printed if \c 0. */
{
PrintStmtNode *p = malloc(sizeof(PrintStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->args = args;
p->nonl = nonl;
return p;
}
/** Deletes a PrintStmtNode structure.
*
* \pre \a node was created by createPrintStmtNode(ExprNodeList *, int).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createPrintStmtNode(ExprNodeList *, int) */
void deletePrintStmtNode(PrintStmtNode *node) /**< [in,out] A pointer to the PrintStmtNode structure to be deleted. */
{
if (!node) return;
deleteExprNodeList(node->args);
free(node);
}
/** Creates an InputStmtNode structure.
*
* \pre \a target was created by createIdentifierNode(char *, const char *, unsigned int).
*
* \return A pointer to an InputStmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteInputStmtNode(InputStmtNode *) */
InputStmtNode *createInputStmtNode(IdentifierNode *target) /**< [in] A pointer to the name of the variable to store the input in. */
{
InputStmtNode *p = malloc(sizeof(InputStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;
return p;
}
/** Deletes an InputStmtNode structure.
*
* \pre \a node was created by createInputStmtNode(IdentifierNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createInputStmtNode(IdentifierNode *) */
void deleteInputStmtNode(InputStmtNode *node) /**< [in,out] A pointer to the InputStmtNode structure to be deleted. */
{
if (!node) return;
deleteIdentifierNode(node->target);
free(node);
}
/** Creates an AssignmentStmtNode structure.
*
* \pre \a target was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a expr was created by createExprNode(ExprType, void *).
*
* \return A pointer to an AssignmentStmtNode structure with the desired
* properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteAssignmentStmtNode(AssignmentStmtNode *) */
AssignmentStmtNode *createAssignmentStmtNode(IdentifierNode *target, /**< [in] A pointer to the name of the variable to store the evaluated contents of a \a expr into. */
ExprNode *expr) /**< [in] A pointer to the expression to evaluate and store in \a target. */
{
AssignmentStmtNode *p = malloc(sizeof(AssignmentStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;
p->expr = expr;
return p;
}
/** Deletes an AssignmentStmtNode structure.
*
* \pre \a node was created by createAssignmentStmtNode(IdentifierNode *, ExprNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createAssignmentStmtNode(IdentifierNode *, ExprNode *) */
void deleteAssignmentStmtNode(AssignmentStmtNode *node) /**< [in,out] A pointer to the AssignmentStmtNode structure to be deleted. */
{
if (!node) return;
deleteIdentifierNode(node->target);
deleteExprNode(node->expr);
free(node);
}
/** Creates a DeclarationStmtNode structure.
*
* \pre \a scope was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a target was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a expr was created by createExprNode(ExprType, void *).
*
* \return A pointer to a DeclarationStmtNode structure with the desired
* properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteDeclarationStmtNode(DeclarationStmtNode *) */
DeclarationStmtNode *createDeclarationStmtNode(IdentifierNode *scope, /**< [in] A pointer to the scope to create the variable in. */
IdentifierNode *target, /**< [in] A pointer to the name of the variable to create. */
ExprNode *expr) /**< [in] An optional pointer to the expression to initialize \a target to. */
{
DeclarationStmtNode *p = malloc(sizeof(DeclarationStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->scope = scope;
p->target = target;
p->expr = expr;
return p;
}
/** Deletes a DeclarationStmtNode structure.
*
* \pre \a node was created by createDeclarationStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createDeclarationStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *) */
void deleteDeclarationStmtNode(DeclarationStmtNode *node) /**< [in,out] A pointer to the DeclarationStmtNode structure to be deleted. */
{
if (!node) return;
deleteIdentifierNode(node->scope);
deleteIdentifierNode(node->target);
deleteExprNode(node->expr);
free(node);
}
/** Creates an IfThenElseStmtNode structure.
*
* \pre \a yes was created by createBlockNode(StmtNodeList *).
* \pre \a no was created by createBlockNode(StmtNodeList *).
* \pre \a guards was created by createExprNodeList(void) and contains items
* added by addExprNode(ExprNodeList *, ExprNode *).
* \pre \a blocks was created by createBlockNodeList(void) and contains items
* added by addBlockNode(BlockNodeList *, BlockNode *).
*
* \return A pointer to a IfThenElseStmtNode structure with the desired
* properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteIfThenElseStmtNode(IfThenElseStmtNode *) */
IfThenElseStmtNode *createIfThenElseStmtNode(BlockNode *yes, /**< [in] A pointer to the block of code to execute if the \ref impvar "implicit variable" casts to \c false. */
BlockNode *no, /**< [in] A pointer to the block of code to execute if the \ref impvar "implicit variable" casts to \c false \b and the evaluations of all of the \a guards cast to \c false. */
ExprNodeList *guards, /**< [in] A pointer to the expressions to test if the \ref impvar "implicit variable" casts to \c false. */
BlockNodeList *blocks) /**< [in] A pointer to the respective blocks of code to execute if one of the evaluated \a guards casts to \c true. */
{
IfThenElseStmtNode *p = malloc(sizeof(IfThenElseStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->yes = yes;
p->no = no;
p->guards = guards;
p->blocks = blocks;
return p;
}
/** Deletes a IfThenElseStmtNode structure.
*
* \pre \a node was created by createIfThenElseStmtNode(BlockNode *, BlockNode *, ExprNodeList *, BlockNodeList *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createIfThenElseStmtNode(BlockNode *, BlockNode *, ExprNodeList *, BlockNodeList *) */
void deleteIfThenElseStmtNode(IfThenElseStmtNode *node) /**< [in,out] A pointer to the IfThenElseStmtNode structure to be deleted. */
{
if (!node) return;
deleteBlockNode(node->yes);
deleteBlockNode(node->no);
deleteExprNodeList(node->guards);
deleteBlockNodeList(node->blocks);
free(node);
}
/** Creates a SwitchStmtNode structure.
*
* \pre \a guards was created by createExprNodeList(void) and contains items
* added by addExprNode(ExprNodeList *, ExprNode *).
* \pre \a blocks was created by createBlockNodeList(void) and contains items
* added by addBlockNode(BlockNodeList *, BlockNode *).
* \pre \a def was created by createBlockNode(StmtNodeList *).
*
* \return A pointer to a SwitchStmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteSwitchStmtNode(SwitchStmtNode *) */
SwitchStmtNode *createSwitchStmtNode(ExprNodeList *guards, /**< [in] A pointer to the expressions to evaluate and compare to the \ref impvar "implicit variable". */
BlockNodeList *blocks, /**< [in] A pointer to the respective blocks of code to execute if one of the \a guards matches the \ref impvar "implicit variable". */
BlockNode *def) /**< [in] A pointer to the default block of code to execute if none of the \a guards match the \ref impvar "implicit variable". */
{
SwitchStmtNode *p = malloc(sizeof(SwitchStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->guards = guards;
p->blocks = blocks;
p->def = def;
return p;
}
/** Deletes a SwitchStmtNode structure.
*
* \pre \a node was created by createSwitchStmtNode(ExprNodeList *, BlockNodeList *, BlockNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createSwitchStmtNode(ExprNodeList *, BlockNodeList *, BlockNode *) */
void deleteSwitchStmtNode(SwitchStmtNode *node) /**< [in,out] A pointer to the SwitchStmtNode structure to be deleted. */
{
if (!node) return;
deleteExprNodeList(node->guards);
deleteBlockNodeList(node->blocks);
deleteBlockNode(node->def);
free(node);
}
/** Creates a ReturnStmtNode structure.
*
* \pre \a value was created by createExprNode(ExprType, void *).
*
* \return A pointer to a ReturnStmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteReturnStmtNode(ReturnStmtNode *) */
ReturnStmtNode *createReturnStmtNode(ExprNode *value) /**< [in] A pointer to the value to return. */
{
ReturnStmtNode *p = malloc(sizeof(ReturnStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->value = value;
return p;
}
/** Deletes a ReturnStmtNode structure.
*
* \pre \a node was created by createReturnStmtNode(ExprNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createReturnStmtNode(ExprNode *) */
void deleteReturnStmtNode(ReturnStmtNode *node) /**< [in,out] A pointer to the ReturnStmtNode structure to be deleted. */
{
if (!node) return;
deleteExprNode(node->value);
free(node);
}
/** Creates a LoopStmtNode structure.
*
* \pre \a name was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a var was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a guard was created by createExprNode(ExprType, void *).
* \pre \a update was created by createExprNode(ExprType, void *).
* \pre \a body was created by createBlockNode(StmtNodeList *).
*
* \return A pointer to a LoopStmtNode structure with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteLoopStmtNode(LoopStmtNode *) */
LoopStmtNode *createLoopStmtNode(IdentifierNode *name, /**< [in] A pointer to the name of the loop. */
IdentifierNode *var, /**< [in] A pointer to the name of the variable to be updated by \a update. */
ExprNode *guard, /**< [in] A pointer to the expression to determine if the loop will continue. */
ExprNode *update, /**< [in] A pointer to the expression to evaluate to update \a var. */
BlockNode *body) /**< [in] A pointer to the block of code to be executed with each iteration of the loop. */
{
LoopStmtNode *p = malloc(sizeof(LoopStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->name = name;
p->var = var;
p->guard = guard;
p->update = update;
p->body = body;
return p;
}
/** Deletes a LoopStmtNode structure.
*
* \pre \a node was created by createLoopStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *, ExprNode *, BlockNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createLoopStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *, ExprNode *, BlockNode *) */
void deleteLoopStmtNode(LoopStmtNode *node) /**< [in,out] A pointer to the LoopStmtNode structure to be deleted. */
{
if (!node) return;
deleteIdentifierNode(node->name);
deleteIdentifierNode(node->var);
deleteExprNode(node->guard);
deleteExprNode(node->update);
deleteBlockNode(node->body);
free(node);
}
/** Creates a FuncDefStmtNode structure.
*
* \pre \a scope was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a name was created by createIdentifierNode(char *, const char *, unsigned int).
* \pre \a args was created by createIdentifierNodeList(void) and contains
* items added by addIdentifierNode(IdentifierNodeList *, IdentifierNode *).
* \pre \a body was created by createBlockNode(StmtNodeList *).
*
* \return A pointer to a FuncDefStmtNode structure with the desired
* properties.
*
* \retval NULL malloc was unable to allocate memory.
*
* \see deleteFuncDefStmtNode(FuncDefStmtNode *) */
FuncDefStmtNode *createFuncDefStmtNode(IdentifierNode *scope, /**< [in] A pointer to the scope to define the function in. */
IdentifierNode *name, /**< [in] A pointer to the name of the function. */
IdentifierNodeList *args, /**< [in] A pointer to an array of the names of the arguments of the function. */
BlockNode *body) /**< [in] A pointer to the block of code defined by the function. */
{
FuncDefStmtNode *p = malloc(sizeof(FuncDefStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->scope = scope;
p->name = name;
p->args = args;
p->body = body;
return p;
}
/** Deletes a FuncDefStmtNode structure.
*
* \pre \a node was created by createFuncDefStmtNode(IdentifierNode *, IdentifierNode *, IdentifierNodeList *, BlockNode *).
*
* \post The memory at \a node and any of its associated members will be
* freed.
*
* \see createFuncDefStmtNode(IdentifierNode *, IdentifierNode *, IdentifierNodeList *, BlockNode *) */
void deleteFuncDefStmtNode(FuncDefStmtNode *node) /**< [in,out] A pointer to the FuncDefStmtNode structure to be deleted. */
{
if (!node) return;
deleteIdentifierNode(node->scope);
deleteIdentifierNode(node->name);
deleteIdentifierNodeList(node->args);
deleteBlockNode(node->body);
free(node);
}
/** Creates an ExprNode structure.
*
* \pre \a expr contains a structure created corresponding to \a type: