shithub: femtolisp

Download patch

ref: 6c30f50bd33ba6675ca81981a734cfc0697394c2
parent: 5039afcaaf87b258e7835184119f8e4e2cb96f15
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Dec 24 12:39:19 EST 2024

ios_put*, fl_print_*, out*: fix awkward argument ordering

--- a/flisp.c
+++ b/flisp.c
@@ -2377,9 +2377,9 @@
 		}
 	}
 	FL_CATCH_NO_INC{
-		ios_puts("fatal error during bootstrap:\n", ios_stderr);
+		ios_puts(ios_stderr, "fatal error during bootstrap: ");
 		fl_print(ios_stderr, FL(lasterror));
-		ios_putc('\n', ios_stderr);
+		ios_putc(ios_stderr, '\n');
 		return 1;
 	}
 	ios_close(value2c(ios_t*, FL(stack)[FL(sp)-1]));
--- a/flmain.c
+++ b/flmain.c
@@ -54,9 +54,9 @@
 		}
 	}
 	FL_CATCH_EXTERN_NO_RESTORE{
-		ios_puts("fatal error:\n", ios_stderr);
+		ios_puts(ios_stderr, "fatal error:\n");
 		fl_print(ios_stderr, FL(lasterror));
-		ios_putc('\n', ios_stderr);
+		ios_putc(ios_stderr, '\n');
 		break;
 	}
 	fl_exit(r);
--- a/ios.c
+++ b/ios.c
@@ -817,7 +817,7 @@
 /* higher level interface */
 
 int
-ios_putc(int c, ios_t *s)
+ios_putc(ios_t *s, int c)
 {
 	char ch = c;
 
--- a/ios.h
+++ b/ios.h
@@ -117,10 +117,10 @@
 
 /* stdio-style functions */
 #define IOS_EOF (-1)
-int ios_putc(int c, ios_t *s);
+int ios_putc(ios_t *s, int c);
 int ios_getc(ios_t *s);
 int ios_peekc(ios_t *s);
-#define ios_puts(str, s) ios_write(s, str, strlen(str))
+#define ios_puts(s, str) ios_write(s, str, strlen(str))
 
 /*
   With memory streams, mixed reads and writes are equivalent to performing
--- a/iostream.c
+++ b/iostream.c
@@ -9,12 +9,12 @@
 print_iostream(value_t v, ios_t *f)
 {
 	USED(v);
-	fl_print_str("#<io stream", f);
+	fl_print_str(f, "#<io stream");
 	if(*f->loc.filename){
-		fl_print_chr(' ', f);
-		fl_print_str(f->loc.filename, f);
+		fl_print_chr(f, ' ');
+		fl_print_str(f, f->loc.filename);
 	}
-	fl_print_chr('>', f);
+	fl_print_chr(f, '>');
 }
 
 static void
--- a/print.c
+++ b/print.c
@@ -8,9 +8,9 @@
 #define LOG2_10 3.321928094887362347870319429489
 
 static void
-outc(char c, ios_t *f)
+outc(ios_t *f, char c)
 {
-	ios_putc(c, f);
+	ios_putc(f, c);
 	if(c == '\n')
 		FL(hpos) = 0;
 	else
@@ -18,14 +18,14 @@
 }
 
 static void
-outs(const char *s, ios_t *f)
+outs(ios_t *f, const char *s)
 {
-	ios_puts(s, f);
+	ios_puts(f, s);
 	FL(hpos) += u8_strwidth(s);
 }
 
 static void
-outsn(const char *s, ios_t *f, size_t n)
+outsn(ios_t *f, const char *s, size_t n)
 {
 	ios_write(f, s, n);
 	FL(hpos) += u8_strwidth(s);
@@ -32,21 +32,21 @@
 }
 
 static int
-outindent(int n, ios_t *f)
+outindent(ios_t *f, int n)
 {
 	// move back to left margin if we get too indented
 	if(n > FL(scr_width)-12)
 		n = 2;
 	int n0 = n;
-	ios_putc('\n', f);
+	ios_putc(f, '\n');
 	FL(vpos)++;
 	FL(hpos) = n;
 	while(n >= 8){
-		ios_putc('\t', f);
+		ios_putc(f, '\t');
 		n -= 8;
 	}
 	while(n){
-		ios_putc(' ', f);
+		ios_putc(f, ' ');
 		n--;
 	}
 	return n0;
@@ -53,15 +53,15 @@
 }
 
 void
-fl_print_chr(char c, ios_t *f)
+fl_print_chr(ios_t *f, char c)
 {
-	outc(c, f);
+	outc(f, c);
 }
 
 void
-fl_print_str(const char *s, ios_t *f)
+fl_print_str(ios_t *f, const char *s)
 {
-	outs(s, f);
+	outs(f, s);
 }
 
 void
@@ -135,22 +135,22 @@
 	}
 	if(escape){
 		if(charescape){
-			outc('|', f);
+			outc(f, '|');
 			i = 0;
 			while(name[i]){
 				if(name[i] == '|' || name[i] == '\\')
-					outc('\\', f);
-				outc(name[i], f);
+					outc(f, '\\');
+				outc(f, name[i]);
 				i++;
 			}
-			outc('|', f);
+			outc(f, '|');
 		}else{
-			outc('|', f);
-			outs(name, f);
-			outc('|', f);
+			outc(f, '|');
+			outs(f, name);
+			outc(f, '|');
 		}
 	}else{
-		outs(name, f);
+		outs(f, name);
 	}
 }
 
@@ -290,12 +290,12 @@
 		// special prefix syntax
 		unmark_cons(v);
 		unmark_cons(cdr_(v));
-		outs(op, f);
+		outs(f, op);
 		fl_print_child(f, car_(cdr_(v)));
 		return;
 	}
 	int startpos = FL(hpos);
-	outc('(', f);
+	outc(f, '(');
 	int newindent = FL(hpos), blk = blockindent(v);
 	int lastv, n = 0, si, ind, est, always = 0, nextsmall, thistiny;
 	if(!blk)
@@ -307,7 +307,7 @@
 	while(1){
 		cd = cdr_(v);
 		if(FL(print_length) >= 0 && n >= FL(print_length) && cd != FL_nil){
-			outsn("...)", f, 4);
+			outsn(f, "...)", 4);
 			break;
 		}
 		lastv = FL(vpos);
@@ -315,10 +315,10 @@
 		fl_print_child(f, car_(v));
 		if(!iscons(cd) || ptrhash_has(&FL(printconses), (void*)cd)){
 			if(cd != FL_nil){
-				outsn(" . ", f, 3);
+				outsn(f, " . ", 3);
 				fl_print_child(f, cd);
 			}
-			outc(')', f);
+			outc(f, ')');
 			break;
 		}
 
@@ -350,11 +350,11 @@
 		}
 
 		if(ind){
-			newindent = outindent(newindent, f);
+			newindent = outindent(f, newindent);
 			n_unindented = 1;
 		}else{
 			n_unindented++;
-			outc(' ', f);
+			outc(f, ' ');
 			if(n == 0){
 				// set indent level after printing head
 				si = specialindent(head);
@@ -392,7 +392,7 @@
 {
 	const char *name;
 	if(FL(print_level) >= 0 && FL(p_level) >= FL(print_level) && (iscons(v) || isvector(v) || isclosure(v))){
-		outc('#', f);
+		outc(f, '#');
 		return;
 	}
 	FL(p_level)++;
@@ -404,28 +404,28 @@
 	case TAG_SYM:
 		name = symbol_name(v);
 		if(FL(print_princ))
-			outs(name, f);
+			outs(f, name);
 		else if(ismanaged(v)){
-			outsn("#:", f, 2);
-			outs(name, f);
+			outsn(f, "#:", 2);
+			outs(f, name);
 		}else
 			print_symbol_name(f, name);
 		break;
 	case TAG_FUNCTION:
 		if(v == FL_t)
-			outsn("#t", f, 2);
+			outsn(f, "#t", 2);
 		else if(v == FL_f)
-			outsn("#f", f, 2);
+			outsn(f, "#f", 2);
 		else if(v == FL_nil)
-			outsn("nil", f, 3);
+			outsn(f, "nil", 3);
 		else if(v == FL_eof)
-			outsn("#<eof>", f, 6);
+			outsn(f, "#<eof>", 6);
 		else if(v == FL_void){
-			outsn("#<void>", f, 7);
+			outsn(f, "#<void>", 7);
 		}else if(isbuiltin(v)){
 			if(!FL(print_princ))
-				outsn("#.", f, 2);
-			outs(builtins[uintval(v)].name, f);
+				outsn(f, "#.", 2);
+			outs(f, builtins[uintval(v)].name);
 		}else{
 			assert(isclosure(v));
 			if(!FL(print_princ)){
@@ -432,7 +432,7 @@
 				if(print_circle_prefix(f, v))
 					break;
 				function_t *fn = ptr(v);
-				outs("#fn(", f);
+				outs(f, "#fn(");
 				char *data = cvalue_data(fn->bcode);
 				size_t i, sz = cvalue_len(fn->bcode);
 				for(i = 0; i < sz; i++)
@@ -440,25 +440,25 @@
 				fl_print_child(f, fn->bcode);
 				for(i = 0; i < sz; i++)
 					data[i] -= 48;
-				outc(' ', f);
+				outc(f, ' ');
 				fl_print_child(f, fn->vals);
 				if(fn->env != FL_nil){
-					outc(' ', f);
+					outc(f, ' ');
 					fl_print_child(f, fn->env);
 				}
 				if(fn->name != FL(lambda)){
-					outc(' ', f);
+					outc(f, ' ');
 					fl_print_child(f, fn->name);
 				}
-				outc(')', f);
+				outc(f, ')');
 			}else{
-				outs("#<function>", f);
+				outs(f, "#<function>");
 			}
 		}
 		break;
 	case TAG_CPRIM:
 		if(v == UNBOUND)
-			outs("#<undefined>", f);
+			outs(f, "#<undefined>");
 		else
 			cvalue_print(f, v);
 		break;
@@ -468,30 +468,30 @@
 		if(!FL(print_princ) && print_circle_prefix(f, v))
 			break;
 		if(isvector(v)){
-			outs("#(", f);
+			outs(f, "#(");
 			int newindent = FL(hpos), est;
 			int i, sz = vector_size(v);
 			for(i = 0; i < sz; i++){
 				if(FL(print_length) >= 0 && i >= FL(print_length) && i < sz-1){
-					outsn("...", f, 3);
+					outsn(f, "...", 3);
 					break;
 				}
 				fl_print_child(f, vector_elt(v, i));
 				if(i < sz-1){
 					if(!FL(print_pretty))
-						outc(' ', f);
+						outc(f, ' ');
 					else{
 						est = lengthestimate(vector_elt(v, i+1));
 						if(FL(hpos) > FL(scr_width)-4 ||
 						   (est != -1 && (FL(hpos)+est > FL(scr_width)-2)) ||
 						   (FL(hpos) > FL(scr_width)/2 && !smallp(vector_elt(v, i+1)) && !tinyp(vector_elt(v, i))))
-							newindent = outindent(newindent, f);
+							newindent = outindent(f, newindent);
 						else
-							outc(' ', f);
+							outc(f, ' ');
 					}
 				}
 			}
-			outc(')', f);
+			outc(f, ')');
 			break;
 		}
 		if(iscvalue(v))
@@ -516,21 +516,21 @@
 		for(i = 0; i < sz; i++){
 			c = str[i];
 			if(c == '\\')
-				outsn("\\\\", f, 2);
+				outsn(f, "\\\\", 2);
 			else if(c == '"')
-				outsn("\\\"", f, 2);
+				outsn(f, "\\\"", 2);
 			else if(c >= 32 && c < 0x7f)
-				outc(c, f);
+				outc(f, c);
 			else{
-				outsn("\\x", f, 2);
-				outc(hexdig[c>>4], f);
-				outc(hexdig[c&0xf], f);
+				outsn(f, "\\x", 2);
+				outc(f, hexdig[c>>4]);
+				outc(f, hexdig[c&0xf]);
 			}
 		}
 	}else{
 		while(i < sz){
 			size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, 1, 0);
-			outsn(buf, f, n-1);
+			outsn(f, buf, n-1);
 		}
 	}
 }
@@ -634,7 +634,7 @@
 	if(type == FL(bytesym)){
 		uint8_t ch = *(uint8_t*)data;
 		if(FL(print_princ))
-			outc(ch, f);
+			outc(f, ch);
 		else if(weak)
 			FL(hpos) += ios_printf(f, "0x%hhx", ch);
 		else
@@ -645,24 +645,24 @@
 		int nb = runetochar(seq, &r);
 		seq[nb] = '\0';
 		if(FL(print_princ)){
-			outsn(seq, f, nb);
+			outsn(f, seq, nb);
 		}else{
-			outsn("#\\", f, 2);
+			outsn(f, "#\\", 2);
 			switch(r){
-			case 0x00: outsn("nul", f, 3); break;
-			case 0x07: outsn("alarm", f, 5); break;
-			case 0x08: outsn("backspace", f, 9); break;
-			case 0x09: outsn("tab", f, 3); break;
-			case 0x0a: outsn("newline", f, 7); break;
-			case 0x0b: outsn("vtab", f, 4); break;
-			case 0x0c: outsn("page", f, 4); break;
-			case 0x0d: outsn("return", f, 6); break;
-			case 0x1b: outsn("esc", f, 3); break;
-			case ' ':  outsn("space", f, 5); break;
-			case 0x7f: outsn("delete", f, 6); break;
+			case 0x00: outsn(f, "nul", 3); break;
+			case 0x07: outsn(f, "alarm", 5); break;
+			case 0x08: outsn(f, "backspace", 9); break;
+			case 0x09: outsn(f, "tab", 3); break;
+			case 0x0a: outsn(f, "newline", 7); break;
+			case 0x0b: outsn(f, "vtab", 4); break;
+			case 0x0c: outsn(f, "page", 4); break;
+			case 0x0d: outsn(f, "return", 6); break;
+			case 0x1b: outsn(f, "esc", 3); break;
+			case ' ':  outsn(f, "space", 5); break;
+			case 0x7f: outsn(f, "delete", 6); break;
 			default:
 				if(u8_iswprint(r))
-					outs(seq, f);
+					outs(f, seq);
 				else
 					FL(hpos) += ios_printf(f, "x%04x", r);
 				break;
@@ -688,22 +688,22 @@
 			if(type == FL(floatsym) && !FL(print_princ) && !weak)
 				FL(hpos) += ios_printf(f, "#%s(%s)", symbol_name(type), rep);
 			else
-				outs(rep, f);
+				outs(f, rep);
 		}else if(d == 0){
 			if(1/d < 0)
-				outsn("-0.0", f, 4);
+				outsn(f, "-0.0", 4);
 			else
-				outsn("0.0", f, 3);
+				outsn(f, "0.0", 3);
 			if(type == FL(floatsym) && !FL(print_princ) && !weak)
-				outc('f', f);
+				outc(f, 'f');
 		}else{
 			snprint_real(buf, sizeof(buf), d, 0, ndec, 3, 10);
 			int hasdec = (strpbrk(buf, ".eE") != nil);
-			outs(buf, f);
+			outs(f, buf);
 			if(!hasdec)
-				outsn(".0", f, 2);
+				outsn(f, ".0", 2);
 			if(type == FL(floatsym) && !FL(print_princ) && !weak)
-				outc('f', f);
+				outc(f, 'f');
 		}
 	}else if(type == FL(uint64sym)){
 		uint64_t ui64 = *(uint64_t*)data;
@@ -754,15 +754,15 @@
 						FL(hpos) += u8_strwidth(data);
 					*/
 				}else{
-					outc('"', f);
+					outc(f, '"');
 					print_string(f, (char*)data, len);
-					outc('"', f);
+					outc(f, '"');
 				}
 				return;
 			}else if(eltype == FL(runesym)){
 				char buf[UTFmax];
 				if(!FL(print_princ))
-					outc('"', f);
+					outc(f, '"');
 				for(i = 0; i < cnt; i++, data = (char*)data + elsize){
 					int n = runetochar(buf, (Rune*)data);
 					if(FL(print_princ))
@@ -771,28 +771,28 @@
 						print_string(f, buf, n);
 				}
 				if(!FL(print_princ))
-					outc('"', f);
+					outc(f, '"');
 				return;
 			}
 			if(!weak){
 				if(eltype == FL(uint8sym)){
-					outsn("#vu8(", f, 5);
+					outsn(f, "#vu8(", 5);
 				}else{
-					outsn("#array(", f, 7);
+					outsn(f, "#array(", 7);
 					fl_print_child(f, eltype);
 					if(cnt > 0)
-						outc(' ', f);
+						outc(f, ' ');
 				}
 			}else{
-				outs("#(", f);
+				outs(f, "#(");
 			}
 			for(i = 0; i < cnt; i++){
 				if(i > 0)
-					outc(' ', f);
+					outc(f, ' ');
 				cvalue_printdata(f, data, elsize, eltype, 1);
 				data = (char*)data + elsize;
 			}
-			outc(')', f);
+			outc(f, ')');
 		}
 	}
 }
@@ -811,11 +811,11 @@
 			FL(hpos) += ios_printf(f, "#<builtin @%p>", fptr);
 		}else{
 			if(FL(print_princ)){
-				outs(symbol_name(label), f);
+				outs(f, symbol_name(label));
 			}else{
-				outsn("#fn(", f, 4);
-				outs(symbol_name(label), f);
-				outc(')', f);
+				outsn(f, "#fn(", 4);
+				outs(f, symbol_name(label));
+				outc(f, ')');
 			}
 		}
 	}else if(cv_class(cv)->vtable != nil && cv_class(cv)->vtable->print != nil){
--- a/print.h
+++ b/print.h
@@ -2,6 +2,6 @@
 
 void fl_print(ios_t *f, value_t v);
 void print_traverse(value_t v);
-void fl_print_chr(char c, ios_t *f);
-void fl_print_str(const char *s, ios_t *f);
+void fl_print_chr(ios_t *f, char c);
+void fl_print_str(ios_t *f, const char *s);
 void fl_print_child(ios_t *f, value_t v);
--- a/sixel.c
+++ b/sixel.c
@@ -218,7 +218,7 @@
 print_sixeloutput(value_t v, ios_t *s)
 {
 	USED(v);
-	fl_print_str("#<sixel output>", s);
+	fl_print_str(s, "#<sixel output>");
 }
 
 static void
--- a/table.c
+++ b/table.c
@@ -11,18 +11,18 @@
 	htable_t *h = cvalue_data(v);
 	size_t i;
 	int first = 1;
-	fl_print_str("#table(", f);
+	fl_print_str(f, "#table(");
 	for(i = 0; i < h->size; i += 2){
 		if(h->table[i+1] != HT_NOTFOUND){
 			if(!first)
-				fl_print_str("  ", f);
+				fl_print_str(f, "  ");
 			fl_print_child(f, (value_t)h->table[i]);
-			fl_print_chr(' ', f);
+			fl_print_chr(f, ' ');
 			fl_print_child(f, (value_t)h->table[i+1]);
 			first = 0;
 		}
 	}
-	fl_print_chr(')', f);
+	fl_print_chr(f, ')');
 }
 
 static void