-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions_types.cpp
168 lines (146 loc) · 3.58 KB
/
functions_types.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 "functions_types.h"
#include "exceptions.h"
namespace latte_type_check
{
FunTypesLoader::FunTypesLoader()
{
}
FunTypesLoader::~FunTypesLoader()
{
}
void FunTypesLoader::visitPProg(PProg *pprog)
{
{
/* Pre-loaded functions:
* void printInt(int)
* void printString(string)
* void error()
* int readInt()
* string readString()
*/
std::shared_ptr<LatteType> tVoid(new LatteType(VOID));
std::shared_ptr<LatteType> tInt(new LatteType(INT));
std::shared_ptr<LatteType> tStr(new LatteType(STR));
{
/* printInt() */
std::shared_ptr<FunType> printInt(new FunType(tVoid));
printInt->addArgType(tInt);
env.insert("printInt", printInt);
}
{
/* printString() */
std::shared_ptr<FunType> printString(new FunType(tVoid));
printString->addArgType(tStr);
env.insert("printString", printString);
}
{
/* error() */
std::shared_ptr<FunType> error(new FunType(tVoid));
env.insert("error", error);
}
{
/* readInt() */
std::shared_ptr<FunType> readInt(new FunType(tInt));
env.insert("readInt", readInt);
}
{
/* readString() */
std::shared_ptr<FunType> readString(new FunType(tStr));
env.insert("readString", readString);
}
}
pprog->listtopdef_->accept(this);
try{
std::shared_ptr<FunType> main = env[std::string("main")];
if(*(main->getReturnType()) != LatteType(INT)){
err.push_back(Error(
0,
std::string("main"),
"function must return int"
));
}
if(! main->getArguments().empty()){
err.push_back(Error(
0,
std::string("main"),
"function must not have arguments"
));
}
}catch(IdentifierNotFoundException ex){
err.push_back(Error(
0,
std::string("<global>"),
"no function main() defined"
));
}
}
void FunTypesLoader::visitTFnDef(TFnDef *tfndef)
{
tfndef->type_->accept(this);
visitIdent(tfndef->ident_);
lastFunType.reset(new FunType(lastType));
tfndef->listarg_->accept(this);
try{
env.insert(*lastIdent, lastFunType);
tfndef->funType = lastFunType;
}catch(IdentifierRepeatedException ex){
err.push_back(Error(tfndef->line_number, *lastIdent, "conflicing function name"));
}
}
void FunTypesLoader::visitAArg(AArg *aarg)
{
aarg->type_->accept(this);
if(*lastType == LatteType(VOID)){
err.push_back(Error(
aarg->line_number,
*lastIdent,
"type not allowed for a function parameter",
"void"
));
} else{
lastFunType->addArgType(lastType);
}
}
void FunTypesLoader::visitTInt(TInt *tint)
{
lastType.reset(new LatteType(tint));
}
void FunTypesLoader::visitTStr(TStr *tstr)
{
lastType.reset(new LatteType(tstr));
}
void FunTypesLoader::visitTBool(TBool *tbool)
{
lastType.reset(new LatteType(tbool));
}
void FunTypesLoader::visitTVoid(TVoid *tvoid)
{
lastType.reset(new LatteType(tvoid));
}
void FunTypesLoader::visitListTopDef(ListTopDef* listtopdef)
{
for (ListTopDef::iterator i = listtopdef->begin() ; i != listtopdef->end() ; ++i)
{
(*i)->accept(this);
}
}
void FunTypesLoader::visitListArg(ListArg* listarg)
{
for (ListArg::iterator i = listarg->begin() ; i != listarg->end() ; ++i)
{
(*i)->accept(this);
}
}
void FunTypesLoader::visitIdent(Ident x)
{
lastIdent.reset(new std::string(x));
}
const Environment<std::shared_ptr<FunType>>& FunTypesLoader::getResultEnv()
{
return env;
}
const Errors& FunTypesLoader::getErrors()
{
return err;
}
}