mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

commit 5a0bcbefc6e60e201276019a137e7891d4587d49
parent b39fea3fa7c6f0b68137153ee791f3e6cc8358be
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Tue,  9 May 2023 23:08:21 -0400

feat: work on fibonacci function, add = comparator.

Diffstat:
MREADME.md | 5+++++
Mmumble | 0
Msrc/mumble.c | 27++++++++++++++++++++++++---
3 files changed, 29 insertions(+), 3 deletions(-)

diff --git 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 Binary files differ. diff --git 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