-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.h
144 lines (123 loc) · 3.21 KB
/
callbacks.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
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
#pragma once
#include <iostream>
#include <functional>
#include <set>
#include "variables.h"
#include "types.h"
using namespace std;
class Callback {
public:
virtual ~Callback() { }
static void clearExtent() {
for (auto cb = _callbacks.begin(); cb != _callbacks.end(); ++cb) {
delete *cb;
}
_callbacks.clear();
}
static void addToExtent(Callback* cb) {
_callbacks.insert(cb);
}
private:
inline static set<Callback*> _callbacks;
};
template <class T>
class ExprEvalCb : public Callback {
public:
static valueEval<T>* create(valueEval<T> evaluator) {
auto callback = new ExprEvalCb<T>(evaluator);
addToExtent(callback);
return &(callback->_evaluator);
}
private:
ExprEvalCb(valueEval<T> evaluator) : _evaluator(evaluator) { }
valueEval<T> _evaluator;
};
class CbAction : Callback {
public:
virtual ~CbAction() { }
template<class T, class... Args>
static action* create(Args&& ... args) {
auto callback = new T(forward<Args>(args)...);
addToExtent(callback);
return &(callback->_action);
}
protected:
action _action;
};
class ExitCb : public CbAction {
public:
ExitCb() {
_action = [](){ exit(0); };
}
};
template <class T>
class PrintExprCb : public CbAction {
public:
PrintExprCb(valueEval<T>* value) {
_action = [value](){ cout << (*value)() << endl; };
}
};
class PrintVarCb : public CbAction {
public:
PrintVarCb(string name) {
_action = [name](){
VariableContainer::printVar(name);
};
}
};
class PrintNewLineCb : public CbAction {
public:
PrintNewLineCb() {
_action = [](){ cout << endl; };
}
};
template <class T>
class AssignVarCb : public CbAction {
public:
AssignVarCb(string varName, valueEval<T>* varValue) {
_action = [varName, varValue]() {
VariableContainer::addVar(varName, (*varValue)());
};
}
};
class IfCb : public CbAction {
public:
IfCb(valueEval<bool>* condition, action* trueFunction, action* falseFunction) {
_action = [condition, trueFunction, falseFunction]() {
if ((*condition)()) {
(*trueFunction)();
} else {
if (falseFunction != nullptr) (*falseFunction)();
}
};
}
};
class WhileCb : public CbAction {
public:
WhileCb(valueEval<bool>* condition, action* instruction) {
_action = [condition, instruction]() {
while((*condition)()) {
(*instruction)();
}
};
}
};
class DoWhileCb : public CbAction {
public:
DoWhileCb(action* instruction, valueEval<bool>* condition) {
_action = [instruction, condition]() {
do {
(*instruction)();
} while ((*condition)());
};
}
};
class CompoundInstrCb : public CbAction {
public:
CompoundInstrCb(action* firstAction, action* secondAction) {
_action = [firstAction, secondAction]() {
(*firstAction)();
(*secondAction)();
};
}
};