Token: Add a copy constructor and fix operator=

This commit is contained in:
apio 2022-11-01 22:27:18 +01:00
parent fb990ac4a8
commit 6b505c82e7
2 changed files with 39 additions and 4 deletions

View File

@ -1,6 +1,22 @@
#include "Token.h"
#include "utils.h"
Token::Token(const Token& other) : type(other.type), location(other.location), m_line_text(other.m_line_text)
{
if (other.int_value.has_value())
{
int_value = other.int_value.value();
}
else if (other.float_value.has_value())
{
float_value = other.float_value.value();
}
else if (other.string_value.has_value())
{
string_value = other.string_value.value();
}
}
Token::Token(TokenType type) : type(type), location(0, 0, "")
{
}
@ -9,7 +25,7 @@ Token::Token(TokenType type, const Location& location) : type(type), location(lo
{
}
Token::Token(TokenType type, std::string value) : type(type), location(0, 0, ""), string_value(std::move(value))
Token::Token(TokenType type, std::string value) : type(type), string_value(std::move(value)), location(0, 0, "")
{
}
@ -45,17 +61,34 @@ Token Token::copy_with_new_type(const TokenType& type) const
result.string_value = string_value.value();
}
return std::move(result);
return result;
}
Token Token::make_with_line(const Token& origin, const std::string& line_text)
{
Token result = origin.copy_with_new_type(origin.type);
return std::move(result);
result.m_line_text = line_text;
return result;
}
void Token::operator=(const Token& other)
{
*this = other.copy_with_new_type(other.type);
type = other.type;
location = other.location;
m_line_text = other.m_line_text;
if (other.int_value.has_value())
{
int_value = other.int_value.value();
}
else if (other.float_value.has_value())
{
float_value = other.float_value.value();
}
else if (other.string_value.has_value())
{
string_value = other.string_value.value();
}
}

View File

@ -56,6 +56,8 @@ struct Token
Location location;
Token(const Token& other);
Token(TokenType type);
Token(TokenType type, const Location& location);