shithub: femtolisp

Download patch

ref: c30d393c163211378fc9d908aab41a6dde0330af
parent: 6bfb55fb32fa2713c5527968733e26540f999af0
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Nov 12 14:27:30 EST 2024

map: sync with Julia's implementation

--- a/flisp.c
+++ b/flisp.c
@@ -2076,35 +2076,37 @@
 		lerrorf(FL(ArgError), "too few arguments");
 	if(!iscons(args[1]))
 		return FL(Nil);
-	value_t first, last, v;
-	intptr_t argSP = args-FL(stack);
+	value_t v;
+	intptr_t first, last, argSP = args-FL(stack);
 	assert(argSP >= 0 && argSP < FL(nstack));
 	if(nargs == 2){
-		if(FL(sp)+3 > FL(nstack))
+		if(FL(sp)+4 > FL(nstack))
 			grow_stack();
 		PUSH(FL(stack)[argSP]);
 		PUSH(car_(FL(stack)[argSP+1]));
 		v = _applyn(1);
+		POPN(2);
 		PUSH(v);
 		v = mk_cons();
 		car_(v) = POP(); cdr_(v) = FL(Nil);
-		last = first = v;
+		PUSH(v);
+		PUSH(v);
+		first = FL(sp)-2;
+		last = FL(sp)-1;
 		FL(stack)[argSP+1] = cdr_(FL(stack)[argSP+1]);
-		fl_gc_handle(&first);
-		fl_gc_handle(&last);
 		while(iscons(FL(stack)[argSP+1])){
-			FL(stack)[FL(sp)-2] = FL(stack)[argSP];
-			FL(stack)[FL(sp)-1] = car_(FL(stack)[argSP+1]);
+			PUSH(FL(stack)[argSP]);
+			PUSH(car_(FL(stack)[argSP+1]));
 			v = _applyn(1);
+			POPN(2);
 			PUSH(v);
 			v = mk_cons();
 			car_(v) = POP(); cdr_(v) = FL(Nil);
-			cdr_(last) = v;
-			last = v;
+			cdr_(FL(stack)[last]) = v;
+			FL(stack)[last] = v;
 			FL(stack)[argSP+1] = cdr_(FL(stack)[argSP+1]);
 		}
 		POPN(2);
-		fl_free_gc_handles(2);
 	}else{
 		int i;
 		while(FL(sp)+nargs+1 > FL(nstack))
@@ -2119,9 +2121,10 @@
 		PUSH(v);
 		v = mk_cons();
 		car_(v) = POP(); cdr_(v) = FL(Nil);
-		last = first = v;
-		fl_gc_handle(&first);
-		fl_gc_handle(&last);
+		PUSH(v);
+		PUSH(v);
+		first = FL(sp)-2;
+		last = FL(sp)-1;
 		while(iscons(FL(stack)[argSP+1])){
 			PUSH(FL(stack)[argSP]);
 			for(i = 1; i < nargs; i++){
@@ -2133,12 +2136,12 @@
 			PUSH(v);
 			v = mk_cons();
 			car_(v) = POP(); cdr_(v) = FL(Nil);
-			cdr_(last) = v;
-			last = v;
+			cdr_(FL(stack)[last]) = v;
+			FL(stack)[last] = v;
 		}
-		fl_free_gc_handles(2);
+		POPN(2);
 	}
-	return first;
+	return FL(stack)[first];
 }
 
 BUILTIN("for-each", for_each)