aboutsummaryrefslogtreecommitdiff
path: root/ell.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-05-21 10:24:14 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2017-05-21 13:19:48 +0200
commit9eb2967340cf35abebb6bc39be4bcd5fd73f866c (patch)
tree605c9f00285b64aeaef6acf30bbe9b41c55375fc /ell.c
parent16a4c3b21a26681605fdb5b1f04b657832d2b739 (diff)
downloadell-9eb2967340cf35abebb6bc39be4bcd5fd73f866c.tar.gz
ell-9eb2967340cf35abebb6bc39be4bcd5fd73f866c.tar.xz
ell-9eb2967340cf35abebb6bc39be4bcd5fd73f866c.zip
Add arithmetics
Diffstat (limited to 'ell.c')
-rwxr-xr-xell.c50
1 files changed, 49 insertions, 1 deletions
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 --------------------------------------------------------------------