-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.c
158 lines (140 loc) · 3.23 KB
/
type.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
#include "type.h"
#include <stdio.h>
#include <stdlib.h>
#include "scope.h"
struct type *type_create(type_kind_t kind, struct param_list *params, struct expr *expr, struct type *subtype, int line, int inAST) {
struct type *t = (struct type *)malloc(sizeof(struct type));
if(!t) {
fprintf(stdout, "malloc fails!\n");
exit(EXIT_FAILURE);
}
t->kind = kind;
t->params = params;
t->expr = expr;
t->subtype = subtype;
t->line = line;
t->inAST = inAST;
return t;
}
void type_print(struct type *t) {
if(!t) return;
switch(t->kind) {
case TYPE_BOOLEAN:
printf("boolean");
break;
case TYPE_CHARACTER:
printf("char");
break;
case TYPE_INTEGER:
printf("integer");
break;
case TYPE_STRING:
printf("string");
break;
case TYPE_ARRAY:
printf("array [");
expr_print(t->expr);
printf("] ");
type_print(t->subtype);
return;
case TYPE_FUNCTION:
printf("function ");
type_print(t->subtype);
printf(" ");
printf("(");
if(t->params) {
printf(" ");
param_list_print(t->params);
printf(" ");
}
printf(")");
return;
case TYPE_VOID:
printf("void");
break;
}
}
int type_equals(struct type *s, struct type *t) {
if(!s && !t) {
return 1;
} else if(s && t && (s->kind == t->kind)) {
if(!(s->subtype) && !(t->subtype) && !(s->params) && !(t->params)) {
return 1;
} else {
return expr_equals(s->expr, t->expr) && type_equals(s->subtype, t->subtype) && params_equals(s->params, t->params);
}
} else {
return 0;
}
}
void type_resolve(struct type *t) {
if(!t) return;
switch(t->kind) {
case TYPE_ARRAY:
expr_resolve(t->expr);
type_resolve(t->subtype);
break;
default:
break;
}
}
void type_typecheck(struct type *t) {
if(!t) return;
switch(t->kind) {
case TYPE_ARRAY:
if(!(t->expr)) {
fprintf(stdout, "type error (line %d): the array size is missing!\n", t->line);
type_error_count += 1;
} else {
if(t->expr->kind != EXPR_INTEGER_LITERAL) {
fprintf(stdout, "type error (line %d): the array size (", t->expr->line);
expr_print(t->expr);
printf(") must be an positive integer literal!\n");
type_error_count += 1;
} else {
if(t->expr->literal_value <= 0) {
fprintf(stdout, "type error (line %d): the array size (", t->expr->line);
expr_print(t->expr);
printf(") must be an positive integer literal!\n");
type_error_count += 1;
}
}
}
type_typecheck(t->subtype);
break;
default:
break;
}
}
void type_arraysize_typecheck(struct type *t, struct expr *init) {
if(t->kind == TYPE_ARRAY) {
int count = t->expr->literal_value;
int init_count = expr_count_item(init->right);
if(count != init_count) {
fprintf(stdout, "type error (line %d): the initializer size of the array does not match the size of the array!\n", t->line);
type_error_count += 1;
}
int min;
if(count >= init_count) {
min = init_count;
} else {
min = count;
}
int i;
for(i = 1; i <= min; i++) {
struct expr *e = expr_get_item(init->right, init_count, i);
type_arraysize_typecheck(t->subtype, e);
}
}
}
void type_free(struct type *t) {
if(!t) return;
if(!(t->inAST)) {
if(t->subtype) {
type_free(t->subtype);
}
if(t->expr)
free(t->expr);
free(t);
}
}