From 9eb2967340cf35abebb6bc39be4bcd5fd73f866c Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sun, 21 May 2017 10:24:14 +0200
Subject: Add arithmetics
---
ell.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 49 insertions(+), 1 deletion(-)
diff --git a/ell.c b/ell.c
index a148ed1..8905e27 100755
--- a/ell.c
+++ b/ell.c
@@ -1093,6 +1093,50 @@ defn (fn_system) {
return check (ctx, (*result = new_format ("%d", system (command->value))));
}
+defn (fn_plus) {
+ double res = 0.0;
+ for (; args; args = args->next) {
+ if (args->type != ITEM_STRING)
+ return set_error (ctx, "arguments must be strings");
+ res += strtod (args->value, NULL);
+ }
+ return check (ctx, (*result = new_format ("%f", res)));
+}
+
+defn (fn_minus) {
+ double res = -0.0;
+ for (; args; args = args->next) {
+ if (args->type != ITEM_STRING)
+ return set_error (ctx, "arguments must be strings");
+ res -= strtod (args->value, NULL);
+ }
+ return check (ctx, (*result = new_format ("%f", res)));
+}
+
+defn (fn_multiply) {
+ double res = 1.0;
+ for (; args; args = args->next) {
+ if (args->type != ITEM_STRING)
+ return set_error (ctx, "arguments must be strings");
+ res *= strtod (args->value, NULL);
+ }
+ return check (ctx, (*result = new_format ("%f", res)));
+}
+
+defn (fn_divide) {
+ if (!args || args->type != ITEM_STRING)
+ return set_error (ctx, "first argument must be string");
+ double res = strtod (args->value, NULL), x;
+ for (args = args->next; args; args = args->next) {
+ if (args->type != ITEM_STRING)
+ return set_error (ctx, "arguments must be strings");
+ if (!(x = strtod (args->value, NULL)))
+ return set_error (ctx, "division by zero");
+ res /= x;
+ }
+ return check (ctx, (*result = new_format ("%f", res)));
+}
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool
@@ -1136,7 +1180,11 @@ init_runtime_library (struct context *ctx) {
&& native_register (ctx, "filter", fn_filter)
&& native_register (ctx, "print", fn_print)
&& native_register (ctx, "..", fn_concatenate)
- && native_register (ctx, "system", fn_system);
+ && native_register (ctx, "system", fn_system)
+ && native_register (ctx, "+", fn_plus)
+ && native_register (ctx, "-", fn_minus)
+ && native_register (ctx, "*", fn_multiply)
+ && native_register (ctx, "/", fn_divide);
}
// --- Main --------------------------------------------------------------------
--
cgit v1.2.3-70-g09d2