Add Object.to_string method
This commit is contained in:
@@ -1,6 +1,26 @@
|
||||
#include "object.hh"
|
||||
#include <string>
|
||||
|
||||
std::string Object::to_string() {
|
||||
switch (type) {
|
||||
case ObjectType::NIL:
|
||||
return "nil";
|
||||
case ObjectType::BOOL: {
|
||||
bool val = std::get<bool>(value);
|
||||
return (val ? "true" : "false");
|
||||
}
|
||||
case ObjectType::IDENTIFIER:
|
||||
case ObjectType::STRING_LIT: {
|
||||
std::string val = std::get<std::string>(value);
|
||||
return val;
|
||||
}
|
||||
case ObjectType::NUMBER: {
|
||||
double val = std::get<double>(value);
|
||||
return std::to_string(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const ObjectType &type) {
|
||||
switch (type) {
|
||||
case ObjectType::NIL:
|
||||
|
||||
@@ -29,6 +29,7 @@ struct Object {
|
||||
ObjectType::STRING_LIT
|
||||
},
|
||||
value{std::move(value)} {};
|
||||
std::string to_string();
|
||||
|
||||
ObjectType type;
|
||||
std::variant<std::monostate, std::string, double, bool> value;
|
||||
|
||||
Reference in New Issue
Block a user