-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions_types.h
105 lines (86 loc) · 2.2 KB
/
functions_types.h
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
#include "parser/Absyn.H"
#ifndef LATTE_FUNCTIONS_TYPES_H
#define LATTE_FUNCTIONS_TYPES_H
#include <string>
#include <vector>
#include <memory>
#include "parser/Printer.H"
#include "environment.h"
#include "error.h"
#include "type.h"
/*
* Class that loads function signatures before actual type check
* can take place.
* */
namespace latte_type_check
{
using namespace latte_parser;
class FunType;
class FunTypesLoader;
typedef Environment<std::shared_ptr<FunType>> FunTypesEnv;
class FunType
{
public:
FunType(std::shared_ptr<LatteType>& _returnType): returnType(_returnType->clone()){}
void addArgType(std::shared_ptr<LatteType>& _argType){
arguments.push_back(std::shared_ptr<LatteType>(_argType->clone()));
}
std::vector<std::shared_ptr<LatteType>> const& getArguments(){
return arguments;
}
std::shared_ptr<LatteType> const& getReturnType(){
return returnType;
}
std::string toString() const{
std::string result = returnType->toString();
result.append("(");
size_t i = 0;
for(auto t : arguments){
i++;
result.append(t->toString());
if(i < arguments.size())
result.append(", ");
}
result.append(")");
return result;
}
std::string toJVMType() const{
std::stringstream result;
result << "(";
for(auto t : arguments){
result << t->toJVMType();
}
result << ")" << returnType->toJVMType();
return result.str();
}
private:
std::shared_ptr<LatteType> returnType;
std::vector<std::shared_ptr<LatteType>> arguments;
};
using namespace latte_parser;
class FunTypesLoader : public Visitor
{
public:
FunTypesLoader();
~FunTypesLoader();
void visitPProg(PProg* p);
void visitTFnDef(TFnDef* p);
void visitAArg(AArg* p);
void visitTInt(TInt* p);
void visitTStr(TStr* p);
void visitTBool(TBool* p);
void visitTVoid(TVoid* p);
void visitListTopDef(ListTopDef* p);
void visitListArg(ListArg* p);
void visitIdent(Ident x);
const FunTypesEnv& getResultEnv();
const Errors& getErrors();
private:
FunTypesEnv env;
std::shared_ptr<LatteType> lastType;
std::shared_ptr<std::string> lastIdent;
std::shared_ptr<FunType> lastFunType;
Errors err;
};
}
#endif