From b443a54822bf84284816dca821ace21b928f1cd0 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 29 Oct 2020 11:23:35 -0400 Subject: [PATCH] Add `pre` for prepending to lists --- src/env.c | 3 ++- src/pebblisp.c | 8 ++++++++ src/pebblisp.h | 1 + src/tests.sh | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/env.c b/src/env.c index 8e567c8..5ed0f35 100644 --- a/src/env.c +++ b/src/env.c @@ -192,7 +192,8 @@ struct Environment defaultEnv() struct symFunc symFuncs[] = { {"+", &add}, {"-", &sub}, {"*", &mul}, {"/", &dvi}, {"%", &mod}, {"=", &equ}, {">", >h}, {"<", <h}, - {"cat", &catObjects}, {"fil", &filter}, {"len", &len}, {"ap", &append}, + {"cat", &catObjects}, {"fil", &filter}, {"len", &len}, + {"ap", &append}, {"pre", &prepend}, {"at", &at}, {"rest", &rest}, {"rev", &reverse}, {"isnum", &isNum}, }; diff --git a/src/pebblisp.c b/src/pebblisp.c index 7138054..9634cdd 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -330,6 +330,14 @@ Object append(Object list, Object newElement, struct Environment *env) return newList; } +Object prepend(Object list, Object newElement, struct Environment *env) +{ + Object newList = listObject(); + nf_addToList(&newList, newElement); + appendList(&newList, &list); + return newList; +} + Object at(Object index, Object list, struct Environment *env) { return cloneObject(*itemAt(&list, index.number)); diff --git a/src/pebblisp.h b/src/pebblisp.h index 6c9ac57..556bb80 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -50,6 +50,7 @@ BASIC_OP(gth); BASIC_OP(lth); Object catObjects(const Object obj1, const Object obj2, struct Environment *env); Object filter(Object obj1, Object obj2, struct Environment *env); Object append(Object list, Object newElement, struct Environment *env); +Object prepend(Object list, Object newElement, struct Environment *env); Object at(Object index, Object list, struct Environment *env); Object rest(Object list, Object ignore, struct Environment *env); Object reverse(Object _list, Object ignore, struct Environment *ignore2); diff --git a/src/tests.sh b/src/tests.sh index 4689f16..967bb89 100755 --- a/src/tests.sh +++ b/src/tests.sh @@ -107,6 +107,8 @@ check "ListIndex" "(at (+ 1 1) (1 2 1000 4 5))" "1000" check "EvalElems" "((* 10 10) 7)" "( 100 7 )" check "AppendToList" "(ap (1 20 300 4000 50000) 600000)" "( 1 20 300 4000 50000 600000 )" check "AppndToEnvList" "(def l (50000 4000 300 20)) (def l (ap l 1)) l" "( 50000 4000 300 20 1 )" +check "PrependToList" "(pre (1 20 300 4000 50000) 600000)" "( 600000 1 20 300 4000 50000 )" +check "PrepndToEnvList" "(def l (50000 4000 300 20)) (def l (pre l 1)) l" "( 1 50000 4000 300 20 )" check "Rest(Tail)OfList" "(def l (50000 4000 300 20)) (rest l)" "( 4000 300 20 )" check "ListLength" "(def l (1 20 3 'abc' 'banana' (+ 10 5))) (len l)" "6" endBlock