Parser.hpp: Prioritize missing conditions over missing body code

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2023-08-08 03:01:38 +02:00
parent c37f91caf3
commit ddaf3a44d1
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 9 additions and 7 deletions

View File

@ -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<Condition::Statement*>(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 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);