0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-10-10 00:25:06 -04:00

Implement %iftoken, test for a single token

Implement %iftoken, a test for a single token.  This is useful in
cases using %+ to splice a macro-provided token.
This commit is contained in:
H. Peter Anvin
2008-02-16 16:41:25 -08:00
parent 88ab419cc6
commit cbf768d67d
5 changed files with 158 additions and 2 deletions

View File

@@ -2659,7 +2659,6 @@ Like most other \c{%if} constructs, \c{%ifidn} has a counterpart
Similarly, \c{%ifidni} has counterparts \i\c{%elifidni}, Similarly, \c{%ifidni} has counterparts \i\c{%elifidni},
\i\c{%ifnidni} and \i\c{%elifnidni}. \i\c{%ifnidni} and \i\c{%elifnidni}.
\S{iftyp} \i\c{%ifid}, \i\c{%ifnum}, \i\c{%ifstr}: Testing Token \S{iftyp} \i\c{%ifid}, \i\c{%ifnum}, \i\c{%ifstr}: Testing Token
Types\I{testing, token types} Types\I{testing, token types}
@@ -2721,6 +2720,21 @@ which case, all but the first two would be lumped together into
The usual \c{%elifXXX}, \c{%ifnXXX} and \c{%elifnXXX} versions exist The usual \c{%elifXXX}, \c{%ifnXXX} and \c{%elifnXXX} versions exist
for each of \c{%ifid}, \c{%ifnum} and \c{%ifstr}. for each of \c{%ifid}, \c{%ifnum} and \c{%ifstr}.
\S{iftoken} \i\c{%iftoken}: Test For A Single Token
Some macros will want to do different things depending on if it is
passed a single token (e.g. paste it to something else using \c{%+})
versus a multi-token sequence.
The conditional assembly construct \c{%iftoken} assembles the
subsequent code if and only if the expanded parameters consist of
exactly one token, possibly surrounded by whitespace.
For example, \c{1} will assemble the subsequent code, but \c{-1} will
not (\c{-} being an operator.)
The usual \i\c{%eliftoken}, \i\c\{%ifntoken}, and \i\c{%elifntoken}
variants are also provided.
\S{pperror} \i\c{%error}: Reporting \i{User-Defined Errors} \S{pperror} \i\c{%error}: Reporting \i{User-Defined Errors}

View File

@@ -10,6 +10,7 @@
*idni *idni
*macro *macro
*num *num
*token
*str *str
%arg %arg
%assign %assign

View File

@@ -1594,7 +1594,21 @@ static bool if_condition(Token * tline, enum preproc_token ct)
!t->text[1])) !t->text[1]))
t = t->next; t = t->next;
j = t && t->type == needtype; j = tok_type_(t, needtype);
break;
case PPC_IFTOKEN:
t = tline = expand_smacro(tline);
while (tok_type_(t, TOK_WHITESPACE))
t = t->next;
j = false;
if (t) {
t = t->next; /* Skip the actual token */
while (tok_type_(t, TOK_WHITESPACE))
t = t->next;
j = !t; /* Should be nothing left */
}
break; break;
case PPC_IF: case PPC_IF:

108
test/iftoken.asm Normal file
View File

@@ -0,0 +1,108 @@
db 'N "1": '
%iftoken 1
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "1": '
%iftoken 1 ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "+1": '
%iftoken +1
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "+1": '
%iftoken +1 ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "1 2": '
%iftoken 1 2
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "1 2": '
%iftoken 1 2 ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "1,2": '
%iftoken 1,2
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "1,2": '
%iftoken 1,2 ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "foo": '
%iftoken foo
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "foo": '
%iftoken foo ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "foo bar": '
%iftoken foo bar
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "foo bar": '
%iftoken foo bar ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "%": '
%iftoken %
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "%": '
%iftoken % ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "+foo": '
%iftoken +foo
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "+foo": '
%iftoken +foo ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif
db 'N "<<": '
%iftoken <<
db 'Yes', 10
%else
db 'No', 10
%endif
db 'C "<<": '
%iftoken << ; With a comment!
db 'Yes', 10
%else
db 'No', 10
%endif

19
test/iftoken.pl Executable file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/perl
@list = ('1', '+1', '1 2', '1,2', 'foo', 'foo bar', '%', '+foo', '<<');
foreach $t (@list) {
print "\tdb 'N \"$t\": '\n";
print "%iftoken $t\n";
print "\tdb 'Yes', 10\n";
print "%else\n";
print "\tdb 'No', 10\n";
print "%endif\n";
print "\tdb 'C \"$t\": '\n";
print "%iftoken $t ; With a comment!\n";
print "\tdb 'Yes', 10\n";
print "%else\n";
print "\tdb 'No', 10\n";
print "%endif\n";
}