true and false supported in constant expressions (C++ only). && || == != < > <= >= operators now return type bool (C++ only) and type int for C as per C/C++ standards.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11677 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2009-09-02 22:33:04 +00:00
commit 7c6aca2ebc
10 changed files with 276 additions and 15 deletions

View file

@ -257,7 +257,7 @@ void skip_decl(void) {
* Lexical scanner.
* ------------------------------------------------------------------------- */
int yylook(void) {
static int yylook(void) {
int tok = 0;
@ -410,6 +410,9 @@ int yylook(void) {
case SWIG_TOKEN_FLOAT:
return NUM_FLOAT;
case SWIG_TOKEN_BOOL:
return NUM_BOOL;
case SWIG_TOKEN_POUND:
Scanner_skip_line(scan);
yylval.id = Swig_copy_string(Char(Scanner_text(scan)));
@ -525,6 +528,7 @@ int yylex(void) {
case NUM_UNSIGNED:
case NUM_LONGLONG:
case NUM_ULONGLONG:
case NUM_BOOL:
if (l == NUM_INT)
yylval.dtype.type = T_INT;
if (l == NUM_FLOAT)
@ -539,6 +543,8 @@ int yylex(void) {
yylval.dtype.type = T_LONGLONG;
if (l == NUM_ULONGLONG)
yylval.dtype.type = T_ULONGLONG;
if (l == NUM_BOOL)
yylval.dtype.type = T_BOOL;
yylval.dtype.val = NewString(Scanner_text(scan));
yylval.dtype.bitfield = 0;
yylval.dtype.throws = 0;

View file

@ -1486,7 +1486,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) {
%token <id> STRING
%token <loc> INCLUDE IMPORT INSERT
%token <str> CHARCONST
%token <dtype> NUM_INT NUM_FLOAT NUM_UNSIGNED NUM_LONG NUM_ULONG NUM_LONGLONG NUM_ULONGLONG
%token <dtype> NUM_INT NUM_FLOAT NUM_UNSIGNED NUM_LONG NUM_ULONG NUM_LONGLONG NUM_ULONGLONG NUM_BOOL
%token <ivalue> TYPEDEF
%token <type> TYPE_INT TYPE_UNSIGNED TYPE_SHORT TYPE_LONG TYPE_FLOAT TYPE_DOUBLE TYPE_CHAR TYPE_WCHAR TYPE_VOID TYPE_SIGNED TYPE_BOOL TYPE_COMPLEX TYPE_TYPEDEF TYPE_RAW TYPE_NON_ISO_INT8 TYPE_NON_ISO_INT16 TYPE_NON_ISO_INT32 TYPE_NON_ISO_INT64
%token LPAREN RPAREN COMMA SEMI EXTERN INIT LBRACE RBRACE PERIOD
@ -5547,6 +5547,7 @@ exprnum : NUM_INT { $$ = $1; }
| NUM_ULONG { $$ = $1; }
| NUM_LONGLONG { $$ = $1; }
| NUM_ULONGLONG { $$ = $1; }
| NUM_BOOL { $$ = $1; }
;
exprcompound : expr PLUS expr {
@ -5591,28 +5592,28 @@ exprcompound : expr PLUS expr {
}
| expr LAND expr {
$$.val = NewStringf("%s&&%s",$1.val,$3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr LOR expr {
$$.val = NewStringf("%s||%s",$1.val,$3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr EQUALTO expr {
$$.val = NewStringf("%s==%s",$1.val,$3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr NOTEQUALTO expr {
$$.val = NewStringf("%s!=%s",$1.val,$3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
/* Sadly this causes 2 reduce-reduce conflicts with templates. FIXME resolve these.
| expr GREATERTHAN expr {
$$.val = NewStringf("%s SWIG_LT %s", $1.val, $3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr LESSTHAN expr {
$$.val = NewStringf("%s SWIG_GT %s", $1.val, $3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
*/
| expr GREATERTHANOREQUALTO expr {
@ -5620,11 +5621,11 @@ exprcompound : expr PLUS expr {
* loop somewhere in the type system. Just workaround for now
* - SWIG_GE is defined in swiglabels.swg. */
$$.val = NewStringf("%s SWIG_GE %s", $1.val, $3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr LESSTHANOREQUALTO expr {
$$.val = NewStringf("%s SWIG_LE %s", $1.val, $3.val);
$$.type = T_INT;
$$.type = cparse_cplusplus ? T_BOOL : T_INT;
}
| expr QUESTIONMARK expr COLON expr %prec QUESTIONMARK {
$$.val = NewStringf("%s?%s:%s", $1.val, $3.val, $5.val);

View file

@ -16,7 +16,8 @@ char cvsroot_scanner_c[] = "$Id$";
#include <ctype.h>
extern String *cparse_file;
extern int cparse_start_line;
extern int cparse_cplusplus;
extern int cparse_start_line;
struct Scanner {
String *text; /* Current token value */
@ -730,15 +731,25 @@ static int look(Scanner * s) {
break;
case 7: /* Identifier */
if ((c = nextchar(s)) == 0)
return SWIG_TOKEN_ID;
if (isalnum(c) || (c == '_') || (c == '$')) {
state = 71;
else if (isalnum(c) || (c == '_') || (c == '$')) {
state = 7;
} else {
retract(s, 1);
return SWIG_TOKEN_ID;
state = 71;
}
break;
case 71: /* Identifier or true/false */
if (cparse_cplusplus) {
if (Strcmp(s->text, "true") == 0)
return SWIG_TOKEN_BOOL;
else if (Strcmp(s->text, "false") == 0)
return SWIG_TOKEN_BOOL;
}
return SWIG_TOKEN_ID;
break;
case 75: /* Special identifier $ */
if ((c = nextchar(s)) == 0)
return SWIG_TOKEN_DOLLAR;
@ -747,7 +758,7 @@ static int look(Scanner * s) {
} else {
retract(s,1);
if (Len(s->text) == 1) return SWIG_TOKEN_DOLLAR;
return SWIG_TOKEN_ID;
state = 71;
}
break;

View file

@ -64,6 +64,7 @@ extern void Scanner_freeze_line(Scanner *s, int val);
#define SWIG_TOKEN_ULONGLONG 29 /* 314ULL */
#define SWIG_TOKEN_QUESTION 30 /* ? */
#define SWIG_TOKEN_COMMENT 31 /* C or C++ comment */
#define SWIG_TOKEN_BOOL 32 /* true or false */
#define SWIG_TOKEN_ILLEGAL 99
#define SWIG_TOKEN_ERROR -1