shithub: femtolisp

Download patch

ref: 849389558d7c989902662f32ba956e34ac4e21c5
parent: 252c5221c6eb32fa05b69f8683b01b5c9f832bcb
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Nov 26 14:50:19 EST 2024

small cleanup

--- a/flisp.c
+++ b/flisp.c
@@ -26,7 +26,7 @@
 
 __thread Fl *fl;
 
-int
+bool
 isbuiltin(value_t x)
 {
 	uint32_t i;
@@ -160,7 +160,7 @@
 
 // symbol table ---------------------------------------------------------------
 
-int
+bool
 fl_is_keyword_name(const char *str, size_t len)
 {
 	return (str[0] == ':' || str[len-1] == ':') && str[1] != '\0';
@@ -211,10 +211,9 @@
 {
 	argcount(nargs, 0);
 	USED(args);
-	gensym_t *gs = alloc_words(sizeof(gensym_t)/sizeof(void*));
+	gensym_t *gs = alloc_words(sizeof(gensym_t)/sizeof(value_t));
 	gs->id = FL(gensym_ctr)++;
 	gs->binding = UNBOUND;
-	gs->isconst = 0;
 	gs->type = nil;
 	return tagptr(gs, TAG_SYM);
 }
@@ -292,7 +291,7 @@
 			gc(1);
 	}
 	first = (value_t*)FL(curheap);
-	FL(curheap) += (n*sizeof(value_t));
+	FL(curheap) += n*sizeof(value_t);
 	return first;
 }
 
@@ -412,10 +411,9 @@
 	}
 	if(t == TAG_SYM){
 		gensym_t *gs = ptr(v);
-		gensym_t *ng = alloc_words(sizeof(gensym_t)/sizeof(void*));
+		gensym_t *ng = alloc_words(sizeof(gensym_t)/sizeof(value_t));
 		ng->id = gs->id;
 		ng->binding = gs->binding;
-		ng->isconst = 0;
 		nc = tagptr(ng, TAG_SYM);
 		forward(v, nc);
 		if(__likely(ng->binding != UNBOUND))
@@ -654,20 +652,20 @@
 	return c;
 }
 
-int
+bool
 fl_isnumber(value_t v)
 {
 	if(isfixnum(v))
-		return 1;
+		return true;
 	if(iscprim(v)){
 		cprim_t *c = ptr(v);
-		return c->type != FL(runetype);
+		return c->type != FL(runetype) && valid_numtype(c->type->numtype);
 	}
 	if(iscvalue(v)){
 		cvalue_t *c = ptr(v);
-		return valid_numtype(cv_class(c)->numtype);
+		return valid_numtype(cp_numtype(c));
 	}
-	return 0;
+	return false;
 }
 
 // eval -----------------------------------------------------------------------
--- a/flisp.h
+++ b/flisp.h
@@ -7,6 +7,8 @@
 #include "htableh.inc"
 HTPROT(ptrhash)
 
+typedef struct fltype_t fltype_t;
+
 enum {
 	TAG_NUM,
 	TAG_CPRIM,
@@ -55,6 +57,10 @@
 }cons_t;
 
 typedef struct _symbol_t {
+	fltype_t *type;
+	void *dlcache;	 // dlsym address
+	struct _symbol_t *left;
+	struct _symbol_t *right;
 	value_t binding;   // global value binding
 	uint32_t hash;
 	uint8_t numtype;
@@ -61,21 +67,12 @@
 	uint8_t size;
 	uint8_t align;
 	uint8_t flags;
-	struct _fltype_t *type;
-	void *dlcache;	 // dlsym address
-	// below fields are private
-	struct _symbol_t *left;
-	struct _symbol_t *right;
-	union {
-		char name[1];
-		void *_pad;	// ensure field aligned to pointer size
-	};
+	char name[1];
 }symbol_t;
 
 typedef struct {
-	value_t isconst;
+	fltype_t *type;
 	value_t binding;   // global value binding
-	struct _fltype_t *type;
 	uint32_t id;
 }gensym_t;
 
@@ -82,8 +79,8 @@
 typedef struct Builtin Builtin;
 
 struct Builtin {
-	char *name;
-	int  nargs;
+	const char *name;
+	int nargs;
 };
 
 typedef value_t (*builtin_t)(value_t*, uint32_t);
@@ -110,15 +107,14 @@
 #define isvector(x) (tag(x) == TAG_VECTOR)
 #define iscvalue(x) (tag(x) == TAG_CVALUE)
 #define iscprim(x)  (tag(x) == TAG_CPRIM)
-#define selfevaluating(x) (tag(x) < 6) /* mag: UNUSED? */
 // doesn't lead to other values
 #define leafp(a) (((a)&3) != 3)
 
 // allocate n consecutive conses
 #define cons_reserve(n) tagptr(alloc_words((n)*2), TAG_CONS)
-#define cons_index(c)  (((cons_t*)ptr(c))-((cons_t*)FL(fromspace)))
-#define ismarked(c)     bitvector_get(FL(consflags), cons_index(c))
-#define mark_cons(c)   bitvector_set(FL(consflags), cons_index(c))
+#define cons_index(c) (((cons_t*)ptr(c))-((cons_t*)FL(fromspace)))
+#define ismarked(c) bitvector_get(FL(consflags), cons_index(c))
+#define mark_cons(c) bitvector_set(FL(consflags), cons_index(c))
 #define unmark_cons(c) bitvector_reset(FL(consflags), cons_index(c))
 
 #define isforwarded(v) (((value_t*)ptr(v))[0] == TAG_FWD)
@@ -142,7 +138,7 @@
 #define fn_vals(f) (((value_t*)ptr(f))[1])
 #define fn_env(f) (((value_t*)ptr(f))[2])
 #define fn_name(f) (((value_t*)ptr(f))[3])
-#define set(s, v)  (((symbol_t*)ptr(s))->binding = (v))
+#define set(s, v) (((symbol_t*)ptr(s))->binding = (v))
 #define setc(s, v) \
 	do{ \
 		((symbol_t*)ptr(s))->flags |= FLAG_CONST; \
@@ -174,7 +170,7 @@
 	}while(0)
 #define POP() (FL(stack)[--FL(sp)])
 
-int isbuiltin(value_t x);
+bool isbuiltin(value_t x);
 void fl_init(size_t initial_heapsize);
 int fl_load_system_image(value_t ios);
 
@@ -200,8 +196,8 @@
 value_t fl_cons(value_t a, value_t b);
 value_t fl_list2(value_t a, value_t b);
 value_t fl_listn(size_t n, ...);
-int fl_isnumber(value_t v);
-int fl_is_keyword_name(const char *str, size_t len);
+bool fl_isnumber(value_t v);
+bool fl_is_keyword_name(const char *str, size_t len);
 value_t alloc_vector(size_t n, int init);
 
 /* safe casts */
@@ -271,19 +267,19 @@
 	void (*print_traverse)(value_t self);
 } cvtable_t;
 
-typedef int (*cvinitfunc_t)(struct _fltype_t*, value_t, void*);
+typedef int (*cvinitfunc_t)(fltype_t*, value_t, void*);
 
-typedef struct _fltype_t {
+struct fltype_t {
 	value_t type;
 	cvtable_t *vtable;
-	struct _fltype_t *eltype;  // for arrays
-	struct _fltype_t *artype;  // (array this)
+	fltype_t *eltype;  // for arrays
+	fltype_t *artype;  // (array this)
 	cvinitfunc_t init;
 	size_t size;
 	size_t elsz;
 	int marked;
 	numerictype_t numtype;
-}fltype_t;
+};
 
 typedef struct {
 	fltype_t *type;