-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuantityParser.y
More file actions
95 lines (83 loc) · 5.12 KB
/
QuantityParser.y
File metadata and controls
95 lines (83 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/***************************************************************************
* Copyright (c) 2013 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
//* Parser for the FreeCAD Units language */
/* Represents the many different ways we can access our data */
%{
#define YYSTYPE Quantity
#define yyparse Quantity_yyparse
#define yyerror Quantity_yyerror
#ifndef DOUBLE_MAX
# define DOUBLE_MAX 1.7976931348623157E+308 /* max decimal value of a "double"*/
#endif
#ifndef DOUBLE_MIN
# define DOUBLE_MIN 2.2250738585072014E-308 /* min decimal value of a "double"*/
#endif
%}
/* Bison declarations. */
%token UNIT ONE NUM MINUSSIGN
%token ACOS ASIN ATAN ATAN2 COS EXP ABS MOD LOG LOG10 POW SIN SINH TAN TANH SQRT;
%left MINUSSIGN '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%right '^' /* exponentiation */
%left ONE NUM
%start input
%%
input: { QuantResult = Quantity(DOUBLE_MIN); /* empty input */ }
| num { QuantResult = $1 ; }
| unit { QuantResult = $1 ; }
| quantity { QuantResult = $1 ; }
| quantity quantity { QuantResult = $1 + $2; }
| quantity quantity quantity { QuantResult = $1 + $2 + $3; }
;
num: NUM { $$ = $1; }
| ONE { $$ = $1; }
| num '+' num { $$ = Quantity($1.getValue() + $3.getValue()); }
| num MINUSSIGN num { $$ = Quantity($1.getValue() - $3.getValue()); }
| num '*' num { $$ = Quantity($1.getValue() * $3.getValue()); }
| num '/' num { $$ = Quantity($1.getValue() / $3.getValue()); }
| MINUSSIGN num %prec NEG { $$ = Quantity(-$2.getValue()); }
| num '^' num { $$ = Quantity(pow ($1.getValue(), $3.getValue()));}
| '(' num ')' { $$ = $2; }
| ACOS '(' num ')' { $$ = Quantity(acos($3.getValue())); }
| ASIN '(' num ')' { $$ = Quantity(asin($3.getValue())); }
| ATAN '(' num ')' { $$ = Quantity(atan($3.getValue())); }
| ABS '(' num ')' { $$ = Quantity(fabs($3.getValue())); }
| EXP '(' num ')' { $$ = Quantity(exp($3.getValue())); }
| LOG '(' num ')' { $$ = Quantity(log($3.getValue())); }
| LOG10 '(' num ')' { $$ = Quantity(log10($3.getValue())); }
| SIN '(' num ')' { $$ = Quantity(sin($3.getValue())); }
| SINH '(' num ')' { $$ = Quantity(sinh($3.getValue())); }
| TAN '(' num ')' { $$ = Quantity(tan($3.getValue())); }
| TANH '(' num ')' { $$ = Quantity(tanh($3.getValue())); }
| SQRT '(' num ')' { $$ = Quantity(sqrt($3.getValue())); }
| COS '(' num ')' { $$ = Quantity(cos($3.getValue())); }
;
unit: UNIT { $$ = $1; }
| ONE '/' unit { $$ = Quantity(1.0)/$3; }
| unit '*' unit { $$ = $1 * $3; }
| unit '/' unit { $$ = $1 / $3; }
| unit '^' num { $$ = $1.pow ($3); }
| '(' unit ')' { $$ = $2; }
;
quantity: num unit { $$ = $1*$2; }
| num '/' unit { $$ = Quantity($1)/$3; }
;
%%