This repository was archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathast.c
73 lines (67 loc) · 1.91 KB
/
ast.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
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "utils.h"
/*
Creates a new AST from a given type filename and linenum. You should not
assume that filename will remain a legal pointer after this function
terminates.
*/
AST* MakeAST(enum NodeType type, char* filename, int linenum) {
/* YOUR CODE HERE. */
return NULL;
}
/*
Takes in a given AST and mallocs a new AST with the exact same details as the
original. Again you should assume the original may be freed at any point
after this function terminates.
*/
AST* CopyAST(AST* original) {
AST* ast = MakeAST(original->type, original->filename, original->linenum);
ast->size = original->size;
ast->capacity = original->capacity;
ast->children = (AST**)realloc(ast->children, sizeof(AST*) * ast->capacity);
if (ast->children == NULL) {
allocation_failed();
}
for (int i = 0; i < ast->size; i++) {
ast->children[i] = CopyAST(original->children[i]);
}
/* Start Unique to Copy */
if (ast->type == NODETYPE_ID) {
ast->data.identifier =
(char*)malloc(sizeof(char) * (strlen(original->data.identifier) + 1));
if (ast->data.identifier == NULL) {
allocation_failed();
}
strcpy(ast->data.identifier, original->data.identifier);
} else if (ast->type == NODETYPE_CONSTANT_STRING) {
ast->data.string =
(char*)malloc(sizeof(char) * (strlen(original->data.string) + 1));
if (ast->data.string == NULL) {
allocation_failed();
}
strcpy(ast->data.string, original->data.string);
}
/* End of Unique to Copy */
return ast;
}
/*
Takes in an two ASTs and concatenates the two by making node a child
of tree.
*/
void AppendAST(AST* tree, AST* node) {
/* YOUR CODE HERE */
}
/*
Frees the memory allocated by a single AST node.
*/
void FreeNode(AST* ast) {
/* YOUR CODE HERE */
}
/*
Frees all the memory allocated by an AST.
*/
void FreeAST(AST* ast) {
/* YOUR CODE HERE */
}