| 1 |
// Copyright(C) 1999-2025 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
%{ |
| 7 |
#include "apr_array.h" |
| 8 |
#include "apr_util.h" |
| 9 |
#include "aprepro.h" |
| 10 |
|
| 11 |
#include <cerrno> |
| 12 |
#include <cfenv> |
| 13 |
#include <cmath> |
| 14 |
#include <cstdio> |
| 15 |
#include <cstdlib> |
| 16 |
#include <cstring> |
| 17 |
#include <fmt/format.h> |
| 18 |
#include <fmt/printf.h> |
| 19 |
#include <iostream> |
| 20 |
|
| 21 |
namespace { |
| 22 |
void reset_error() |
| 23 |
{ |
| 24 |
#if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \ |
| 25 |
!defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__) |
| 26 |
#ifndef math_errhandling |
| 27 |
#define math_errhandling MATH_ERRNO |
| 28 |
#endif |
| 29 |
|
| 30 |
if (math_errhandling & MATH_ERREXCEPT) { |
| 31 |
std::feclearexcept(FE_ALL_EXCEPT); |
| 32 |
} |
| 33 |
if (math_errhandling & MATH_ERRNO) { |
| 34 |
errno = 0; |
| 35 |
} |
| 36 |
#endif |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
namespace SEAMS { |
| 41 |
extern bool echo; |
| 42 |
} |
| 43 |
|
| 44 |
%} |
| 45 |
|
| 46 |
%require "3.2" |
| 47 |
|
| 48 |
/* add debug output code to generated parser. disable this for release |
| 49 |
* versions. */ |
| 50 |
%debug |
| 51 |
|
| 52 |
/* write out a header file containing the token defines */ |
| 53 |
%defines |
| 54 |
|
| 55 |
/* use newer C++ skeleton file */ |
| 56 |
%skeleton "lalr1.cc" |
| 57 |
|
| 58 |
/* namespace to enclose parser in */ |
| 59 |
%define api.prefix {SEAMS} |
| 60 |
|
| 61 |
/* set the parser's class identifier */ |
| 62 |
%define api.parser.class {Parser} |
| 63 |
|
| 64 |
%define parse.error verbose |
| 65 |
|
| 66 |
/* aprepro is passed by reference to the parser and to the scanner. This |
| 67 |
* provides a simple but effective pure interface, not relying on global |
| 68 |
* variables. */ |
| 69 |
%parse-param { class Aprepro& aprepro } |
| 70 |
|
| 71 |
%union { |
| 72 |
double val; /* For returning numbers. */ |
| 73 |
struct symrec *tptr; /* For returning symbol-table pointers */ |
| 74 |
char *string; /* For returning quoted strings */ |
| 75 |
struct array *arrval; /* For returning arrays */ |
| 76 |
} |
| 77 |
|
| 78 |
%token <val> NUM /* Simple double precision number */ |
| 79 |
%token <string> QSTRING /* Quoted string */ |
| 80 |
%token <tptr> UNDVAR /* Variable and function */ |
| 81 |
%token <tptr> VAR |
| 82 |
%token <tptr> SVAR /* String Variable */ |
| 83 |
%token <tptr> IMMVAR /* Immutable Variable */ |
| 84 |
%token <tptr> IMMSVAR /* Immutable String Variable */ |
| 85 |
%token <tptr> AVAR /* array data [i,j] */ |
| 86 |
%token <tptr> FNCT |
| 87 |
%token <tptr> SFNCT |
| 88 |
%token <tptr> AFNCT |
| 89 |
%type <val> exp |
| 90 |
%type <arrval> aexp |
| 91 |
%type <val> bool |
| 92 |
%type <string> sexp |
| 93 |
|
| 94 |
%token END 0 "end of file" |
| 95 |
%token COMMA LPAR RPAR LBRACK RBRACK LBRACE RBRACE SEMI |
| 96 |
/* Precedence (Lowest to Highest) and associativity */ |
| 97 |
%right EQUAL |
| 98 |
%right EQ_PLUS EQ_MINUS |
| 99 |
%right EQ_TIME EQ_DIV |
| 100 |
%right EQ_POW |
| 101 |
%right QUEST COLON |
| 102 |
%left LOR /* Logical OR */ |
| 103 |
%left LAND /* Logical AND */ |
| 104 |
%left LT GT LE GE EQ NE /* <=, >=, ==, != */ |
| 105 |
%left PLU SUB |
| 106 |
%left DIV TIM MOD |
| 107 |
%left UNARY NOT /* Negation--unary minus/plus */ |
| 108 |
%right POW /* Exponentiation */ |
| 109 |
%left INC DEC /* increment (++), decrement (--) */ |
| 110 |
%left CONCAT /* Concatenate Strings */ |
| 111 |
|
| 112 |
%{ |
| 113 |
|
| 114 |
#include "aprepro.h" |
| 115 |
#include "apr_scanner.h" |
| 116 |
|
| 117 |
/* this "connects" the bison parser in aprepro to the flex scanner class |
| 118 |
* object. it defines the yylex() function call to pull the next token from the |
| 119 |
* current lexer object of the aprepro context. */ |
| 120 |
#undef yylex |
| 121 |
#define yylex aprepro.lexer->lex |
| 122 |
|
| 123 |
%} |
| 124 |
|
| 125 |
/* Grammar Rules: */ |
| 126 |
%% |
| 127 |
input: /* empty rule */ |
| 128 |
| input line |
| 129 |
; |
| 130 |
|
| 131 |
line: '\n' { if (echo) aprepro.lexer->LexerOutput("\n", 1); } |
| 132 |
| LBRACE exp RBRACE { if (echo) { |
| 133 |
SEAMS::symrec *format = aprepro.getsym("_FORMAT"); |
| 134 |
if (format->value.svar.empty()) { |
| 135 |
auto tmpstr = fmt::format("{}", $2); |
| 136 |
aprepro.lexer->LexerOutput(tmpstr.c_str(), tmpstr.size()); |
| 137 |
} |
| 138 |
else { |
| 139 |
static char tmpstr[512]; |
| 140 |
int len = snprintf(tmpstr, 512, format->value.svar.c_str(), $2); |
| 141 |
aprepro.lexer->LexerOutput(tmpstr, len); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
| LBRACE sexp RBRACE { if (echo && $2 != NULL) { |
| 146 |
aprepro.lexer->LexerOutput($2, strlen($2)); |
| 147 |
} |
| 148 |
} |
| 149 |
| LBRACE aexp RBRACE { } |
| 150 |
| LBRACE RBRACE { } |
| 151 |
| error RBRACE { yyerrok; } |
| 152 |
; |
| 153 |
|
| 154 |
bool: exp LT exp { $$ = $1 < $3; } |
| 155 |
| exp GT exp { $$ = $1 > $3; } |
| 156 |
| NOT exp { $$ = !($2); } |
| 157 |
| exp LE exp { $$ = $1 <= $3; } |
| 158 |
| exp GE exp { $$ = $1 >= $3; } |
| 159 |
| exp EQ exp { $$ = $1 == $3; } |
| 160 |
| exp NE exp { $$ = $1 != $3; } |
| 161 |
| exp LOR exp { $$ = $1 || $3; } |
| 162 |
| exp LAND exp { $$ = $1 && $3; } |
| 163 |
| bool LOR bool { $$ = $1 || $3; } |
| 164 |
| bool LAND bool { $$ = $1 && $3; } |
| 165 |
| bool LOR exp { $$ = $1 || $3; } |
| 166 |
| bool LAND exp { $$ = $1 && $3; } |
| 167 |
| exp LOR bool { $$ = $1 || $3; } |
| 168 |
| exp LAND bool { $$ = $1 && $3; } |
| 169 |
| LPAR bool RPAR { $$ = $2; } |
| 170 |
|
| 171 |
| UNDVAR LOR exp { $$ = 0 || $3; undefined_error(aprepro, $1->name); } |
| 172 |
| UNDVAR LAND exp { $$ = 0 && $3; undefined_error(aprepro, $1->name); } |
| 173 |
| exp LOR UNDVAR { $$ = $1 || 0; undefined_error(aprepro, $3->name); } |
| 174 |
| exp LAND UNDVAR { $$ = $1 && 0; undefined_error(aprepro, $3->name); } |
| 175 |
| bool LOR UNDVAR { $$ = $1 || 0; undefined_error(aprepro, $3->name); } |
| 176 |
| bool LAND UNDVAR { $$ = $1 && 0; undefined_error(aprepro, $3->name); } |
| 177 |
| UNDVAR LOR bool { $$ = 0 || $3; undefined_error(aprepro, $1->name); } |
| 178 |
| UNDVAR LAND bool { $$ = 0 && $3; undefined_error(aprepro, $1->name); } |
| 179 |
; |
| 180 |
|
| 181 |
bool: sexp LT sexp { $$ = (strcmp($1,$3) < 0 ? 1 : 0); } |
| 182 |
| sexp GT sexp { $$ = (strcmp($1,$3) > 0 ? 1 : 0); } |
| 183 |
| sexp LE sexp { $$ = (strcmp($1,$3) <= 0 ? 1 : 0); } |
| 184 |
| sexp GE sexp { $$ = (strcmp($1,$3) >= 0 ? 1 : 0); } |
| 185 |
| sexp EQ sexp { $$ = (strcmp($1,$3) == 0 ? 1 : 0); } |
| 186 |
| sexp NE sexp { $$ = (strcmp($1,$3) != 0 ? 1 : 0); } |
| 187 |
|
| 188 |
| exp LT sexp { $$ = false; yyerror(aprepro, "Comparison of arithmetic with string not defined"); yyerrok;} |
| 189 |
| exp GT sexp { $$ = false; yyerror(aprepro, "Comparison of arithmetic with string not defined"); yyerrok;} |
| 190 |
| exp LE sexp { $$ = false; yyerror(aprepro, "Comparison of arithmetic with string not defined"); yyerrok;} |
| 191 |
| exp GE sexp { $$ = false; yyerror(aprepro, "Comparison of arithmetic with string not defined"); yyerrok;} |
| 192 |
| exp EQ sexp { $$ = false; } |
| 193 |
| exp NE sexp { $$ = true; } |
| 194 |
|
| 195 |
| sexp LT exp { $$ = false; yyerror(aprepro, "Comparison of string with arithmetic not defined"); yyerrok;} |
| 196 |
| sexp GT exp { $$ = false; yyerror(aprepro, "Comparison of string with arithmetic not defined"); yyerrok;} |
| 197 |
| sexp LE exp { $$ = false; yyerror(aprepro, "Comparison of string with arithmetic not defined"); yyerrok;} |
| 198 |
| sexp GE exp { $$ = false; yyerror(aprepro, "Comparison of string with arithmetic not defined"); yyerrok;} |
| 199 |
| sexp EQ exp { $$ = false; } |
| 200 |
| sexp NE exp { $$ = true; } |
| 201 |
|
| 202 |
| UNDVAR LT sexp { $$ = (strcmp("",$3) < 0 ? 1 : 0); undefined_error(aprepro, $1->name); } |
| 203 |
| UNDVAR GT sexp { $$ = (strcmp("",$3) > 0 ? 1 : 0); undefined_error(aprepro, $1->name); } |
| 204 |
| UNDVAR LE sexp { $$ = (strcmp("",$3) <= 0 ? 1 : 0); undefined_error(aprepro, $1->name); } |
| 205 |
| UNDVAR GE sexp { $$ = (strcmp("",$3) >= 0 ? 1 : 0); undefined_error(aprepro, $1->name); } |
| 206 |
| UNDVAR EQ sexp { $$ = (strcmp("",$3) == 0 ? 1 : 0); undefined_error(aprepro, $1->name); } |
| 207 |
| UNDVAR NE sexp { $$ = (strcmp("",$3) != 0 ? 1 : 0); undefined_error(aprepro, $1->name); } |
| 208 |
|
| 209 |
| sexp LT UNDVAR { $$ = (strcmp($1,"") < 0 ? 1 : 0); undefined_error(aprepro, $3->name); } |
| 210 |
| sexp GT UNDVAR { $$ = (strcmp($1,"") > 0 ? 1 : 0); undefined_error(aprepro, $3->name); } |
| 211 |
| sexp LE UNDVAR { $$ = (strcmp($1,"") <= 0 ? 1 : 0); undefined_error(aprepro, $3->name); } |
| 212 |
| sexp GE UNDVAR { $$ = (strcmp($1,"") >= 0 ? 1 : 0); undefined_error(aprepro, $3->name); } |
| 213 |
| sexp EQ UNDVAR { $$ = (strcmp($1,"") == 0 ? 1 : 0); undefined_error(aprepro, $3->name); } |
| 214 |
| sexp NE UNDVAR { $$ = (strcmp($1,"") != 0 ? 1 : 0); undefined_error(aprepro, $3->name); } |
| 215 |
|
| 216 |
| UNDVAR LT exp { $$ = 0 < $3; undefined_error(aprepro, $1->name); } |
| 217 |
| UNDVAR GT exp { $$ = 0 > $3; undefined_error(aprepro, $1->name); } |
| 218 |
| UNDVAR LE exp { $$ = 0 <= $3; undefined_error(aprepro, $1->name); } |
| 219 |
| UNDVAR GE exp { $$ = 0 >= $3; undefined_error(aprepro, $1->name); } |
| 220 |
| UNDVAR EQ exp { $$ = 0 == $3; undefined_error(aprepro, $1->name); } |
| 221 |
| UNDVAR NE exp { $$ = 0 != $3; undefined_error(aprepro, $1->name); } |
| 222 |
|
| 223 |
| exp LT UNDVAR { $$ = $1 < 0; undefined_error(aprepro, $3->name); } |
| 224 |
| exp GT UNDVAR { $$ = $1 > 0; undefined_error(aprepro, $3->name); } |
| 225 |
| exp LE UNDVAR { $$ = $1 <= 0; undefined_error(aprepro, $3->name); } |
| 226 |
| exp GE UNDVAR { $$ = $1 >= 0; undefined_error(aprepro, $3->name); } |
| 227 |
| exp EQ UNDVAR { $$ = $1 == 0; undefined_error(aprepro, $3->name); } |
| 228 |
| exp NE UNDVAR { $$ = $1 != 0; undefined_error(aprepro, $3->name); } |
| 229 |
|
| 230 |
aexp: AVAR { $$ = aprepro.make_array(*($1->value.avar)); } |
| 231 |
| AFNCT LPAR sexp RPAR { |
| 232 |
if (arg_check($1, $1->value.arrfnct_c == NULL)) |
| 233 |
$$ = (*($1->value.arrfnct_c))($3); |
| 234 |
else |
| 235 |
yyerrok; |
| 236 |
} |
| 237 |
| AFNCT LPAR sexp COMMA exp RPAR { |
| 238 |
if (arg_check($1, $1->value.arrfnct_cd == NULL)) |
| 239 |
$$ = (*($1->value.arrfnct_cd))($3,$5); |
| 240 |
else |
| 241 |
yyerrok; |
| 242 |
} |
| 243 |
| AFNCT LPAR sexp COMMA sexp RPAR { |
| 244 |
if (arg_check($1, $1->value.arrfnct_cc == NULL)) |
| 245 |
$$ = (*($1->value.arrfnct_cc))($3,$5); |
| 246 |
else |
| 247 |
yyerrok; |
| 248 |
} |
| 249 |
| AFNCT LPAR exp COMMA exp COMMA exp RPAR { |
| 250 |
if (arg_check($1, $1->value.arrfnct_ddd == NULL)) |
| 251 |
$$ = (*($1->value.arrfnct_ddd))($3,$5,$7); |
| 252 |
else |
| 253 |
yyerrok; |
| 254 |
} |
| 255 |
| AFNCT LPAR exp COMMA exp RPAR { |
| 256 |
if (arg_check($1, $1->value.arrfnct_dd == NULL)) |
| 257 |
$$ = (*($1->value.arrfnct_dd))($3,$5); |
| 258 |
else |
| 259 |
yyerrok; |
| 260 |
} |
| 261 |
| AFNCT LPAR exp RPAR { |
| 262 |
if (arg_check($1, $1->value.arrfnct_d == NULL)) |
| 263 |
$$ = (*($1->value.arrfnct_d))($3); |
| 264 |
else |
| 265 |
yyerrok; |
| 266 |
} |
| 267 |
| AFNCT LPAR aexp RPAR { |
| 268 |
if (arg_check($1, $1->value.arrfnct_a == NULL)) |
| 269 |
$$ = (*($1->value.arrfnct_a))($3); |
| 270 |
else |
| 271 |
yyerrok; |
| 272 |
} |
| 273 |
| SVAR EQUAL aexp { $$ = $3; |
| 274 |
$1->value.avar = $3; |
| 275 |
redefined_warning(aprepro, $1); |
| 276 |
set_type(aprepro, $1, token::AVAR); } |
| 277 |
| VAR EQUAL aexp { $$ = $3; |
| 278 |
$1->value.avar= $3; |
| 279 |
redefined_warning(aprepro, $1); |
| 280 |
set_type(aprepro, $1, token::AVAR); } |
| 281 |
| AVAR EQUAL aexp { $$ = $3; aprepro.redefine_array($1->value.avar); $1->value.avar = $3; |
| 282 |
redefined_warning(aprepro, $1); |
| 283 |
set_type(aprepro, $1, token::AVAR); } |
| 284 |
| UNDVAR EQUAL aexp { $$ = $3; $1->value.avar = $3; |
| 285 |
set_type(aprepro, $1, token::AVAR); } |
| 286 |
| aexp PLU aexp { if ($1->cols == $3->cols && $1->rows == $3->rows ) { |
| 287 |
$$ = array_add($1, $3); |
| 288 |
} |
| 289 |
else { |
| 290 |
yyerror(aprepro, "Arrays do not have same row and column count"); |
| 291 |
yyerrok; |
| 292 |
} |
| 293 |
} |
| 294 |
| SUB aexp %prec UNARY { $$ = array_scale($2, -1.0); } |
| 295 |
|
| 296 |
| aexp SUB aexp { if ($1->cols == $3->cols && $1->rows == $3->rows ) { |
| 297 |
$$ = array_sub($1, $3); |
| 298 |
} |
| 299 |
else { |
| 300 |
yyerror(aprepro, "Arrays do not have same row and column count"); |
| 301 |
yyerrok; |
| 302 |
} |
| 303 |
} |
| 304 |
| aexp TIM exp { $$ = array_scale($1, $3); } |
| 305 |
| aexp DIV exp { $$ = array_scale($1, 1.0/$3); } |
| 306 |
| exp TIM aexp { $$ = array_scale($3, $1); } |
| 307 |
| aexp TIM aexp { if ($1->cols == $3->rows) { |
| 308 |
$$ = array_mult($1, $3); |
| 309 |
} |
| 310 |
else { |
| 311 |
yyerror(aprepro, "Column count of first array does not match row count of second array"); |
| 312 |
yyerrok; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
sexp: QSTRING { $$ = $1; } |
| 317 |
| SVAR { $$ = (char*)$1->value.svar.c_str(); } |
| 318 |
| IMMSVAR { $$ = (char*)$1->value.svar.c_str(); } |
| 319 |
| UNDVAR EQUAL sexp { $$ = $3; $1->value.svar = $3; |
| 320 |
set_type(aprepro, $1, Parser::token::SVAR); } |
| 321 |
| SVAR EQUAL sexp { $$ = $3; |
| 322 |
$1->value.svar = $3; |
| 323 |
redefined_warning(aprepro, $1); } |
| 324 |
| VAR EQUAL sexp { $$ = $3; |
| 325 |
$1->value.svar= $3; |
| 326 |
redefined_warning(aprepro, $1); |
| 327 |
set_type(aprepro, $1, token::SVAR); } |
| 328 |
| AVAR EQUAL sexp { $$ = $3; |
| 329 |
aprepro.redefine_array($1->value.avar); |
| 330 |
$1->value.svar= $3; |
| 331 |
redefined_warning(aprepro, $1); |
| 332 |
set_type(aprepro, $1, token::SVAR); } |
| 333 |
| IMMSVAR EQUAL sexp { $$ = (char*)$1->value.svar.c_str(); immutable_modify(aprepro, $1); } |
| 334 |
| IMMVAR EQUAL sexp { immutable_modify(aprepro, $1); YYERROR; } |
| 335 |
| SFNCT LPAR sexp RPAR { |
| 336 |
if (arg_check($1, $1->value.strfnct_c == NULL)) |
| 337 |
$$ = (char*)(*($1->value.strfnct_c))($3); |
| 338 |
else |
| 339 |
$$ = (char*)""; |
| 340 |
} |
| 341 |
| SFNCT LPAR RPAR { |
| 342 |
if (arg_check($1, $1->value.strfnct == NULL)) |
| 343 |
$$ = (char*)(*($1->value.strfnct))(); |
| 344 |
else |
| 345 |
$$ = (char*)""; |
| 346 |
} |
| 347 |
| SFNCT LPAR exp RPAR { |
| 348 |
if (arg_check($1, $1->value.strfnct_d == NULL)) |
| 349 |
$$ = (char*)(*($1->value.strfnct_d))($3); |
| 350 |
else |
| 351 |
$$ = (char*)""; |
| 352 |
} |
| 353 |
| SFNCT LPAR aexp RPAR { |
| 354 |
if (arg_check($1, $1->value.strfnct_a == NULL)) |
| 355 |
$$ = (char*)(*($1->value.strfnct_a))($3); |
| 356 |
else |
| 357 |
$$ = (char*)""; |
| 358 |
} |
| 359 |
| sexp CONCAT sexp { concat_string($1, $3, &$$); } |
| 360 |
| sexp PLU sexp { concat_string($1, $3, &$$); } |
| 361 |
| SFNCT LPAR exp COMMA exp RPAR { |
| 362 |
if (arg_check($1, $1->value.strfnct_dd == NULL)) |
| 363 |
$$ = (char*)(*($1->value.strfnct_dd))($3, $5); |
| 364 |
else |
| 365 |
$$ = (char*)""; |
| 366 |
} |
| 367 |
| SFNCT LPAR exp COMMA sexp COMMA sexp COMMA sexp COMMA sexp RPAR { |
| 368 |
if (arg_check($1, $1->value.strfnct_dcccc == NULL)) |
| 369 |
$$ = (char*)(*($1->value.strfnct_dcccc))($3, $5, $7, $9, $11); |
| 370 |
else |
| 371 |
$$ = (char*)""; |
| 372 |
} |
| 373 |
| SFNCT LPAR exp COMMA sexp COMMA sexp RPAR { |
| 374 |
if (arg_check($1, $1->value.strfnct_dcc == NULL)) |
| 375 |
$$ = (char*)(*($1->value.strfnct_dcc))($3, $5, $7); |
| 376 |
else |
| 377 |
$$ = (char*)""; |
| 378 |
} |
| 379 |
| SFNCT LPAR exp COMMA sexp RPAR { |
| 380 |
if (arg_check($1, $1->value.strfnct_dc == NULL)) |
| 381 |
$$ = (char*)(*($1->value.strfnct_dc))($3, $5); |
| 382 |
else |
| 383 |
$$ = (char*)""; |
| 384 |
} |
| 385 |
| SFNCT LPAR sexp COMMA exp RPAR { |
| 386 |
if (arg_check($1, $1->value.strfnct_cd == NULL)) |
| 387 |
$$ = (char*)(*($1->value.strfnct_cd))($3, $5); |
| 388 |
else |
| 389 |
$$ = (char*)""; |
| 390 |
} |
| 391 |
| SFNCT LPAR sexp COMMA sexp COMMA sexp RPAR { |
| 392 |
if (arg_check($1, $1->value.strfnct_ccc == NULL)) |
| 393 |
$$ = (char*)(*($1->value.strfnct_ccc))($3, $5, $7); |
| 394 |
else |
| 395 |
$$ = (char*)""; |
| 396 |
} |
| 397 |
| SFNCT LPAR sexp COMMA sexp RPAR { |
| 398 |
if (arg_check($1, $1->value.strfnct_cc == NULL)) |
| 399 |
$$ = (char*)(*($1->value.strfnct_cc))($3, $5); |
| 400 |
else |
| 401 |
$$ = (char*)""; |
| 402 |
} |
| 403 |
| bool QUEST sexp COLON sexp { $$ = ($1) ? ($3) : ($5); } |
| 404 |
| exp TIM sexp { $$ = (char*)""; yyerror(aprepro, "Multiplying an arithmetic with a string is not defined"); yyerrok;} |
| 405 |
| sexp TIM exp { $$ = (char*)""; yyerror(aprepro, "Multiplying a string with an arithmetic is not defined"); yyerrok;} |
| 406 |
| sexp TIM sexp { $$ = (char*)""; yyerror(aprepro, "Multiplying a string with a string is not defined"); yyerrok;} |
| 407 |
| sexp DIV exp { $$ = (char*)""; yyerror(aprepro, "Dividing a string by an arithmetic is not defined"); yyerrok;} |
| 408 |
| exp DIV sexp { $$ = (char*)""; yyerror(aprepro, "Dividing an arithmetic by a string is not defined"); yyerrok;} |
| 409 |
| sexp DIV sexp { $$ = (char*)""; yyerror(aprepro, "Dividing a string by a string is not defined"); yyerrok;} |
| 410 |
| exp PLU sexp { $$ = (char*)""; yyerror(aprepro, "Adding an arithmetic and a string is not defined"); yyerrok;} |
| 411 |
| sexp PLU exp { $$ = (char*)""; yyerror(aprepro, "Adding a string and an arithmetic is not defined"); yyerrok;} |
| 412 |
| exp SUB sexp { $$ = (char*)""; yyerror(aprepro, "Subtracting an arithmetic and a string is not defined"); yyerrok;} |
| 413 |
| sexp SUB exp { $$ = (char*)""; yyerror(aprepro, "Subtracting a string and an arithmetic is not defined"); yyerrok;} |
| 414 |
| sexp SUB sexp { $$ = (char*)""; yyerror(aprepro, "Subtracting a string from a string is not defined"); yyerrok;} |
| 415 |
|
| 416 |
exp: NUM { $$ = $1; } |
| 417 |
| INC NUM { $$ = $2 + 1; } |
| 418 |
| DEC NUM { $$ = $2 - 1; } |
| 419 |
| VAR { $$ = $1->value.var; } |
| 420 |
| IMMVAR { $$ = $1->value.var; } |
| 421 |
| INC VAR { $$ = ++($2->value.var); } |
| 422 |
| DEC VAR { $$ = --($2->value.var); } |
| 423 |
| VAR INC { $$ = ($1->value.var)++; } |
| 424 |
| VAR DEC { $$ = ($1->value.var)--; } |
| 425 |
| VAR EQUAL exp { $$ = $3; $1->value.var = $3; |
| 426 |
redefined_warning(aprepro, $1); } |
| 427 |
| SVAR EQUAL exp { $$ = $3; $1->value.var = $3; |
| 428 |
redefined_warning(aprepro, $1); |
| 429 |
set_type(aprepro, $1, token::VAR); } |
| 430 |
| AVAR EQUAL exp { $$ = $3; |
| 431 |
aprepro.redefine_array($1->value.avar); |
| 432 |
$1->value.var= $3; |
| 433 |
redefined_warning(aprepro, $1); |
| 434 |
set_type(aprepro, $1, token::VAR); } |
| 435 |
| VAR EQ_PLUS exp { $1->value.var += $3; $$ = $1->value.var; } |
| 436 |
| VAR EQ_MINUS exp { $1->value.var -= $3; $$ = $1->value.var; } |
| 437 |
| VAR EQ_TIME exp { $1->value.var *= $3; $$ = $1->value.var; } |
| 438 |
| VAR EQ_DIV exp { $1->value.var /= $3; $$ = $1->value.var; } |
| 439 |
| VAR EQ_POW exp { reset_error(); |
| 440 |
$1->value.var = std::pow($1->value.var,$3); |
| 441 |
$$ = $1->value.var; |
| 442 |
SEAMS::math_error(aprepro, "Power"); |
| 443 |
} |
| 444 |
| INC IMMVAR { $$ = $2->value.var; immutable_modify(aprepro, $2); } |
| 445 |
| DEC IMMVAR { $$ = $2->value.var; immutable_modify(aprepro, $2); } |
| 446 |
| IMMVAR INC { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 447 |
| IMMVAR DEC { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 448 |
| IMMVAR EQUAL exp { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 449 |
| IMMSVAR EQUAL exp { immutable_modify(aprepro, $1); YYERROR; } |
| 450 |
| IMMVAR EQ_PLUS exp { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 451 |
| IMMVAR EQ_MINUS exp { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 452 |
| IMMVAR EQ_TIME exp { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 453 |
| IMMVAR EQ_DIV exp { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 454 |
| IMMVAR EQ_POW exp { $$ = $1->value.var; immutable_modify(aprepro, $1); } |
| 455 |
|
| 456 |
| UNDVAR { $$ = $1->value.var; |
| 457 |
undefined_error(aprepro, $1->name); } |
| 458 |
| INC UNDVAR { $$ = ++($2->value.var); |
| 459 |
set_type(aprepro, $2, token::VAR); |
| 460 |
undefined_error(aprepro, $2->name); } |
| 461 |
| DEC UNDVAR { $$ = --($2->value.var); |
| 462 |
set_type(aprepro, $2, token::VAR); |
| 463 |
undefined_error(aprepro, $2->name); } |
| 464 |
| UNDVAR INC { $$ = ($1->value.var)++; |
| 465 |
set_type(aprepro, $1, token::VAR); |
| 466 |
undefined_error(aprepro, $1->name); } |
| 467 |
| UNDVAR DEC { $$ = ($1->value.var)--; |
| 468 |
set_type(aprepro, $1, token::VAR); |
| 469 |
undefined_error(aprepro, $1->name); } |
| 470 |
| UNDVAR EQUAL exp { $$ = $3; $1->value.var = $3; |
| 471 |
set_type(aprepro, $1, token::VAR); } |
| 472 |
| UNDVAR EQ_PLUS exp { $1->value.var += $3; $$ = $1->value.var; |
| 473 |
set_type(aprepro, $1, token::VAR); |
| 474 |
undefined_error(aprepro, $1->name); } |
| 475 |
| UNDVAR EQ_MINUS exp { $1->value.var -= $3; $$ = $1->value.var; |
| 476 |
set_type(aprepro, $1, token::VAR); |
| 477 |
undefined_error(aprepro, $1->name); } |
| 478 |
| UNDVAR EQ_TIME exp { $1->value.var *= $3; $$ = $1->value.var; |
| 479 |
set_type(aprepro, $1, token::VAR); |
| 480 |
undefined_error(aprepro, $1->name); } |
| 481 |
| UNDVAR EQ_DIV exp { $1->value.var /= $3; $$ = $1->value.var; |
| 482 |
set_type(aprepro, $1, token::VAR); |
| 483 |
undefined_error(aprepro, $1->name); } |
| 484 |
| UNDVAR EQ_POW exp { reset_error(); |
| 485 |
$1->value.var = std::pow($1->value.var,$3); |
| 486 |
$$ = $1->value.var; |
| 487 |
set_type(aprepro, $1, token::VAR); |
| 488 |
SEAMS::math_error(aprepro, "Power"); |
| 489 |
undefined_error(aprepro, $1->name); } |
| 490 |
|
| 491 |
| FNCT LPAR RPAR { |
| 492 |
if (arg_check($1, $1->value.fnctptr == NULL)) |
| 493 |
$$ = (*($1->value.fnctptr))(); |
| 494 |
else |
| 495 |
$$ = 0.0; |
| 496 |
} |
| 497 |
|
| 498 |
| FNCT LPAR exp RPAR { |
| 499 |
if (arg_check($1, $1->value.fnctptr_d == NULL)) |
| 500 |
$$ = (*($1->value.fnctptr_d))($3); |
| 501 |
else |
| 502 |
$$ = 0.0; |
| 503 |
} |
| 504 |
|
| 505 |
| FNCT LPAR sexp RPAR { |
| 506 |
if (arg_check($1, $1->value.fnctptr_c == NULL)) |
| 507 |
$$ = (*($1->value.fnctptr_c))($3); |
| 508 |
else |
| 509 |
$$ = 0.0; |
| 510 |
} |
| 511 |
|
| 512 |
| FNCT LPAR aexp RPAR { |
| 513 |
if (arg_check($1, $1->value.fnctptr_a == NULL)) |
| 514 |
$$ = (*($1->value.fnctptr_a))($3); |
| 515 |
else |
| 516 |
$$ = 0.0; |
| 517 |
} |
| 518 |
|
| 519 |
| FNCT LPAR sexp COMMA exp RPAR { |
| 520 |
if (arg_check($1, $1->value.fnctptr_cd == NULL)) |
| 521 |
$$ = (*($1->value.fnctptr_cd))($3, $5); |
| 522 |
else |
| 523 |
$$ = 0.0; |
| 524 |
} |
| 525 |
|
| 526 |
| FNCT LPAR exp COMMA sexp RPAR { |
| 527 |
if (arg_check($1, $1->value.fnctptr_dc == NULL)) |
| 528 |
$$ = (*($1->value.fnctptr_dc))($3, $5); |
| 529 |
else |
| 530 |
$$ = 0.0; |
| 531 |
} |
| 532 |
|
| 533 |
| FNCT LPAR sexp COMMA sexp RPAR { |
| 534 |
if (arg_check($1, $1->value.fnctptr_cc == NULL)) |
| 535 |
$$ = (*($1->value.fnctptr_cc))($3, $5); |
| 536 |
else |
| 537 |
$$ = 0.0; |
| 538 |
} |
| 539 |
|
| 540 |
| FNCT LPAR sexp COMMA sexp COMMA sexp RPAR { |
| 541 |
if (arg_check($1, $1->value.fnctptr_ccc == NULL)) |
| 542 |
$$ = (*($1->value.fnctptr_ccc))($3,$5,$7); |
| 543 |
else |
| 544 |
yyerrok; |
| 545 |
} |
| 546 |
|
| 547 |
| FNCT LPAR exp COMMA exp RPAR { |
| 548 |
if (arg_check($1, $1->value.fnctptr_dd == NULL)) |
| 549 |
$$ = (*($1->value.fnctptr_dd))($3, $5); |
| 550 |
else |
| 551 |
$$ = 0.0; |
| 552 |
} |
| 553 |
| FNCT LPAR exp COMMA exp COMMA exp RPAR { |
| 554 |
if (arg_check($1, $1->value.fnctptr_ddd == NULL)) |
| 555 |
$$ = (*($1->value.fnctptr_ddd))($3, $5, $7); |
| 556 |
else |
| 557 |
$$ = 0.0; |
| 558 |
} |
| 559 |
| FNCT LPAR sexp COMMA sexp COMMA exp RPAR { |
| 560 |
if (arg_check($1, $1->value.fnctptr_ccd == NULL)) |
| 561 |
$$ = (*($1->value.fnctptr_ccd))($3, $5, $7); |
| 562 |
else |
| 563 |
$$ = 0.0; |
| 564 |
} |
| 565 |
| FNCT LPAR exp COMMA exp SEMI exp COMMA exp RPAR { |
| 566 |
if (arg_check($1, $1->value.fnctptr_dddd == NULL)) |
| 567 |
$$ = (*($1->value.fnctptr_dddd))($3, $5, $7, $9); |
| 568 |
else |
| 569 |
$$ = 0.0; |
| 570 |
} |
| 571 |
| FNCT LPAR exp COMMA exp COMMA exp COMMA exp RPAR { |
| 572 |
if (arg_check($1, $1->value.fnctptr_dddd == NULL)) |
| 573 |
$$ = (*($1->value.fnctptr_dddd))($3, $5, $7, $9); |
| 574 |
else |
| 575 |
$$ = 0.0; |
| 576 |
} |
| 577 |
| FNCT LPAR exp COMMA exp COMMA exp COMMA exp COMMA sexp RPAR { |
| 578 |
if (arg_check($1, $1->value.fnctptr_ddddc == NULL)) |
| 579 |
$$ = (*($1->value.fnctptr_ddddc))($3, $5, $7, $9, $11); |
| 580 |
else |
| 581 |
$$ = 0.0; |
| 582 |
} |
| 583 |
| FNCT LPAR exp COMMA exp COMMA exp COMMA exp COMMA exp COMMA exp RPAR { |
| 584 |
if (arg_check($1, $1->value.fnctptr_dddddd == NULL)) |
| 585 |
$$ = (*($1->value.fnctptr_dddddd))($3, $5, $7, $9, $11, $13); |
| 586 |
else |
| 587 |
$$ = 0.0; |
| 588 |
} |
| 589 |
| exp PLU exp { $$ = $1 + $3; } |
| 590 |
| exp SUB exp { $$ = $1 - $3; } |
| 591 |
| exp TIM exp { $$ = $1 * $3; } |
| 592 |
| exp DIV exp { if ($3 == 0.) |
| 593 |
{ |
| 594 |
$$ = std::numeric_limits<double>::infinity(); |
| 595 |
yyerror(aprepro, "Zero divisor"); |
| 596 |
yyerrok; |
| 597 |
} |
| 598 |
else |
| 599 |
$$ = $1 / $3; } |
| 600 |
| exp MOD exp { if ((int)$3 == 0.) |
| 601 |
{ |
| 602 |
$$ = (int)$1; |
| 603 |
yyerror(aprepro, "Zero divisor"); |
| 604 |
yyerrok; |
| 605 |
} |
| 606 |
else |
| 607 |
$$ = (int)$1 % (int)$3; } |
| 608 |
| SUB exp %prec UNARY { $$ = -$2; } |
| 609 |
| PLU exp %prec UNARY { $$ = $2; } |
| 610 |
| exp POW exp { reset_error(); |
| 611 |
$$ = std::pow($1, $3); |
| 612 |
SEAMS::math_error(aprepro, "Power"); } |
| 613 |
| LPAR exp RPAR { $$ = $2; } |
| 614 |
| LBRACK exp RBRACK { reset_error(); |
| 615 |
$$ = (double)($2 < 0 ? -floor(-($2)): floor($2) ); |
| 616 |
SEAMS::math_error(aprepro, "floor (int)"); } |
| 617 |
| bool { $$ = ($1) ? 1 : 0; } |
| 618 |
| bool QUEST exp COLON exp { $$ = ($1) ? ($3) : ($5); } |
| 619 |
| AVAR LBRACK exp RBRACK { $$ = array_value($1->value.avar, $3, 0); } |
| 620 |
| AVAR LBRACK exp COMMA exp RBRACK { $$ = array_value($1->value.avar, $3, $5); } |
| 621 |
| AVAR LBRACK exp RBRACK EQUAL exp |
| 622 |
{ $$ = $6; |
| 623 |
array *arr = $1->value.avar; |
| 624 |
int cols = arr->cols; |
| 625 |
if (cols > 1) { |
| 626 |
yyerror(aprepro, "Cannot use [index] array access with multi-column array"); |
| 627 |
yyerrok; |
| 628 |
} |
| 629 |
int rows = arr->rows; |
| 630 |
int row = $3; |
| 631 |
if (aprepro.ap_options.one_based_index) { |
| 632 |
row--; |
| 633 |
} |
| 634 |
if (row < rows) { |
| 635 |
int offset = row*cols; |
| 636 |
$1->value.avar->data[offset] = $6; |
| 637 |
} |
| 638 |
else { |
| 639 |
yyerror(aprepro, "Row or Column index out of range"); |
| 640 |
yyerrok; |
| 641 |
} |
| 642 |
} |
| 643 |
| AVAR LBRACK exp COMMA exp RBRACK EQUAL exp |
| 644 |
{ $$ = $8; |
| 645 |
array *arr = $1->value.avar; |
| 646 |
int cols = arr->cols; |
| 647 |
int rows = arr->rows; |
| 648 |
int row = $3; |
| 649 |
int col = $5; |
| 650 |
if (aprepro.ap_options.one_based_index) { |
| 651 |
row--; |
| 652 |
col--; |
| 653 |
} |
| 654 |
if (row < rows && col < cols) { |
| 655 |
int offset = row*cols+col; |
| 656 |
$1->value.avar->data[offset] = $8; |
| 657 |
} |
| 658 |
else { |
| 659 |
yyerror(aprepro, "Row or Column index out of range"); |
| 660 |
yyerrok; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
/* End of grammar */ |
| 666 |
%% |
| 667 |
|
| 668 |
void SEAMS::Parser::error(const std::string& m) |
| 669 |
{ |
| 670 |
aprepro.error(m); |
| 671 |
} |