From 781b3865c03cbea7dddf861f539ac9a3adea9c1e Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Tue, 27 Aug 2024 20:19:47 +0100 Subject: [PATCH] Add assert function to stdlib. Useful for tests --- src/error.hpp | 1 + src/stdlib.cpp | 12 ++++++++++++ src/stdlib.hpp | 1 + 3 files changed, 14 insertions(+) diff --git a/src/error.hpp b/src/error.hpp index e3e68e1..4180bc2 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -17,6 +17,7 @@ enum class ErrorCode { ArgumentCountMismatch, IOError, Interrupt, + AssertionFailed, }; void seterr(const char* err); diff --git a/src/stdlib.cpp b/src/stdlib.cpp index ce3f9e2..958b7f9 100644 --- a/src/stdlib.cpp +++ b/src/stdlib.cpp @@ -53,6 +53,17 @@ Result stdlib_println(const Array& params) { Result stdlib_prn(const Array& params) { return ERROR(NotImplemented); } +Result stdlib_assert(const Array& params) { + for (uint64_t i = 0; i < params.size(); i++) { + Value param = TRY(params.get(i)); + if (!param.is()) return ERROR(AssertionFailed); + auto v = param.to()->value(); + if (!v) return ERROR(AssertionFailed); + } + + return Value(TRY(Nil::create())); +} + #define STDLIB_FUNCTION(name, id) \ [(uint64_t)StdlibFunctionId::id] = {#name, StdlibFunctionId::id, \ stdlib_##name} @@ -62,6 +73,7 @@ static StdlibFunctionEntry function_entries[] = { STDLIB_FUNCTION(print, Print), STDLIB_FUNCTION(println, PrintLn), STDLIB_FUNCTION(prn, Prn), + STDLIB_FUNCTION(assert, Assert), [(uint64_t)StdlibFunctionId::Max] = {0, StdlibFunctionId::Max, stdlib_unknown}, }; diff --git a/src/stdlib.hpp b/src/stdlib.hpp index 5f30d8a..6043808 100644 --- a/src/stdlib.hpp +++ b/src/stdlib.hpp @@ -9,6 +9,7 @@ enum class StdlibFunctionId : uint64_t { Print, PrintLn, Prn, + Assert, Max, };