-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.c
More file actions
77 lines (67 loc) · 1.47 KB
/
types.c
File metadata and controls
77 lines (67 loc) · 1.47 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
#include "types.h"
extern int lineno;
int isBool(node* nptr)
{
if(nptr->type==TYPE_BOOL)
return 1;
return typeerror2(TYPE_BOOL,nptr->type);
}
int isInt(node* nptr)
{
if(nptr->type==TYPE_INT)
return 1;
return typeerror2(TYPE_INT,nptr->type);
}
int isType(node* nptr,int type)
{
if(nptr->type==type)
return 1;
return typeerror2(type,nptr->type);;
}
int getType(node* nptr)
{
return nptr->type;
}
int typeCheckLogop(node *left,node *right)
{
if(left->type!=TYPE_BOOL||right->type!=TYPE_BOOL)
typeerror3(TYPE_BOOL,left->type,right->type);
return 1;
}
int typeCheckRelop(node *left,node *right)
{
if(left->type!=TYPE_INT||right->type!=TYPE_INT)
typeerror3(TYPE_INT,left->type,right->type);
return 1;
}
int typeCheckArith(node *left,node *right)
{
return typeCheckRelop(left,right);
}
int typeCheckReturnStmt(int type,node *rtrn)
{
if(rtrn->type!=type)
return typeerror2(type,rtrn->type);
return 0;
}
int typeerror()
{
char buf[30];
sprintf(buf,"Type error ");
yyerror(buf);
exit(1);
}
int typeerror2(int typeexp,int type2)
{
char buf[50];
sprintf(buf,"Type error, expected type %s found %s",typeexp==TYPE_INT?"integer":"boolean",type2==TYPE_INT?"integer":"boolean");
yyerror(buf);
exit(1);
}
int typeerror3(int typeexp,int type2,int type3)
{
char buf[50];
sprintf(buf,"Type error, expected type two %ss found %s and %s",typeexp==TYPE_INT?"integer":"boolean",type2==TYPE_INT?"integer":"boolean",type3==TYPE_INT?"integer":"boolean");
yyerror(buf);
exit(1);
}