diff --git a/Makefile b/Makefile index 385679a..4103a6b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SOURCE:= src/*.c TEST_SOURCE:= src/test/*.c INCLUDE:= include/ -CFLAGS:= -Wall -g --std=gnu99 +CFLAGS:= -Wall -g --std=c11 OBJ:= obj BUILD:= build LFLAGS:= -L./$(BUILD) -lse diff --git a/include/libse.h b/include/libse.h index b9460dd..49299e8 100644 --- a/include/libse.h +++ b/include/libse.h @@ -13,6 +13,8 @@ typedef struct { typedef struct cons { union { void* ptr; + struct cons* car_l; + char* car_s; car_t car; }; struct cons* cdr; @@ -41,5 +43,8 @@ enum _se_type se_typeof(list_t* cons); //Get type of cons's car value. list_t* se_new(char* from, int dupe); //Create new cons. #define se_newd(f) se_new((f), 1) list_t* se_last(list_t* list); //Get last non-nil cons. +car_t se_make(const char* fmt); //Parse "input" as string and "(input)" as sexpr and return as car_t. (NULL returns NIL). +void se_rplaca(list_t* cons, car_t car); //Replace car of cons with `car' +void se_rplacd(list_t* cons, list_t* cdr); //Replace cdr of cons with 'cdr' #endif /* _LIBSE_H */ diff --git a/src/libse.c b/src/libse.c index 0c9eb35..ff900bc 100644 --- a/src/libse.c +++ b/src/libse.c @@ -5,6 +5,15 @@ #include #include +static char* strdup(const char* str) +{ + register size_t size = strlen(str); + char* ret = malloc(size+1); + memcpy(ret, str, size); + ret[size] = 0; + return ret; +} + static void _addcar(list_t* out, exString* token, int nil, int ucase) { if(ucase) @@ -304,3 +313,60 @@ list_t* se_new(char* str, int dupe) { return _se_new(str, SE_STRING, dupe); //We don't need the list part. } + +car_t se_make(const char* fmt) +{ + if(!fmt) + { + car_t c; + memset(&c, 0, sizeof(car_t)); + return c; + } + + while(*fmt && iws(*fmt)) + { + fmt+=1; + } + if(!*fmt) return se_make(NULL); + else if(*fmt == '(') { + car_t c; + list_t* ret = se_parse(fmt, SETP_DEFAULT); + memset(&c,0,sizeof(car_t)); + c.ptr = ret; + c.alloced= 1; + c.size = sizeof(list_t); + c.sexpr = 1; + return c; + } + else { + car_t c; + memset(&c,0,sizeof(car_t)); + c.ptr = strdup(fmt); + c.alloced=1; + c.size = strlen((char*)c.ptr); + return c; + } +} + +void se_rplaca(list_t* cons, car_t car) +{ + if(cons->car.alloced && cons->ptr) + { + switch(se_typeof(cons)) + { + case SE_LIST: + se_free(cons->ptr); break; + case SE_STRING: + free(cons->ptr); break; + default: break; + } + } + memcpy(&cons->car, &car, sizeof(car_t)); +} + +void se_rplacd(list_t* cons, list_t* cdr) +{ + if(cons->cdr) + se_free(cons->cdr); + cons->cdr = cdr; +} diff --git a/src/test/main.c b/src/test/main.c index 9b4de4d..a1e8ee8 100644 --- a/src/test/main.c +++ b/src/test/main.c @@ -25,12 +25,12 @@ void test_exs() exs_nappendc(test, 'A', 6); - exs_appendf(test, "\nCurrent size: %d -> %d", strlen(EXS(test)), exs_realsize(test)); + exs_appendf(test, "\nCurrent size: %ld -> %ld", strlen(EXS(test)), exs_realsize(test)); exs_appendf(test, "AOISDJOAIJS%s", "sokdok"); printf("%s\n", EXS(test)); - printf("strlen() %d, ->currentSize %d\n", strlen(EXS(test)), exs_realsize(test)); + printf("strlen() %ld, ->currentSize %ld\n", strlen(EXS(test)), exs_realsize(test)); exs_free(test); } @@ -59,6 +59,10 @@ void test_parse() return; } //printf("Parse okay.\n"); + + se_rplaca(list, se_make("test1")); + se_rplaca(list->cdr, se_make("(test)")); + se_rplacd(list->cdr->car_l, se_make("(list)").ptr); se_print(NULL, list); printf("\n");