-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.c
More file actions
168 lines (149 loc) · 3.57 KB
/
type.c
File metadata and controls
168 lines (149 loc) · 3.57 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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include "defi.h"
bool SameSymbol(Symbol p,Symbol q)
{
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
if(p->SymbolType!=q->SymbolType) return false;
switch(p->SymbolType)
{
case Function: return SameFunction(p->u.func,q->u.func);
case Variable: return SameType(p->u.type,q->u.type);
}
}
bool SameFunction(FunDec p,FunDec q)
{
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
if(!SameType(p->type,q->type)) return false;
return SameVarList(p->args,q->args);
}
bool SameVarList(VarList p,VarList q)
{
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
if(!SameType(p->type,q->type)) return false;
return SameVarList(p->tail,q->tail);
}
bool IsInt(Type p)
{
return (p->kind==BASIC&&p->u.basic==0);
}
bool IsFloat(Type p)
{
return (p->kind==BASIC&&p->u.basic==1);
}
bool IsArray(Type p)
{
return (p->kind==ARRAY);
}
bool IsStruct(Type p)
{
return (p->kind==STRUCTURE);
}
bool IsVariable(Symbol p)
{
return p->SymbolType==Variable;
}
bool IsFunction(Symbol p)
{
return p->SymbolType==Function;
}
Type newint()
{
Type tp=malloc(sizeof(struct Type_));
tp->kind=BASIC;
tp->u.basic=0;
}
Type newfloat()
{
Type tp=malloc(sizeof(struct Type_));
tp->kind=BASIC;
tp->u.basic=1;
}
bool SameType(Type p,Type q)
{
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
if(p->kind!=q->kind) return false;
switch(p->kind)
{
case BASIC: return p->u.basic==q->u.basic;
case ARRAY: return p->u.array.elem==q->u.array.elem;
case STRUCTURE: return SameField(p->u.structure,q->u.structure);
}
}
bool SameField(FieldList p,FieldList q)
{
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL) return false;
return SameType(p->type,q->type)&&SameField(p->tail,q->tail);
}
Type construct_basic(ast node)
{
Type ty=malloc(sizeof(struct Type_));
ty->kind=BASIC;
if(!strcmp(node->nodename,"int")) ty->u.basic=0;
else ty->u.basic=1;
return ty;
}
Type search_struct(Type tp,char *name)
{
FieldList fl=tp->u.structure;
while(fl!=NULL)
{
if(!strcmp(fl->name,name)) return fl->type;
fl=fl->tail;
}
return NULL;
}
Type construct_struct(ast node)
{
Type ty=malloc(sizeof(struct Type_));
ty->kind=STRUCTURE;
switch(node->opnum)
{
case 2:
{
char *name=extract_name(p2(node));
Symbol sym=Variable_Lookup(name);
if(sym==NULL||sym->SymbolType!=Variable||sym->u.type->kind!=STRUCTURE)
{
char msg[100];
sprintf(msg,"Undefined struct \"%s\"",name);
semantic_error(17,node->lineno,msg);
ty->u.structure=NULL;
return ty;
}
ty=sym->u.type;
break;
}
case 1:
{
DefList df=construct_DefList(p4(node));
FieldList fl=NULL;
while(df!=NULL)
{
FieldList nfl=malloc(sizeof(struct FieldList_));
nfl->name=df->name;
nfl->type=df->type;
nfl->tail=fl;
df=df->tail;
fl=nfl;
}
ty->u.structure=fl;
}
}
return ty;
}
Type construct_type(ast node)
{
switch(node->opnum)
{
case 1: {return construct_basic(p1(node)); break;}
case 2: {return construct_struct(p1(node)); break;}
}
}