-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.c
More file actions
194 lines (176 loc) · 5.54 KB
/
Copy pathvariable.c
File metadata and controls
194 lines (176 loc) · 5.54 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**************************************************************************
* C S 429 EEL interpreter
*
* variable.c - This file contains the skeleton of functions to be implemented
* for EEL-2. When completed, it will contain the code to maintain a hashtable
* for defined variables.
*
* Work on it only after finishing EEL-0 and EEL-1.
*
* Copyright (c) 2021. S. Chatterjee, X. Shen, T. Byrd. All rights reserved.
* May not be used, modified, or copied without permission.
**************************************************************************/
#include "ci.h"
table_t *var_table = NULL;
static char *bool_print[] = {"false", "true"};
void init_table(void) {
var_table = (table_t *) calloc(1, sizeof(table_t));
if (! var_table) {
logging(LOG_FATAL, "failed to allocate table");
return;
}
var_table->entries = (entry_t **) calloc(CAPACITY, sizeof(entry_t *));
if (! var_table->entries) {
free(var_table);
logging(LOG_FATAL, "failed to allocate entries");
}
return;
}
void delete_entry(entry_t *eptr) {
if (! eptr) return;
if (eptr->type == STRING_TYPE) {
free(eptr->val.sval);
}
free(eptr->id);
free(eptr);
return;
}
void delete_entries(entry_t *eptr) {
if (! eptr) return;
delete_entries(eptr->next);
delete_entry(eptr);
return;
}
void delete_table(void) {
if (! var_table) return;
for (int i = 0; i < CAPACITY; ++i) {
delete_entries(var_table->entries[i]);
}
free(var_table->entries);
free(var_table);
return;
}
/* Pre-defined hash function to index variables by their names. */
unsigned long hash_function(char *s) {
unsigned long i = 0;
for (int j=0; s[j]; j++)
i += s[j];
return i % CAPACITY;
}
/* init_entry() - provided entry constructor
* Parameters: Variable name, pointer to a node.
* Return value: An allocated entry. */
entry_t * init_entry(char *id, node_t *nptr) {
if (nptr == NULL) {
logging(LOG_FATAL, "failed to allocate entry");
return NULL;
}
entry_t *eptr = (entry_t *) calloc(1, sizeof(entry_t));
if (! eptr) {
logging(LOG_FATAL, "failed to allocate entry");
return NULL;
}
eptr->id = (char *) malloc(strlen(id) + 1);
if (! id) {
logging(LOG_FATAL, "failed to allocate entry id");
free(eptr);
return NULL;
}
strcpy(eptr->id, id);
eptr->type = nptr->type;
if (eptr->type == STRING_TYPE) {
(eptr->val).sval = (char *) malloc(strlen(nptr->val.sval) + 1);
if (! eptr->val.sval) {
logging(LOG_FATAL, "failed to allocate string");
free(eptr->id);
free(eptr);
return NULL;
}
strcpy(eptr->val.sval, nptr->val.sval);
} else {
eptr->val.ival = nptr->val.ival;
}
return eptr;
}
/* put() - insert an entry into the hashtable or update the existing entry.
* Use a linked list to handle collisions.
* Parameters: Variable name, pointer to a node.
* Return value: None.
* Side effect: The entry is inserted into the hashtable, or is updated if
* it already exists.
* (STUDENT TODO)
*/
void put(char *id, node_t *nptr) {
// find the proper hashing value
int theHashingIndex = hash_function(id);
if (var_table->entries[theHashingIndex] == NULL) {
var_table->entries[theHashingIndex] = init_entry(id, nptr);
} else {
entry_t *newEntry = var_table->entries[theHashingIndex];
while (newEntry->next != NULL && strcmp(newEntry->id, id) != 0) {
newEntry = newEntry->next;
}
if ((strcmp(newEntry->id, id) == 0)) {
newEntry->type = nptr->type;
if (nptr->type == STRING_TYPE) {
// malloc space.
newEntry->val.sval = malloc(strlen(nptr->val.sval) + 1);
strcpy(newEntry->val.sval, nptr->val.sval);
} else if (nptr->type == INT_TYPE) {
newEntry->val.ival = nptr->val.ival;
}
} else {
newEntry->next = init_entry(id, nptr);
}
}
}
/* get() - search for an entry in the hashtable.
* Parameter: Variable name.
* Return value: Pointer to the matching entry, or NULL if not found.
* (STUDENT TODO)
*/
entry_t* get(char* id) {
int theHashingIndex = hash_function(id);
if (var_table->entries[theHashingIndex] == NULL) {
return NULL;
} else {
entry_t *newEntry = var_table->entries[theHashingIndex];
while (newEntry != NULL && (strcmp(newEntry->id, id) != 0))
newEntry = newEntry->next;
if (newEntry == NULL)
return NULL;
else
return newEntry;
}
}
void print_entry(entry_t *eptr) {
if (! eptr) return;
switch (eptr->type) {
case INT_TYPE:
fprintf(outfile, "%s = %d; ", eptr->id, eptr->val.ival);
break;
case BOOL_TYPE:
fprintf(outfile, "%s = %s; ", eptr->id, bool_print[eptr->val.bval]);
break;
case STRING_TYPE:
fprintf(outfile, "%s = \"%s\"; ", eptr->id, eptr->val.sval);
break;
default:
logging(LOG_ERROR, "unsupported entry type for printing");
break;
}
print_entry(eptr->next);
return;
}
void print_table(void) {
if (! var_table) {
logging(LOG_ERROR, "variable table doesn't exist");
return;
}
fprintf(outfile, "\t");
for (int i = 0; i < CAPACITY; ++i) {
print_entry(var_table->entries[i]);
}
fprintf(outfile, "\n");
return;
}