Add expression evaluation
This commit is contained in:
@@ -21,6 +21,35 @@ std::string Object::to_string() {
|
||||
}
|
||||
}
|
||||
|
||||
bool Object::operator==(const Object &other) {
|
||||
if (type != other.type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case ObjectType::NIL: return true;
|
||||
case ObjectType::BOOL: {
|
||||
bool left = std::get<bool>(value);
|
||||
bool right = std::get<bool>(other.value);
|
||||
|
||||
return left == right;
|
||||
}
|
||||
case ObjectType::IDENTIFIER:
|
||||
case ObjectType::STRING_LIT: {
|
||||
std::string left = std::get<std::string>(value);
|
||||
std::string right = std::get<std::string>(other.value);
|
||||
|
||||
return left == right;
|
||||
}
|
||||
case ObjectType::NUMBER: {
|
||||
double left = std::get<double>(value);
|
||||
double right = std::get<double>(other.value);
|
||||
|
||||
return left == right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const ObjectType &type) {
|
||||
switch (type) {
|
||||
case ObjectType::NIL:
|
||||
@@ -64,3 +93,7 @@ std::ostream &operator<<(std::ostream &os, const Object &obj) {
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
bool Object::operator!=(const Object &other) {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ struct Object {
|
||||
},
|
||||
value{std::move(value)} {};
|
||||
std::string to_string();
|
||||
bool operator==(const Object &other);
|
||||
bool operator!=(const Object &other);
|
||||
|
||||
ObjectType type;
|
||||
std::variant<std::monostate, std::string, double, bool> value;
|
||||
|
||||
Reference in New Issue
Block a user