mumble

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

commit 289de06edf7f00dfb0bcaf14136ef4c61505fa6c
parent e7b46b0ff92b872d83cac770e924979d1c70a48e
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Mon,  1 May 2023 19:54:32 -0400

feat: get working version!

The main bug I was having was infinite recursion!

Diffstat:
Mmumble | 0
Msrc/mumble.c | 58++++++++--------------------------------------------------
2 files changed, 8 insertions(+), 50 deletions(-)

diff --git a/mumble b/mumble Binary files differ. diff --git a/src/mumble.c b/src/mumble.c @@ -169,7 +169,7 @@ void print_lispval_parenthesis(lispval* v) printf("%li ", v->num); break; case LISPVAL_ERR: - printf("[Error: %s] ", v->err); + printf("%s ", v->err); break; case LISPVAL_SYM: printf("%s ", v->sym); @@ -228,7 +228,6 @@ lispval* take_lispval(lispval* v, int i){ delete_lispval(v); return x; } -/* lispval* builtin_op(char* op, lispval* v){ // For now, ensure all args are numbers for(int i=0; i<v->count; i++){ @@ -240,7 +239,7 @@ lispval* builtin_op(char* op, lispval* v){ if(v->count == 0){ return lispval_err("Error: No numbers on which to operate!"); } else if(v->count == 1) { - if(strcmp(op, "-")){ + if(strcmp(op, "-") == 0){ return lispval_num(-v->cell[0]->num); }else { return lispval_err("Error: Non minus unary operation"); @@ -271,56 +270,15 @@ lispval* builtin_op(char* op, lispval* v){ return lispval_err("Error: Incorrect number of args. Perhaps a lispval->count was wrongly initialized?"); } } -*/ -lispval* builtin_op(lispval* a, char* op) { - return lispval_num(1); - /* Ensure all arguments are numbers */ - for (int i = 0; i < a->count; i++) { - if (a->cell[i]->type != LISPVAL_NUM) { - // delete_lispval(a); - return lispval_err("Cannot operate on non-number!"); - } - } - - /* Pop the first element */ - lispval* x = pop_lispval(a, 0); - - /* If no arguments and sub then perform unary negation */ - if ((strcmp(op, "-") == 0) && a->count == 0) { - x->num = -x->num; - } - - /* While there are still elements remaining */ - while (a->count > 0) { - - /* Pop the next element */ - lispval* y = pop_lispval(a, 0); - - if (strcmp(op, "+") == 0) { x->num += y->num; } - if (strcmp(op, "-") == 0) { x->num -= y->num; } - if (strcmp(op, "*") == 0) { x->num *= y->num; } - if (strcmp(op, "/") == 0) { - if (y->num == 0) { - delete_lispval(x); delete_lispval(y); - return lispval_err("Division By Zero!"); - } - x->num /= y->num; - } - - delete_lispval(y); - } - - delete_lispval(a); return x; -} - lispval* evaluate_lispval(lispval* l) { - // Evaluate the children + // Evaluate the children if needed for(int i=0; i<l->count; i++){ - l->cell[i] = evaluate_lispval(l->cell[i]); - + if(l->cell[i]->type == LISPVAL_SEXPR){ + l->cell[i] = evaluate_lispval(l->cell[i]); + } } // Check if any are errors. for(int i=0; i<l->count; i++){ @@ -332,7 +290,7 @@ lispval* evaluate_lispval(lispval* l) // Check if the first element is an operation. if(l->count >=2 && ( (l->cell[0])->type == LISPVAL_SYM)){ lispval* op = pop_lispval(l, 0); - lispval* result = builtin_op(l, op->sym); + lispval* result = builtin_op(op->sym, l); return result; } return l; @@ -394,7 +352,7 @@ int main(int argc, char** argv) print_lispval_parenthesis(l); } lispval* result = evaluate_lispval(l); - print_lispval_parenthesis(result); + printf("\n"); print_lispval_parenthesis(result); delete_lispval(l); } else { /* Otherwise Print the Error */