From 91714d102555855732cad5a929da59948609470a Mon Sep 17 00:00:00 2001 From: "Javier B. Torres" Date: Mon, 12 Jan 2026 09:01:33 -0300 Subject: [PATCH] begin work on strings --- .gitignore | 1 + include/wolflisp.h | 8 ++++++++ meson.build | 1 + src/print.c | 5 +++++ src/read.c | 2 +- src/string.c | 15 +++++++++++++++ src/type.c | 1 + 7 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d654e30..7dd96ee 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ wscm compile_commands.json .cache .envrc +test.lisp diff --git a/include/wolflisp.h b/include/wolflisp.h index 14c1c79..d0051bc 100644 --- a/include/wolflisp.h +++ b/include/wolflisp.h @@ -38,6 +38,11 @@ typedef struct Sy { U8 *data; } Sy; +typedef struct Ss { + Z len; + char data[]; +} Ss; + // Closure typedef struct Cl { O args, body, env; @@ -73,6 +78,7 @@ enum { TYPE_SYM = 2, TYPE_PRIM = 4, TYPE_PAIR, + TYPE_STR, TYPE_CLOS, TYPE_MAC, TYPE_FWD, @@ -197,6 +203,8 @@ O pair_make(In *in, O head, O tail); // Unwrap a pair Pa *pair_unwrap(O obj); +O string_make(In *in, const char *cstr, I len); + V print(O obj); V println(O obj); diff --git a/meson.build b/meson.build index a22f9ca..f807883 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,7 @@ src = [ 'src/prim.c', 'src/print.c', 'src/read.c', + 'src/string.c', 'src/symbol.c', 'src/type.c', ] diff --git a/src/print.c b/src/print.c index 884b8cd..8b8ce53 100644 --- a/src/print.c +++ b/src/print.c @@ -55,6 +55,11 @@ void print(O obj) { case TYPE_PAIR: print_pair(obj); break; + case TYPE_STR: { + Ss *s = (Ss *)(h + 1); + printf("%.*s", (int)s->len, s->data); + break; + } case TYPE_CLOS: { Cl *cl = (Cl *)(h + 1); printf("<#fn "); diff --git a/src/read.c b/src/read.c index 06f09ac..43b6a45 100644 --- a/src/read.c +++ b/src/read.c @@ -76,7 +76,7 @@ int read_expr(In *in, Lx *lex, O *result) { } case TOK_STRING: - *result = symbol_make(in, lex->buffer); + *result = string_make(in, lex->buffer, -1); break; case TOK_WORD: diff --git a/src/string.c b/src/string.c index e69de29..59ea883 100644 --- a/src/string.c +++ b/src/string.c @@ -0,0 +1,15 @@ +#include +#include + +O string_make(In *in, const char *cstr, I len) { + if (len < 0) + len = strlen(cstr); + Z size = sizeof(Gh) + sizeof(Ss) + len + 1; + Gh *hdr = gc_alloc(&in->gc, size); + hdr->type = TYPE_STR; + Ss *s = (Ss *)(hdr + 1); + s->len = len; + memcpy(s->data, cstr, len); + s->data[len] = 0; + return BOX(hdr); +} diff --git a/src/type.c b/src/type.c index 3305bd0..7d9ada6 100644 --- a/src/type.c +++ b/src/type.c @@ -10,6 +10,7 @@ static const char *typenames[] = { [TYPE_SYM] = "symbol", [TYPE_PRIM] = "primitive", [TYPE_PAIR] = "pair", + [TYPE_STR] = "string", [TYPE_CLOS] = "closure", [TYPE_MAC] = "macro", };