From 6151706fb563c0fa4c82e24193e8ec9d170d04e8 Mon Sep 17 00:00:00 2001 From: Daniel Cerqueira Date: Fri, 30 May 2025 22:01:46 +0100 Subject: add split --- TODO | 6 +++--- liblali.c | 25 +++++++++++++++++++++++++ liblali.h | 2 ++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 1fea73e..9b7a05e 100644 --- a/TODO +++ b/TODO @@ -1,14 +1,14 @@ maybe make the repl use the readline library. -create lisp code for addition. use secure functions. make quest a thing. remove macros? -split, join. -make unit testing. instead of returning "#car; + + if (symbol == nil || (symbol->type != TYPE_SYMBOL && + symbol->type != TYPE_STRING && + symbol->type != TYPE_NUMBER)) + exceptionWithObject(symbol, "is not a symbol, not a number, not a string"); + + GC_TRACE(gcRes, nil); + GC_TRACE(gcElement, nil); + char string[2] = {'\0'}; + size_t size = strlen(symbol->string); + for (unsigned long i = 0; i < size; i++) { + string[0] = symbol->string[i]; + if (symbol->type == TYPE_STRING) + *gcElement = newString(string, GC_ROOTS); + else + *gcElement = newSymbol(string, GC_ROOTS); + + *gcRes = newCons(gcElement, gcRes, GC_ROOTS); + } + return reverseList(*gcRes); +} + typedef struct Primitive { char *name; int nMinArgs, nMaxArgs; @@ -1118,6 +1142,7 @@ Primitive primitives[] = { { ">", 1, -1, primitiveGreater }, /* { ">=", 1, -1, primitiveGreaterEqual } */ { "join", 1, 1, primitiveJoin }, + { "split", 1, 1, primitiveSplit }, }; // Special forms handled by evalExpr. Must be in the same order as above. diff --git a/liblali.h b/liblali.h index a9b17d0..d1d7c24 100644 --- a/liblali.h +++ b/liblali.h @@ -333,6 +333,8 @@ Object *primitiveGreater(Object **args, GC_PARAM); Object *primitiveJoin(Object **args, GC_PARAM); +Object *primitiveSplit(Object **args, GC_PARAM); + // EVALUATION ///////////////////////////////////////////////////////////////// /* Scheme-style tail recursive evaluation. evalProg, evalProgs, evalCond, and -- cgit