-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab.c
More file actions
112 lines (93 loc) · 1.8 KB
/
Copy pathsymtab.c
File metadata and controls
112 lines (93 loc) · 1.8 KB
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
#include "shell.h"
#include "node.h"
#include "symtab.h"
ST_S_strc symtab_stack;
int symtab_level;
/**
* init_symtab - initialize symbol table
*/
void init_symtab(void) /* Don't touch this function */
{
ST_strc *global_symtab;
symtab_stack.symtab_count = 1;
symtab_level = 0;
global_symtab = malloc(sizeof(ST_strc));
if (!global_symtab)
{
print("fatal error: no memory for global symbol table\n", STDERR_FILENO);
exit(EXIT_FAILURE);
}
_memset(global_symtab, 0, sizeof(ST_strc));
symtab_stack.global_symtab = global_symtab;
symtab_stack.local_symtab = global_symtab;
symtab_stack.symtab_list[0] = global_symtab;
global_symtab->level = 0;
}
/**
* new_symtab - create a new symbol table
* @level: The level of the symbol table
*
* Return: The new symbol table
*/
ST_strc *new_symtab(int level)
{
ST_strc *symtab = malloc(sizeof(ST_strc));
if (!symtab)
{
print("fatal error: no memory for new symbol table\n", STDERR_FILENO);
exit(EXIT_FAILURE);
}
memset(symtab, 0, sizeof(ST_strc));
symtab->level = level;
return (symtab);
}
/**
* free_symtab - free a symbol table
* @symtab: The symbol table
*
*/
void free_symtab(ST_strc *symtab)
{
ST_entry *entry;
ST_entry *next;
if (symtab == NULL)
return;
entry = symtab->first;
while (entry)
{
if (entry->name)
{
free(entry->name);
}
if (entry->val)
{
free(entry->val);
}
if (entry->func_body)
{
free_node_tree(entry->func_body);
}
next = entry->next;
free(entry);
entry = next;
}
free(symtab);
}
/**
* free_symtab2 - free a symbol table
* @symtab: The symbol table
*
*/
void free_symtab2(ST_strc *symtab)
{
ST_entry *current = symtab->first;
while (current != NULL)
{
ST_entry *temp = current;
current = current->next;
free(temp->name);
free(temp->val);
free(temp);
}
free(symtab);
}