-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-symbol.c
53 lines (48 loc) · 1.43 KB
/
type-symbol.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
#include "type-symbol.h"
#include<stdio.h>
#include<assert.h>
#include<string.h>
int TypeMatch(Type* type_a, Type* type_b){
if(type_a==NULL && type_b==NULL) return 1;
if(type_a==NULL || type_b==NULL) return 0;
if(type_a->kind != type_b->kind){
return 0;
}
if(type_a->kind == BASIC){
if(type_a->u.basic==type_b->u.basic) return 1;
else return 0;
}
else if(type_a->kind == ARRAY){
if(type_a->u.array.size != type_b->u.array.size) return 0;
else return TypeMatch(type_a->u.array.elem, type_b->u.array.elem);
}
else if(type_a->kind == STRUCTURE){
if(type_a->u.structure.struct_name==NULL || type_b->u.structure.struct_name==NULL) return 0;
else {
if(strcmp(type_a->u.structure.struct_name, type_b->u.structure.struct_name)==0) return 1;
else return 0;
}
}
else assert(0);
}
Field* HasFld(Field* fld, char* name){
Field* res = fld;
while(res!=NULL && strcmp(res->name, name)!=0) res = res->nxt;
return res;
}
int TypeSize(Type *type){
if(type->kind == BASIC) return 4; // 4 bytes
if(type->kind == STRUCTURE){
int res = 0;
Field *fld = type->u.structure.field;
while(fld){
res += TypeSize(fld->type);
fld = fld->nxt;
}
return res;
}
else{
int ele_size = TypeSize(type->u.array.elem);
return ele_size * type->u.array.size;
}
}