begin work on strings

This commit is contained in:
Lobo 2026-01-12 09:01:33 -03:00
parent 83fce46449
commit 91714d1025
7 changed files with 32 additions and 1 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ wscm
compile_commands.json compile_commands.json
.cache .cache
.envrc .envrc
test.lisp

View file

@ -38,6 +38,11 @@ typedef struct Sy {
U8 *data; U8 *data;
} Sy; } Sy;
typedef struct Ss {
Z len;
char data[];
} Ss;
// Closure // Closure
typedef struct Cl { typedef struct Cl {
O args, body, env; O args, body, env;
@ -73,6 +78,7 @@ enum {
TYPE_SYM = 2, TYPE_SYM = 2,
TYPE_PRIM = 4, TYPE_PRIM = 4,
TYPE_PAIR, TYPE_PAIR,
TYPE_STR,
TYPE_CLOS, TYPE_CLOS,
TYPE_MAC, TYPE_MAC,
TYPE_FWD, TYPE_FWD,
@ -197,6 +203,8 @@ O pair_make(In *in, O head, O tail);
// Unwrap a pair // Unwrap a pair
Pa *pair_unwrap(O obj); Pa *pair_unwrap(O obj);
O string_make(In *in, const char *cstr, I len);
V print(O obj); V print(O obj);
V println(O obj); V println(O obj);

View file

@ -18,6 +18,7 @@ src = [
'src/prim.c', 'src/prim.c',
'src/print.c', 'src/print.c',
'src/read.c', 'src/read.c',
'src/string.c',
'src/symbol.c', 'src/symbol.c',
'src/type.c', 'src/type.c',
] ]

View file

@ -55,6 +55,11 @@ void print(O obj) {
case TYPE_PAIR: case TYPE_PAIR:
print_pair(obj); print_pair(obj);
break; break;
case TYPE_STR: {
Ss *s = (Ss *)(h + 1);
printf("%.*s", (int)s->len, s->data);
break;
}
case TYPE_CLOS: { case TYPE_CLOS: {
Cl *cl = (Cl *)(h + 1); Cl *cl = (Cl *)(h + 1);
printf("<#fn "); printf("<#fn ");

View file

@ -76,7 +76,7 @@ int read_expr(In *in, Lx *lex, O *result) {
} }
case TOK_STRING: case TOK_STRING:
*result = symbol_make(in, lex->buffer); *result = string_make(in, lex->buffer, -1);
break; break;
case TOK_WORD: case TOK_WORD:

View file

@ -0,0 +1,15 @@
#include <string.h>
#include <wolflisp.h>
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);
}

View file

@ -10,6 +10,7 @@ static const char *typenames[] = {
[TYPE_SYM] = "symbol", [TYPE_SYM] = "symbol",
[TYPE_PRIM] = "primitive", [TYPE_PRIM] = "primitive",
[TYPE_PAIR] = "pair", [TYPE_PAIR] = "pair",
[TYPE_STR] = "string",
[TYPE_CLOS] = "closure", [TYPE_CLOS] = "closure",
[TYPE_MAC] = "macro", [TYPE_MAC] = "macro",
}; };