-
Notifications
You must be signed in to change notification settings - Fork 0
/
typetable.c
113 lines (110 loc) · 2.97 KB
/
typetable.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
#include "typetable.h"
// FUNCTIONS FOR DATA TYPE TABLE
// Search Type table entry by Name
struct typeTable * getTypeTuple(char * dataType){
struct typeTable *typeTuple;
typeTuple = typeTableHead;
while(typeTuple){
if(strcmp(dataType,typeTuple->name) == 0){
return typeTuple;
}
typeTuple = typeTuple->next;
}
return NULL;
}
// To create a new type table entry
struct typeTable * newTypeTuple(){
//printf("Reached Here");
struct typeTable *t;
t = malloc(sizeof(struct typeTable));
if(t == NULL){
outOfMemory();
}
return t;
}
//Function to initialize typeTableHead and typeTableTail
void createTypeTableHead(char * type,int size){
//printf("Reached Here\n");
//Initializing Head
typeTableHead = newTypeTuple();
typeTableHead->name = malloc(strlen(type) + 1);
strcpy(typeTableHead->name,type);
typeTableHead->size = size;
typeTableHead->fields = NULL;
typeTableHead->next = NULL;
typeTableTail = typeTableHead;
}
//Fuction to add new entry to type Table
void addNewTypeTableEntry(char * type, int size){
//printf("Reached Here\n");
typeTableTail->next = newTypeTuple();
//Newly Allocated one is the new tail
typeTableTail = typeTableTail->next;
typeTableTail->name = malloc(strlen(type) + 1);
strcpy(typeTableTail->name,type);
typeTableTail->size = size;
typeTableTail->fields = NULL;
typeTableTail->next = NULL;
}
//Fuction For Creating Basic Data Types {int,str,bool}
void makeBasicTypeTable(){
//printf("Reached Here");
createTypeTableHead("integer",1);
addNewTypeTableEntry("char",1);
addNewTypeTableEntry("boolean",1);
}
//Function To Get Size of data type from Type Table
int getDataTypeSize(char *dataType){
struct typeTable *typeTuple;
typeTuple = typeTableHead;
while(typeTuple){
if(strcmp(dataType,typeTuple->name) == 0){
return typeTuple->size;
}
typeTuple = typeTuple->next;
}
return 0;
}
struct typeTable * installType(char * name,struct Fieldlist * fields){
typeTableTail->next = newTypeTuple();
typeTableTail = typeTableTail->next;
typeTableTail->fields = fields;
typeTableTail->name = name;
}
struct Fieldlist * installFieldList(char * name,struct typeTable * typeTuple){
struct Fieldlist * field;
field = malloc(sizeof(struct Fieldlist));
field->name = name;
field->type = typeTuple;
field->next = NULL;
return field;
}
int getAttributeBinding(char * name,struct typeTable * type){
struct Fieldlist * fields;
fields = type->fields;
int ret = 0;
while(fields){
if(strcmp(fields->name,name) == 0){
return ret;
}
ret += 1;
fields = fields->next;
}
return -1;
}
//To Print Type Table Entries (For Debugging)
void printTypeTable(){
struct typeTable *t;
t = typeTableHead;
while(t){
printf("%s\n",t->name);
struct Fieldlist * field;
field = t->fields;
while(field){
printf("\t%s\n",field->name);
printf("\t\t type - %s , size - %d\n",field->type->name,field->type->size);
field = field->next;
}
t=t->next;
}
}