-
Notifications
You must be signed in to change notification settings - Fork 1
/
type.h
95 lines (81 loc) · 1.84 KB
/
type.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
#ifndef LATTE_TYPE_H
#define LATTE_TYPE_H
#include <string>
#include <boost/operators.hpp>
#include "parser/Absyn.H"
#include "exceptions.h"
/*
* Class representing any variable type in Latte.
*
* Special type ERROR is added to allow Latte type checker
* continue after first type error.
* */
namespace latte_type_check
{
enum TYPES {
INT,
STR,
BOOL,
VOID,
ERROR
};
class LatteType : boost::equality_comparable<LatteType>
{
public:
LatteType(latte_parser::Type const* t){
if(dynamic_cast<latte_parser::TInt const*>(t) != 0){
type = INT;
}else if(dynamic_cast<latte_parser::TStr const*>(t) != 0){
type = STR;
}else if(dynamic_cast<latte_parser::TBool const*>(t) != 0){
type = BOOL;
}else if(dynamic_cast<latte_parser::TVoid const*>(t) != 0){
type = VOID;
}else {
throw InternalErrorException();
}
}
LatteType(LatteType const& rhs) = default;
LatteType(TYPES _type) : type(_type) {}
public:
bool operator==(LatteType const& rhs) const{
return this->type == rhs.type;
}
LatteType* clone(){
return new LatteType(*this);
}
std::string toString() const{
switch(type){
case INT:
return std::string("int");
case STR:
return std::string("string");
case BOOL:
return std::string("boolean");
case VOID:
return std::string("void");
case ERROR:
return std::string("<error type>");
default:
throw InternalErrorException();
}
}
std::string toJVMType() const{
switch(type){
case INT:
return std::string("I");
case STR:
return std::string("Ljava/lang/String;");
case BOOL:
return std::string("I");
case VOID:
return std::string("V");
default:
throw InternalErrorException();
}
}
private:
TYPES type;
};
};
#endif