Implement simple if conditions
This commit is contained in:
@ -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() });
|
||||
|
@ -17,6 +17,11 @@ namespace elna::source
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(question_mark_statement *statement)
|
||||
{
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(compound_statement *statement)
|
||||
{
|
||||
for (auto& nested_statement : statement->statements())
|
||||
@ -30,6 +35,18 @@ namespace elna::source
|
||||
statement->rvalue().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(if_statement *statement)
|
||||
{
|
||||
statement->prerequisite().accept(this);
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(while_statement *statement)
|
||||
{
|
||||
statement->prerequisite().accept(this);
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void empty_visitor::visit(block *block)
|
||||
{
|
||||
for (const auto& block_definition : block->definitions())
|
||||
@ -57,6 +74,10 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
void empty_visitor::visit(boolean_literal *boolean)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
@ -148,6 +169,21 @@ namespace elna::source
|
||||
return m_number;
|
||||
}
|
||||
|
||||
boolean_literal::boolean_literal(const bool value)
|
||||
: m_boolean(value)
|
||||
{
|
||||
}
|
||||
|
||||
void boolean_literal::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
bool boolean_literal::boolean() const noexcept
|
||||
{
|
||||
return m_boolean;
|
||||
}
|
||||
|
||||
variable_expression::variable_expression(const std::string& name)
|
||||
: m_name(name)
|
||||
{
|
||||
@ -221,6 +257,21 @@ namespace elna::source
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
question_mark_statement::question_mark_statement(std::unique_ptr<condition>&& body)
|
||||
: m_body(std::move(body))
|
||||
{
|
||||
}
|
||||
|
||||
void question_mark_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
condition& question_mark_statement::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
compound_statement::compound_statement(std::vector<std::unique_ptr<statement>>&& statements)
|
||||
: m_statements(std::move(statements))
|
||||
{
|
||||
@ -256,6 +307,46 @@ namespace elna::source
|
||||
return *m_rvalue;
|
||||
}
|
||||
|
||||
if_statement::if_statement(std::unique_ptr<condition>&& prerequisite, std::unique_ptr<statement>&& body)
|
||||
: m_prerequisite(std::move(prerequisite)), m_body(std::move(body))
|
||||
{
|
||||
}
|
||||
|
||||
void if_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
condition& if_statement::prerequisite()
|
||||
{
|
||||
return *m_prerequisite;
|
||||
}
|
||||
|
||||
statement& if_statement::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
while_statement::while_statement(std::unique_ptr<condition>&& prerequisite, std::unique_ptr<statement>&& body)
|
||||
: m_prerequisite(std::move(prerequisite)), m_body(std::move(body))
|
||||
{
|
||||
}
|
||||
|
||||
void while_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
condition& while_statement::prerequisite()
|
||||
{
|
||||
return *m_prerequisite;
|
||||
}
|
||||
|
||||
statement& while_statement::body()
|
||||
{
|
||||
return *m_body;
|
||||
}
|
||||
|
||||
parser::parser(lexer&& tokens)
|
||||
: iterator(std::move(tokens))
|
||||
{
|
||||
@ -336,6 +427,17 @@ namespace elna::source
|
||||
return term;
|
||||
}
|
||||
|
||||
std::unique_ptr<condition> parser::parse_condition()
|
||||
{
|
||||
if (iterator->of() == source::token::token::type::boolean)
|
||||
{
|
||||
auto result = std::make_unique<boolean_literal>(iterator->number());
|
||||
++iterator;
|
||||
return result;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<definition> parser::parse_definition()
|
||||
{
|
||||
auto definition_identifier = iterator.advance(token::type::identifier);
|
||||
@ -363,7 +465,13 @@ namespace elna::source
|
||||
{
|
||||
auto declaration_identifier = iterator.advance(token::type::identifier);
|
||||
|
||||
if (!declaration_identifier.has_value())
|
||||
if (!declaration_identifier.has_value() || !iterator.skip(token::type::colon))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto type_identifier = iterator.advance(token::type::identifier);
|
||||
|
||||
if (!type_identifier.has_value())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
@ -380,10 +488,22 @@ namespace elna::source
|
||||
{
|
||||
return parse_bang_statement();
|
||||
}
|
||||
else if (iterator.current(token::type::question_mark))
|
||||
{
|
||||
return parse_question_mark_statement();
|
||||
}
|
||||
else if (iterator.current(token::type::begin))
|
||||
{
|
||||
return parse_compound_statement();
|
||||
}
|
||||
else if (iterator.current(token::type::when))
|
||||
{
|
||||
return parse_if_statement();
|
||||
}
|
||||
else if (iterator.current(token::type::_while))
|
||||
{
|
||||
return parse_while_statement();
|
||||
}
|
||||
iterator.add_error(*iterator);
|
||||
return nullptr;
|
||||
}
|
||||
@ -400,7 +520,21 @@ namespace elna::source
|
||||
{
|
||||
return std::make_unique<bang_statement>(std::move(bang_body));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<question_mark_statement> parser::parse_question_mark_statement()
|
||||
{
|
||||
if (!iterator.advance(token::type::question_mark))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto question_mark_body = parse_condition();
|
||||
|
||||
if (question_mark_body != nullptr)
|
||||
{
|
||||
return std::make_unique<question_mark_statement>(std::move(question_mark_body));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -452,6 +586,48 @@ namespace elna::source
|
||||
return std::make_unique<assign_statement>(name.value().get().identifier(), std::move(rvalue));
|
||||
}
|
||||
|
||||
std::unique_ptr<if_statement> parser::parse_if_statement()
|
||||
{
|
||||
if (!iterator.skip(token::type::when))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto condition = parse_condition();
|
||||
|
||||
if (condition == nullptr || !iterator.skip(token::type::then))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto body = parse_statement();
|
||||
|
||||
if (body == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<if_statement>(std::move(condition), std::move(body));
|
||||
}
|
||||
|
||||
std::unique_ptr<while_statement> parser::parse_while_statement()
|
||||
{
|
||||
if (!iterator.skip(token::type::_while))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto condition = parse_condition();
|
||||
|
||||
if (condition == nullptr || !iterator.skip(token::type::_do))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto body = parse_statement();
|
||||
|
||||
if (body == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<while_statement>(std::move(condition), std::move(body));
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<definition>> parser::parse_definitions()
|
||||
{
|
||||
std::vector<std::unique_ptr<definition>> definitions;
|
||||
|
Reference in New Issue
Block a user