Implement comparison operators
This commit is contained in:
@ -262,6 +262,24 @@ namespace elna::source
|
||||
case '/':
|
||||
this->m_operator = binary_operator::division;
|
||||
break;
|
||||
case '=':
|
||||
this->m_operator = binary_operator::equals;
|
||||
break;
|
||||
case 'n':
|
||||
this->m_operator = binary_operator::not_equals;
|
||||
break;
|
||||
case '<':
|
||||
this->m_operator = binary_operator::less;
|
||||
break;
|
||||
case 'l':
|
||||
this->m_operator = binary_operator::less_equal;
|
||||
break;
|
||||
case '>':
|
||||
this->m_operator = binary_operator::greater;
|
||||
break;
|
||||
case 'g':
|
||||
this->m_operator = binary_operator::greater_equal;
|
||||
break;
|
||||
default:
|
||||
throw std::logic_error("Invalid binary operator");
|
||||
}
|
||||
@ -442,7 +460,7 @@ namespace elna::source
|
||||
{
|
||||
++iterator;
|
||||
|
||||
auto expression = parse_expression();
|
||||
auto expression = parse_condition();
|
||||
|
||||
++iterator;
|
||||
|
||||
@ -489,6 +507,38 @@ namespace elna::source
|
||||
return term;
|
||||
}
|
||||
|
||||
std::unique_ptr<expression> parser::parse_condition()
|
||||
{
|
||||
std::unique_ptr<expression> lhs;
|
||||
|
||||
if ((lhs = parse_expression()) == nullptr)
|
||||
{
|
||||
return lhs;
|
||||
}
|
||||
unsigned char _operator{ 0 };
|
||||
|
||||
if (iterator.current().of() == source::token::type::equals)
|
||||
{
|
||||
_operator = '=';
|
||||
}
|
||||
else if (iterator.current().of() == source::token::type::comparison_operator)
|
||||
{
|
||||
_operator = iterator->identifier()[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
return lhs;
|
||||
}
|
||||
++iterator;
|
||||
auto rhs = parse_expression();
|
||||
|
||||
if (rhs == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<binary_expression>(std::move(lhs), std::move(rhs), _operator);
|
||||
}
|
||||
|
||||
std::unique_ptr<constant_definition> parser::parse_constant_definition()
|
||||
{
|
||||
auto definition_identifier = iterator.advance(token::type::identifier);
|
||||
@ -595,7 +645,7 @@ namespace elna::source
|
||||
{
|
||||
return parse_if_statement();
|
||||
}
|
||||
else if (iterator.current(token::type::_while))
|
||||
else if (iterator.current(token::type::loop))
|
||||
{
|
||||
return parse_while_statement();
|
||||
}
|
||||
@ -618,7 +668,7 @@ namespace elna::source
|
||||
++iterator;
|
||||
return call;
|
||||
}
|
||||
while ((argument_expression = parse_expression()) != nullptr)
|
||||
while ((argument_expression = parse_condition()) != nullptr)
|
||||
{
|
||||
call->arguments().push_back(std::move(argument_expression));
|
||||
|
||||
@ -674,7 +724,7 @@ namespace elna::source
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto rvalue = parse_expression();
|
||||
auto rvalue = parse_condition();
|
||||
|
||||
if (rvalue == nullptr)
|
||||
{
|
||||
@ -689,7 +739,7 @@ namespace elna::source
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto condition = parse_expression();
|
||||
auto condition = parse_condition();
|
||||
|
||||
if (condition == nullptr || !iterator.skip(token::type::then))
|
||||
{
|
||||
@ -706,11 +756,11 @@ namespace elna::source
|
||||
|
||||
std::unique_ptr<while_statement> parser::parse_while_statement()
|
||||
{
|
||||
if (!iterator.skip(token::type::_while))
|
||||
if (!iterator.skip(token::type::loop))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
auto condition = parse_expression();
|
||||
auto condition = parse_condition();
|
||||
|
||||
if (condition == nullptr || !iterator.skip(token::type::_do))
|
||||
{
|
||||
|
Reference in New Issue
Block a user