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 <string>
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<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 <iostream>
#include <string_view>
#include <variant>
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<std::monostate, std::string, double> value;
};
std::ostream &operator<<(std::ostream &os, const ObjectType &type);
std::ostream &operator<<(std::ostream &os, const Object &obj);

View File

@ -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));
}

View File

@ -3,7 +3,7 @@
#include "object.hh"
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
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;
};