From e54dbcf574347b8647b681b6baf86b7de95b7628 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 9 Jan 2021 21:47:46 +0100 Subject: [PATCH] Added assert function. Resolves #123 --- include/lang/evaluator.hpp | 2 ++ source/lang/builtin_functions.cpp | 10 ++++++++++ source/lang/evaluator.cpp | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/include/lang/evaluator.hpp b/include/lang/evaluator.hpp index d6b14f02f..4326fe4a9 100644 --- a/include/lang/evaluator.hpp +++ b/include/lang/evaluator.hpp @@ -98,6 +98,8 @@ namespace hex::lang { BUILTIN_FUNCTION(readUnsigned); BUILTIN_FUNCTION(readSigned); + BUILTIN_FUNCTION(assert); + #undef BUILTIN_FUNCTION }; diff --git a/source/lang/builtin_functions.cpp b/source/lang/builtin_functions.cpp index 69071f51c..a675c4780 100644 --- a/source/lang/builtin_functions.cpp +++ b/source/lang/builtin_functions.cpp @@ -86,6 +86,16 @@ namespace hex::lang { }, address, size); } + BUILTIN_FUNCTION(assert) { + auto condition = asType(params[0])->getValue(); + auto error = asType(params[1])->getString(); + + if (LITERAL_COMPARE(condition, condition == 0)) + throwEvaluateError(hex::format("assertion failed: '%s'", error.data()), 1); + + return nullptr; + } + #undef BUILTIN_FUNCTION diff --git a/source/lang/evaluator.cpp b/source/lang/evaluator.cpp index f98cd9513..1f1eab496 100644 --- a/source/lang/evaluator.cpp +++ b/source/lang/evaluator.cpp @@ -24,6 +24,10 @@ namespace hex::lang { this->addFunction("readSigned", 2, [this](auto params) { return this->builtin_readSigned(params); }); + + this->addFunction("assert", 2, [this](auto params) { + return this->builtin_assert(params); + }); } ASTNodeIntegerLiteral* Evaluator::evaluateScopeResolution(ASTNodeScopeResolution *node) {