aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-05-25 13:51:47 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2017-05-25 13:51:47 +0200
commit6cce0e5595b87a35ac01320fced5bd699b55a20f (patch)
treeaf562995652e7c0b45814c47ca7c0e1f7a816190
parentcc302bc17c515f20b72a2dc6db227159468c5495 (diff)
downloadell-6cce0e5595b87a35ac01320fced5bd699b55a20f.tar.gz
ell-6cce0e5595b87a35ac01320fced5bd699b55a20f.tar.xz
ell-6cce0e5595b87a35ac01320fced5bd699b55a20f.zip
Add try/throw
-rw-r--r--ell.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/ell.c b/ell.c
index b221393..9cb77aa 100644
--- a/ell.c
+++ b/ell.c
@@ -1044,6 +1044,38 @@ defn (fn_parse) {
return ok;
}
+defn (fn_try) {
+ struct item *body = args, *handler;
+ if (!body || body->type != ITEM_LIST)
+ return set_error (ctx, "first argument must be a function");
+ if (!(handler = body->next) || handler->type != ITEM_LIST)
+ return set_error (ctx, "second argument must be a function");
+ if (execute (ctx, body->head, result))
+ return true;
+
+ struct item *message;
+ if (ctx->memory_failure
+ || !check (ctx, (message = new_string (ctx->error, strlen (ctx->error)))))
+ return false;
+
+ free (ctx->error); ctx->error = NULL;
+ item_free_list (*result); *result = NULL;
+
+ bool ok = set_single_argument (ctx, message)
+ && execute (ctx, handler->head, result);
+ item_free (message);
+ return ok;
+}
+
+defn (fn_throw) {
+ (void) result;
+
+ struct item *message = args;
+ if (!message || message->type != ITEM_STRING)
+ return set_error (ctx, "first argument must be string");
+ return set_error (ctx, message->value);
+}
+
defn (fn_plus) {
double res = 0.0;
for (; args; args = args->next) {
@@ -1226,6 +1258,8 @@ init_runtime_library (struct context *ctx) {
&& native_register (ctx, "..", fn_concatenate)
&& native_register (ctx, "system", fn_system)
&& native_register (ctx, "parse", fn_parse)
+ && native_register (ctx, "try", fn_try)
+ && native_register (ctx, "throw", fn_throw)
&& native_register (ctx, "+", fn_plus)
&& native_register (ctx, "-", fn_minus)
&& native_register (ctx, "*", fn_multiply)