diff --git a/README.md b/README.md index bc0722f..14f2a67 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,11 @@ mumble> sq 44 mumble> def {sqsum} (@ {x y} {(+ (sq x) (sq y))}) mumble> sqsum 2 3 mumble> def {init} (@ {xs} { list((head xs)) } ) +mumble> def {unwrap} (@ {x} { head (list xx) } ) mumble> init {1 2} +mumble> ifelse 1 2 3 +mumble> ifelse 0 1 2 +mumble> ifelse {1 2 3} (1) (1) ``` ## To do @@ -84,6 +88,7 @@ mumble> init {1 2} - [x] Define functions - [ ] Define if, = and > - [ ] Build fibonacci function + - Should look something like: def {fib} (@ {x} { ifelse x (+ x (fib (- x 1))) 0 } ) ## Gotchas diff --git a/mumble b/mumble index 79b96de..491d6d6 100755 Binary files a/mumble and b/mumble differ diff --git a/src/mumble.c b/src/mumble.c index 8548256..2f5713f 100644 --- a/src/mumble.c +++ b/src/mumble.c @@ -846,15 +846,35 @@ lispval* builtin_ifelse(lispval* v, lispenv* e) lispval* alternative = v->cell[2]; if( choice->type == LISPVAL_NUM && choice->num == 0){ - lispval* answer = clone_lispval(result); + lispval* answer = clone_lispval(alternative); return answer; }else { - lispval* answer = clone_lispval(alternative); + lispval* answer = clone_lispval(result); return answer; } } -// Comparators: =, > +// Comparators: =, > (also potentially <, >=, <=, <=) +// For numbers. + +lispval* builtin_equal(lispval* v, lispenv* e) +{ + // ifelse 1 {a} b + LISPVAL_ASSERT(v->count == 2, "Error: function = takes two numeric arguments. Try (= 1 2)"); + + lispval* a = v->cell[0]; + lispval* b = v->cell[1]; + + LISPVAL_ASSERT(a->type == LISPVAL_NUM, "Error: Functio = only takes numeric arguments."); + LISPVAL_ASSERT(b->type == LISPVAL_NUM, "Error: Functio = only takes numeric arguments."); + + if(a->num == b->num){ + return lispval_num(1); + }else { + return lispval_num(0); + } +} + // Simple math ops lispval* builtin_math_ops(char* op, lispval* v, lispenv* e) @@ -956,6 +976,7 @@ void lispenv_add_builtins(lispenv* env) lispenv_add_builtin("def", builtin_def, env); lispenv_add_builtin("@", builtin_define_lambda, env); lispenv_add_builtin("ifelse", builtin_ifelse, env); + lispenv_add_builtin("=", builtin_equal, env); } // Evaluate the lispval