-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypetable.cpp
More file actions
126 lines (102 loc) · 2.65 KB
/
typetable.cpp
File metadata and controls
126 lines (102 loc) · 2.65 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
#include <stdlib.h>
#include <math.h>
#include "exptree.hpp"
#include <string.h>
#include <string.h>
#include "y.tab.hpp"
#include <stdio.h>
#include <iostream>
using namespace std;
char *INTEGER_NAME;
char *BOOLEAN_NAME;
char *VOID_NAME;
char *ID_NAME;
struct Typetable *Typetable_Table = NULL;
struct Fieldlist *Fieldlist_Table = NULL;
void Typetable_Crate()
{
/**For initilizing the Typetable entries to the primitive data types.**/
INTEGER_NAME = (char *)malloc(20*sizeof(char));
BOOLEAN_NAME = (char *)malloc(20*sizeof(char));
VOID_NAME = (char *)malloc(20*sizeof(char));
ID_NAME = (char *)malloc(20*sizeof(char));
strcpy(INTEGER_NAME,"integer");
strcpy(BOOLEAN_NAME,"boolean");
strcpy(VOID_NAME,"void");
strcpy(ID_NAME,"ID");
Tinstall(INTEGER_NAME, NULL);
Tinstall(BOOLEAN_NAME,NULL);
Tinstall(VOID_NAME,NULL);
Tinstall(ID_NAME,NULL);
}
struct Typetable *Tlookup(char *NAME)
{
/**Searches through the typetable and returns the pointer to the type table entry of type name**/
struct Typetable *temp = new Typetable;
temp = Typetable_Table;
while(temp != NULL)
{
if (strcmp(temp->NAME,NAME) == 0)
{
return temp;
}
temp = temp->Next;
}
return temp;
}
struct Typetable *Tinstall(char *NAME,struct Fieldlist *Fields)
{
/**
Installs a new type table entry for the name with the given fields and returns the pointer to the typetable Entry.
**/
struct Typetable *new_node = new Typetable;
new_node->NAME = (char *)malloc(20*sizeof(char));
if (Tlookup(NAME) == NULL)
{
// cout<<" ="<<Fields<<endl;
strcpy(new_node->NAME,NAME);
new_node->Fields = new Fieldlist;
new_node->Fields = Fields;
new_node->Next = Typetable_Table;
Typetable_Table = new_node;
}
else
{
yyerror("redeclaration of ‘" + string(NAME) + "’");
}
return new_node;
}
void Finstall(char *NAME,struct Typetable *TYPE)
{
/**Creates a Fieldlist entry with the given name and the type**/
struct Fieldlist *new_node = new Fieldlist;
if (Tlookup(NAME) == NULL)
{
new_node->NAME = (char *)malloc(20*sizeof(char));
strcpy(new_node->NAME,NAME);
new_node->TYPE = new Typetable;
new_node->TYPE = TYPE;
new_node->Next = Fieldlist_Table;
Fieldlist_Table = new_node;
}
else
{
yyerror("redeclaration of ‘" + string(NAME) + "’");
}
}
struct Fieldlist *Flookup(char *NAME,struct Fieldlist *List)
{
/**Searches for the field of the given name in the list and returns the pointer to the matchingg entry.**/
struct Fieldlist *temp = new Fieldlist;
temp = List;
while(temp != NULL)
{
// cout<<"NAME = "<<temp->NAME<<" NAME = "<<NAME<<endl;
if (strcmp(temp->NAME,NAME) == 0)
{
return temp;
}
temp = temp->Next;
}
return temp;
}