From 94a0a6bd3ddf561b93cd2fa09f02fe178f400d65 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sat, 27 Nov 2021 18:47:24 +0100 Subject: [PATCH] Berry add `bool()` function --- lib/libesp32/Berry/src/be_baselib.c | 15 ++++++++++++++- lib/libesp32/Berry/src/be_libs.c | 4 ++-- lib/libesp32/Berry/tests/bool.be | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/libesp32/Berry/src/be_baselib.c b/lib/libesp32/Berry/src/be_baselib.c index f80b321b1..6e92b47be 100644 --- a/lib/libesp32/Berry/src/be_baselib.c +++ b/lib/libesp32/Berry/src/be_baselib.c @@ -340,6 +340,17 @@ static int l_str(bvm *vm) be_return(vm); } +static int l_bool(bvm *vm) +{ + if (be_top(vm)) { + be_pushbool(vm, be_tobool(vm, 1)); + } else { + be_pushbool(vm, bfalse); + } + be_return(vm); +} + + static int l_size(bvm *vm) { if (be_top(vm) && be_isstring(vm, 1)) { @@ -462,9 +473,10 @@ void be_load_baselib(bvm *vm) } /* call must be added later to respect order of builtins */ -void be_load_baselib_call(bvm *vm) +void be_load_baselib_next(bvm *vm) { be_regfunc(vm, "call", l_call); + be_regfunc(vm, "bool", l_bool); } #else extern const bclass be_class_list; @@ -497,6 +509,7 @@ vartab m_builtin (scope: local) { range, class(be_class_range) bytes, class(be_class_bytes) call, func(l_call) + bool, func(l_bool) } @const_object_info_end */ #include "../generate/be_fixed_m_builtin.h" diff --git a/lib/libesp32/Berry/src/be_libs.c b/lib/libesp32/Berry/src/be_libs.c index 0c55717f3..a9a2f0e39 100644 --- a/lib/libesp32/Berry/src/be_libs.c +++ b/lib/libesp32/Berry/src/be_libs.c @@ -8,7 +8,7 @@ #include "be_libs.h" extern void be_load_baselib(bvm *vm); -extern void be_load_baselib_call(bvm *vm); +extern void be_load_baselib_next(bvm *vm); extern void be_load_listlib(bvm *vm); extern void be_load_maplib(bvm *vm); extern void be_load_rangelib(bvm *vm); @@ -24,6 +24,6 @@ void be_loadlibs(bvm *vm) be_load_rangelib(vm); be_load_filelib(vm); be_load_byteslib(vm); - be_load_baselib_call(vm); + be_load_baselib_next(vm); #endif } diff --git a/lib/libesp32/Berry/tests/bool.be b/lib/libesp32/Berry/tests/bool.be index 670f9bfd8..807d36148 100644 --- a/lib/libesp32/Berry/tests/bool.be +++ b/lib/libesp32/Berry/tests/bool.be @@ -16,3 +16,24 @@ def test(a, b) end end test(true, true) + +# bug in unary +def f(i) + var j = !i # bug if i is erroneously modified + return i +end +assert(f(1) == 1) + +#- addind bool() function -# +assert(bool() == false) +assert(bool(0) == false) +assert(bool(0.0) == false) +assert(bool(false) == false) +assert(bool(nil) == false) + +assert(bool(-1) == true) +assert(bool(3.5) == true) +assert(bool('') == true) +assert(bool('a') == true) +assert(bool(list) == true) +assert(bool(list()) == true)