From 73e9ebcfdd91b6e70b21147e3ce99583a635bf25 Mon Sep 17 00:00:00 2001
From: Devine Lu Linvega <aliceffekt@gmail.com>
Date: Sun, 21 Apr 2024 09:13:30 -0700
Subject: [PATCH] Only check for register value once

---
 examples/tests.modal |  1 -
 src/modal.c          | 69 ++++++++++++++++++++++----------------------
 2 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/examples/tests.modal b/examples/tests.modal
index 2d52858..121d991 100644
--- a/examples/tests.modal
+++ b/examples/tests.modal
@@ -85,4 +85,3 @@ implode (12 (34 (56 ()))) 123456 test
 <> (reverse List (?x ?y) ?z) (reverse List ?y (?x ?z))
 
 reverse (modal) = ladom test
-
diff --git a/src/modal.c b/src/modal.c
index ff780b3..7f22e9d 100644
--- a/src/modal.c
+++ b/src/modal.c
@@ -46,51 +46,50 @@ set_reg(int r, char *b)
 }
 
 static void
-put_reg(char r)
+put_reg(char r, char *reg)
 {
-	char c, *s = regs[(int)r], *ss = walk(s);
+	char c, *cap = walk(reg);
 	if(r == '*') {
 		int i, depth = 0;
-		if(*s == '(') { /* special explode tuple */
-			s++;
-			while(s < ss) {
-				while((c = *s) && !spacer(c))
-					*dst_++ = c, s++;
+		if(*reg == '(') { /* special explode tuple */
+			reg++;
+			while(reg < cap) {
+				while((c = *reg) && !spacer(c))
+					*dst_++ = c, reg++;
 				*dst_++ = ' ';
-				*dst_++ = '(', s++, depth++;
+				*dst_++ = '(', reg++, depth++;
 			}
 		} else { /* special explode token */
-			while((c = *s++) && !spacer(c))
+			while((c = *reg++) && !spacer(c))
 				*dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++;
 		}
 		for(i = 0; i < depth; i++)
 			*dst_++ = ')';
 	} else if(r == '.') { /* special unpack */
-		if(*s == '(') s++, --ss;
-		while(s < ss) *dst_++ = *s++;
+		if(*reg == '(') reg++, --cap;
+		while(reg < cap) *dst_++ = *reg++;
 	} else if(r == '^') { /* special join */
-		if(*s == '(') s++, --ss;
-		while(s < ss && (c = *s++))
+		if(*reg == '(') reg++, --cap;
+		while(reg < cap && (c = *reg++))
 			if(!spacer(c)) *dst_++ = c;
 	} else if(r == '~') { /* special stdin */
 		while(fread(&c, 1, 1, stdin) && c >= ' ')
 			*dst_++ = c;
 	} else if(r == ':') { /* special stdout */
-		if(*s == '(') s++, --ss;
-		while(s < ss) {
-			c = *s++;
+		if(*reg == '(') reg++, --cap;
+		while(reg < cap) {
+			c = *reg++;
 			if(c == '\\') {
-				switch(*s++) {
+				switch(*reg++) {
 				case 't': putc(0x09, stdout); break;
 				case 'n': putc(0x0a, stdout); break;
 				case 's': putc(0x20, stdout); break;
-				case '?': putc(0x3f, stdout); break;
 				}
 			} else
 				putc(c, stdout);
 		}
 	} else
-		while(s < ss) *dst_++ = *s++;
+		while(reg < cap) *dst_++ = *reg++;
 }
 
 static char *
@@ -136,34 +135,34 @@ commit_rule(Rule *r, char *s, int create)
 }
 
 static int
-write_rule(Rule *r, char *res)
+write_rule(Rule *r, char *s)
 {
-	char c, last = 0, *b = r->b, *origin = dst_;
+	char c, *b = r->b, *reg, *origin = dst_;
 	while((c = *b++))
-		if(c == '?' && last != '\\' && regs[(int)*b])
-			put_reg(*b++);
+		if(c == '?' && (reg = regs[(int)*b]))
+			put_reg(*b++, reg);
 		else
-			*dst_++ = c, last = c;
+			*dst_++ = c;
 	if(dst_ == origin) {
-		while(*res == ' ') res++;
-		if(*res == ')' && *(dst_ - 1) == ' ') dst_--;
+		while(*s == ' ') s++;
+		if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
 	}
-	return commit_rule(r, res, 0);
+	return commit_rule(r, s, 0);
 }
 
 static char *
 parse_frag(char *s)
 {
-	char c, *ss;
-	while((c = *s) && c <= ' ') s++;
+	char c, *cap;
+	while((c = *s) && c == ' ') s++;
 	if(c != ')' && !(c == '<' && s[1] == '>')) {
-		ss = walk(s);
+		cap = walk(s);
 		if(c == '(') {
 			s++;
-			while(s < ss - 1) *dict_++ = *s++;
+			while(s < cap - 1) *dict_++ = *s++;
 			s++;
 		} else
-			while(s < ss) *dict_++ = *s++;
+			while(s < cap) *dict_++ = *s++;
 	}
 	*dict_++ = 0;
 	return s;
@@ -182,20 +181,20 @@ static int
 rewrite(void)
 {
 	char c, last = 0, *cap, *s = src_, *res;
-	while((c = *s) && c <= ' ') s++;
+	while(*s == ' ') s++;
 	while((c = *s)) {
 		if(spacer(last)) {
 			Rule *r;
 			if(c == '<' && s[1] == '>') {
 				r = rules_++;
 				s = create_rule(r, rules_ - rules - 1, s);
-				while((c = *s) && c <= ' ') s++;
+				while(*s == ' ') s++;
 				return commit_rule(r, s, 1);
 			}
 			if(c == '?' && s[1] == '(') {
 				r = &lambda, cap = walk(s + 1);
 				create_rule(&lambda, -1, s), s = cap;
-				while((c = *s) && c <= ' ') s++;
+				while(*s == ' ') s++;
 				if((res = match_rule(&lambda, s)) != NULL)
 					return write_rule(&lambda, res);
 			}