Commit 1430c7be authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

expr: added support for operator ?: (short ifelse test version)

parent e1e83cdc
......@@ -2,7 +2,11 @@
* Version 1.6.8 released
2015-03-21 Uwe Schulzweida
2015-03-23 Uwe Schulzweida
* expr: added support for operator ?: (short ifelse test version)
2015-03-22 Uwe Schulzweida
* configure: check whether netCDF4/HDF5 is threadsafe
......
......@@ -305,8 +305,7 @@ nodeType *expr_var_var(int oper, nodeType *p1, nodeType *p2)
long ngp1 = gridInqSize(p1->gridID);
long ngp2 = gridInqSize(p2->gridID);
if ( ngp1 != ngp2 )
cdoAbort("Number of grid points differ. ngp1 = %ld, ngp2 = %ld", ngp1, ngp2);
if ( ngp1 != ngp2 ) cdoAbort("Number of grid points differ. ngp1 = %ld, ngp2 = %ld", ngp1, ngp2);
long ngp = ngp1;
......@@ -679,6 +678,83 @@ nodeType *ex_uminus(nodeType *p1)
return (p);
}
static
nodeType *ex_ifelse(nodeType *p1, nodeType *p2, nodeType *p3)
{
if ( cdoVerbose ) printf("\t %s ? %s : %s\n", p1->u.var.nm, p2->u.var.nm, p3->u.var.nm);
if ( p1->type == typeCon ) cdoAbort("First expression is a constant but must be a variable!");
if ( p2->type == typeCon ) cdoAbort("Second expression is a constant but must be a variable!");
if ( p2->type == typeCon ) cdoAbort("Third expression is a constant but must be a variable!");
int nmiss1 = p1->nmiss;
double missval1 = p1->missval;
double missval2 = p2->missval;
double missval3 = p3->missval;
long ngp1 = gridInqSize(p1->gridID);
long ngp2 = gridInqSize(p2->gridID);
long ngp3 = gridInqSize(p3->gridID);
if ( ngp1 != ngp2 ) cdoAbort("Number of grid points differ. ngp1 = %ld, ngp2 = %ld", ngp1, ngp2);
if ( ngp1 != ngp3 ) cdoAbort("Number of grid points differ. ngp1 = %ld, ngp3 = %ld", ngp1, ngp3);
long ngp = ngp1;
long nlev1 = zaxisInqSize(p1->zaxisID);
long nlev2 = zaxisInqSize(p2->zaxisID);
long nlev3 = zaxisInqSize(p3->zaxisID);
if ( nlev1 != nlev2 ) cdoAbort("Number of levels differ. nlev1 = %ld, nlev2 = %ld", nlev1, nlev2);
if ( nlev1 != nlev3 ) cdoAbort("Number of levels differ. nlev1 = %ld, nlev3 = %ld", nlev1, nlev3);
nodeType *p = (nodeType*) malloc(sizeof(nodeType));
p->type = typeVar;
p->tmpvar = 1;
p->u.var.nm = strdupx("tmp");
long nlev = nlev1;
p->gridID = p1->gridID;
p->zaxisID = p1->zaxisID;
p->missval = p1->missval;
p->data = (double*) malloc(ngp*nlev*sizeof(double));
long loff, loff1, loff2, loff3;
for ( long k = 0; k < nlev; ++k )
{
loff = k*ngp;
if ( nlev1 == 1 ) loff1 = 0;
else loff1 = k*ngp;
if ( nlev2 == 1 ) loff2 = 0;
else loff2 = k*ngp;
if ( nlev3 == 1 ) loff3 = 0;
else loff3 = k*ngp;
const double *restrict idat1 = p1->data+loff1;
const double *restrict idat2 = p2->data+loff2;
const double *restrict idat3 = p3->data+loff3;
double *restrict odat = p->data+loff;
for ( long i = 0; i < ngp; ++i )
{
if ( nmiss1 && DBL_IS_EQUAL(idat1[i],missval1) )
odat[i] = missval1;
else if ( IS_NOT_EQUAL(idat1[i], 0) )
odat[i] = DBL_IS_EQUAL(idat2[i],missval2) ? missval1 : idat2[i];
else
odat[i] = DBL_IS_EQUAL(idat3[i],missval3) ? missval1 : idat3[i];
}
}
return (p);
}
int exNode(nodeType *p, parse_parm_t *parse_arg)
{
......@@ -890,6 +966,24 @@ nodeType *expr_run(nodeType *p, parse_parm_t *parse_arg)
rnode = ex_uminus(expr_run(p->u.opr.op[0], parse_arg));
}
break;
case '?':
if ( parse_arg->init )
{
expr_run(p->u.opr.op[0], parse_arg);
expr_run(p->u.opr.op[1], parse_arg);
expr_run(p->u.opr.op[2], parse_arg);
if ( parse_arg->debug )
printf("\t?:\n");
}
else
{
rnode = ex_ifelse(expr_run(p->u.opr.op[0], parse_arg),
expr_run(p->u.opr.op[1], parse_arg),
expr_run(p->u.opr.op[2], parse_arg));
}
break;
default:
if ( parse_arg->init )
......
......@@ -388,8 +388,8 @@ static yyconst flex_int32_t yy_ec[256] =
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 4, 5, 1, 6, 1, 1, 1, 1, 7,
8, 8, 9, 1, 9, 10, 8, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 1, 8, 12,
13, 14, 1, 1, 15, 15, 15, 16, 17, 15,
11, 11, 11, 11, 11, 11, 11, 8, 8, 12,
13, 14, 8, 1, 15, 15, 15, 16, 17, 15,
15, 15, 18, 15, 15, 16, 19, 15, 15, 20,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
1, 1, 1, 8, 21, 1, 15, 15, 15, 16,
......
......@@ -69,7 +69,7 @@ M_E {
}
[-()<>=+*/;{}^.] {
[-()<>=+*/;{}^.?:] {
return *yytext;
}
......
This diff is collapsed.
/* A Bison parser, made by GNU Bison 3.0.4. */
/* A Bison parser, made by GNU Bison 2.7.12-4996. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
......@@ -26,13 +26,13 @@
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef YY_YY_EXPR_YACC_H_INCLUDED
# define YY_YY_EXPR_YACC_H_INCLUDED
/* Debug traces. */
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
......@@ -40,37 +40,55 @@
extern int yydebug;
#endif
/* Token type. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
enum yytokentype
{
CONSTANT = 258,
VARIABLE = 259,
FUNCTION = 260,
LEG = 261,
GE = 262,
LE = 263,
EQ = 264,
NE = 265,
UMINUS = 266
};
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
CONSTANT = 258,
VARIABLE = 259,
FUNCTION = 260,
NE = 261,
EQ = 262,
LE = 263,
GE = 264,
LEG = 265,
UMINUS = 266
};
#endif
/* Tokens. */
#define CONSTANT 258
#define VARIABLE 259
#define FUNCTION 260
#define LEG 261
#define GE 262
#define NE 261
#define EQ 262
#define LE 263
#define EQ 264
#define NE 265
#define GE 264
#define LEG 265
#define UMINUS 266
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (parse_parm_t *parse_arg, void *scanner);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_YY_EXPR_YACC_H_INCLUDED */
......@@ -39,6 +39,7 @@ int expr_run(nodeType *p, parse_parm_t *parse_arg);
%left LEG GE LE EQ NE '>' '<' '='
%left '+' '-'
%left '*' '/'
%left '?' ':'
%right '^'
%nonassoc UMINUS
......@@ -84,6 +85,7 @@ expr:
| expr NE expr { $$ = expr_opr(NE, 2, $1, $3); }
| expr EQ expr { $$ = expr_opr(EQ, 2, $1, $3); }
| expr LEG expr { $$ = expr_opr(LEG, 2, $1, $3); }
| expr '?' expr ':' expr { $$ = expr_opr('?', 3, $1, $3, $5); }
| '(' expr ')' { $$ = $2; }
| FUNCTION '(' expr ')' { $$ = expr_fun($1, $3); }
;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment