Fix Object

This commit is contained in:
Abdelrahman Said 2025-06-08 16:42:16 +01:00
parent cec9ef24de
commit cae1a9347e
4 changed files with 43 additions and 28 deletions

View File

@ -1,28 +1,41 @@
#include "object.hh" #include "object.hh"
#include <string>
Object::Object(StringObjectType string_type, const std::string &value) : str{value} { std::ostream &operator<<(std::ostream &os, const ObjectType &type) {
switch (string_type) { switch (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) {
case ObjectType::NIL: case ObjectType::NIL:
os << "ObjectType::NIL";
break; break;
case ObjectType::IDENTIFIER: case ObjectType::IDENTIFIER:
os << "ObjectType::IDENTIFIER";
break;
case ObjectType::STRING_LIT: case ObjectType::STRING_LIT:
os << obj.str; os << "ObjectType::STRING_LIT";
break; break;
case ObjectType::NUMBER: case ObjectType::NUMBER:
os << obj.number; os << "ObjectType::NUMBER";
break; break;
} }
return os; 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<std::string>(obj.value);
break;
case ObjectType::NUMBER:
os << ", " << std::get<double>(obj.value);
break;
}
os << ')';
return os;
}

View File

@ -2,7 +2,7 @@
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
#include <string_view> #include <variant>
enum class StringObjectType : uint8_t { enum class StringObjectType : uint8_t {
IDENTIFIER, IDENTIFIER,
@ -17,15 +17,15 @@ enum class ObjectType : uint8_t {
}; };
struct Object { struct Object {
Object() : type{ObjectType::NIL} {}; Object() : type{ObjectType::NIL}, value{std::monostate{}} {};
Object(double number) : type{ObjectType::NUMBER}, number{number} {}; Object(double number) : type{ObjectType::NUMBER}, value{number} {};
Object(StringObjectType string_type, const std::string &value); Object(StringObjectType string_type, std::string value)
: type{string_type == StringObjectType::IDENTIFIER ? ObjectType::IDENTIFIER : ObjectType::STRING_LIT},
value{std::move(value)} {};
ObjectType type; ObjectType type;
union { std::variant<std::monostate, std::string, double> value;
std::string_view str;
double number;
};
}; };
std::ostream &operator<<(std::ostream &os, const ObjectType &type);
std::ostream &operator<<(std::ostream &os, const Object &obj); std::ostream &operator<<(std::ostream &os, const Object &obj);

View File

@ -116,6 +116,8 @@ void Scanner::string() {
advance(); advance();
// Trim the surrounding quotes. // 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)); add_token(TokenType::STRING, Object(StringObjectType::LITERAL, value));
} }

View File

@ -3,7 +3,7 @@
#include "object.hh" #include "object.hh"
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <string_view> #include <utility>
enum class TokenType : uint8_t { enum class TokenType : uint8_t {
// Single-character tokens. // Single-character tokens.
@ -27,12 +27,12 @@ enum class TokenType : uint8_t {
}; };
struct Token { struct Token {
Token(TokenType type, const std::string &lexeme, Object literal, int line) Token(TokenType type, std::string lexeme, Object literal, int line)
: type{type}, lexeme{lexeme}, literal{literal}, line{line} : type{type}, lexeme{std::move(lexeme)}, literal{literal}, line{line}
{}; {};
TokenType type; TokenType type;
std::string_view lexeme; std::string lexeme;
Object literal; Object literal;
int line; int line;
}; };