From 6be4a1d094f7049230dc04329e8c36aca3b20e96 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Wed, 31 May 2023 13:02:57 -0400 Subject: actually check assertions under new compiler --- lib94/warrior.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib94/warrior.cpp b/lib94/warrior.cpp index adcf34e..48a5b57 100644 --- a/lib94/warrior.cpp +++ b/lib94/warrior.cpp @@ -345,7 +345,7 @@ namespace lib94 { }; struct info_from_preprocessor { - std::vector> assertions; + std::vector assertions; std::optional name; std::optional author; }; @@ -374,10 +374,10 @@ namespace lib94 { line = line.substr(0, semicolon); if (comment.starts_with("assert ")) { - std::unique_ptr assertion; + std::unique_ptr expr; try { - assertion = std::move(to_assert_expr(comment.substr(7))); + expr = std::move(to_assert_expr(comment.substr(7))); } catch (const assert_expr_exception &iex) { compiler_exception ex; @@ -385,7 +385,11 @@ namespace lib94 { ex.message = iex.message; } - info.assertions.push_back(std::move(assertion)); + info.assertions.push_back((assertion){ + .source_line = line_number, + .expr = std::move(expr), + .offset = (number_t)into.size() + }); } else if (comment.starts_with("name ")) { @@ -825,6 +829,30 @@ namespace lib94 { } } + for (const assertion &a : info.assertions) { + + bool success; + + try { + success = a.expr->is_true(a.offset, inline_macros, labels); + } + + catch (const number_expr_exception &iex) { + compiler_exception ex; + ex.line_number = a.source_line; + ex.message = iex.message; + throw ex; + } + + if (!success) { + compiler_exception ex; + ex.line_number = a.source_line; + ex.message = "failed assertion"; + throw ex; + } + + } + return new warrior { .name = info.name.value(), .author = info.author.value(), -- cgit v1.2.3