From 5dc1e4c991efae2e0dd96b59bc57b2cd81a8c5fb Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 24 Jul 2026 18:11:43 +0200 Subject: Check the compound statement condition is a boolean --- boot/type_check.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'boot') diff --git a/boot/type_check.cc b/boot/type_check.cc index 761c4b2..9bab537 100644 --- a/boot/type_check.cc +++ b/boot/type_check.cc @@ -107,6 +107,9 @@ namespace elna::boot case kind::for_loop: return "for-loop variable must be an array or a slice, got '" + this->actual.to_string() + "'"; + case kind::condition: + return "Condition must be a boolean, got '" + + this->actual.to_string() + "'"; default: __builtin_unreachable(); } @@ -212,6 +215,16 @@ namespace elna::boot return true; } + void type_analysis_visitor::visit_and_validate_condition(expression& condition) + { + condition.accept(this); + if (!is_primitive_type(condition.type_decoration, "Bool")) + { + add_error(condition.position(), + type_kind_error::kind::condition, condition.type_decoration); + } + } + /* * Checks whether derived has base in its record parent chain. * @@ -492,6 +505,61 @@ namespace elna::boot this->bag.leave(); } + void type_analysis_visitor::visit(repeat_statement *statement) + { + visit_and_validate_condition(statement->condition()); + + for (auto *body_statement : statement->body) + { + body_statement->accept(this); + } + } + + void type_analysis_visitor::visit(while_statement *statement) + { + visit_and_validate_condition(statement->branch().prerequisite()); + + for (auto *branch_statement : statement->branch().statements) + { + branch_statement->accept(this); + } + for (conditional_statements *branch : statement->branches) + { + visit_and_validate_condition(branch->prerequisite()); + + for (auto *branch_statement : branch->statements) + { + branch_statement->accept(this); + } + } + } + + void type_analysis_visitor::visit(if_statement *statement) + { + visit_and_validate_condition(statement->branch().prerequisite()); + + for (auto *branch_statement : statement->branch().statements) + { + branch_statement->accept(this); + } + for (conditional_statements *branch : statement->branches) + { + visit_and_validate_condition(branch->prerequisite()); + + for (auto *branch_statement : branch->statements) + { + branch_statement->accept(this); + } + } + if (statement->alternative != nullptr) + { + for (auto *branch_statement : *statement->alternative) + { + branch_statement->accept(this); + } + } + } + void type_analysis_visitor::visit(type_declaration *declaration) { std::vector alias_path; @@ -564,6 +632,7 @@ namespace elna::boot type(std::make_shared()), call->callable().type_decoration); } + // else callable is not declared which is already reported. } void type_analysis_visitor::visit(record_constructor_expression *expression) -- cgit v1.2.3