-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cpp
185 lines (169 loc) · 7.7 KB
/
ast.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
#include "ast.h"
Value *Conditional::execute(Environment &env)
{
Value* return_val;
if( ((Bool*)condition->execute(env))->value == true ){
return_val = true_path->execute(env);
}
else{
return_val = false_path->execute(env);
}
return return_val;
}
bool Value::equals(Value *other)
{
return this->smallerThan(other) == other->smallerThan(this);
/*int comparedClasses = this->getValueClassID();
if( comparedClasses == other->getValueClassID() ){
// class specific checks
if( comparedClasses == UNBOUND_VALUE_VARIABLE_CLASS_ID){
throw std::runtime_error("UnboundPatternVariable values shouldn't be compared at runtime!");
}
else if( comparedClasses == IDENTIFIER_CLASS_ID){
throw std::runtime_error("Identifier values shouldn't be compared at runtime!");
}
else if( comparedClasses == FUNCTION_CLASS_ID || comparedClasses == BUILT_IN_FUNCTION_CLASS_ID){
throw std::runtime_error("Function values shouldn't be compared at runtime!");
}
else if( comparedClasses == COMPLEX_VALUE_CLASS_ID){
if( ((ComplexValue*)this)->constructor_name != ((ComplexValue*)other)->constructor_name) return false;
for(unsigned int i = 0; i < ((ComplexValue*)this)->aggregatedValues.size() ; ++i){
if( ((ComplexValue*)this)->aggregatedValues[i]->equals(((ComplexValue*)other)->aggregatedValues[i]) == false ) return false;
}
return true;
}
else if( comparedClasses == PRIMITIVE_CLASS_ID){
if(exp_type.type_name == other->exp_type.type_name){
if(exp_type.type_name == "int"){
return ((Integer*)this)->value == ((Integer*)other)->value;
}
else if(exp_type.type_name == "float"){
return ((Float*)this)->value == ((Float*)other)->value;
}
else if(exp_type.type_name == "bool"){
return ((Bool*)this)->value == ((Bool*)other)->value;
}
else if(exp_type.type_name == "string"){
return ((String*)this)->value == ((String*)other)->value;
}
else throw std::runtime_error("comparing unknown Primitive Value Classes");
}
else return false;
}
else throw std::runtime_error("comparing unknown Value Classes!");
}
else throw std::runtime_error("comparing values of different Classes");*/
} // equals
bool Value::smallerThan(Value *other)
{
int comparedClasses = this->getValueClassID();
if( comparedClasses == other->getValueClassID() ){
if( comparedClasses == COMPLEX_VALUE_CLASS_ID){
if( ((ComplexValue*)this)->constructor_name < ((ComplexValue*)other)->constructor_name) return true;
else if( ((ComplexValue*)this)->constructor_name > ((ComplexValue*)other)->constructor_name) return false;
else{
for(unsigned int i = 0; i < ((ComplexValue*)this)->aggregatedValues.size() ; ++i){
if( ((ComplexValue*)this)->aggregatedValues[i]->smallerThan(((ComplexValue*)other)->aggregatedValues[i])) return true;
if( ((ComplexValue*)other)->aggregatedValues[i]->smallerThan(((ComplexValue*)this)->aggregatedValues[i])) return false;
}
return false;
}
}
else if( comparedClasses == PRIMITIVE_CLASS_ID){
if(exp_type.type_name == other->exp_type.type_name){
if(exp_type.type_name == "int"){
return ((Integer*)this)->value < ((Integer*)other)->value;
}
else if(exp_type.type_name == "float"){
return ((Float*)this)->value < ((Float*)other)->value;
}
else if(exp_type.type_name == "bool"){
return ((Bool*)this)->value < ((Bool*)other)->value;
}
else if(exp_type.type_name == "string"){
return ((String*)this)->value < ((String*)other)->value;
}
else throw std::runtime_error("comparing unknown Primitive Value Classes");
}
else throw std::runtime_error("comparing primitives of different types: " + exp_type.type_name + " and " + other->exp_type.type_name);
}
else throw std::runtime_error("comparing uncomparable Value Classes!");
}
else throw std::runtime_error("comparing values of different Classes");
}
bool Value::matchWithValue(Value *other)
{
if(this->getValueClassID() == UNBOUND_VALUE_VARIABLE_CLASS_ID) return true; // unbound variable matches to anything
else{
int comparedClasses = this->getValueClassID();
if( comparedClasses == other->getValueClassID() ){
if( comparedClasses == COMPLEX_VALUE_CLASS_ID){
if( ((ComplexValue*)this)->constructor_name != ((ComplexValue*)other)->constructor_name) return false;
for(unsigned int i = 0; i < ((ComplexValue*)this)->aggregatedValues.size() ; ++i){
if( ((ComplexValue*)this)->aggregatedValues[i]->matchWithValue(((ComplexValue*)other)->aggregatedValues[i]) == false ) return false;
}
return true;
}
else if( comparedClasses == PRIMITIVE_CLASS_ID){
return this->equals(other);
}
else throw std::runtime_error("comparing unknown Value Classes!");
}
else return false;
}
} // match with value
void Value::applyMatch(Value *other, Environment &env)
{
if(this->getValueClassID() == UNBOUND_VALUE_VARIABLE_CLASS_ID){
if( ((UnboundPatternVariable*)this)->name != "_" ) env.addValue(Identifier( ((UnboundPatternVariable*)this)->name ), other);
}
else{
int comparedClasses = this->getValueClassID();
if( comparedClasses == COMPLEX_VALUE_CLASS_ID){
for(unsigned int i = 0; i < ((ComplexValue*)this)->aggregatedValues.size() ; ++i){
((ComplexValue*)this)->aggregatedValues[i]->applyMatch( ((ComplexValue*)other)->aggregatedValues[i], env);
}
}
}
} // match with value
Value *TupleCreation::execute(Environment &env)
{
ComplexValue* return_val = new ComplexValue(exp_type, std::to_string(tuple_elements->size()) + "Tuple");
for(unsigned int i = 0; i < tuple_elements->size(); ++i){
return_val->aggregatedValues.push_back( tuple_elements->operator [](i)->execute(env) );
}
return return_val;
}
Value *Let::execute(Environment &env)
{
if(recursive){
std::vector<Function*> function_copies(patterns.size());
for(unsigned int i = 0;i<patterns.size();++i){
function_copies[i] = new Function(*(Function*)expressions[i]);
env.addValue(*(Identifier*)patterns[i], function_copies[i]);
}
for(unsigned int i = 0;i<patterns.size();++i){
function_copies[i]->env_copy = env;
}
// im not executing function_copy as it will return another copy
return nullptr;
}
else{
for(unsigned int i = 0;i<patterns.size();++i){
Value* expression_result = expressions[i]->execute(env);
if(pattern_values[i]->matchWithValue(expression_result)) pattern_values[i]->applyMatch(expression_result, env);
else throw std::runtime_error("Failed to match!");
}
return nullptr;
}
} // Let::execute
Type TypeDefsAST::deduceType(Environment &env, Type)
{
env.addTypes(typeDefs);
return env.getType(Identifier(typeDefs[0]->type_name));
}
Type TypeDefAST::deduceType(Environment &env, Type)
{
env.addType(this);
return env.getType(Identifier(this->type_name));
}