-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.h
143 lines (123 loc) · 3.62 KB
/
Commands.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
/*
* Commands.h
*
* Created on: 31.12.2013
* Author: atamas
*/
#ifndef COMMANDS_H_
#define COMMANDS_H_
#include "ASTNode.h"
#include <iostream>
#include <string>
#include <map>
struct command{
command(){};
virtual int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag) = 0;
virtual ~command(){};
};
struct VarDef: public command{
VarDef(std::string var, ASTNode * formula){
_var = var;
_formula = formula;
}
int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag){
returnFlag = false;
varNamespace[_var] = _formula->eval(varNamespace, funcNamespace);
return 0;
}
std::string _var;
ASTNode * _formula;
};
struct commandPrint: public command{
commandPrint(ASTNode * formula){
_formula = formula;
}
int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag){
returnFlag = false;
std::cout << _formula->eval(varNamespace, funcNamespace) << std::endl;
return 0;
}
ASTNode * _formula;
};
struct commandGoTo: public command{
commandGoTo(int pos){
_pos = pos;
}
int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag){
returnFlag = false;
pos = _pos;
return 0;
}
~commandGoTo(){
}
private:
int _pos;
};
struct commandIf: public command{
public:
commandIf(ASTNode * leftPart, std::string comparator, ASTNode * rightPart, int endIfPos){
_leftPart = leftPart;
_comparator = comparator;
_rightPart = rightPart;
_endIfPos = endIfPos;
}
int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag){
returnFlag = false;
if(_comparator == "=="){
if (!(_leftPart->eval(varNamespace, funcNamespace) == _rightPart->eval(varNamespace, funcNamespace))){
pos = _endIfPos;
}
} else if(_comparator == ">="){
if (!(_leftPart->eval(varNamespace, funcNamespace) >= _rightPart->eval(varNamespace, funcNamespace))){
pos = _endIfPos;
}
} else if(_comparator == "<="){
if (!(_leftPart->eval(varNamespace, funcNamespace) <= _rightPart->eval(varNamespace, funcNamespace))){
pos = _endIfPos;
}
} else if(_comparator == "!="){
if (!(_leftPart->eval(varNamespace, funcNamespace) != _rightPart->eval(varNamespace, funcNamespace))){
pos = _endIfPos;
}
} else if(_comparator == ">"){
if (!(_leftPart->eval(varNamespace, funcNamespace) > _rightPart->eval(varNamespace, funcNamespace))){
pos = _endIfPos;
}
} else if(_comparator == "<"){
if (!(_leftPart->eval(varNamespace, funcNamespace) < _rightPart->eval(varNamespace, funcNamespace))){
pos = _endIfPos;
}
}
return 0;
}
private:
ASTNode * _leftPart;
std::string _comparator;
ASTNode * _rightPart;
int _endIfPos;
};
struct commandRead: public command{
commandRead(std::string var){
_var = var;
}
int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag){
returnFlag = false;
int val;
std::cin >> val;
varNamespace[_var] = val;
return 0;
}
std::string _var;
};
struct commandReturn: public command{
commandReturn(ASTNode * formula){
_formula = formula;
}
int execute(std::map<std::string,int> & varNamespace, std::map<std::string, Function> & funcNamespace, int & pos, bool & returnFlag){
returnFlag = true;
return _formula->eval(varNamespace, funcNamespace);
}
private:
ASTNode * _formula;
};
#endif /* COMMANDS_H_ */