From 5625b0abb184d0302cff0979d6c1ae33d46db7ef Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Wed, 14 May 2014 23:44:00 +0300 Subject: eval: add 'mod' function --- eval.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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 #include 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)) { -- cgit v1.2.3