Skip to content

Commit

Permalink
support quantities in a string of the form: value / unit
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 3, 2019
1 parent adccd0d commit 0b47fb6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
88 changes: 47 additions & 41 deletions src/Base/QuantityParser.c
Expand Up @@ -396,9 +396,9 @@ union yyalloc
/* YYNNTS -- Number of nonterminals. */
#define YYNNTS 5
/* YYNRULES -- Number of rules. */
#define YYNRULES 36
#define YYNRULES 37
/* YYNSTATES -- Number of states. */
#define YYNSTATES 94
#define YYNSTATES 96

/* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned
by yylex, with out-of-bounds checking. */
Expand Down Expand Up @@ -449,7 +449,7 @@ static const yytype_uint8 yyrline[] =
0, 34, 34, 35, 36, 37, 38, 39, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
65, 66, 67, 68, 69, 70, 72
65, 66, 67, 68, 69, 70, 72, 73
};
#endif

Expand Down Expand Up @@ -492,15 +492,15 @@ static const yytype_uint16 yytoknum[] =
static const yytype_int16 yypact[] =
{
72, -26, -26, -26, 117, -25, -23, -10, -7, -5,
-4, -3, 21, 22, 23, 24, 26, 28, 72, 57,
92, -12, 117, -2, -26, 117, 31, 117, 117, 117,
-4, -3, 21, 22, 23, 24, 26, 28, 72, 58,
92, -12, 117, -2, -26, 117, 35, 117, 117, 117,
117, 117, 117, 117, 117, 117, 117, 117, 117, 117,
36, -18, -26, 37, 117, 117, 117, 117, 117, -2,
-12, -2, -2, 117, 92, 117, 39, 80, 125, 134,
141, 149, 156, 163, 171, 178, 185, 193, 200, 207,
-26, -26, -8, -8, 31, 31, 31, 39, 39, -13,
-26, -26, -26, -26, -26, -26, -26, -26, -26, -26,
-26, -26, -26, -26
36, -18, -26, 39, 117, 117, 117, 72, 117, -2,
-12, -2, -2, 117, 92, 117, 41, 117, 80, 125,
134, 141, 149, 156, 163, 171, 178, 185, 193, 200,
207, -26, -26, -8, -8, 35, 35, -12, 35, 41,
41, -13, -26, -26, -26, -26, -26, -26, -26, -26,
-26, -26, -26, -26, -26, -26
};

/* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
Expand All @@ -515,9 +515,9 @@ static const yytype_uint8 yydefact[] =
0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
36, 0, 0, 0, 0, 6, 31, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, 35, 11, 10, 12, 13, 15, 32, 33, 34,
7, 17, 18, 19, 29, 21, 20, 22, 23, 24,
25, 26, 27, 28
0, 16, 35, 11, 10, 12, 13, 37, 15, 32,
33, 34, 7, 17, 18, 19, 29, 21, 20, 22,
23, 24, 25, 26, 27, 28
};

/* YYPGOTO[NTERM-NUM]. */
Expand All @@ -538,29 +538,29 @@ static const yytype_int8 yydefgoto[] =
static const yytype_uint8 yytable[] =
{
20, 1, 43, 27, 26, 28, 51, 52, 55, 53,
45, 71, 51, 52, 48, 53, 46, 47, 29, 48,
21, 30, 54, 31, 32, 33, 49, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
50, 80, 44, 56, 72, 73, 74, 75, 76, 34,
35, 36, 37, 79, 38, 54, 39, 42, 48, 45,
46, 47, 23, 48, 0, 70, 53, 0, 0, 0,
0, 77, 78, 0, 50, 1, 2, 3, 4, 5,
45, 72, 51, 52, 48, 53, 46, 57, 29, 48,
21, 30, 54, 31, 32, 33, 49, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
50, 82, 44, 56, 73, 74, 75, 76, 78, 34,
35, 36, 37, 81, 38, 54, 39, 76, 42, 45,
46, 57, 48, 48, 23, 71, 0, 77, 53, 0,
0, 79, 80, 0, 50, 1, 2, 3, 4, 5,
6, 7, 0, 8, 9, 10, 44, 11, 12, 0,
13, 14, 15, 16, 17, 1, 43, 0, 44, 0,
18, 0, 0, 45, 46, 47, 0, 48, 0, 81,
18, 0, 0, 45, 46, 57, 0, 48, 0, 83,
0, 0, 0, 0, 0, 45, 46, 47, 0, 48,
49, 24, 3, 4, 5, 6, 7, 0, 8, 9,
10, 44, 11, 12, 0, 13, 14, 15, 16, 17,
44, 0, 0, 0, 0, 25, 0, 44, 45, 46,
47, 0, 48, 0, 82, 44, 0, 45, 46, 47,
0, 48, 44, 83, 45, 46, 47, 0, 48, 44,
84, 0, 45, 46, 47, 0, 48, 44, 85, 45,
46, 47, 0, 48, 44, 86, 45, 46, 47, 0,
48, 44, 87, 0, 45, 46, 47, 0, 48, 44,
88, 45, 46, 47, 0, 48, 44, 89, 45, 46,
47, 0, 48, 44, 90, 0, 45, 46, 47, 0,
48, 0, 91, 45, 46, 47, 0, 48, 0, 92,
45, 46, 47, 0, 48, 0, 93
57, 0, 48, 0, 84, 44, 0, 45, 46, 57,
0, 48, 44, 85, 45, 46, 57, 0, 48, 44,
86, 0, 45, 46, 57, 0, 48, 44, 87, 45,
46, 57, 0, 48, 44, 88, 45, 46, 57, 0,
48, 44, 89, 0, 45, 46, 57, 0, 48, 44,
90, 45, 46, 57, 0, 48, 44, 91, 45, 46,
57, 0, 48, 44, 92, 0, 45, 46, 57, 0,
48, 0, 93, 45, 46, 57, 0, 48, 0, 94,
45, 46, 57, 0, 48, 0, 95
};

static const yytype_int8 yycheck[] =
Expand All @@ -570,8 +570,8 @@ static const yytype_int8 yycheck[] =
0, 28, 22, 28, 28, 28, 28, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
20, 55, 6, 23, 44, 45, 46, 47, 48, 28,
28, 28, 28, 53, 28, 55, 28, 0, 27, 23,
24, 25, 25, 27, -1, 29, 27, -1, -1, -1,
28, 28, 28, 53, 28, 55, 28, 57, 0, 23,
24, 25, 27, 27, 25, 29, -1, 47, 27, -1,
-1, 51, 52, -1, 54, 3, 4, 5, 6, 7,
8, 9, -1, 11, 12, 13, 6, 15, 16, -1,
18, 19, 20, 21, 22, 3, 4, -1, 6, -1,
Expand Down Expand Up @@ -600,11 +600,11 @@ static const yytype_uint8 yystos[] =
32, 33, 34, 25, 4, 28, 32, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
32, 33, 0, 4, 6, 23, 24, 25, 27, 28,
33, 24, 25, 27, 32, 34, 33, 32, 32, 32,
33, 24, 25, 27, 32, 34, 33, 25, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
29, 29, 32, 32, 32, 32, 32, 33, 33, 32,
34, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29
32, 29, 29, 32, 32, 32, 32, 33, 32, 33,
33, 32, 34, 29, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29
};

/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
Expand All @@ -613,7 +613,7 @@ static const yytype_uint8 yyr1[] =
0, 30, 31, 31, 31, 31, 31, 31, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
33, 33, 33, 33, 33, 33, 34
33, 33, 33, 33, 33, 33, 34, 34
};

/* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */
Expand All @@ -622,7 +622,7 @@ static const yytype_uint8 yyr2[] =
0, 2, 0, 1, 1, 1, 2, 3, 1, 1,
3, 3, 3, 3, 2, 3, 3, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
1, 3, 3, 3, 3, 3, 2
1, 3, 3, 3, 3, 3, 2, 3
};


Expand Down Expand Up @@ -1508,8 +1508,14 @@ yyparse (void)
#line 1509 "QuantityParser.c" /* yacc.c:1646 */
break;

case 37:
#line 73 "QuantityParser.y" /* yacc.c:1646 */
{ (yyval) = Quantity((yyvsp[-2]))/(yyvsp[0]); }
#line 1515 "QuantityParser.c" /* yacc.c:1646 */
break;


#line 1513 "QuantityParser.c" /* yacc.c:1646 */
#line 1519 "QuantityParser.c" /* yacc.c:1646 */
default: break;
}
/* User semantic actions sometimes alter yychar, and that requires
Expand Down Expand Up @@ -1737,5 +1743,5 @@ yyparse (void)
#endif
return yyresult;
}
#line 76 "QuantityParser.y" /* yacc.c:1906 */
#line 77 "QuantityParser.y" /* yacc.c:1906 */

3 changes: 2 additions & 1 deletion src/Base/QuantityParser.y
Expand Up @@ -65,11 +65,12 @@
unit: UNIT { $$ = $1; }
| ONE '/' unit { $$ = Quantity(1.0)/$3; }
| unit '*' unit { $$ = $1 * $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; }
;


Expand Down

0 comments on commit 0b47fb6

Please sign in to comment.