From cae1a9347e4e18db3bcabb9d156d1109753b530f Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 8 Jun 2025 16:42:16 +0100 Subject: [PATCH] Fix Object --- cclox_src/object.cc | 43 ++++++++++++++++++++++++++++--------------- cclox_src/object.hh | 16 ++++++++-------- cclox_src/scanner.cc | 4 +++- cclox_src/token.hh | 8 ++++---- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/cclox_src/object.cc b/cclox_src/object.cc index db516e6..aae991a 100644 --- a/cclox_src/object.cc +++ b/cclox_src/object.cc @@ -1,28 +1,41 @@ #include "object.hh" +#include -Object::Object(StringObjectType string_type, const std::string &value) : str{value} { - switch (string_type) { - case StringObjectType::IDENTIFIER: - type = ObjectType::IDENTIFIER; - break; - case StringObjectType::LITERAL: - type = ObjectType::STRING_LIT; - break; - } -} - -std::ostream &operator<<(std::ostream &os, const Object &obj) { - switch (obj.type) { +std::ostream &operator<<(std::ostream &os, const ObjectType &type) { + switch (type) { case ObjectType::NIL: + os << "ObjectType::NIL"; break; case ObjectType::IDENTIFIER: + os << "ObjectType::IDENTIFIER"; + break; case ObjectType::STRING_LIT: - os << obj.str; + os << "ObjectType::STRING_LIT"; break; case ObjectType::NUMBER: - os << obj.number; + os << "ObjectType::NUMBER"; break; } return os; } + +std::ostream &operator<<(std::ostream &os, const Object &obj) { + os << "Object(" << obj.type; + + switch (obj.type) { + case ObjectType::NIL: + break; + case ObjectType::IDENTIFIER: + case ObjectType::STRING_LIT: + os << ", " << std::get(obj.value); + break; + case ObjectType::NUMBER: + os << ", " << std::get(obj.value); + break; + } + + os << ')'; + + return os; +} diff --git a/cclox_src/object.hh b/cclox_src/object.hh index 6dfdca1..e42f064 100644 --- a/cclox_src/object.hh +++ b/cclox_src/object.hh @@ -2,7 +2,7 @@ #include #include -#include +#include enum class StringObjectType : uint8_t { IDENTIFIER, @@ -17,15 +17,15 @@ enum class ObjectType : uint8_t { }; struct Object { - Object() : type{ObjectType::NIL} {}; - Object(double number) : type{ObjectType::NUMBER}, number{number} {}; - Object(StringObjectType string_type, const std::string &value); + Object() : type{ObjectType::NIL}, value{std::monostate{}} {}; + Object(double number) : type{ObjectType::NUMBER}, value{number} {}; + Object(StringObjectType string_type, std::string value) + : type{string_type == StringObjectType::IDENTIFIER ? ObjectType::IDENTIFIER : ObjectType::STRING_LIT}, + value{std::move(value)} {}; ObjectType type; - union { - std::string_view str; - double number; - }; + std::variant value; }; +std::ostream &operator<<(std::ostream &os, const ObjectType &type); std::ostream &operator<<(std::ostream &os, const Object &obj); diff --git a/cclox_src/scanner.cc b/cclox_src/scanner.cc index 457a4f0..dbc0a1d 100644 --- a/cclox_src/scanner.cc +++ b/cclox_src/scanner.cc @@ -116,6 +116,8 @@ void Scanner::string() { advance(); // Trim the surrounding quotes. - std::string value = code.substr(start + 1, current - 1); + size_t no_quote_start = start + 1; + size_t no_quote_end = current - 1; + std::string value = code.substr(no_quote_start, no_quote_end - no_quote_start); add_token(TokenType::STRING, Object(StringObjectType::LITERAL, value)); } diff --git a/cclox_src/token.hh b/cclox_src/token.hh index f303749..ed88260 100644 --- a/cclox_src/token.hh +++ b/cclox_src/token.hh @@ -3,7 +3,7 @@ #include "object.hh" #include #include -#include +#include enum class TokenType : uint8_t { // Single-character tokens. @@ -27,12 +27,12 @@ enum class TokenType : uint8_t { }; struct Token { - Token(TokenType type, const std::string &lexeme, Object literal, int line) - : type{type}, lexeme{lexeme}, literal{literal}, line{line} + Token(TokenType type, std::string lexeme, Object literal, int line) + : type{type}, lexeme{std::move(lexeme)}, literal{literal}, line{line} {}; TokenType type; - std::string_view lexeme; + std::string lexeme; Object literal; int line; };