| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2013 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
/* Parser for the FreeCAD Units language */ |
| 24 |
|
| 25 |
/* Represents the many different ways we can access our data */ |
| 26 |
%{ |
| 27 |
#define YYSTYPE Quantity |
| 28 |
#define yyparse Quantity_yyparse |
| 29 |
#define yyerror Quantity_yyerror |
| 30 |
%} |
| 31 |
|
| 32 |
/* Bison declarations. */ |
| 33 |
%token UNIT ONE NUM MINUSSIGN |
| 34 |
%token ACOS ASIN ATAN ATAN2 COS EXP ABS MOD LOG LOG10 POW SIN SINH TAN TANH SQRT; |
| 35 |
%left MINUSSIGN '+' |
| 36 |
%left '*' '/' |
| 37 |
%left NEG /* negation--unary minus */ |
| 38 |
%right '^' /* exponentiation */ |
| 39 |
%left ONE NUM |
| 40 |
|
| 41 |
%start input |
| 42 |
|
| 43 |
%% |
| 44 |
|
| 45 |
input: { QuantResult = Quantity(std::numeric_limits<double>::min()); /* empty input */ } |
| 46 |
| num { QuantResult = $1; } |
| 47 |
| unit { QuantResult = $1; } |
| 48 |
| quantity { QuantResult = $1; } |
| 49 |
| quantity quantity { QuantResult = $1 + $2; } |
| 50 |
| quantity quantity quantity { QuantResult = $1 + $2 + $3; } |
| 51 |
; |
| 52 |
num: NUM { $$ = $1; } |
| 53 |
| ONE { $$ = $1; } |
| 54 |
| num '+' num { $$ = Quantity($1.getValue() + $3.getValue()); } |
| 55 |
| num MINUSSIGN num { $$ = Quantity($1.getValue() - $3.getValue()); } |
| 56 |
| num '*' num { $$ = Quantity($1.getValue() * $3.getValue()); } |
| 57 |
| num '/' num { $$ = Quantity($1.getValue() / $3.getValue()); } |
| 58 |
| MINUSSIGN num %prec NEG { $$ = Quantity(-$2.getValue()); } |
| 59 |
| num '^' num { $$ = Quantity(pow($1.getValue(), $3.getValue())); } |
| 60 |
| '(' num ')' { $$ = $2; } |
| 61 |
| ACOS '(' num ')' { $$ = Quantity(acos($3.getValue())); } |
| 62 |
| ASIN '(' num ')' { $$ = Quantity(asin($3.getValue())); } |
| 63 |
| ATAN '(' num ')' { $$ = Quantity(atan($3.getValue())); } |
| 64 |
| ABS '(' num ')' { $$ = Quantity(fabs($3.getValue())); } |
| 65 |
| EXP '(' num ')' { $$ = Quantity(exp($3.getValue())); } |
| 66 |
| LOG '(' num ')' { $$ = Quantity(log($3.getValue())); } |
| 67 |
| LOG10 '(' num ')' { $$ = Quantity(log10($3.getValue())); } |
| 68 |
| SIN '(' num ')' { $$ = Quantity(sin($3.getValue())); } |
| 69 |
| SINH '(' num ')' { $$ = Quantity(sinh($3.getValue())); } |
| 70 |
| TAN '(' num ')' { $$ = Quantity(tan($3.getValue())); } |
| 71 |
| TANH '(' num ')' { $$ = Quantity(tanh($3.getValue())); } |
| 72 |
| SQRT '(' num ')' { $$ = Quantity(sqrt($3.getValue())); } |
| 73 |
| COS '(' num ')' { $$ = Quantity(cos($3.getValue())); } |
| 74 |
; |
| 75 |
|
| 76 |
unit: UNIT { $$ = $1; } |
| 77 |
| ONE '/' unit { $$ = Quantity(1.0)/$3; } |
| 78 |
| unit '*' unit { $$ = $1 * $3; } |
| 79 |
| unit '/' unit { $$ = $1 / $3; } |
| 80 |
| unit '^' num { $$ = $1.pow ($3); } |
| 81 |
| '(' unit ')' { $$ = $2; } |
| 82 |
; |
| 83 |
quantity: num unit { $$ = $1*$2; } |
| 84 |
| num '/' unit { $$ = Quantity($1)/$3; } |
| 85 |
; |
| 86 |
|
| 87 |
%% |