diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index 3c0a7f1..81dc165 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -101,28 +101,30 @@ namespace Parser { parsingError(current, "unexpected \"" + current.toktext + "\" without preceding if statement"); } } + const bool has_condition = next.toktype == LPAR and ((lexed.begin() + i - parametersDistance) + 2) < lexed.begin() + i; + if (not has_condition and not is_branch) parsingError(current, "missing condition after an \"" + current.toktext + "\" statement"); if (*(lexed.begin() + i + 1) != tok::LBRACE) parsingError(*(lexed.begin() + i), "missing statement body"); const auto body_end = find_corresponding(lexed.begin() + i + 2, lexed.end(), LBRACE, RBRACE); auto* statement = (is_branch or is_conditional_branch) ? dynamic_cast(parseTree[parseTree.size() - 1].get()) : nullptr; auto& else_branches = statement->else_branches; if ((is_conditional_branch or is_branch) and not statement->is_last_branch_conditional()) parsingError(current, "<- unexpected branch", true); - optional condition; + Condition condition ((ParseTree())); // TODO Check that the condition is valid - if (next.toktype == LPAR and ((lexed.begin() + i - parametersDistance) + 2) < lexed.begin() + i) - condition.emplace(parse((lexed.begin() + i - parametersDistance) + 2, lexed.begin() + i)); - if (condition.has_value() or is_branch) { + if (has_condition) + condition = Condition(parse((lexed.begin() + i - parametersDistance) + 2, lexed.begin() + i)); + if (has_condition or is_branch) { if (is_conditional_branch or is_branch) { - auto branch = (condition.has_value() ? (Condition::Statement::Branch(move(condition.value()))) : Condition::Statement::Branch()); + auto branch = (has_condition ? (Condition::Statement::Branch(move(condition))) : Condition::Statement::Branch()); branch.ParseTree::operator=(parse(lexed.begin() + i + 2, body_end)); else_branches << branch; } else { - Condition::Statement new_statement (move(condition.value())); + Condition::Statement new_statement (move(condition)); new_statement.ParseTree::operator=(parse(lexed.begin() + i + 2, body_end)); parseTree << new_statement; } i += distance(lexed.begin() + i, body_end); - } else if (not is_branch) parsingError(current, "missing condition after an \"" + current.toktext + "\" statement"); + } continue; } else if (nextAre({LCOMP, LCOMP, LBRACE})) { Function function(current.toktext);