-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymtab_2.c
More file actions
175 lines (148 loc) · 2.88 KB
/
symtab_2.c
File metadata and controls
175 lines (148 loc) · 2.88 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
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
#include "shell.h"
#include "node.h"
#include "symtab.h"
/**
* add_to_symtab - Add a symbol table entry
* @symbol: The symbol table entry's name
*
* Return: The symbol table entry
*/
ST_entry *add_to_symtab(char *symbol)
{
ST_strc *st;
ST_entry *entry = NULL;
if (!symbol || symbol[0] == '\0')
return (NULL);
st = symtab_stack.local_symtab;
entry = do_lookup(symbol, st);
if (entry)
return (entry);
entry = malloc(sizeof(ST_entry));
if (!entry)
{
print("fatal error: no memory for new symbol table entry\n", STDERR_FILENO);
exit(EXIT_FAILURE);
}
_memset(entry, 0, sizeof(ST_entry));
entry->name = malloc(strlen(symbol) + 1);
if (!entry->name)
{
print("fatal error: no memory for new symbol table entry\n", STDERR_FILENO);
exit(EXIT_FAILURE);
}
strcpy(entry->name, symbol);
if (!st->first)
{
st->first = entry;
st->last = entry;
}
else
{
st->last->next = entry;
st->last = entry;
}
return (entry);
}
/**
* rem_from_symtab - Remove a symbol table entry
* @entry: The symbol table entry
* @symtab: The symbol table
*
* Return: 1 if the entry was removed, 0 otherwise
*/
int rem_from_symtab(ST_entry *entry, ST_strc *symtab)
{
int res = 0;
if (entry->val)
free(entry->val);
if (entry->func_body)
free_node_tree(entry->func_body);
free(entry->name);
if (symtab->first == entry)
{
symtab->first = symtab->first->next;
if (symtab->last == entry)
symtab->last = NULL;
res = 1;
}
else
{
ST_entry *e = symtab->first;
ST_entry *p = NULL;
while (e && e != entry)
{
p = e;
e = e->next;
}
if (e == entry)
{
p->next = entry->next;
res = 1;
}
}
free(entry);
return (res);
}
/**
* do_lookup - Look up a symbol table entry
* @str: The symbol table entry's name
* @symtable: The symbol table
*
* Return: The symbol table entry
*/
ST_entry *do_lookup(char *str, ST_strc *symtable)
{
ST_entry *entry;
if (!str || !symtable)
return (NULL);
entry = symtable->first;
while (entry)
{
if (strcmp(entry->name, str) == 0)
{
return (entry);
}
entry = entry->next;
}
return (NULL);
}
/**
* get_symtab_entry - Get a symbol table entry
* @str: The symbol table entry's name
*
* Return: The symbol table entry
*/
ST_entry *get_symtab_entry(char *str)
{
int i = symtab_stack.symtab_count - 1;
do {
ST_strc *symtab = symtab_stack.symtab_list[i];
ST_entry *entry = do_lookup(str, symtab);
if (entry)
return (entry);
} while (--i >= 0);
return (NULL);
}
/**
* symtab_entry_setval - Set the value of a symbol table entry
* @entry: The symbol table entry
* @val: The value to set
*/
void symtab_entry_setval(ST_entry *entry, char *val)
{
if (entry->val)
free(entry->val);
if (!val)
{
entry->val = NULL;
}
else
{
char *val2 = malloc(strlen(val) + 1);
if (val2)
strcpy(val2, val);
else
print("error: no memory for symbol table entry's value\n", STDERR_FILENO);
entry->val = val2;
}
}