Add Object.to_string method
This commit is contained in:
parent
b3291a9e3a
commit
7b8903c19c
@ -19,23 +19,7 @@ std::string AstPrinter::print(const Expr &expr) {
|
|||||||
case ExprType::LITERAL: {
|
case ExprType::LITERAL: {
|
||||||
std::shared_ptr<_Literal> literal = std::get<std::shared_ptr<_Literal>>(expr.value);
|
std::shared_ptr<_Literal> literal = std::get<std::shared_ptr<_Literal>>(expr.value);
|
||||||
assert(literal != nullptr);
|
assert(literal != nullptr);
|
||||||
switch (literal->value.type) {
|
return literal->value.to_string();
|
||||||
case ObjectType::NIL:
|
|
||||||
return "nil";
|
|
||||||
case ObjectType::BOOL: {
|
|
||||||
bool value = std::get<bool>(literal->value.value);
|
|
||||||
return (value ? "true" : "false");
|
|
||||||
}
|
|
||||||
case ObjectType::IDENTIFIER:
|
|
||||||
case ObjectType::STRING_LIT: {
|
|
||||||
std::string value = std::get<std::string>(literal->value.value);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
case ObjectType::NUMBER: {
|
|
||||||
double value = std::get<double>(literal->value.value);
|
|
||||||
return std::to_string(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case ExprType::UNARY: {
|
case ExprType::UNARY: {
|
||||||
std::shared_ptr<_Unary> unary = std::get<std::shared_ptr<_Unary>>(expr.value);
|
std::shared_ptr<_Unary> unary = std::get<std::shared_ptr<_Unary>>(expr.value);
|
||||||
|
@ -1,6 +1,26 @@
|
|||||||
#include "object.hh"
|
#include "object.hh"
|
||||||
#include <string>
|
#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) {
|
std::ostream &operator<<(std::ostream &os, const ObjectType &type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ObjectType::NIL:
|
case ObjectType::NIL:
|
||||||
|
@ -29,6 +29,7 @@ struct Object {
|
|||||||
ObjectType::STRING_LIT
|
ObjectType::STRING_LIT
|
||||||
},
|
},
|
||||||
value{std::move(value)} {};
|
value{std::move(value)} {};
|
||||||
|
std::string to_string();
|
||||||
|
|
||||||
ObjectType type;
|
ObjectType type;
|
||||||
std::variant<std::monostate, std::string, double, bool> value;
|
std::variant<std::monostate, std::string, double, bool> value;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user