-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolTable.h
76 lines (65 loc) · 1.97 KB
/
symbolTable.h
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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
struct Entry {
char type[50];
bool isArray;
int arraySize;
float array[100];
float value;
int line;
};
struct SymbolTable {
char keys[50][50];
struct Entry values[50];
int nextEntryIndex;
};
struct SymbolTable symbolTableInsert(struct SymbolTable s, char k[], char t[], bool isArr, float v, int l, int arrSize)
{
strcpy(s.keys[s.nextEntryIndex], k);
struct Entry newEntry;
strcpy(newEntry.type, t);
newEntry.value = v;
newEntry.line = l;
newEntry.isArray = isArr;
if (isArr)
{
newEntry.arraySize = arrSize;
printf("It's an array with size: %d\n", newEntry.arraySize);
}
s.values[s.nextEntryIndex] = newEntry;
printf("AFTER insert: %s, %s, %d, %f, %d\n", s.keys[s.nextEntryIndex], s.values[s.nextEntryIndex].type, s.values[s.nextEntryIndex].isArray, s.values[s.nextEntryIndex].value, s.values[s.nextEntryIndex].line);
return s;
}
struct Entry symbolTableGet( struct SymbolTable s, int index )
{
return s.values[index];
}
struct SymbolTable symbolTableUpdate(struct SymbolTable s, int index, char t[], bool isArr, float v, int l, int arrSize)
{
strcpy(s.values[index].type, t);
s.values[index].isArray = isArr;
s.values[index].value = v;
s.values[index].line = l;
if (isArr)
{
s.values[index].arraySize = arrSize;
}
printf("AFTER update: %s, %s, %d, %f, %d\n", s.keys[index], s.values[index].type, s.values[index].isArray, s.values[index].value, s.values[index].line);
return s;
}
int symbolTableContains(struct SymbolTable s, char k[] )
{
printf("FOR k: %s\n", k);
for(int i = 0; i < s.nextEntryIndex; i++) {
printf("FOR contains: %i, %s\n", i, s.keys[i]);
if (strcmp(s.keys[i], k) == 0) {
return i;
}
}
return -1;
}
// remember:
// check if already exists before insert
// increase next entry index (check if too large)
// delete test table