ref: 9ceaddc0e5d8146dabfe56aea3acd54bf7e25ea3
parent: 7de6b74a4cca7a1519df57903f194edfb655b706
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Feb 5 16:32:05 EST 2025
more bool
--- a/src/bitvector.c
+++ b/src/bitvector.c
@@ -1,7 +1,7 @@
#include "flisp.h"
uint32_t *
-bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz, int initzero)
+bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz, bool zero)
{
uint32_t *p;
size_t sz = ((newsz+31)>>5) * sizeof(uint32_t);
@@ -8,7 +8,7 @@
p = MEM_REALLOC(b, sz);
if(p == nil)
return nil;
- if(initzero && newsz>oldsz){
+ if(zero && newsz > oldsz){
size_t osz = ((oldsz+31)>>5) * sizeof(uint32_t);
memset(&p[osz/sizeof(uint32_t)], 0, sz-osz);
}
--- a/src/bitvector.h
+++ b/src/bitvector.h
@@ -1,4 +1,4 @@
-uint32_t *bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz, int initzero);
+uint32_t *bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz, bool zero);
#define bitvector_new(n, initzero) bitvector_resize(nil, 0, (n), (initzero))
#define bitvector_nwords(nbits) (((uint64_t)(nbits)+31)>>5)
--- a/src/equal.c
+++ b/src/equal.c
@@ -315,9 +315,8 @@
// *oob: output argument, means we hit the limit specified by 'bound'
static uintptr_t
-bounded_hash(value_t a, int bound, int *oob)
+bounded_hash(value_t a, int bound, bool *oob)
{
- *oob = 0;
union {
double d;
int64_t i64;
@@ -328,8 +327,10 @@
cprim_t *cp;
void *data;
uintptr_t h = 0;
- int oob2, tg = tag(a);
+ int tg = tag(a);
+ bool oob2;
+ *oob = false;
switch(tg){
case TAG_NUM :
case TAG_NUM1:
@@ -363,7 +364,7 @@
case TAG_VECTOR:
if(bound <= 0){
- *oob = 1;
+ *oob = true;
return 1;
}
len = vector_size(a);
@@ -378,7 +379,7 @@
case TAG_CONS:
do{
if(bound <= 0){
- *oob = 1;
+ *oob = true;
return h;
}
h = MIX(h, bounded_hash(car_(a), bound/2, &oob2));
@@ -411,7 +412,7 @@
uintptr_t
hash_lispvalue(value_t a)
{
- int oob = 0;
+ bool oob = false;
return bounded_hash(a, BOUNDED_HASH_BOUND, &oob);
}
--- a/src/flisp.c
+++ b/src/flisp.c
@@ -301,7 +301,7 @@
n = ALIGNED(n, 2); // only allocate multiples of 2 words
if(fl_unlikely((value_t*)FL(curheap) > (value_t*)FL(lim)+2-n)){
fl_gc(0);
- while((value_t*)FL(curheap) > ((value_t*)FL(lim))+2-n)
+ while(fl_unlikely((value_t*)FL(curheap) > ((value_t*)FL(lim))+2-n))
fl_gc(1);
}
first = (value_t*)FL(curheap);
@@ -310,7 +310,7 @@
}
value_t
-alloc_vector(size_t n, int init)
+alloc_vector(size_t n, bool init)
{
if(n == 0)
return FL(the_empty_vector);
@@ -318,8 +318,7 @@
value_t v = tagptr(c, TAG_VECTOR);
vector_setsize(v, n);
if(init){
- unsigned int i;
- for(i = 0; i < n; i++)
+ for(size_t i = 0; i < n; i++)
vector_elt(v, i) = FL_void;
}
return v;
--- a/src/flisp.h
+++ b/src/flisp.h
@@ -208,7 +208,7 @@
value_t fl_list2(value_t a, value_t b);
value_t fl_listn(size_t n, ...);
bool fl_isnumber(value_t v) fl_purefn;
-value_t alloc_vector(size_t n, int init);
+value_t alloc_vector(size_t n, bool init);
/* consistent iswprint and wcwidth */
int fl_iswprint(Rune c) fl_constfn;
--- a/src/ios.c
+++ b/src/ios.c
@@ -136,7 +136,7 @@
down to this size, in which case we need to copy. */
s->buf = &s->local[0];
s->maxsize = IOS_INLSIZE;
- s->ownbuf = 1;
+ s->ownbuf = true;
return s->buf;
}
@@ -154,7 +154,7 @@
temp = MEM_ALLOC(sz);
if(temp == nil)
return nil;
- s->ownbuf = 1;
+ s->ownbuf = true;
if(s->buf != nil)
memcpy(temp, s->buf, s->size);
}
@@ -228,7 +228,7 @@
// can't get any more data
s->state = bst_rd;
if(avail == 0)
- s->_eof = 1;
+ s->_eof = true;
return avail;
}
@@ -250,16 +250,16 @@
_os_read(s->fd, dest, n, &got);
tot += got;
if(got == 0)
- s->_eof = 1;
+ s->_eof = true;
return tot;
}else{
// refill buffer
if(_os_read(s->fd, s->buf, s->maxsize, &got)){
- s->_eof = 1;
+ s->_eof = true;
return tot;
}
if(got == 0){
- s->_eof = 1;
+ s->_eof = true;
return tot;
}
s->size = got;
@@ -377,7 +377,7 @@
{
if(s->state == bst_closed)
return 0;
- s->_eof = 0;
+ s->_eof = false;
if(s->bm == bm_mem){
if((size_t)pos > s->size)
return -1;
@@ -397,7 +397,7 @@
{
if(s->state == bst_closed)
return 0;
- s->_eof = 1;
+ s->_eof = true;
if(s->bm == bm_mem){
s->bpos = s->size;
}else{
@@ -427,7 +427,7 @@
}else if(offs < 0){
if(-offs <= (off_t)s->bpos){
s->bpos += offs;
- s->_eof = 0;
+ s->_eof = false;
return 0;
}else if(s->bm == bm_mem){
return -1;
@@ -442,7 +442,7 @@
if(fdpos == (off_t)-1)
return fdpos;
s->bpos = s->size = 0;
- s->_eof = 0;
+ s->_eof = false;
}
return 0;
}
@@ -615,7 +615,7 @@
}
int
-ios_setbuf(ios_t *s, uint8_t *buf, size_t size, int own)
+ios_setbuf(ios_t *s, uint8_t *buf, size_t size, bool own)
{
ios_flush(s);
size_t nvalid;
@@ -654,7 +654,7 @@
return;
ios_flush(s);
s->state = bst_none;
- s->readonly = 1;
+ s->readonly = true;
}
static size_t
@@ -665,7 +665,7 @@
do{
avail = ios_readprep(from, IOS_BUFSIZE/2);
if(avail == 0){
- from->_eof = 1;
+ from->_eof = true;
break;
}
size_t written, ntowrite;
@@ -704,11 +704,11 @@
ios_copyuntil(ios_t *to, ios_t *from, uint8_t delim)
{
size_t total = 0, avail = from->size - from->bpos;
- int first = 1;
+ bool first = true;
if(!ios_eof(from)){
do{
if(avail == 0){
- first = 0;
+ first = false;
avail = ios_readprep(from, LINE_CHUNK_SIZE);
}
size_t written;
@@ -727,7 +727,7 @@
}
}while(!ios_eof(from) && (first || avail >= LINE_CHUNK_SIZE));
}
- from->_eof = 1;
+ from->_eof = true;
return total;
}
@@ -740,7 +740,7 @@
s->state = bst_none;
s->fpos = -1;
s->fd = -1;
- s->ownbuf = 1;
+ s->ownbuf = true;
s->loc.lineno = 1;
}
@@ -747,7 +747,7 @@
/* stream object initializers. we do no allocation. */
ios_t *
-ios_file(ios_t *s, char *fname, int rd, int wr, int creat, int trunc)
+ios_file(ios_t *s, char *fname, bool rd, bool wr, bool creat, bool trunc)
{
int fd;
if(!(rd || wr)) // must specify read and/or write
@@ -766,7 +766,7 @@
if(fd < 0)
goto open_file_err;
if(!wr)
- s->readonly = 1;
+ s->readonly = true;
s->loc.filename = MEM_STRDUP(fname);
return s;
open_file_err:
@@ -795,12 +795,12 @@
}
ios_t *
-ios_fd(ios_t *s, int fd, int isfile, int own)
+ios_fd(ios_t *s, int fd, bool isfile, bool own)
{
_ios_init(s);
s->fd = fd;
if(isfile)
- s->rereadable = 1;
+ s->rereadable = true;
_buf_init(s, bm_block);
s->ownfd = own;
if(fd == STDERR_FILENO)
@@ -925,7 +925,7 @@
for(i = 0; i < sizeof(buf); i++){
if((c = ios_getc(s)) == IOS_EOF){
- s->_eof = 1;
+ s->_eof = true;
return IOS_EOF;
}
buf[i] = c;
--- a/src/ios.h
+++ b/src/ios.h
@@ -42,25 +42,15 @@
int fd;
- uint8_t readonly:1;
- uint8_t ownbuf:1;
- uint8_t ownfd:1;
- uint8_t _eof:1;
+ bool readonly;
+ bool ownbuf;
+ bool ownfd;
+ bool _eof;
// this means you can read, seek back, then read the same data
// again any number of times. usually only true for files and strings.
- uint8_t rereadable:1;
+ bool rereadable;
- // this enables "stenciled writes". you can alternately write and
- // seek without flushing in between. this performs read-before-write
- // to populate the buffer, so "rereadable" capability is required.
- // this is off by default.
- //uint8_t stenciled:1;
-
- // request durable writes (fsync)
- // uint8_t durable:1;
-
- // todo: mutex
uint8_t local[IOS_INLSIZE];
}ios_t;
@@ -80,7 +70,7 @@
void ios_free(ios_t *s);
uint8_t *ios_takebuf(ios_t *s, size_t *psize); // null-terminate and release buffer to caller
// set buffer space to use
-int ios_setbuf(ios_t *s, uint8_t *buf, size_t size, int own);
+int ios_setbuf(ios_t *s, uint8_t *buf, size_t size, bool own);
int ios_bufmode(ios_t *s, bufmode_t mode);
void ios_set_readonly(ios_t *s);
size_t ios_copy(ios_t *to, ios_t *from, size_t nbytes);
@@ -90,10 +80,10 @@
int ios_wait(ios_t *s, double ws);
/* stream creation */
-ios_t *ios_file(ios_t *s, char *fname, int rd, int wr, int create, int trunc);
+ios_t *ios_file(ios_t *s, char *fname, bool rd, bool wr, bool create, bool trunc);
ios_t *ios_mem(ios_t *s, size_t initsize);
ios_t *ios_static_buffer(ios_t *s, const uint8_t *buf, size_t sz);
-ios_t *ios_fd(ios_t *s, int fd, int isfile, int own);
+ios_t *ios_fd(ios_t *s, int fd, bool isfile, bool own);
// todo: ios_socket
extern ios_t *ios_stdin;
extern ios_t *ios_stdout;
--- a/src/iostream.c
+++ b/src/iostream.c
@@ -75,7 +75,7 @@
if(nargs < 1)
argcount(nargs, 1);
size_t i;
- int r = 0, w = 0, c = 0, t = 0, a = 0;
+ bool r = false, w = false, c = false, t = false, a = false;
for(i = 1; i < nargs; i++){
if(args[i] == FL_rdsym)
r = 1;
@@ -88,8 +88,8 @@
else if(args[i] == FL_truncsym)
t = w = 1;
}
- if((r|w|c|t|a) == 0)
- r = 1; // default to reading
+ if(!r && !w && !c && !t && !a)
+ r = true; // default to reading
value_t f = cvalue(FL(iostreamtype), sizeof(ios_t));
char *fname = tostring(args[0]);
ios_t *s = value2c(ios_t*, f);
--- a/src/operators.c
+++ b/src/operators.c
@@ -104,7 +104,7 @@
}
fl_purefn
-int
+bool
cmp_same_lt(void *a, void *b, numerictype_t tag)
{
switch(tag){
@@ -120,11 +120,11 @@
case T_FLOAT: return *(float*)a < *(float*)b;
case T_DOUBLE: return *(double*)a < *(double*)b;
}
- return 0;
+ return false;
}
fl_purefn
-int
+bool
cmp_same_eq(void *a, void *b, numerictype_t tag)
{
switch(tag){
@@ -140,13 +140,13 @@
case T_FLOAT: return *(float*)a == *(float*)b && !isnan(*(float*)a);
case T_DOUBLE: return *(double*)a == *(double*)b && !isnan(*(double*)b);
}
- return 0;
+ return false;
}
/* FIXME one is allocated for all compare ops */
static mpint *cmpmpint;
-int
+bool
cmp_lt(void *a, numerictype_t atag, void *b, numerictype_t btag)
{
if(atag == btag)
@@ -158,9 +158,9 @@
// casting to double will only get the wrong answer for big int64s
// that differ in low bits
if(da < db && !isnan(da) && !isnan(db))
- return 1;
+ return true;
if(db < da)
- return 0;
+ return false;
if(cmpmpint == nil && (atag == T_MPINT || btag == T_MPINT))
cmpmpint = mpnew(0);
@@ -193,11 +193,11 @@
if(atag == T_MPINT)
return mpcmp(*(mpint**)a, vtomp(*(int64_t*)b, cmpmpint)) < 0;
}
- return 0;
+ return false;
}
-int
-cmp_eq(void *a, numerictype_t atag, void *b, numerictype_t btag, int equalnans)
+bool
+cmp_eq(void *a, numerictype_t atag, void *b, numerictype_t btag, bool equalnans)
{
union {
double d;
@@ -219,7 +219,7 @@
}
if(da != db)
- return 0;
+ return false;
if(cmpmpint == nil && (atag == T_MPINT || btag == T_MPINT))
cmpmpint = mpnew(0);
@@ -258,5 +258,5 @@
if(atag == T_MPINT)
return mpcmp(*(mpint**)a, vtomp(*(int64_t*)b, cmpmpint)) == 0;
}
- return 1;
+ return true;
}
--- a/src/operators.h
+++ b/src/operators.h
@@ -3,10 +3,10 @@
mpint * conv_to_mpint(void *data, numerictype_t tag);
double conv_to_double(void *data, numerictype_t tag);
-int cmp_same_lt(void *a, void *b, numerictype_t tag);
-int cmp_same_eq(void *a, void *b, numerictype_t tag);
-int cmp_lt(void *a, numerictype_t atag, void *b, numerictype_t btag);
-int cmp_eq(void *a, numerictype_t atag, void *b, numerictype_t btag, int equalnans);
+bool cmp_same_lt(void *a, void *b, numerictype_t tag);
+bool cmp_same_eq(void *a, void *b, numerictype_t tag);
+bool cmp_lt(void *a, numerictype_t atag, void *b, numerictype_t btag);
+bool cmp_eq(void *a, numerictype_t atag, void *b, numerictype_t btag, bool equalnans);
int64_t conv_to_int64(void *data, numerictype_t tag);
uint64_t conv_to_uint64(void *data, numerictype_t tag);
--- a/src/print.c
+++ b/src/print.c
@@ -116,19 +116,20 @@
static void
print_symbol_name(ios_t *f, const char *name)
{
- int i, escape = 0, charescape = 0;
+ int i;
+ bool escape = false, charescape = false;
if((name[0] == '\0') ||
(name[0] == '.' && name[1] == '\0') ||
(name[0] == '#') ||
fl_read_numtok(name, nil, 0))
- escape = 1;
+ escape = true;
i = 0;
while(name[i]){
if(!symchar(name[i])){
- escape = 1;
+ escape = true;
if(name[i] == '|' || name[i] == '\\'){
- charescape = 1;
+ charescape = true;
break;
}
}
--- a/src/string.c
+++ b/src/string.c
@@ -368,7 +368,7 @@
if(nargs < 1 || nargs > 2)
argcount(nargs, 2);
value_t n = args[0];
- int neg = 0;
+ bool neg = false;
uint64_t num;
int radix = 10;
if(nargs == 2)
@@ -401,7 +401,7 @@
}
if(numval(fl_compare(args[0], fixnum(0), false)) < 0){
num = -num;
- neg = 1;
+ neg = true;
}
char buf[128], *str = uint2str(buf, sizeof(buf), num, radix);
if(neg && str > buf)