Support else in if then conditions

This commit is contained in:
2024-12-30 23:12:47 +01:00
parent c558c3d6b4
commit 75a691134f
8 changed files with 97 additions and 17 deletions

View File

@ -485,8 +485,9 @@ namespace source
}
if_statement::if_statement(const struct position position, std::unique_ptr<expression>&& prerequisite,
std::unique_ptr<statement>&& body)
: statement(position), m_prerequisite(std::move(prerequisite)), m_body(std::move(body))
std::unique_ptr<statement>&& body, std::unique_ptr<statement>&& alternative)
: statement(position), m_prerequisite(std::move(prerequisite)), m_body(std::move(body)),
m_alternative(std::move(alternative))
{
}
@ -505,6 +506,11 @@ namespace source
return *m_body;
}
std::unique_ptr<statement>& if_statement::alternative()
{
return m_alternative;
}
while_statement::while_statement(const struct position position, std::unique_ptr<expression>&& prerequisite,
std::unique_ptr<statement>&& body)
: statement(position), m_prerequisite(std::move(prerequisite)), m_body(std::move(body))

View File

@ -37,6 +37,9 @@ if {
then {
return yy::parser::make_THEN(this->location);
}
else {
return yy::parser::make_ELSE(this->location);
}
while {
return yy::parser::make_WHILE(this->location);
}

View File

@ -59,7 +59,7 @@
%token <std::string> IDENTIFIER "identifier"
%token <std::int32_t> NUMBER "number"
%token <bool> BOOLEAN
%token IF THEN WHILE DO
%token IF WHILE DO
%token CONST VAR PROCEDURE
%token BEGIN_BLOCK END_BLOCK
%token LEFT_PAREN RIGHT_PAREN SEMICOLON DOT COMMA
@ -67,6 +67,9 @@
%token PLUS MINUS MULTIPLICATION DIVISION
%token ASSIGNMENT COLON HAT AT
%precedence THEN
%precedence ELSE
%type <std::unique_ptr<elna::source::integer_literal>> integer_literal;
%type <std::unique_ptr<elna::source::boolean_literal>> boolean_literal;
%type <std::unique_ptr<elna::source::constant_definition>> constant_definition;
@ -169,6 +172,11 @@ if_statement:
$$ = std::make_unique<elna::source::if_statement>(elna::source::make_position(@1),
std::move($2), std::move($4));
}
| IF expression THEN statement ELSE statement
{
$$ = std::make_unique<elna::source::if_statement>(elna::source::make_position(@1),
std::move($2), std::move($4), std::move($6));
}
pointer:
integer_literal { $$ = std::move($1); }
| boolean_literal { $$ = std::move($1); }