comparison 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
comparison
equal deleted inserted replaced
554:7627d2b3b81f 555:bbc600db5016
253 snprintf(ctcbuf, 25, "%d", nargs); 253 snprintf(ctcbuf, 25, "%d", nargs);
254 for (p3 = ctcbuf; *p3; p3++) 254 for (p3 = ctcbuf; *p3; p3++)
255 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); 255 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
256 p2++; 256 p2++;
257 } 257 }
258 else if (*p2 == '\\' && (p2[1] == 'L' || p2[1] == 'l') && isdigit(p2[2]))
259 {
260 int n;
261 int clen = 0;
262 char numbuf[10];
263
264 p2 += 2;
265 n = *p2 - '0';
266 if (n == 0)
267 clen = strlen(m -> name);
268 else if (n >= 1 && n <= nargs)
269 clen = strlen(args[n - 1]);
270 snprintf(numbuf, 10, "%d", clen);
271 for (p3 = numbuf; *p3; p3++)
272 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
273 continue;
274 }
258 else if (*p2 == '\\' && isdigit(p2[1])) 275 else if (*p2 == '\\' && isdigit(p2[1]))
259 { 276 {
260 int n; 277 int n;
261 278
262 p2++; 279 p2++;
274 continue; 291 continue;
275 } 292 }
276 else if (*p2 == '{') 293 else if (*p2 == '{')
277 { 294 {
278 int n = 0, n2; 295 int n = 0, n2;
279 p2++; 296 int dolen = 0;
297 char numbuf[10];
298
299 p2++;
300 if (*p2 == 'L' || *p2 == 'l')
301 {
302 dolen = 1;
303 p2++;
304 }
280 while (*p2 && isdigit(*p2)) 305 while (*p2 && isdigit(*p2))
281 { 306 {
282 n2 = *p2 - '0'; 307 n2 = *p2 - '0';
283 if (n2 < 0 || n2 > 9) 308 if (n2 < 0 || n2 > 9)
284 n2 = 0; 309 n2 = 0;
289 // to prevent overconsuming input characters 314 // to prevent overconsuming input characters
290 if (*p2 != '}') 315 if (*p2 != '}')
291 p2--; 316 p2--;
292 317
293 if (n == 0) 318 if (n == 0)
294 { 319 p3 = m -> name;
295 for (p3 = m -> name; *p3; p3++) 320 else if (n < 1 || n > nargs)
296 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); 321 p3 = "";
297 continue; 322 else
298 } 323 p3 = args[n - 1];
299 if (n < 1 || n > nargs) 324
300 continue; 325 if (dolen)
301 for (p3 = args[n - 1]; *p3; p3++) 326 {
327 snprintf(numbuf, 10, "%d", (int)strlen(p3));
328 p3 = numbuf;
329 }
330 for (; *p3; p3++)
302 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); 331 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
303 continue; 332 continue;
304 } 333 }
305 else 334 else
306 { 335 {