-
Notifications
You must be signed in to change notification settings - Fork 107
/
parser.c
4192 lines (3709 loc) · 99.6 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 the main code block of a program.
*
* \param [in] block The first code block to execute.
*
* \return A pointer to the main code block with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
MainNode *createMainNode(BlockNode *block)
{
MainNode *p = malloc(sizeof(MainNode));
if (!p) {
perror("malloc");
return NULL;
}
p->block = block;
return p;
}
/**
* Deletes the main code block of a program.
*
* \param [in,out] node The main code block to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteMainNode(MainNode *node)
{
if (!node) return;
deleteBlockNode(node->block);
free(node);
}
/**
* Creates a code block.
*
* \param [in] stmts The list of statements which comprise the code block.
*
* \return A pointer to the code block with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
BlockNode *createBlockNode(StmtNodeList *stmts)
{
BlockNode *p = malloc(sizeof(BlockNode));
if (!p) {
perror("malloc");
return NULL;
}
p->stmts = stmts;
return p;
}
/**
* Deletes a code block.
*
* \param [in,out] node The code block to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteBlockNode(BlockNode *node)
{
if (!node) return;
deleteStmtNodeList(node->stmts);
free(node);
}
/**
* Creates an empty code block list.
*
* \return A pointer to an empty code block list.
*
* \retval NULL Memory allocation failed.
*/
BlockNodeList *createBlockNodeList(void)
{
BlockNodeList *p = malloc(sizeof(BlockNodeList));
if (!p) {
perror("malloc");
return NULL;
}
p->num = 0;
p->blocks = NULL;
return p;
}
/**
* Adds a code block to a list.
*
* \param [in,out] list The code block list to add \a node to.
*
* \param [in] node The code block to add to \a list.
*
* \post \a node will be added to \a list and its size will be updated.
*
* \retval 0 Memory allocation failed.
*
* \retval 1 \a node was added to \a list.
*/
int addBlockNode(BlockNodeList *list,
BlockNode *node)
{
unsigned int newsize = list->num + 1;
void *mem = realloc(list->blocks, sizeof(BlockNode *) * newsize);
if (!mem) {
perror("realloc");
return 0;
}
list->blocks = mem;
list->blocks[list->num] = node;
list->num = newsize;
return 1;
}
/**
* Deletes a code block list.
*
* \param [in,out] list The code block list to delete.
*
* \post The memory at \a list and all of its members will be freed.
*/
void deleteBlockNodeList(BlockNodeList *list)
{
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 constant.
*
* \param [in] data The boolean constant value.
*
* \return A pointer to a boolean constant which will be FALSE if \a data is \c
* 0 and TRUE otherwise.
*
* \retval NULL Memory allocation failed.
*/
ConstantNode *createBooleanConstantNode(int 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 constant.
*
* \param [in] data The integer constant value.
*
* \return A pointer to an integer constant whose value is \a data.
*
* \retval NULL Memory allocation failed.
*/
ConstantNode *createIntegerConstantNode(long long data)
{
ConstantNode *p = malloc(sizeof(ConstantNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = CT_INTEGER;
p->data.i = data;
return p;
}
/**
* Creates a float constant.
*
* \param [in] data The float constant value.
*
* \return A pointer to a float constant whose value is \a data.
*
* \retval NULL Memory allocation failed.
*/
ConstantNode *createFloatConstantNode(float 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 constant.
*
* \param [in] data The string constant value.
*
* \return A pointer to the string constant whose value is \a data.
*
* \retval NULL Memory allocation failed.
*/
ConstantNode *createStringConstantNode(char *data)
{
ConstantNode *p = malloc(sizeof(ConstantNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = CT_STRING;
p->data.s = data;
return p;
}
/**
* Deletes a constant.
*
* \param [in,out] node The constant to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteConstantNode(ConstantNode *node)
{
if (!node) return;
if (node->type == CT_STRING) free(node->data.s);
free(node);
}
/**
* Creates an indentifier.
*
* \param [in] type The type of the identifier \a id.
*
* \param [in] id The identifier data.
*
* \param [in] slot An optional slot to index.
*
* \param [in] fname The file containing the identifier.
*
* \param [in] line The line the identifier occurred on.
*
* \return A pointer to the identifier with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
IdentifierNode *createIdentifierNode(IdentifierType type,
void *id,
IdentifierNode *slot,
const char *fname,
unsigned int line)
{
IdentifierNode *p = malloc(sizeof(IdentifierNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = type;
p->id = id;
p->slot = slot;
if (fname) {
p->fname = malloc(sizeof(char) * (strlen(fname) + 1));
strcpy(p->fname, fname);
}
else {
p->fname = NULL;
}
p->line = line;
return p;
}
/**
* Deletes an identifier.
*
* \param [in,out] node The identifier to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteIdentifierNode(IdentifierNode *node)
{
if (!node) return;
switch (node->type) {
case IT_DIRECT: {
free(node->id);
break;
}
case IT_INDIRECT: {
deleteExprNode(node->id);
break;
}
default:
error(PR_UNKNOWN_IDENTIFIER_TYPE, node->fname, node->line);
break;
}
if (node->slot) deleteIdentifierNode(node->slot);
if (node->fname) free(node->fname);
free(node);
}
/**
* Creates an identifier list.
*
* \return A pointer to an identifier list.
*
* \retval NULL Memory allocation failed.
*/
IdentifierNodeList *createIdentifierNodeList(void)
{
IdentifierNodeList *p = malloc(sizeof(IdentifierNodeList));
if (!p) {
perror("malloc");
return NULL;
}
p->num = 0;
p->ids = NULL;
return p;
}
/**
* Adds an identifier to a list.
*
* \param [in,out] list The list of identifiers to add \a node to.
*
* \param [in] node The identifier to add to \a list.
*
* \post \a token will be added to the end of \a list and the size of \a list
* will be updated.
*
* \retval 0 Memory allocation failed.
*
* \retval 1 \a node was added to \a list.
*/
int addIdentifierNode(IdentifierNodeList *list,
IdentifierNode *node)
{
unsigned int newsize = list->num + 1;
void *mem = realloc(list->ids, sizeof(IdentifierNode *) * newsize);
if (!mem) {
perror("realloc");
return 0;
}
list->ids = mem;
list->ids[list->num] = node;
list->num = newsize;
return 1;
}
/**
* Deletes an identifier list.
*
* \param [in,out] list The list of identifiers to delete.
*
* \post The memory at \a list and all of its members will be freed.
*/
void deleteIdentifierNodeList(IdentifierNodeList *list)
{
unsigned int n;
if (!list) return;
for (n = 0; n < list->num; n++)
deleteIdentifierNode(list->ids[n]);
free(list->ids);
free(list);
}
/**
* Creates a type.
*
* \param [in] type The type to create.
*
* \return A pointer to a new type with the desired properties.
*
* \retval NULL Memory allocatin failed.
*/
TypeNode *createTypeNode(ConstantType type)
{
TypeNode *p = malloc(sizeof(TypeNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = type;
return p;
}
/**
* Deletes a type.
*
* \param [in,out] node The type to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteTypeNode(TypeNode *node)
{
if (!node) return;
free(node);
}
/**
* Creates a statement.
*
* \param [in] type The type of statement.
*
* \param [in] stmt The statement data.
*
* \return A pointer to a statement with the desired properties.
*
* \retval NULL malloc was unable to allocate memory.
*/
StmtNode *createStmtNode(StmtType type,
void *stmt)
{
StmtNode *p = malloc(sizeof(StmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->type = type;
p->stmt = stmt;
return p;
}
/**
* Deletes a statement.
*
* \param [in,out] node The statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteStmtNode(StmtNode *node)
{
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_DEALLOCATION: {
DeallocationStmtNode *stmt = (DeallocationStmtNode *)node->stmt;
deleteDeallocationStmtNode(stmt);
break;
}
case ST_FUNCDEF: {
FuncDefStmtNode *stmt = (FuncDefStmtNode *)node->stmt;
deleteFuncDefStmtNode(stmt);
break;
}
case ST_EXPR: {
ExprNode *expr = (ExprNode *)node->stmt;
deleteExprNode(expr);
break;
}
case ST_ALTARRAYDEF: {
AltArrayDefStmtNode *stmt = (AltArrayDefStmtNode *)node->stmt;
deleteAltArrayDefStmtNode(stmt);
break;
}
default:
error(PR_UNKNOWN_STATEMENT_TYPE);
break;
}
free(node);
}
/**
* Creates an empty statement list.
*
* \return A pointer to an empty statement list.
*
* \retval NULL Memory allocation failed.
*/
StmtNodeList *createStmtNodeList(void)
{
StmtNodeList *p = malloc(sizeof(StmtNodeList));
if (!p) {
perror("malloc");
return NULL;
}
p->num = 0;
p->stmts = NULL;
return p;
}
/**
* Adds a statement to a list.
*
* \param [in,out] list The statement list to add \a node to.
*
* \param [in] node The statement to add to \a list.
*
* \post \a node will be added to \a list and its size will be updated.
*
* \retval 0 Memory allocation failed.
*
* \retval 1 \a node was added to \a list.
*/
int addStmtNode(StmtNodeList *list,
StmtNode *node)
{
unsigned int newsize = list->num + 1;
void *mem = realloc(list->stmts, sizeof(StmtNode *) * newsize);
if (!mem) {
perror("realloc");
return 0;
}
list->stmts = mem;
list->stmts[list->num] = node;
list->num = newsize;
return 1;
}
/**
* Deletes a statement list.
*
* \param [in,out] list The statement list to delete.
*
* \post The memory at \a list and all of its members will be freed.
*/
void deleteStmtNodeList(StmtNodeList *list)
{
unsigned int n;
if (!list) return;
for (n = 0; n < list->num; n++)
deleteStmtNode(list->stmts[n]);
free(list->stmts);
free(list);
}
/**
* Creates a cast statement.
*
* \param [in] target The variable to cast to \a newtype.
*
* \param [in] newtype The type to cast \a target to.
*
* \return A pointer to a cast statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
CastStmtNode *createCastStmtNode(IdentifierNode *target,
TypeNode *newtype)
{
CastStmtNode *p = malloc(sizeof(CastStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;
p->newtype = newtype;
return p;
}
/**
* Deletes a cast statement.
*
* \param [in,out] node The cast statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteCastStmtNode(CastStmtNode *node)
{
if (!node) return;
deleteIdentifierNode(node->target);
deleteTypeNode(node->newtype);
free(node);
}
/**
* Creates a print statement.
*
* \param [in] args The expressions to print.
*
* \param [in] file Where to print (\c stdout or \c stderr).
*
* \param [in] nonl Whether an ending newline should be surpressed.
*
* \return A pointer to the print statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
PrintStmtNode *createPrintStmtNode(ExprNodeList *args,
FILE *file,
int nonl)
{
PrintStmtNode *p = malloc(sizeof(PrintStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->args = args;
p->file = file;
p->nonl = nonl;
return p;
}
/**
* Deletes a print statement.
*
* \param [in,out] node The print statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deletePrintStmtNode(PrintStmtNode *node)
{
if (!node) return;
deleteExprNodeList(node->args);
free(node);
}
/**
* Creates an input statement.
*
* \param [in] target The variable to store input in.
*
* \return A pointer to an input statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
InputStmtNode *createInputStmtNode(IdentifierNode *target)
{
InputStmtNode *p = malloc(sizeof(InputStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;
return p;
}
/**
* Deletes an input statement.
*
* \param [in,out] node The input statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteInputStmtNode(InputStmtNode *node)
{
if (!node) return;
deleteIdentifierNode(node->target);
free(node);
}
/**
* Creates an assignment statement.
*
* \param [in] target The variable to store \a expr in.
*
* \param [in] expr The expression to store in \a target.
*
* \return A pointer to an assignment statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
AssignmentStmtNode *createAssignmentStmtNode(IdentifierNode *target,
ExprNode *expr)
{
AssignmentStmtNode *p = malloc(sizeof(AssignmentStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;
p->expr = expr;
return p;
}
/**
* Deletes an assignment statement.
*
* \param [in,out] node The assignment statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteAssignmentStmtNode(AssignmentStmtNode *node)
{
if (!node) return;
deleteIdentifierNode(node->target);
deleteExprNode(node->expr);
free(node);
}
/**
* Creates a declaration statement.
*
* \param [in] scope The scope to create the variable in.
*
* \param [in] target The variable to create.
*
* \param [in] expr An optional expression to initialize \a target to.
*
* \param [in] type An optional type to initialize \a target to.
*
* \param [in] parent The optional parent to inherit from.
*
* \return A pointer to a declaration statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
DeclarationStmtNode *createDeclarationStmtNode(IdentifierNode *scope,
IdentifierNode *target,
ExprNode *expr,
TypeNode *type,
IdentifierNode *parent)
{
DeclarationStmtNode *p = malloc(sizeof(DeclarationStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->scope = scope;
p->target = target;
p->expr = expr;
p->type = type;
p->parent = parent;
return p;
}
/**
* Deletes a declaration statement.
*
* \param [in,out] node The declaration statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteDeclarationStmtNode(DeclarationStmtNode *node)
{
if (!node) return;
deleteIdentifierNode(node->scope);
deleteIdentifierNode(node->target);
deleteExprNode(node->expr);
deleteTypeNode(node->type);
deleteIdentifierNode(node->parent);
free(node);
}
/**
* Creates an if/then/else statement.
*
* \param [in] yes The code block to execute if the \ref impvar "implicit
* variable" is \c true.
*
* \param [in] no The code block to execute if the \ref impvar "implicit
* variable" is \c false and all \a guards are \c false.
*
* \param [in] guards The expressions to test if the \ref impvar "implicit
* variable" is \c false.
*
* \param [in] blocks The code blocks to execute if the respective guard is \c
* true.
*
* \return A pointer to the if/then/else statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
IfThenElseStmtNode *createIfThenElseStmtNode(BlockNode *yes,
BlockNode *no,
ExprNodeList *guards,
BlockNodeList *blocks)
{
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 an if/then/else statement.
*
* \param [in,out] node The if/then/else statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteIfThenElseStmtNode(IfThenElseStmtNode *node)
{
if (!node) return;
deleteBlockNode(node->yes);
deleteBlockNode(node->no);
deleteExprNodeList(node->guards);
deleteBlockNodeList(node->blocks);
free(node);
}
/**
* Creates a switch statement.
*
* \param [in] guards The expressions to compare the the \ref impvar "implicit
* variable".
*
* \param [in] blocks The code blocks to execute if a respective guard matches
* the \ref impvar "implicit variable".
*
* \param [in] def The default code block to execute if none of the \a guards
* match the \ref impvar "implicit variable".
*
* \return A pointer to a switch statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
SwitchStmtNode *createSwitchStmtNode(ExprNodeList *guards,
BlockNodeList *blocks,
BlockNode *def)
{
SwitchStmtNode *p = malloc(sizeof(SwitchStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->guards = guards;
p->blocks = blocks;
p->def = def;
return p;
}
/**
* Deletes a switch statement.
*
* \param [in,out] node The switch statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteSwitchStmtNode(SwitchStmtNode *node)
{
if (!node) return;
deleteExprNodeList(node->guards);
deleteBlockNodeList(node->blocks);
deleteBlockNode(node->def);
free(node);
}
/**
* Creates a return statement.
*
* \param [in] value The return value.
*
* \return A pointer to a return statement of the desired properties.
*
* \retval NULL Memory allocation failed.
*/
ReturnStmtNode *createReturnStmtNode(ExprNode *value)
{
ReturnStmtNode *p = malloc(sizeof(ReturnStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->value = value;
return p;
}
/**
* Deletes a return statement.
*
* \param [in,out] node The return statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteReturnStmtNode(ReturnStmtNode *node)
{
if (!node) return;
deleteExprNode(node->value);
free(node);
}
/**
* Creates a loop statement.
*
* \param [in] name The name of the loop.
*
* \param [in] var The induction variable.
*
* \param [in] guard The expression to determine if the loop continues.
*
* \param [in] update The expression to update \a var using.
*
* \param [in] body The code block to execute with each loop iteration.
*
* \return A pointer to a loop statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
LoopStmtNode *createLoopStmtNode(IdentifierNode *name,
IdentifierNode *var,
ExprNode *guard,
ExprNode *update,
BlockNode *body)
{
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 loop statement.
*
* \param [in,out] node The loop statement to delete.
*
* \post The memory at \a node and all of its members will be freed.
*/
void deleteLoopStmtNode(LoopStmtNode *node)
{
if (!node) return;
deleteIdentifierNode(node->name);
deleteIdentifierNode(node->var);
deleteExprNode(node->guard);
deleteExprNode(node->update);
deleteBlockNode(node->body);
free(node);
}
/**
* Creates a deallocation statement.
*
* \param [in] target The variable to deallocate.
*
* \return A pointer to a deallocation statement with the desired properties.
*
* \retval NULL Memory allocation failed.
*/
DeallocationStmtNode *createDeallocationStmtNode(IdentifierNode *target)
{
DeallocationStmtNode *p = malloc(sizeof(DeallocationStmtNode));
if (!p) {
perror("malloc");
return NULL;
}
p->target = target;