Add try/throw

This commit is contained in:
Přemysl Eric Janouch 2017-05-25 13:51:47 +02:00
parent cc302bc17c
commit 6cce0e5595
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 34 additions and 0 deletions

34
ell.c
View File

@ -1044,6 +1044,38 @@ defn (fn_parse) {
return ok; 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) { defn (fn_plus) {
double res = 0.0; double res = 0.0;
for (; args; args = args->next) { for (; args; args = args->next) {
@ -1226,6 +1258,8 @@ init_runtime_library (struct context *ctx) {
&& native_register (ctx, "..", fn_concatenate) && native_register (ctx, "..", fn_concatenate)
&& native_register (ctx, "system", fn_system) && native_register (ctx, "system", fn_system)
&& native_register (ctx, "parse", fn_parse) && 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_plus)
&& native_register (ctx, "-", fn_minus) && native_register (ctx, "-", fn_minus)
&& native_register (ctx, "*", fn_multiply) && native_register (ctx, "*", fn_multiply)