diff lwasm/macro.c @ 555:bbc600db5016

Add \Ln and {Ln} standing for the length of the specified argument in macros It seems useful to be able to identify the length of a macro argument. Add argument expansion syntax replaced with the length of the specified argument: \Ln or {Ln} will return the length of the nth argument, or zero if there is no nth argument. Argument 0 is the macro name.
author William Astle <lost@l-w.ca>
date Sun, 23 Jul 2023 17:07:58 -0600
parents 4536f0e61425
children
line wrap: on
line diff
--- a/lwasm/macro.c	Sun Jul 23 16:42:29 2023 -0600
+++ b/lwasm/macro.c	Sun Jul 23 17:07:58 2023 -0600
@@ -255,6 +255,23 @@
 					macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
 				p2++;
 			}
+			else if (*p2 == '\\' && (p2[1] == 'L' || p2[1] == 'l') && isdigit(p2[2]))
+			{
+				int n;
+				int clen = 0;
+				char numbuf[10];
+				
+				p2 += 2;
+				n = *p2 - '0';
+				if (n == 0)
+					clen = strlen(m -> name);
+				else if (n >= 1 && n <= nargs)
+					clen = strlen(args[n - 1]);
+				snprintf(numbuf, 10, "%d", clen);
+				for (p3 = numbuf; *p3; p3++)
+					macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
+				continue;
+			}
 			else if (*p2 == '\\' && isdigit(p2[1]))
 			{
 				int n;
@@ -276,7 +293,15 @@
 			else if (*p2 == '{')
 			{
 				int n = 0, n2;
+				int dolen = 0;
+				char numbuf[10];
+
 				p2++;
+				if (*p2 == 'L' || *p2 == 'l')
+				{
+					dolen = 1;
+					p2++;
+				}
 				while (*p2 && isdigit(*p2))
 				{
 					n2 = *p2 - '0';
@@ -291,14 +316,18 @@
 					p2--;
 				 
 				if (n == 0)
+					p3 = m -> name;
+				else if (n < 1 || n > nargs)
+					p3 = "";
+				else
+					p3 = args[n - 1];
+
+				if (dolen)
 				{
-					for (p3 = m -> name; *p3; p3++)
-						macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
-					continue;
+					snprintf(numbuf, 10, "%d", (int)strlen(p3));
+					p3 = numbuf;
 				}
-				if (n < 1 || n > nargs)
-					continue;
-				for (p3 = args[n - 1]; *p3; p3++)
+				for (; *p3; p3++)
 					macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
 				continue;
 			}