forked from sedlabadkar/rpalInterpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl.cpp
More file actions
executable file
·265 lines (252 loc) · 7.02 KB
/
Control.cpp
File metadata and controls
executable file
·265 lines (252 loc) · 7.02 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "Control.h"
string Control::toStr()
{
string str ;
switch(type)
{
case DELTA :
str = "<D" + std::to_string(index) + ">" ;
return str;
case NAME :
return variables.at(0);//.front() ; //not sure y front() is not working
case LAMBDA :
str = "[lambda closure: " ;
for( int i = 0 ; i < variables.size() ; i++ )
str += variables.at(i) + ": " ;
str += to_string(index) + "]" ;
return str ;
case GAMMA :
return "Gamma" ;
case AUG :
return "AUG" ;
case BETA :
return "BETA" ;
case OR :
return "OR" ;
case AND_LOGICAL :
return "AND" ;
case NOT :
return "NOT" ;
case GR :
return ">" ;
case GE :
return ">=" ;
case LS :
return "<" ;
case LE :
return "<=" ;
case EQ :
return "=" ;
case NE :
return "!=" ;
case ADD :
return "+" ;
case SUBTRACT :
return "-" ;
case NEG :
return "NEG" ;
case MULTIPLY :
return "*" ;
case DIVIDE :
return "/" ;
case EXP :
return "**" ;
case AT :
return "@" ;
case TRUE :
return "true" ;
case FALSE :
return "false" ;
case NIL :
return "nil" ;
case DUMMY :
return "dummy" ;
case YSTAR :
return "Y" ;
case TAU :
str = "<T" + to_string(index) + ">" ;
return str;
case STRING :
return ctrlVal +"";
case INTEGER :
str = ctrlVal + "" ;
return str;
case ENV :
str = "e" + to_string(index) ;
return str;
case ETA :
str = "<ETA," + to_string(index) ;
for( int i = 0 ; i < variables.size() ; i++ )
str += "," + variables.at(i) ;
str += ">" ;
return str ;
case TUPLE :
str = "(" ;
for( int i = 0 ; i < ctrlTuples.size() ; i++ )
{
str += ctrlTuples.at(i)->toStr() ;
if( i != ctrlTuples.size()-1 )
str += ", " ;
}
str += ")" ;
return str ;
default :
return "unknown" ;
}
}
Control::Control(Control *cntrl)
{
index = cntrl->index;
ctrlVal = cntrl->ctrlVal ;
type = cntrl->type ;
associatedENV = cntrl->associatedENV;
if( !cntrl->variables.empty() ){
for(int i=0;i<cntrl->variables.size();i++)
variables.push_back(cntrl->variables.at(i)) ;
}
if( !cntrl->ctrlTuples.empty() ){
for(int i=0;i<cntrl->ctrlTuples.size();i++)
ctrlTuples.push_back(cntrl->ctrlTuples.at(i)) ;
}
}
Control::Control(Control::Type type, int index){
if(Control::DELTA != type){
printf ("Control::DELTA 's constructor called for : %d", type);
};
this->type = type;
this->index = index;
ctrlStruct = new vector<Control *>();
}
Control::Control(Control::Type type,vector<string> *vars, Control *del_ptr, int deltaindex){
this->type = type;//Must be LAMBDA
index = deltaindex;
if(vars != NULL){
for(int i=0;i<vars->size();i++){
variables.push_back(vars->at(i));
}
}
}
Control::Control(Control::Type type, int index, bool watever){
if(type != Control::TAU && type != Control::ENV){
printf("Control::TAU 's constructor called for : %d" , type );
}
this->type = type;
this->index = index;
}
Control::Control(string var_value, Control::Type type ){
this->type = type;
variables.push_back(var_value);
}
Control::Control(Control::Type type, string value){
this->type = type;
ctrlVal = value;
}
Control::Control(){
}
Control::Control(Control::Type type) {
this->type = type;
}
void Control::pretty_print(){
if(type!=Control::DELTA){
printf("pretty_print called on non delta node, Not a delta node, cannot pretty print");
};
cout << toStr() << " ";
for(int i=0; i< ctrlStruct->size(); i++){
cout << ctrlStruct->at(i)->toStr() << " " ;
};
cout << endl;
}
void Control::addCtrl(treeNode* node, int type, string value, vector<string> *variables, Control* del_ptr, int deltas_size){
int tau_count;
Control *temp = NULL;
switch(node->type){
case treeNode::LAMBDA:
temp = new Control(Control::LAMBDA, variables, del_ptr, deltas_size-1 );
break;
case treeNode::INTEGER:
//Remove <ID: and > from the node string before storing it
temp = new Control(Control::INTEGER, node->nodeString);
break;
case treeNode::MULTIPLY:
temp = new Control(Control::MULTIPLY, node->nodeString);
break;
case treeNode::ADD:
temp = new Control(Control::ADD, node->nodeString);
break;
case treeNode::SUBTRACT:
temp = new Control(Control::SUBTRACT, node->nodeString);
break;
case treeNode::DIVIDE:
temp = new Control(Control::DIVIDE, node->nodeString);
break;
case treeNode::GAMMA:
temp = new Control(Control::GAMMA, node->nodeString);
break;
case treeNode::IDENTIFIER:
temp = new Control(node->nodeString, Control::NAME);
break;
case treeNode::STRING:
//Remove <ID: and > from the node string before storing it
temp = new Control(Control::STRING, node->nodeString.substr(1, node->nodeString.length() - 2));
break;
case treeNode::TAU:
if(variables!=NULL)
tau_count = variables->size();
else
cout << "TAU add_control NULL variables sent!" << endl;
temp = new Control(Control::TAU, tau_count, false);
break;
case treeNode::AUG:
temp = new Control(Control::AUG);
break;
case treeNode::NIL:
temp = new Control(Control::NIL);
break;
case treeNode::YSTAR:
temp = new Control(Control::YSTAR);
break;
case treeNode::AND_LOGICAL:
temp = new Control(Control::AND_LOGICAL);
break;
case treeNode::OR:
temp = new Control(Control::OR);
break;
case treeNode::NE:
temp = new Control(Control::NE);
break;
case treeNode::EQ:
temp = new Control(Control::EQ);
break;
case treeNode::LS:
temp = new Control(Control::LS);
break;
case treeNode::LE:
temp = new Control(Control::LE);
break;
case treeNode::GR:
temp = new Control(Control::GR);
break;
case treeNode::GE:
temp = new Control(Control::GE);
break;
case treeNode::NEG:
temp = new Control(Control::NEG);
break;
case treeNode::FALSE:
temp = new Control(Control::FALSE);
break;
case treeNode::TRUE:
temp = new Control(Control::TRUE);
break;
case treeNode::NOT:
temp = new Control(Control::NOT);
break;
case treeNode::DUMMY:
temp = new Control(Control::DUMMY);
break;
default:
printf ("This treenode node handled. val: %s type: %d", value.c_str(), type);
break;
};
ctrlStruct->push_back(temp);
}