Skip to content

Commit 664a794

Browse files
author
H. Peter Anvin
committed
preproc: add %count() function
Add %count(), giving a count of the number of arguments. Signed-off-by: H. Peter Anvin <[email protected]>
1 parent 16b49d4 commit 664a794

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

asm/preproc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7013,6 +7013,16 @@ stdmac_cond_sel(const SMacro *s, Token **params, int nparams)
70137013
return new_Token(NULL, tok_smac_param(which), "", 0);
70147014
}
70157015

7016+
/* %count() */
7017+
static Token *
7018+
stdmac_count(const SMacro *s, Token **params, int nparams)
7019+
{
7020+
(void)s;
7021+
(void)params;
7022+
7023+
return make_tok_num(NULL, nparams);
7024+
}
7025+
70167026
/* Add magic standard macros */
70177027
struct magic_macros {
70187028
const char *name;
@@ -7028,6 +7038,7 @@ static void pp_add_magic_stdmac(void)
70287038
{ "__?LINE?__", true, 0, 0, stdmac_line },
70297039
{ "__?BITS?__", true, 0, 0, stdmac_bits },
70307040
{ "__?PTR?__", true, 0, 0, stdmac_ptr },
7041+
{ "%count", false, 1, SPARM_VARADIC, stdmac_count },
70317042
{ "%eval", false, 1, SPARM_EVAL|SPARM_VARADIC, stdmac_join },
70327043
{ "%str", false, 1, SPARM_GREEDY|SPARM_STR, stdmac_join },
70337044
{ "%strcat", false, 1, SPARM_GREEDY, stdmac_strcat },

doc/nasmdoc.src

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,13 +2867,28 @@ to \c{%sel(2-!(x),y,z)}.
28672867
The argument not selected is never expanded.
28682868

28692869

2870+
\S{f_count} \i\c{%count()} Function
2871+
2872+
The \c{%count()} function expands to the number of argments passed to
2873+
the macro. Note that just as for single-line macros, \c{%count()}
2874+
treats an empty argument list as a single empty argument.
2875+
2876+
\c %xdefine empty %count() ; %define empty 1
2877+
\c %xdefine one %count(1) ; %define one 1
2878+
\c %xdefine two %count(5,q) ; %define two 2
2879+
\c %define list a,b,46
2880+
\c %xdefine lc1 %count(list) ; %define lc 1 (just one argument)
2881+
\c %xdefine lc2 %count(%[list]) ; %define lc 3 (indirection expands)
2882+
2883+
28702884
\S{f_eval} \i\c{%eval()} Function
28712885

28722886
The \c{%eval()} function evaluates its argument as a numeric
2873-
expression in much the same way the \i\c{%assign} directive would, see
2874-
\k{assign}. Unlike \c{%assign}, \c{%eval()} supports more than one
2875-
argument; if more than one argument is specified, it is expanded to a
2876-
comma-separated list of values.
2887+
expression and expands to the result as an integer constant in much
2888+
the same way the \i\c{%assign} directive would, see \k{assign}. Unlike
2889+
\c{%assign}, \c{%eval()} supports more than one argument; if more than
2890+
one argument is specified, it is expanded to a comma-separated list of
2891+
values.
28772892

28782893
\c %assign a 2
28792894
\c %assign b 3

0 commit comments

Comments
 (0)