@@ -916,82 +916,20 @@ private:
916
916
};
917
917
918
918
struct Num {
919
- std::unique_ptr<Num_t> num_ptr;
920
-
921
- // Constructions and destruction
922
- Num() : num_ptr(nullptr) {}
923
-
924
- Num(std::unique_ptr<Num_t> num_ptr_) : num_ptr(std::move(num_ptr_)) {}
925
-
926
- Num &operator=(const Num &other) {
927
- if (this != &other) {
928
- num_ptr = other.num_ptr ? other.num_ptr->clone() : nullptr;
929
- }
930
- return *this;
931
- }
932
-
933
- Num(const Num &other) {
934
- num_ptr = other.num_ptr ? other.num_ptr->clone() : nullptr;
935
- }
936
-
937
- Num(Num &&other) noexcept = default;
938
-
939
- Num &operator=(Num &&other) noexcept = default;
940
-
941
- ~Num() = default;
942
-
943
- int32_t toInt() const { return num_ptr->toInt(); }
944
-
945
- int32_t toLong() const { return num_ptr->toLong(); }
946
-
947
- void display() const { num_ptr->display(); }
948
-
949
- Num operator+(const Num &other) const {
950
- if (dynamic_cast<I32V_t *>(num_ptr.get()) &&
951
- dynamic_cast<I32V_t *>(other.num_ptr.get())) {
952
- return Num(
953
- std::make_unique<I32V_t>(I32V_t(this->toInt() + other.toInt())));
954
- } else if (dynamic_cast<I64V_t *>(num_ptr.get()) &&
955
- dynamic_cast<I64V_t *>(other.num_ptr.get())) {
956
- return Num(
957
- std::make_unique<I64V_t>(I64V_t(this->toLong() + other.toLong())));
958
- } else {
959
- throw std::runtime_error("Operands are of different types");
960
- }
961
- }
962
-
963
- Num operator-(const Num &other) const {
964
- if (dynamic_cast<I32V_t *>(num_ptr.get()) &&
965
- dynamic_cast<I32V_t *>(other.num_ptr.get())) {
966
- return Num(
967
- std::make_unique<I32V_t>(I32V_t(this->toInt() - other.toInt())));
968
- } else if (dynamic_cast<I64V_t *>(num_ptr.get()) &&
969
- dynamic_cast<I64V_t *>(other.num_ptr.get())) {
970
- return Num(
971
- std::make_unique<I64V_t>(I64V_t(this->toLong() - other.toLong())));
972
- } else {
973
- throw std::runtime_error("Operands are of different types");
974
- }
975
- }
976
-
977
- bool operator==(const Num &other) const {
978
- if (dynamic_cast<I32V_t *>(num_ptr.get()) &&
979
- dynamic_cast<I32V_t *>(other.num_ptr.get())) {
980
- return this->toInt() == other.toInt();
981
- } else if (dynamic_cast<I64V_t *>(num_ptr.get()) &&
982
- dynamic_cast<I64V_t *>(other.num_ptr.get())) {
983
- return this->toLong() == other.toLong();
984
- } else {
985
- throw std::runtime_error("Operands are of different types");
986
- }
987
- }
988
-
989
- bool operator!=(const Num &other) const { return !(this->operator==(other)); }
919
+ Num(int64_t value) : value(value) {}
920
+ Num() : value(0) {}
921
+ int64_t value;
922
+ int32_t toInt() { return static_cast<int32_t>(value); }
923
+
924
+ bool operator==(const Num &other) const { return value == other.value; }
925
+ bool operator!=(const Num &other) const { return !(*this == other); }
926
+ Num operator+(const Num &other) const { return Num(value + other.value); }
927
+ Num operator-(const Num &other) const { return Num(value - other.value); }
990
928
};
991
929
992
- static Num I32V(int v) { return Num(std::make_unique<I32V_t>(v)) ; }
930
+ static Num I32V(int v) { return v ; }
993
931
994
- static Num I64V(int64_t v) { return Num(std::make_unique<I64V_t>(v)) ; }
932
+ static Num I64V(int64_t v) { return v ; }
995
933
996
934
// struct Slice {
997
935
// int32_t start;
@@ -1003,22 +941,15 @@ using Slice = std::vector<Num>;
1003
941
1004
942
class Stack_t {
1005
943
public:
1006
- void push(Num &&num) {
1007
- assert(num.num_ptr != nullptr);
1008
- stack_.push_back(std::move(num));
1009
- }
944
+ void push(Num &&num) { stack_.push_back(std::move(num)); }
1010
945
1011
- void push(Num &num) {
1012
- assert(num.num_ptr != nullptr);
1013
- stack_.push_back(num);
1014
- }
946
+ void push(Num &num) { stack_.push_back(num); }
1015
947
1016
948
Num pop() {
1017
949
if (stack_.empty()) {
1018
950
throw std::runtime_error("Stack underflow");
1019
951
}
1020
952
Num num = std::move(stack_.back());
1021
- assert(num.num_ptr != nullptr);
1022
953
stack_.pop_back();
1023
954
return num;
1024
955
}
@@ -1034,7 +965,7 @@ public:
1034
965
assert(index >= 0);
1035
966
assert(index < stack_.size());
1036
967
return stack_[index];
1037
- }
968
+ }
1038
969
1039
970
int32_t size() { return stack_.size(); }
1040
971
@@ -1060,7 +991,7 @@ public:
1060
991
void print() {
1061
992
std::cout << "Stack contents: " << std::endl;
1062
993
for (const auto &num : stack_) {
1063
- num.display() ;
994
+ std::cout << num.value << " " ;
1064
995
}
1065
996
}
1066
997
@@ -1103,7 +1034,6 @@ public:
1103
1034
1104
1035
Num get(std::int32_t index) {
1105
1036
auto ret = top()[index];
1106
- assert(ret.num_ptr != nullptr);
1107
1037
return ret;
1108
1038
}
1109
1039
@@ -1121,9 +1051,7 @@ public:
1121
1051
frames.push_back(frame);
1122
1052
}
1123
1053
1124
- void putAll(Slice slice) {
1125
- top().putAll(slice);
1126
- }
1054
+ void putAll(Slice slice) { top().putAll(slice); }
1127
1055
1128
1056
private:
1129
1057
std::vector<Frame_t> frames;
0 commit comments