diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2014-05-14 23:44:00 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2014-05-14 23:44:00 +0300 |
| commit | 5625b0abb184d0302cff0979d6c1ae33d46db7ef (patch) | |
| tree | a1fb9dbe2eb66260fa24c088373e5215366865a6 | |
| parent | d45a5b21de22438c004e1db96a8f154da09cdc0e (diff) | |
| download | lispish-5625b0abb184d0302cff0979d6c1ae33d46db7ef.tar.gz lispish-5625b0abb184d0302cff0979d6c1ae33d46db7ef.zip | |
eval: add 'mod' function
| -rw-r--r-- | eval.c | 22 |
1 files changed, 22 insertions, 0 deletions
@@ -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)) { |
