Implement simple if conditions

This commit is contained in:
2024-03-17 01:00:44 +01:00
parent 99a1ef5f96
commit 27197c7725
13 changed files with 592 additions and 33 deletions

View File

@ -135,7 +135,7 @@ namespace elna::source
{
m_value.identifier = that.identifier();
}
else if (that.of() == type::number)
else if (that.is_numeric())
{
m_value.number = that.number();
}
@ -154,7 +154,7 @@ namespace elna::source
{
m_value.identifier = std::move(that.identifier());
}
else if (that.of() == type::number)
else if (that.is_numeric())
{
m_value.number = that.number();
}
@ -181,7 +181,7 @@ namespace elna::source
std::int32_t token::number() const
{
if (of() != type::number)
if (!is_numeric())
{
throw std::bad_variant_access();
}
@ -200,6 +200,12 @@ namespace elna::source
|| of() == type::factor_operator;
}
bool token::is_numeric() const noexcept
{
return of() == type::number
|| of() == type::boolean;
}
unexpected_character::unexpected_character(const std::string& character, const source::position position)
: error(position), character(character)
{
@ -342,6 +348,10 @@ namespace elna::source
{
tokens.emplace_back(token::type::bang, iterator.position());
}
else if (*iterator == '?')
{
tokens.emplace_back(token::type::question_mark, iterator.position());
}
else if (*iterator == '.')
{
tokens.emplace_back(token::type::dot, iterator.position());
@ -371,6 +381,30 @@ namespace elna::source
{
tokens.emplace_back(token::type::end, iterator.position());
}
else if (word == "if")
{
tokens.emplace_back(token::type::when, iterator.position());
}
else if (word == "then")
{
tokens.emplace_back(token::type::then, iterator.position());
}
else if (word == "while")
{
tokens.emplace_back(token::type::_while, iterator.position());
}
else if (word == "do")
{
tokens.emplace_back(token::type::_do, iterator.position());
}
else if (word == "True")
{
tokens.emplace_back(token::type::boolean, 1, iterator.position());
}
else if (word == "False")
{
tokens.emplace_back(token::type::boolean, 0, iterator.position());
}
else
{
tokens.emplace_back(token::type::identifier, word.c_str(), iterator.position());
@ -395,6 +429,10 @@ namespace elna::source
tokens.emplace_back(token::type::assignment, iterator.position());
++iterator;
}
else if (*iterator == ':')
{
tokens.emplace_back(token::type::colon, iterator.position());
}
else
{
return result<lexer>(unexpected_character{ std::string{ *iterator }, iterator.position() });