-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.cpp
168 lines (149 loc) · 5.31 KB
/
check.cpp
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
#include "check.hpp"
#include <unordered_map>
#include <stack>
#include <string>
#include <iostream>
using std::unordered_map, std::stack, std::string;
using std::make_pair;
void DeleteType(struct type * typ) {
if (typ == nullptr) return;
if (typ -> t == DT_INT) delete typ;
else {
DeleteType(typ -> d.input);
DeleteType(typ -> d.output);
delete typ;
}
typ = nullptr;
}
struct type * CopyType(struct type * typ) {
if (typ -> t == DT_INT) {
return TPInt();
} else {
return TPFunc(CopyType(typ -> d.input), CopyType(typ -> d.output));
}
}
bool TypeComp(const struct type * type1, const struct type * type2 ){
if (type1 == nullptr || type2 == nullptr) return false;
if (type1 -> t != type2 -> t) return false;
if (type1 -> t == DT_INT) return true;
return TypeComp(type1 -> d.input, type2 -> d.input) && TypeComp(type1 -> d.output, type2 -> d.output);
}
struct checkResult check(struct expr * root, bool inner){
static unordered_map<string, stack<struct type *> > var_table;
if(!inner) var_table.clear();
struct checkResult res;
res.t = nullptr;
res.success = false;
switch(root -> t) {
case T_CONST_NAT: {
res.t = TPInt();
res.success = true;
return res;
}
case T_VAR: {
auto var = var_table.find(string(root -> d.VAR.name));
if (var == var_table.end()) {
printf("undefined variable!\n");
return res;
}
auto var_type = var -> second.top();
res.t = CopyType(var_type);
res.success = true;
return res;
}
case T_CONST_BINOP: {
res.t = TPFunc(TPInt(), TPFunc(TPInt(), TPInt()));
res.success = true;
return res;
}
case T_CONST_UNOP: {
res.t = TPFunc(TPInt(), TPInt());
res.success = true;
return res;
}
case T_FUN_APP: {
auto func = check(root -> d.FUN_APP.left, true);
auto var = check(root -> d.FUN_APP.right, true);
if (func.t == nullptr || func.t -> t == DT_INT) {
DeleteType(func.t);
DeleteType(var.t);
return res;
}
if (!func.success || !var.success || !TypeComp(func.t -> d.input, var.t)) {
DeleteType(func.t);
DeleteType(var.t);
return res;
}
DeleteType(var.t);
DeleteType(func.t -> d.input);
res.t = func.t -> d.output;
res.success = true;
return res;
}
case T_FUN_ABS:{
auto var = var_table.find(string(root -> d.FUN_ABS.name));
if (var != var_table.end()) {
var -> second.push(root -> d.FUN_ABS.typ);
} else {
stack<type *> tmp;
tmp.push(root -> d.FUN_ABS.typ);
var_table.insert(make_pair(string(root -> d.FUN_ABS.name), tmp));
}
auto func = check(root -> d.FUN_ABS.arg, true);
if (!func.success) {
DeleteType(func.t);
return res;
}
res.t = TPFunc(CopyType(root -> d.FUN_ABS.typ), func.t);
res.success = true;
var = var_table.find(string(root -> d.FUN_ABS.name));
if(var == var_table.end()) {
printf("name stack error in check process");
exit(1);
}
var -> second.pop();
return res;
}
case T_IF_EXPR:{
auto cond = check(root -> d.IF_EXPR.cond, true);
auto true_expr = check(root -> d.IF_EXPR.true_exp, true);
auto false_expr = check(root -> d.IF_EXPR.false_exp, true);
auto type_int = TPInt();
if (!cond.success || !true_expr.success || !false_expr.success
|| !TypeComp(cond.t, type_int) || !TypeComp(true_expr.t, false_expr.t)) {
DeleteType(type_int);
DeleteType(cond.t);
DeleteType(true_expr.t);
DeleteType(false_expr.t);
return res;
}
DeleteType(type_int);
DeleteType(cond.t);
DeleteType(false_expr.t);
res = true_expr;
return res;
}
case T_LET_IN:{
auto substi = check(root -> d.LET_IN.expr1,true);
auto var = var_table.find(string(root -> d.LET_IN.name));
if (var != var_table.end()) {
var -> second.push(root -> d.LET_IN.typ);
} else {
stack<type *> tmp;
tmp.push(root -> d.LET_IN.typ);
var_table.insert(make_pair(string(root -> d.LET_IN.name), tmp));
}
auto domain = check(root -> d.LET_IN.expr2,true);
if (!substi.success || !domain.success || !TypeComp(substi.t,root -> d.LET_IN.typ)){
DeleteType(substi.t);
DeleteType(domain.t);
return res;
}
var = var_table.find(string(root -> d.LET_IN.name));
var -> second.pop();
res = domain;
return res;
}
default: exit(1);
}
}