-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbollist.c
168 lines (148 loc) · 3.74 KB
/
symbollist.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
#include "symbollist.h"
/**
* print all the values in the list for the head node to EOF
* if the list is empty print to the user
*/
void print_symbol_list()
{
symbolListPtr current = symbolListHead;
if(symbolListHead == NULL) {
printf("\n the list is empty\n");
return;
}
printf("--------Symbols List-------\n");
while(current != NULL) {
printf("--------------------------\n");
printf("name: %s\n",current->symbol.name);
printf("type: %d\n",current->symbol.type);
printf("value: %d\n",current->symbol.value);
printf("size: %d\n",current->symbol.size);
printf("isMacro: %d\n",current->symbol.isMacro);
printf("isExternal: %d\n",current->symbol.isExternal);
printf("--------------------------\n");
current = current->next;
}
}
void push_symbol_to_list(Symbol symbol)
{
symbolListPtr current = symbolListHead;
/* if head is empty*/
if(symbolListHead == NULL) {
/* push item to head*/
symbolListHead = (symbolListPtr) malloc(sizeof(SymbolListItem));
symbolListHead->symbol = symbol;
symbolListHead->next = NULL;
} else {
/* go to last list item*/
while(current->next != NULL) {
current = current->next;
}
/* push symbol to last item */
current->next = (symbolListPtr) malloc(sizeof(SymbolListItem));
current->next->symbol = symbol;
current->next->next = NULL;
}
}
/* remove ONE item from list with the name value the same to the given name */
void remove_from_symbol_list(char *nameToRemove)
{
/* two pointer one is helper that get the priveus node and one that run on the list */
symbolListPtr current = symbolListHead;
symbolListPtr helper=symbolListHead-1;
/*if the list is empty the function return with a messege that the list is empty */
if(symbolListHead == NULL){
printf("\n the list is empty\n");
return;
}
/*
* run on the list with str cmp if value equal to zero
* (mean that the name is equal to the given) the curret node next replaced with the next
* of the helper and get freed then the function break.
*/
while(current->next != NULL) {
if (strcmp(current->symbol.name, nameToRemove) == 0)
{
helper->next=current->next;
free(current);
break;
}
current = current->next;
helper = helper->next;
}
}
bool is_in_symbol_list(Symbol symbol)
{
symbolListPtr current = symbolListHead;
if(current != NULL) {
while(current != NULL) {
if (strcmp(current->symbol.name, symbol.name) == 0) {
return true;
}
current = current->next;
}
}
return false;
}
void add_symbol_to_list(Symbol symbol)
{
if(is_in_symbol_list(symbol)) {
printf("WARNING: %s declared twice, ignored.\n", symbol.name);
return;
}
push_symbol_to_list(symbol);
}
int get_symbol_value_by_name(char * name)
{
symbolListPtr current = symbolListHead;
while(current != NULL) {
if (strcmp(current->symbol.name, name) == 0) {
return current->symbol.value;
}
current = current->next;
}
return -1;
}
bool is_symbol_external(char * name)
{
symbolListPtr current = symbolListHead;
while(current != NULL) {
if (strcmp(current->symbol.name, name) == 0) {
return current->symbol.isExternal;
}
current = current->next;
}
return false;
}
bool is_symbol_macro(char * name)
{
symbolListPtr current = symbolListHead;
while(current != NULL) {
if (strcmp(current->symbol.name, name) == 0) {
return current->symbol.isMacro;
}
current = current->next;
}
return false;
}
bool is_symbol_in_list(char * name)
{
symbolListPtr current = symbolListHead;
while(current != NULL) {
if (strcmp(current->symbol.name, name) == 0) {
return true;
}
current = current->next;
}
return false;
}
void reset_symbol_list()
{
symbolListPtr current = symbolListHead;
while(current) {
current->symbol.name = NULL;
free(current->symbol.name);
free(current);
current = current->next;
}
symbolListHead = NULL;
}