-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunc_Block.cpp
220 lines (196 loc) · 8.84 KB
/
Func_Block.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
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
#include "Func_Block.hpp"
#include "runwindow.h"
#include "ui_runwindow.h"
#include <QString>
#include "Var_Block.hpp"
namespace WriteBackend
{
::RunWindow* *runWindowPtr {nullptr};
static ::QPushButton* *runWindowStep {nullptr};
void setRunWindowStep(QPushButton* *ptr) {runWindowStep = ptr;}
void setRunWindowPtr(RunWindow* *ptr) {runWindowPtr = ptr;}
std::type_info const &convertToTypeinfo(varType_e _t)
{
switch(_t)
{
case BOOL:
return typeid(Var_Block<bool>);
case INT:
return typeid(Var_Block<int>);
case DOUBLE:
return typeid(Var_Block<double>);
case BOOLARR:
return typeid(Var_Block<bool*>);
case INTARR:
return typeid(Var_Block<int*>);
case DOUBLEARR:
return typeid(Var_Block<double*>);
default:
return typeid(Invalid_type);
}
}
Func_Block::Func_Block(std::string name, unsigned int numOfParam, BuiltInRunFunc_helper_base *runFunc): Block {name},
myRunFunc {runFunc},
numOfParams {numOfParam},
myParams {numOfParams ? new Param_info_t [numOfParams] : nullptr},
myBody {} {}
bool Func_Block::isStepping {false};
void Func_Block::set_isStepping(bool v)
{
Func_Block::isStepping = v;
}
Func_Block::~Func_Block()
{
for (auto i : myBody)
{
if (i.bodyFuncs[0].args) delete [] i.bodyFuncs[0].args;
if (i.bodyFuncs[1].args) delete [] i.bodyFuncs[1].args;
} // NOTE wierd... but seems that only delete these things here will not cause problem....
if (myParams) delete [] myParams;
}
inline bool Func_Block::typeCheck(std::type_info const &info, varType_e const &type_e) // check whether the var 'info' is valid according to the type_e
{
return ((type_e == varType_e::ANYTYPE) ||
((type_e == varType_e::NUMTYPE) && ((info == convertToTypeinfo(varType_e::INT)) || (info == convertToTypeinfo(varType_e::DOUBLE)))) ||
((type_e == varType_e::NUMARRTYPE) && ((info == convertToTypeinfo(varType_e::INTARR)) || (info == convertToTypeinfo(varType_e::DOUBLEARR)))) ||
(info == convertToTypeinfo(type_e)) ||
((type_e == varType_e::ARRTYPE) && ((info == convertToTypeinfo(varType_e::INTARR)) || (info == convertToTypeinfo(varType_e::BOOLARR)) || (info == convertToTypeinfo(varType_e::DOUBLEARR)))) ||
((type_e == varType_e::NONARRTYPE) && ((info == convertToTypeinfo(varType_e::INT)) || (info == convertToTypeinfo(varType_e::DOUBLE)) || (info == convertToTypeinfo(varType_e::BOOL))))
);
}
void Func_Block::setParamType(varType_e t, int which)
{
myParams[which].argType = t;
}
bool Func_Block::setRealArg(Block* b, int which)
{
std::type_info const& info {typeid(*b)};
if (typeCheck(info, myParams[which].argType)) // typecheck
{
myParams[which].realArg = b;
return true;
}
std::cerr << "type wrong in " << __PRETTY_FUNCTION__ << std::endl;
return false;
}
void Func_Block::clearMyBody()
{
myBody.clear();
}
void Func_Block::setNextBody(bodyType_e type, Block* *condition, Func_Block *func1, Func_Block *func2) // set the next "line" of code which the user write
{
if (
(!func1) ||
( (type == bodyType_e::IF) && ((!condition) || (!(*condition)) || (typeid(**condition) != typeid(Var_Block<bool>))) ) ||
(myRunFunc) ||
(type == bodyType_e::ERROR)
)
{
std::cerr << "type wrong in " << __PRETTY_FUNCTION__ << std::endl;
return;
}
myBody.push_back({});
(myBody.back()).statType = type;
(myBody.back()).condition = condition;
(((myBody.back()).bodyFuncs)[0]).func = func1;
(((myBody.back()).bodyFuncs)[0]).args = new Block** [func1->numOfParams];
if (func2) // only for the if block, the func2 is not nullptr
{
(((myBody.back()).bodyFuncs)[1]).func = func2;
(((myBody.back()).bodyFuncs)[1]).args = new Block** [func2->numOfParams];
}
}
bool Func_Block::setNextBodyParam(unsigned int whichParam, int val, unsigned int whichFunc) // set the parameters for the fucntion callings in next "line"
{
// NOTE this function should be called right after the previous function to ensure the correctness
if ((whichParam < myBody.back().bodyFuncs[whichFunc].func->numOfParams) && whichFunc < 2)
{
// std::cout << ((myBody.back().bodyFuncs[whichFunc]).func->myParams[whichParam].argType) << std::endl;
if ( typeCheck(convertToTypeinfo(myParams[val].argType), ((myBody.back().bodyFuncs[whichFunc]).func->myParams[whichParam].argType)) )
{
// std::cout << name << " set " << myBody.back().bodyFuncs[whichFunc].func->name << " " << whichParam << " " << &(myParams[val].realArg) << std::endl;
(myBody.back().bodyFuncs[whichFunc]).args[whichParam] = &(myParams[val].realArg);
return true;
}
}
std::cerr << "type wrong in " << __PRETTY_FUNCTION__ << std::endl;
return false;
}
bool Func_Block::setNextBodyParam(unsigned int whichParam, Block** ref, unsigned int whichFunc) // set the parameters for the function callings in the next "line", just another interface which allow user to ref to global var
{
if (!ref) return true;
// NOTE this function should be called right after the previous funciton called to ensure the correctness
if ((whichParam < myBody.back().bodyFuncs[whichFunc].func->numOfParams) && whichFunc < 2)
{
// std::cout << ((myBody.back().bodyFuncs[whichFunc]).func->myParams[whichParam].argType) << std::endl;
if ( typeCheck(typeid(**ref), myBody.back().bodyFuncs[whichFunc].func->myParams[whichParam].argType) )
{
// std::cout << name << " set " << myBody.back().bodyFuncs[whichFunc].func->name << " " << whichParam << " " << *ref << std::endl;
(myBody.back().bodyFuncs[whichFunc]).args[whichParam] = ref;
return true;
}
}
std::cerr << " type wrong when " << name << " are tring to set " << (myBody.back().bodyFuncs[whichFunc].func->name) << " using " << __PRETTY_FUNCTION__ << std::endl;
return false;
}
void Func_Block::runFunc(FuncBody_info_t &toRun, int which)
{
// step 1, set the param for the next properly, no typeCheck here but should be fine since we have already checked type previously
std::cout << name << " calling " << toRun.bodyFuncs[which].func->name << std::endl;
QLabel *labelFlow = new QLabel {};
labelFlow->setText(QString::fromStdString(std::string(name) + std::string(" calling ") + std::string(toRun.bodyFuncs[which].func->name)));
(*runWindowPtr)->getMyUI()->showFlowArea_content_layout->addWidget(labelFlow);
// step 1.5: dealing with stepping
if (Func_Block::isStepping)
{
QEventLoop el;
QObject::connect(*runWindowStep, &QPushButton::clicked, &el, &QEventLoop::quit);
el.exec();
}
Param_info_t *temp = toRun.bodyFuncs[which].func->numOfParams ? new Param_info_t [toRun.bodyFuncs[which].func->numOfParams] : nullptr;
for (unsigned int i {0}; i < (toRun.bodyFuncs[which].func->numOfParams); ++i)
{
// std::cout << ((toRun.bodyFuncs[which]).args[i]) << " " << (((toRun.bodyFuncs[which]).args[i]) ? *((toRun.bodyFuncs[which]).args[i]) : nullptr) << std::endl;
temp[i].argType = (((toRun.bodyFuncs[which]).func)->myParams[i]).argType;
temp[i].realArg = (toRun.bodyFuncs[which].func->myParams[i]).realArg;
(toRun.bodyFuncs[which].func->myParams[i]).realArg = ((toRun.bodyFuncs[which]).args[i]) ? *((toRun.bodyFuncs[which]).args[i]) : nullptr;
}
// step 2, run the proper next properly
toRun.bodyFuncs[which].func->run();
// step 3, reset the param of the func being runned
for (unsigned int i {0}; i < (toRun.bodyFuncs[which].func->numOfParams); ++i)
{
(toRun.bodyFuncs[which].func->myParams[i]).realArg = temp[i].realArg;
}
if (temp) { delete [] temp; }
}
void Func_Block::run()
{
if (myRunFunc)
{
myRunFunc->operator()(this);
return;
}
for (auto i : myBody)
{
switch(i.statType)
{
case bodyType_e::FUNCCALL:
runFunc(i);
break;
case bodyType_e::WHILE:
while((**(i.condition)).val._bool) { runFunc(i); }
break;
case bodyType_e::IF:
if ((**(i.condition)).val._bool) { runFunc(i, 0); }
else { runFunc(i, 1); }
break;
case bodyType_e::ERROR:
default:
std::cerr << "something wrong at " << name << std::endl;
exit(-1);
}
}
}
void Func_Block::show_val() {}
} // namespace WriteBackend