Add expression evaluation

This commit is contained in:
2025-06-29 02:15:10 +01:00
parent 7b8903c19c
commit e352df9d7d
9 changed files with 256 additions and 2 deletions

View File

@@ -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);
}

View File

@@ -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;