From 2d7568afb438bb3544eb7de0a164aa2c5c2b2009 Mon Sep 17 00:00:00 2001 From: Daniel Cerqueira Date: Fri, 30 May 2025 19:26:17 +0100 Subject: make joining strings produce a string --- liblali.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/liblali.c b/liblali.c index 7394ca3..ac552c2 100644 --- a/liblali.c +++ b/liblali.c @@ -1041,19 +1041,23 @@ Object *primitiveJoin(Object **args, GC_PARAM) { exceptionWithObject((*args)->car, "is not a list"); Object *tmpCons = (*args)->car; + + int elementsType = tmpCons->car->type; + unsigned long i = strlen(tmpCons->car->string); - for (; tmpCons->cdr != nil; tmpCons = tmpCons->cdr) + for (; tmpCons->cdr != nil; tmpCons = tmpCons->cdr) { i += strlen(tmpCons->cdr->car->string); - + if (elementsType != tmpCons->cdr->car->type) + exception("cannot join list atoms of different type"); + } char symbolResult[i+1]; + tmpCons = (*args)->car; unsigned long wordLength = strlen(tmpCons->car->string); unsigned long j = 0; - // first list element for (; j < wordLength; j++) symbolResult[j] = tmpCons->car->string[j]; - // rest of list elements if (tmpCons->cdr->car != nil) { for (i = wordLength; tmpCons->cdr != nil; tmpCons = tmpCons->cdr) { @@ -1064,7 +1068,11 @@ Object *primitiveJoin(Object **args, GC_PARAM) { } } symbolResult[i] = '\0'; - return newSymbol(symbolResult, GC_ROOTS); + + if (elementsType == TYPE_STRING) + return newString(symbolResult, GC_ROOTS); + else + return newSymbol(symbolResult, GC_ROOTS); } typedef struct Primitive { -- cgit