forked from t123yh/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_function.cpp
227 lines (199 loc) · 8.29 KB
/
parser_function.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Created by t123yh on 2020/10/3.
//
#include "parser_function.h"
#include "token_parser.h"
#include "parser_const.h"
#include "parser_variable.h"
#include "parser_statement.h"
#include "parser_expression.h"
decl_header_parser::return_type decl_header_parser::parse(parsing_context &context) const {
function_signature sign;
auto idf = context.expect(token_parser<INTTK, CHARTK>(), token_parser<IDENFR>());
sign.return_type = std::get<0>(idf)->type;
sign.identifier = std::get<1>(idf);
context.record("声明头部");
return sign;
}
parameter_list_parser::return_type parameter_list_parser::parse(parsing_context &context) const {
std::vector<var_def> ret;
if (!context.match(token_parser<RPARENT, LBRACE>())) { // LBRACE in case of missing RPARENT
do {
auto idf = context.expect(token_parser<INTTK, CHARTK>(), token_parser<IDENFR>());
var_def def;
def.array = var_def::PARAM;
def.type = std::get<0>(idf)->type;
def.identifier = std::get<1>(idf);
ret.push_back(def);
if (context.strategy == FINAL) {
context.add_symbol(std::make_shared<variable_symbol>(ret.back()));
}
} while (context.parse_if_match(token_parser<COMMA>()));
}
context.record("参数表");
return ret;
}
compound_statement_parser::return_type compound_statement_parser::parse(parsing_context &context) const {
statement_block ret;
if (context.match(
token_parser<CONSTTK>(),
const_definition_parser()
)) {
ret.variables = context.expect_one(const_description_parser());
}
if (context.match(var_definition_parser())) {
auto r = context.expect_one(var_description_parser());
for (auto &x : r) {
ret.variables.push_back(x);
}
}
auto statements = context.expect_one(statements_parser());
ret.statement = std::make_shared<block_statement>(std::move(statements));
context.record("复合语句");
return ret;
}
statements_parser::return_type statements_parser::parse(parsing_context &context) const {
return_type ret;
while (!context.match(token_parser<RBRACE>())) {
ret.push_back(context.expect_one(statement_parser()));
}
context.record("语句列");
return ret;
}
calling_parser::return_type calling_parser::parse(parsing_context &context) const {
auto info = std::shared_ptr<function_call_info>(new function_call_info);
info->name = context.expect_one(token_parser<IDENFR>());
context.expect_one(token_parser<LPARENT>());
info->arguments = context.expect_one(arguments_parser());
context.expect_one(token_parser<RPARENT>(true));
if (context.strategy == FINAL) {
auto func = dynamic_cast<function_symbol*>(context.symbols.find_symbol(info->name->text));
if (func == nullptr) {
context.errors.push_back(error{info->name->line, E_UNDEFINED_SYMBOL});
} else {
if (func->sign.return_type != VOIDTK) {
context.record("有返回值函数调用语句");
} else {
context.record("无返回值函数调用语句");
}
if (func->sign.parameters.size() != info->arguments.size()) {
context.errors.push_back(error{info->name->line, E_PARAMETER_COUNT_MISMATCH});
} else {
for (int i = 0; i < func->sign.parameters.size(); i++) {
auto x = get_expression_type(info->arguments[i].get(), context);
auto y = func->sign.parameters[i].type;
if (x != y) {
context.errors.push_back(error{info->name->line, E_PARAMETER_TYPE_MISMATCH});
}
}
}
}
}
return info;
}
arguments_parser::return_type arguments_parser::parse(parsing_context &context) const {
std::vector<std::shared_ptr<expression>> arg_list;
if (!context.match(token_parser<RPARENT, SEMICN, RBRACK>())) {
do {
arg_list.push_back(context.expect_one(expression_parser()));
} while (context.parse_if_match(token_parser<COMMA>()));
}
context.record("值参数表");
return arg_list;
}
void find_all_return(std::vector<return_statement*> &results, const std::shared_ptr<statement> &statement) {
#define CHK(VAR, TYPE) auto VAR = dynamic_cast<TYPE*>(statement.get()); if (VAR)
CHK(a1, return_statement) {
results.push_back(a1);
}
CHK(a2, block_statement) {
for (const auto &v : a2->statements) {
find_all_return(results, v);
}
}
CHK(a3, if_statement) {
find_all_return(results, a3->if_body);
find_all_return(results, a3->else_body);
}
CHK(a4, while_statement) {
find_all_return(results, a4->body);
}
CHK(a5, switch_statement) {
for (const auto& v : a5->conditions) {
find_all_return(results, v.body);
}
find_all_return(results, a5->default_body);
}
#undef CHK
}
void find_all_return(std::vector<return_statement*>& results, const std::vector<std::shared_ptr<statement>> &statements) {
for (const auto& s : statements) {
find_all_return(results, s);
}
}
function_parser::return_type function_parser::parse(parsing_context &context) const {
std::shared_ptr<function> func = std::shared_ptr<function>(new function);
function_signature &sign = func->signature;
if (context.parse_if_match(token_parser<VOIDTK>())) {
sign.return_type = VOIDTK;
sign.identifier = context.expect_one(token_parser<IDENFR>());
} else {
sign = context.expect_one(decl_header_parser());
}
function_symbol* newsymb = nullptr;
if (context.strategy == FINAL) {
newsymb = dynamic_cast<function_symbol*>(context.add_symbol(std::make_shared<function_symbol>(sign)));
context.symbols.enter_layer();
}
sign.parameters = std::get<1>(
context.expect(token_parser<LPARENT>(), parameter_list_parser(), token_parser<RPARENT>()));
if (newsymb != nullptr)
newsymb->sign = sign;
func->statements = std::get<1>(
context.expect(token_parser<LBRACE>(), compound_statement_parser(), token_parser<RBRACE>()));
std::vector<return_statement*> returns;
find_all_return(returns, func->statements.statement->statements);
if (func->signature.return_type == VOIDTK) {
for (auto* r : returns) {
if (r->is_fucking_return || r->val != nullptr) {
context.errors.push_back(error{r->line, E_RETURN_MISMATCH_FOR_VOID_FUNCTIONS});
}
}
context.record("无返回值函数定义");
} else {
if (returns.empty()) {
context.errors.push_back(error{context.prev_line(), E_RETURN_MISMATCH_FOR_RETURNING_FUNCTIONS});
} else for (auto* r : returns) {
bool type_mismatch = get_expression_type(r->val.get(), context) != sign.return_type;
if (r->is_fucking_return || r->val == nullptr || type_mismatch) {
context.errors.push_back(error{r->line, E_RETURN_MISMATCH_FOR_RETURNING_FUNCTIONS});
}
}
context.record("有返回值函数定义");
}
if (context.strategy == FINAL) {
context.symbols.pop_layer();
}
return func;
}
main_function_parser::return_type main_function_parser::parse(parsing_context &context) const {
if (context.strategy == FINAL) {
context.symbols.enter_layer();
}
auto tks = context.expect(token_parser<VOIDTK>(), token_parser<MAINTK>(), token_parser<LPARENT>(),
token_parser<RPARENT>());
function_signature sign{.return_type = VOIDTK, .identifier = std::get<0>(tks), .parameters = {}};
auto r = std::get<1>(context.expect(token_parser<LBRACE>(), compound_statement_parser(), token_parser<RBRACE>()));
context.record("主函数");
std::vector<return_statement*> returns;
find_all_return(returns, r.statement->statements);
for (auto* rt : returns) {
if (rt->is_fucking_return || rt->val != nullptr) {
context.errors.push_back(error{rt->line, E_RETURN_MISMATCH_FOR_VOID_FUNCTIONS});
}
}
if (context.strategy == FINAL) {
context.symbols.pop_layer();
}
return r;
}