-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparators.cpp
34 lines (25 loc) · 1.03 KB
/
comparators.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
#include "comparators.h"
#include <functional>
#include <optional>
#include <sstream>
#include "object.h"
#include "object_holder.h"
using namespace std;
namespace Runtime {
bool Equal(ObjectHolder lhs, ObjectHolder rhs) {
if (lhs.TryAs<String>() && rhs.TryAs<String>()) {
return lhs.TryAs<String>()->GetValue() == rhs.TryAs<String>()->GetValue();
} else if (lhs.TryAs<Number>() && rhs.TryAs<Number>()) {
return lhs.TryAs<Number>()->GetValue() == rhs.TryAs<Number>()->GetValue();
}
throw std::runtime_error("unsupported operand types for Equal()");
}
bool Less(ObjectHolder lhs, ObjectHolder rhs) {
if (lhs.TryAs<String>() && rhs.TryAs<String>()) {
return lhs.TryAs<String>()->GetValue() < rhs.TryAs<String>()->GetValue();
} else if (lhs.TryAs<Number>() && rhs.TryAs<Number>()) {
return lhs.TryAs<Number>()->GetValue() < rhs.TryAs<Number>()->GetValue();
}
throw std::runtime_error("unsupported operand types for Less()");
}
} /* namespace Runtime */