aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-05-21 11:26:59 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2017-05-21 13:19:48 +0200
commit04364b75eab3287bb18fe658f28d058f37dd690f (patch)
treef15577bf83bf06ce58f99a4cb50ceaac28149d54
parentc8e3e2eed63e33e6ff298cc41d76d5706909ecb4 (diff)
downloadell-04364b75eab3287bb18fe658f28d058f37dd690f.tar.gz
ell-04364b75eab3287bb18fe658f28d058f37dd690f.tar.xz
ell-04364b75eab3287bb18fe658f28d058f37dd690f.zip
Add "eq?" and "lt?"
-rw-r--r--README.adoc4
-rwxr-xr-xell.c33
2 files changed, 34 insertions, 3 deletions
diff --git a/README.adoc b/README.adoc
index e1b663c..7bbb17b 100644
--- a/README.adoc
+++ b/README.adoc
@@ -29,7 +29,7 @@ The parser, however, does a bunch of transformations:
As an example, consider the following snippet:
- print (if { = @var foo } {
+ print (if { eq? @var foo } {
quote 'Hello world\n'
} else {
quote 'Error\n'
@@ -37,7 +37,7 @@ As an example, consider the following snippet:
which gets expanded to the following:
- ((print (if (quote ((= (set var) foo)))
+ ((print (if (quote ((eq? (set var) foo)))
(quote ((quote 'Hello world\n')))
else
(quote ((quote 'Error\n'))))))
diff --git a/ell.c b/ell.c
index 406474f..8a46c6b 100755
--- a/ell.c
+++ b/ell.c
@@ -1159,6 +1159,35 @@ defn (fn_or) {
return check (ctx, (*result = new_boolean (res)));
}
+defn (fn_eq) {
+ struct item *etalon = args;
+ if (!etalon || etalon->type != ITEM_STRING)
+ return set_error (ctx, "first argument must be string");
+ bool res = true;
+ for (args = etalon->next; args; args = args->next) {
+ if (args->type != ITEM_STRING)
+ return set_error (ctx, "arguments must be strings");
+ if (!(res &= !strcmp (etalon->value, args->value)))
+ break;
+ }
+ return check (ctx, (*result = new_boolean (res)));
+}
+
+defn (fn_lt) {
+ struct item *etalon = args;
+ if (!etalon || etalon->type != ITEM_STRING)
+ return set_error (ctx, "first argument must be string");
+ bool res = true;
+ for (args = etalon->next; args; args = args->next) {
+ if (args->type != ITEM_STRING)
+ return set_error (ctx, "arguments must be strings");
+ if (!(res &= strcmp (etalon->value, args->value) < 0))
+ break;
+ etalon = args;
+ }
+ return check (ctx, (*result = new_boolean (res)));
+}
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool
@@ -1209,7 +1238,9 @@ init_runtime_library (struct context *ctx) {
&& native_register (ctx, "/", fn_divide)
&& native_register (ctx, "not", fn_not)
&& native_register (ctx, "and", fn_and)
- && native_register (ctx, "or", fn_or);
+ && native_register (ctx, "or", fn_or)
+ && native_register (ctx, "eq?", fn_eq)
+ && native_register (ctx, "lt?", fn_lt);
}
// --- Main --------------------------------------------------------------------