-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpers.cpp
More file actions
101 lines (86 loc) · 3.23 KB
/
Copy pathOpers.cpp
File metadata and controls
101 lines (86 loc) · 3.23 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
#include "Opers.h"
namespace SmallTranslator
{
Operators* Operators::self = nullptr;
std::string Operators::ArithmeticalFunction(const char* oper, std::string& a, std::string& b, std::string& c)
{
std::string result;
result += "mov\t" + a + "\t" + b + "\n";
result += std::string(oper) + "\t" + a + "\t" + c + "\n";
return result;
}
std::string Operators::ComparingFunction(const char* reg, std::string& a, std::string& b, std::string& c)
{
std::string result;
result += "mov\t" + a + "\t" + b + "\n";
result += "cmp\t" + a + "\t" + c + "\n";
result += "mov\t" + a + "\t" + std::string(reg) + "\n";
return result;
}
std::string Operators::AssignFunction(std::string& a, std::string& b, std::string& _no)
{
std::string result;
result += "mov\t" + a + "\t" + b + "\n";
return result;
}
Operators::Operators()
{
using namespace std::placeholders;
dict["+"] = { Priority::PlusBin, OperatorType::Arithmetic };
dict["+"].func = std::bind(ArithmeticalFunction, "add", _1, _2, _3);
dict["-"] = { Priority::MinusBin, OperatorType::Arithmetic };
dict["-"].func = std::bind(ArithmeticalFunction, "sub", _1, _2, _3);
dict["*"] = { Priority::MulBin, OperatorType::Arithmetic };
dict["*"].func = std::bind(ArithmeticalFunction, "mul", _1, _2, _3);
dict["/"] = { Priority::DivBin, OperatorType::Arithmetic };
dict["/"].func = std::bind(ArithmeticalFunction, "div", _1, _2, _3);
dict["%"] = { Priority::ModBin, OperatorType::Arithmetic };
dict["%"].func = std::bind(ArithmeticalFunction, "mod", _1, _2, _3);
dict["="] = { Priority::Assign, OperatorType::Assign };
dict["="].func = AssignFunction;
dict["<"] = { Priority::LogicLess, OperatorType::Logic };
dict["<"].func = std::bind(ComparingFunction, RegisterLess, _1, _2, _3);
dict[">"] = { Priority::LogicGreather, OperatorType::Logic };
dict[">"].func = std::bind(ComparingFunction, RegisterGreather, _1, _2, _3);
dict["<="] = { Priority::LogicLessEqual, OperatorType::Logic };
dict["<="].func = std::bind(ComparingFunction, RegisterLessEqual, _1, _2, _3);
dict[">="] = { Priority::LogicGreatherEqual, OperatorType::Logic };
dict[">="].func = std::bind(ComparingFunction, RegisterGrEqual, _1, _2, _3);
dict["=="] = { Priority::LogicEqual, OperatorType::Logic };
dict["=="].func = std::bind(ComparingFunction, RegisterEqual, _1, _2, _3);
dict["!="] = { Priority::LogicNotEqual, OperatorType::Logic };
dict["!="].func = std::bind(ComparingFunction, RegisterNotEqual, _1, _2, _3);
dict["and"] = { Priority::ArithmAnd, OperatorType::Arithmetic };
dict["and"].func = std::bind(ArithmeticalFunction, "and", _1, _2, _3);
dict["or"] = { Priority::ArithmOr, OperatorType::Arithmetic };
dict["or"].func = std::bind(ArithmeticalFunction, "or", _1, _2, _3);
dict["("] = { Priority::Bracket, OperatorType::Arithmetic };
dict[")"] = { Priority::Bracket, OperatorType::Arithmetic };
}
Operators* Operators::Inst()
{
if (self == nullptr)
self = new Operators();
return self;
}
void Operators::Release()
{
if (self)
{
delete self;
self = nullptr;
}
}
Operator* Operators::Get(std::string& op)
{
auto result = dict.find(op);
if (result != dict.cend())
{
return &result->second;
}
return nullptr;
}
Operators::~Operators()
{
}
}