Support ternary conditionals (a ? b : c) in constant expressions.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9330 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Olly Betts 2006-09-23 09:35:31 +00:00
commit 4c1b1332a5
4 changed files with 22 additions and 3 deletions

View file

@ -10,9 +10,9 @@ Version 1.3.30 (in progress)
%template(x_1plus2) x<1+2>; %template(x_1plus2) x<1+2>;
Also, constant expressions can now include comparisons (>, <, >=, Also, constant expressions can now include comparisons (>, <, >=,
<=, !=, ==) and modulus (%). <=, !=, ==), modulus (%), and ternary conditionals (a ? b : c).
Fixes bugs #646275, #925555, #956282. Fixes bugs #646275, #925555, #956282, #994301.
09/22/2006: wsfulton 09/22/2006: wsfulton
Fix %ignore on director methods - Bugs #1546254, #1543533 Fix %ignore on director methods - Bugs #1546254, #1543533

View file

@ -46,7 +46,17 @@ class X {};
%} %}
// bug 1338527 // bug #994301
%inline %{
template<int foo> class X994301 {};
%}
%template(X994301_ternary) X<(7 >= 4) ? 1 + 1 : 1>;
// bug #1338527 (still broken)
%inline %{ %inline %{

View file

@ -605,6 +605,7 @@ int yylook(void) {
else if (c == '0') state = 83; /* An octal or hex value */ else if (c == '0') state = 83; /* An octal or hex value */
else if (c == '\'') state = 9; /* A character constant */ else if (c == '\'') state = 9; /* A character constant */
else if (c == '.') state = 100; /* Maybe a number, maybe just a period */ else if (c == '.') state = 100; /* Maybe a number, maybe just a period */
else if (c == '?') return (QUESTIONMARK); /* Ternary conditional operator */
else if (c == '`') { else if (c == '`') {
state = 200; /* Back-tick type */ state = 200; /* Back-tick type */
yylen = 0; yylen = 0;

View file

@ -1390,6 +1390,7 @@ static void default_arguments(Node *n) {
%token WARN %token WARN
%token LESSTHAN GREATERTHAN MODULO DELETE_KW %token LESSTHAN GREATERTHAN MODULO DELETE_KW
%token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO %token LESSTHANOREQUALTO GREATERTHANOREQUALTO EQUALTO NOTEQUALTO
%token QUESTIONMARK
%token TYPES PARMS %token TYPES PARMS
%token NONID DSTAR DCNOT %token NONID DSTAR DCNOT
%token <ivalue> TEMPLATE %token <ivalue> TEMPLATE
@ -1398,6 +1399,7 @@ static void default_arguments(Node *n) {
%token PARSETYPE PARSEPARM PARSEPARMS %token PARSETYPE PARSEPARM PARSEPARMS
%left CAST %left CAST
%left QUESTIONMARK
%left LOR %left LOR
%left LAND %left LAND
%left OR %left OR
@ -5445,6 +5447,12 @@ exprcompound : expr PLUS expr {
$$.val = NewStringf("%s<=%s",$1.val,$3.val); $$.val = NewStringf("%s<=%s",$1.val,$3.val);
$$.type = T_INT; $$.type = T_INT;
} }
| expr QUESTIONMARK expr COLON expr {
$$.val = NewStringf("%s?%s:%s", $1.val, $3.val, $5.val);
/* This may not be exactly right, but is probably good enough
* for the purposes of parsing constant expressions. */
$$.type = promote($3.type, $5.type);
}
| MINUS expr %prec UMINUS { | MINUS expr %prec UMINUS {
$$.val = NewStringf("-%s",$2.val); $$.val = NewStringf("-%s",$2.val);
$$.type = $2.type; $$.type = $2.type;