Add Object.to_string method

This commit is contained in:
2025-06-29 00:19:18 +01:00
parent b3291a9e3a
commit 7b8903c19c
3 changed files with 22 additions and 17 deletions

View File

@@ -19,23 +19,7 @@ std::string AstPrinter::print(const Expr &expr) {
case ExprType::LITERAL: {
std::shared_ptr<_Literal> literal = std::get<std::shared_ptr<_Literal>>(expr.value);
assert(literal != nullptr);
switch (literal->value.type) {
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);
}
}
return literal->value.to_string();
}
case ExprType::UNARY: {
std::shared_ptr<_Unary> unary = std::get<std::shared_ptr<_Unary>>(expr.value);