-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperand.hpp
123 lines (103 loc) · 3.56 KB
/
operand.hpp
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
#ifndef OPERAND_HPP
#define OPERAND_HPP
#include "parser.hpp"
#include <typeinfo>
#include "Ioperand.hpp"
#include "avm.hpp"
#include <math.h>
template<typename T>
class Operand : public IOperand {
private:
eOperandType _type;
T _val;
std::string _str;
public:
Operand(eOperandType type, std::string const &str) {
std::stringstream ss;
ss.str(str);
_type = type;
if (type == Int8)
_val = static_cast<int8_t>(stoi(str));
else
ss >> _val;
_str = str;
}
Operand(Operand const& o){
_type = o._type;
_str = o._str;
_val = o._type;
}
~Operand(){}
Operand &operator=(Operand const& o){
if (this != &o){
_type = o._type;
_str = o._str;
_val = o._val;
}
return *this;
}
std::string const & toString() const {
return _str;
}
int getPrecision() const{
return static_cast<int>(_type);
}
eOperandType getType() const{
return _type;
}
IOperand const * operator+( IOperand const & rhs ) const{
std::stringstream ss;
eOperandType precise;
long double value;
ss << rhs.toString();
ss >> value;
precise = _type >= rhs.getType() ? _type : rhs.getType();
value = value + _val;
return Parser::createOperand(precise, value);
}
IOperand const * operator-( IOperand const & rhs ) const{
std::stringstream ss;
eOperandType precise;
long double value;
ss << rhs.toString();
ss >> value;
precise = _type >= rhs.getType() ? _type : rhs.getType();
value = value - _val;
return Parser::createOperand(precise, value);
}
IOperand const * operator*( IOperand const & rhs ) const{
std::stringstream ss;
eOperandType precise;
long double value;
ss << rhs.toString();
ss >> value;
precise = _type >= rhs.getType() ? _type : rhs.getType();
value = value * _val;
// std::cout <<"mul val "<< value << std::endl;
return Parser::createOperand(precise, value);
}
IOperand const * operator/( IOperand const & rhs ) const{
std::stringstream ss;
eOperandType precise;
long double value;
ss << rhs.toString();
ss >> value;
precise = _type >= rhs.getType() ? _type : rhs.getType();
value = value / _val;
return Parser::createOperand(precise, value);
}
IOperand const * operator%( IOperand const & rhs ) const{
std::stringstream ss;
eOperandType precise;
long double value;
ss << rhs.toString();
ss >> value;
precise = _type >= rhs.getType() ? _type : rhs.getType();
if (rhs.getType() >= Float || _type >= Float)
value = fmod(value,static_cast<long double>(_val));
else
value = static_cast<int>(value) % static_cast<int>(_val);
return Parser::createOperand(precise, value);
}
};
#endif