fio: allow arithmetic expressions to be used in job files
[fio.git] / exp / expression-parser.y
CommitLineData
b470a02c
SC
1%{
2
3/*
4 * (C) Copyright 2014, Stephen M. Cameron.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21#include <stdio.h>
22#include <string.h>
23
24struct parser_value_type {
25 double dval;
26 long long ival;
27 int has_dval;
28 int has_error;
29};
30
31typedef union valtype {
32 struct parser_value_type v;
33} PARSER_VALUE_TYPE;
34
35#define YYSTYPE PARSER_VALUE_TYPE
36
37int yyerror(__attribute__((unused)) long long *result,
38 __attribute__((unused)) double *dresult,
39 __attribute__((unused)) int *has_error,
40 __attribute__((unused)) int *bye, const char *msg);
41
42extern int yylex(void);
43extern void yyrestart(FILE *file);
44
45%}
46
47%union valtype {
48 struct parser_value_type {
49 double dval;
50 long long ival;
51 int has_dval;
52 int has_error;
53 } v;
54};
55
56%token <v> NUMBER
57%token <v> BYE
58%left '-' '+'
59%left '*' '/'
60%nonassoc UMINUS
61%parse-param { long long *result }
62%parse-param { double *dresult }
63%parse-param { int *has_error }
64%parse-param { int *bye }
65
66%type <v> expression
67%%
68
69top_level: expression {
70 *result = $1.ival;
71 *dresult = $1.dval;
72 *has_error = $1.has_error;
73 }
74 | expression error {
75 *result = $1.ival;
76 *dresult = $1.dval;
77 *has_error = 1;
78 }
79expression: expression '+' expression {
80 if (!$1.has_dval && !$3.has_dval)
81 $$.ival = $1.ival + $3.ival;
82 else
83 $$.ival = (long long) ($1.dval + $3.dval);
84 $$.dval = $1.dval + $3.dval;
85 $$.has_error = $1.has_error || $3.has_error;
86 }
87 | expression '-' expression {
88 if (!$1.has_dval && !$3.has_dval)
89 $$.ival = $1.ival - $3.ival;
90 else
91 $$.ival = (long long) ($1.dval - $3.dval);
92 $$.dval = $1.dval - $3.dval;
93 $$.has_error = $1.has_error || $3.has_error;
94 }
95 | expression '*' expression {
96 if (!$1.has_dval && !$3.has_dval)
97 $$.ival = $1.ival * $3.ival;
98 else
99 $$.ival = (long long) ($1.dval * $3.dval);
100 $$.dval = $1.dval * $3.dval;
101 $$.has_error = $1.has_error || $3.has_error;
102 }
103 | expression '/' expression {
104 if ($3.ival == 0)
105 yyerror(0, 0, 0, 0, "divide by zero");
106 else
107 $$.ival = $1.ival / $3.ival;
108 if ($3.dval < 1e-20 && $3.dval > -1e-20)
109 yyerror(0, 0, 0, 0, "divide by zero");
110 else
111 $$.dval = $1.dval / $3.dval;
112 if ($3.has_dval || $1.has_dval)
113 $$.ival = (long long) $$.dval;
114 $$.has_error = $1.has_error || $3.has_error;
115 }
116 | '-' expression %prec UMINUS {
117 $$.ival = -$2.ival;
118 $$.dval = -$2.dval;
119 $$.has_error = $2.has_error;
120 }
121 | '(' expression ')' { $$ = $2; }
122 | NUMBER { $$ = $1; }
123 | BYE { $$ = $1; *bye = 1; };
124%%
125#include <stdio.h>
126
127/* Urgh. yacc and lex are kind of horrible. This is not thread safe, obviously. */
128static int lexer_read_offset = 0;
129static char lexer_input_buffer[1000];
130
131int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
132{
133 int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
134
135 if (bytes_requested > bytes_left )
136 bytes_requested = bytes_left;
137 memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
138 *bytes_read = bytes_requested;
139 lexer_read_offset += bytes_requested;
140 return 0;
141}
142
143static void setup_to_parse_string(const char *string)
144{
145 unsigned int len;
146
147 len = strlen(string);
148 if (len > sizeof(lexer_input_buffer) - 3)
149 len = sizeof(lexer_input_buffer) - 3;
150
151 strncpy(lexer_input_buffer, string, len);
152 lexer_input_buffer[len] = '\0';
153 lexer_input_buffer[len + 1] = '\0'; /* lex/yacc want string double null terminated! */
154 lexer_read_offset = 0;
155}
156
157int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
158{
159 int rc, bye = 0, has_error = 0;
160
161 setup_to_parse_string(buffer);
162 rc = yyparse(ival, dval, &has_error, &bye);
163 yyrestart(NULL);
164 if (rc || bye || has_error) {
165 *ival = 0;
166 *dval = 0;
167 has_error = 1;
168 }
169 return has_error;
170}
171
172int yyerror(__attribute__((unused)) long long *result,
173 __attribute__((unused)) double *dresult,
174 __attribute__((unused)) int *has_error,
175 __attribute__((unused)) int *bye, const char *msg)
176{
177 fprintf(stderr, "%s\n", msg);
178 return 0;
179}
180