-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtable.c
158 lines (135 loc) · 3.68 KB
/
symtable.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
/* IFJ Project 2018/19
* Implementation of Symbol Table using Hash Table
*
* TEAM:
* Simon Kobyda
* Michal Zelenak
* Juraj Samuel Salon
* Denis Filo
*
* FILE: symtable.c
* FILE AUTHOR: Simon Kobyda
*/
/***SYSTEM FILES***/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
/***LOCAL FILES***/
#include "symtable.h"
SymTablePtr globalTable = NULL; //table contains all identifiers of functions
static bool SymTableInsertFunctions(SymTablePtr table) {
char* functions[8];
functions[0] = malloc(sizeof(char) * 7);
functions[1] = malloc(sizeof(char) * 7);
functions[2] = malloc(sizeof(char) * 7);
functions[3] = malloc(sizeof(char) * 7);
functions[4] = malloc(sizeof(char) * 7);
functions[5] = malloc(sizeof(char) * 7);
functions[6] = malloc(sizeof(char) * 7);
functions[7] = malloc(sizeof(char) * 7);
strcpy (functions[0], "print");
strcpy (functions[1], "inputs");
strcpy (functions[2], "inputi");
strcpy (functions[3], "inputf");
strcpy (functions[4], "substr");
strcpy (functions[5], "length");
strcpy (functions[6], "ord");
strcpy (functions[7], "chr");
for (int i = 0; i < 8; i++) {
SymbolPtr symbol = malloc(sizeof(struct Symbol));
if (!symbol) {
printf("ERROR: malloc of symbol\n");
return false;
}
symbol->name = functions[i];
symbol->nextSymbol = NULL;
symbol->iType = FUNCTION;
switch (i) {
case 0:
symbol->numOfParameters = -1;
break;
case 1: case 2: case 3:
symbol->numOfParameters = 0;
break;
case 4:
symbol->numOfParameters = 3;
break;
case 5:
symbol->numOfParameters = 1;
break;
case 6:
symbol->numOfParameters = 2;
break;
case 7:
symbol->numOfParameters = 1;
break;
default:
break;
}
SymTableAdd(table, symbol);
}
return true;
}
static unsigned int HashFunction(char* key) {
int val = 0;
for (unsigned int i = 0; i < strlen(key); i++)
val += key[i];
return val % TABLESIZE;
}
SymTablePtr SymTableInit(SymTablePtr ParentTable) {
SymTablePtr table = NULL;
table = malloc(sizeof(struct SymTable));
if (!table) {
return NULL;
}
table->parentTable = ParentTable;
for (int i = 0; i < TABLESIZE; i++)
table->arr[i] = NULL;
if(SymTableInsertFunctions(table))
return table;
else
return NULL;
}
void SymTableDestroy(SymTablePtr table) {
for (int i = 0; i < TABLESIZE; i++) {
SymbolPtr symbol = table->arr[i];
while(symbol) {
SymbolPtr tmp = symbol;
symbol = tmp->nextSymbol;
free(tmp->name);
free(tmp);
}
}
free(table);
}
void SymTableAdd(SymTablePtr table, SymbolPtr symbol) {
if (!symbol) {
return;
}
if (!table) {
return;
}
unsigned int index = HashFunction(symbol->name);
SymbolPtr tmp = table->arr[index];
if (!tmp) {
table->arr[index] = symbol;
} else {
while(tmp->nextSymbol)
tmp = tmp->nextSymbol;
tmp->nextSymbol = symbol;
}
}
SymbolPtr SymTableFind(SymTablePtr table, char *name) {
if (!name) {
return NULL;
}
unsigned int index = HashFunction(name);
SymbolPtr symbol = table->arr[index];
while (symbol) {
if (!strcmp(symbol->name, name))
break;
symbol = symbol->nextSymbol;
}
return symbol;
}