From 664a79473d73f1fb65eadd75bdc093bc8608a129 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 14 Nov 2022 01:48:38 -0800 Subject: [PATCH] preproc: add %count() function Add %count(), giving a count of the number of arguments. Signed-off-by: H. Peter Anvin --- asm/preproc.c | 11 +++++++++++ doc/nasmdoc.src | 23 +++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/asm/preproc.c b/asm/preproc.c index fdb761b7..a41ed2be 100644 --- a/asm/preproc.c +++ b/asm/preproc.c @@ -7013,6 +7013,16 @@ stdmac_cond_sel(const SMacro *s, Token **params, int nparams) return new_Token(NULL, tok_smac_param(which), "", 0); } +/* %count() */ +static Token * +stdmac_count(const SMacro *s, Token **params, int nparams) +{ + (void)s; + (void)params; + + return make_tok_num(NULL, nparams); +} + /* Add magic standard macros */ struct magic_macros { const char *name; @@ -7028,6 +7038,7 @@ static void pp_add_magic_stdmac(void) { "__?LINE?__", true, 0, 0, stdmac_line }, { "__?BITS?__", true, 0, 0, stdmac_bits }, { "__?PTR?__", true, 0, 0, stdmac_ptr }, + { "%count", false, 1, SPARM_VARADIC, stdmac_count }, { "%eval", false, 1, SPARM_EVAL|SPARM_VARADIC, stdmac_join }, { "%str", false, 1, SPARM_GREEDY|SPARM_STR, stdmac_join }, { "%strcat", false, 1, SPARM_GREEDY, stdmac_strcat }, diff --git a/doc/nasmdoc.src b/doc/nasmdoc.src index 745e6536..111dea36 100644 --- a/doc/nasmdoc.src +++ b/doc/nasmdoc.src @@ -2867,13 +2867,28 @@ to \c{%sel(2-!(x),y,z)}. The argument not selected is never expanded. +\S{f_count} \i\c{%count()} Function + +The \c{%count()} function expands to the number of argments passed to +the macro. Note that just as for single-line macros, \c{%count()} +treats an empty argument list as a single empty argument. + +\c %xdefine empty %count() ; %define empty 1 +\c %xdefine one %count(1) ; %define one 1 +\c %xdefine two %count(5,q) ; %define two 2 +\c %define list a,b,46 +\c %xdefine lc1 %count(list) ; %define lc 1 (just one argument) +\c %xdefine lc2 %count(%[list]) ; %define lc 3 (indirection expands) + + \S{f_eval} \i\c{%eval()} Function The \c{%eval()} function evaluates its argument as a numeric -expression in much the same way the \i\c{%assign} directive would, see -\k{assign}. Unlike \c{%assign}, \c{%eval()} supports more than one -argument; if more than one argument is specified, it is expanded to a -comma-separated list of values. +expression and expands to the result as an integer constant in much +the same way the \i\c{%assign} directive would, see \k{assign}. Unlike +\c{%assign}, \c{%eval()} supports more than one argument; if more than +one argument is specified, it is expanded to a comma-separated list of +values. \c %assign a 2 \c %assign b 3