aboutsummaryrefslogtreecommitdiff
path: root/eval.c
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2014-05-14 23:44:00 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2014-05-14 23:44:00 +0300
commit5625b0abb184d0302cff0979d6c1ae33d46db7ef (patch)
treea1fb9dbe2eb66260fa24c088373e5215366865a6 /eval.c
parentd45a5b21de22438c004e1db96a8f154da09cdc0e (diff)
downloadlispish-5625b0abb184d0302cff0979d6c1ae33d46db7ef.tar.gz
lispish-5625b0abb184d0302cff0979d6c1ae33d46db7ef.zip
eval: add 'mod' function
Diffstat (limited to 'eval.c')
-rw-r--r--eval.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/eval.c b/eval.c
index 43e1e05..23a6992 100644
--- a/eval.c
+++ b/eval.c
@@ -3,6 +3,7 @@
#include "atom.h"
#include "parse.h"
+#include <stdio.h>
#include <string.h>
static int atom_cmp(struct list *a, struct list *b)
@@ -195,6 +196,27 @@ struct list *eval(struct list *list)
else
return eval(false_case);
}
+ else if (strcmp(sym, "mod") == 0)
+ {
+ struct list *a = CDR(l);
+ struct list *b = CDR(a);
+
+ if (!a || !b)
+ {
+ printf("error: mod takes two arguments\n");
+ return list_append(NULL, &nil_atom);
+ }
+
+ if (!IS_INT(a) || !IS_INT(b))
+ {
+ printf("error: mod arguments must be integers\n");
+ return list_append(NULL, &nil_atom);
+ }
+
+ long result = LIST_GET_ATOM(a)->l % LIST_GET_ATOM(b)->l;
+
+ return list_append(NULL, atom_new_int(result));
+ }
}
else if (IS_LIST(l))
{