From c8e3e2eed63e33e6ff298cc41d76d5706909ecb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sun, 21 May 2017 10:39:39 +0200 Subject: Add boolean operators --- ell.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/ell.c b/ell.c index e1a2831..406474f 100755 --- a/ell.c +++ b/ell.c @@ -923,6 +923,8 @@ truthy (struct item *item) { && ((item->type == ITEM_STRING && item->len != 0) || item->head); } +static struct item * new_boolean (bool b) { return new_string ("1", b); } + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - defn (fn_set) { @@ -1137,6 +1139,26 @@ defn (fn_divide) { return check (ctx, (*result = new_number (res))); } +defn (fn_not) { + if (!args) + return set_error (ctx, "missing argument"); + return check (ctx, (*result = new_boolean (!truthy (args)))); +} + +defn (fn_and) { + bool res = true; + for (; args; args = args->next) + res &= truthy (args); + return check (ctx, (*result = new_boolean (res))); +} + +defn (fn_or) { + bool res = false; + for (; args; args = args->next) + res |= truthy (args); + return check (ctx, (*result = new_boolean (res))); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static bool @@ -1184,7 +1206,10 @@ init_runtime_library (struct context *ctx) { && native_register (ctx, "+", fn_plus) && native_register (ctx, "-", fn_minus) && native_register (ctx, "*", fn_multiply) - && native_register (ctx, "/", fn_divide); + && native_register (ctx, "/", fn_divide) + && native_register (ctx, "not", fn_not) + && native_register (ctx, "and", fn_and) + && native_register (ctx, "or", fn_or); } // --- Main -------------------------------------------------------------------- -- cgit v1.2.3