-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_expression.c
211 lines (192 loc) · 7.11 KB
/
type_expression.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Group 36
2017B4A70495P Manan Soni
2017B4A70549P Siddharth Singh
2017B4A70636P Nayan Khanna
2017B4A70740P Aditya Tulsyan
*/
#include "type_expression.h"
extern linked_list* all_errors;
type_expression *get_integer_type()
{
type_expression *txp = (type_expression *)calloc(1, sizeof(type_expression));
set_declare_flag(txp);
txp->variable_type = PRIMITIVE_TYPE;
txp->union_to_be_named.primitive_data = t_INTEGER;
return txp;
}
type_expression *get_bool_type()
{
type_expression *txp = (type_expression *)calloc(1, sizeof(type_expression));
set_declare_flag(txp);
txp->variable_type = PRIMITIVE_TYPE;
txp->union_to_be_named.primitive_data = t_BOOLEAN;
return txp;
}
type_expression *get_real_type()
{
type_expression *txp = (type_expression *)calloc(1, sizeof(type_expression));
set_declare_flag(txp);
txp->variable_type = PRIMITIVE_TYPE;
txp->union_to_be_named.primitive_data = t_REAL;
return txp;
}
char *get_str_primitive_type(t_primitive_type primitive_data)
{
switch(primitive_data){
case(t_BOOLEAN):
return "bool";
break;
case(t_INTEGER):
return "integer";
break;
case(t_REAL):
return "real";
break;
}
}
void print_type_expression(type_expression *tp)
{
if(!tp)
printf("NULL");
char buffer[30];
strcpy(buffer, get_string_representation(tp));
printf("%s\n", buffer);
}
char *str_type(type_expression *txp)
{
if(txp==NULL) return "NULL";
switch (txp->variable_type)
{
case (PRIMITIVE_TYPE):
return get_str_primitive_type(txp->union_to_be_named.primitive_data);
break;
case (RECT_ARRAY):
return "rect arr";
break;
case (JAGGED_ARRAY):
return "jagged arr";
break;
}
}
// get the desired String Representation of TypeExpression
char* get_string_representation(type_expression* tp){
union_to_be_named union_ds = tp->union_to_be_named;
char* result = (char*)calloc(MAX_BUFFER_SIZE, sizeof(char));
switch(tp->variable_type){
case(PRIMITIVE_TYPE):
{
snprintf(result, MAX_BUFFER_SIZE, "<type = %s>", get_str_primitive_type(union_ds.primitive_data));
return result;
break;
}
case(RECT_ARRAY):
{
/*
<type=rectangularArray, dimensions=2, range_R1= (2, 5), range_R2 = (3, 6), basicElementType = integer>
*/
linked_list* ll = union_ds.rect_array_data.array_ranges;
snprintf(result, MAX_BUFFER_SIZE, "<type = rectangularArray, dimensions = %d, ",
union_ds.rect_array_data.dimensions);
char temp[30];
char* temp1 = temp;
for(int i=1;i<=union_ds.rect_array_data.dimensions;i++){
rect_array_range* r = (rect_array_range*)ll_get(ll, i-1);
snprintf(temp1, 30, "range_R%d= (%d,%d), ", i,r->lower_bound, r->upper_bound);
result = strcat(result, temp1);
}
result = strcat(result,"basicElementType = integer>");
return result;
break;
}
case(JAGGED_ARRAY):
{
char res2[40];
char *str = res2;
//3d : <type = jaggedArray, dimensions = 3, range_R1 = (4, 7), range_R2 = (3 [5, 3, 5], 2 [3, 5], 3 [5, 4, 3], 4 [2, 5, 4, 4]), basicElementType = integer>
//2d : <type = jaggedArray, dimensions = 2, range_R1 = (3, 8), range_R2 = (3, 6, 2, 4, 1, 5), basicElementType = integer>
int dimen = tp->union_to_be_named.jagged_array_data.dimensions;
snprintf(result, MAX_BUFFER_SIZE, "<type = jaggedArray, dimensions = %d,",
dimen);
if(dimen == 2){
jagged_2d jagg_2d = tp->union_to_be_named.jagged_array_data.array_type.j2d;
int l_bound = jagg_2d.lower_bound;
int u_bound = jagg_2d.upper_bound;
snprintf(str, 40, " range_R1 = (%d, %d), range_R2 = (", l_bound, u_bound);
result = strcat(result, str);
r2_dimension r2d = jagg_2d.row_sizes;
int i, *data;
for(i = 0; i < (u_bound-l_bound); i++){
data = (int *)ll_get(r2d.sizes, i);
snprintf(str, 30, "%d, ", *data);
result = strcat(result, str);
}
data = (int *)ll_get(r2d.sizes, i);
snprintf(str, 30, "%d)", *data);
result = strcat(result, str);
result = strcat(result, ", basicElementType = integer>");
return result;
}
else if(dimen == 3){
jagged_3d jagg_3d = tp->union_to_be_named.jagged_array_data.array_type.j3d;
int l_bound = jagg_3d.lower_bound;
int u_bound = jagg_3d.upper_bound;
snprintf(str, 40, "range_R1 = (%d, %d), range_R2 = (", l_bound, u_bound);
result = strcat(result, str);
linked_list* ll = jagg_3d.row_sizes;
for(int i = 0; i < ll->num_nodes; i++){
r2_dimension* r = (r2_dimension*)ll_get(ll, i);
int s = r->sizes->num_nodes;
snprintf(str, 30, "%d [", s);
result = strcat(result, str);
for(int j = 0; j < s-1; j++){
int* data = (int *)ll_get(r->sizes, j);
snprintf(str, 30, "%d, ", *data);
result = strcat(result, str);
}
int *data = (int *)ll_get(r->sizes, s-1);
snprintf(str, 30, "%d]", *data);
result = strcat(result, str);
if(i < ll->num_nodes-1){
result = strcat(result, ", ");
}
}
result = strcat(result, "), basicElementType = integer>");
return result;
}
break;
}
}
}
// set is_declared to true on encountering decl_stmt
void set_declare_flag(type_expression* tp){
tp->is_declared = true;
}
type_expression* construct_type_expression(VariableType variable_type, union_to_be_named union_ds){
type_expression* tp = (type_expression*)calloc(1, sizeof(type_expression));
set_declare_flag(tp);
tp->variable_type = variable_type;
tp->union_to_be_named = union_ds;
return tp;
}
union_to_be_named* populate_union(VariableType variable_type, Parse_tree_node* p){
union_to_be_named* u = (union_to_be_named*)calloc(1,sizeof(union_to_be_named));
switch(variable_type){
case(PRIMITIVE_TYPE):
{
u->primitive_data = *get_primitive_type(p);
break;
}
case(RECT_ARRAY):
{
u->rect_array_data = *create_rect_array_type(p);
break;
}
case(JAGGED_ARRAY):
{
u->jagged_array_data = *create_jagged_array_type(p);
break;
}
}
return u;
}