lexer_input((buffer), &(bytes_read), (bytes_requested))
extern int yyerror(long long *result, double *dresult,
- int *has_error, int *bye, const char *msg);
+ int *has_error, int *units_specified, int *bye, const char *msg);
static void __attribute__((unused)) yyunput(int c,char *buf_ptr);
static int __attribute__((unused)) input(void);
yylval.v.has_error = 0;
return NUMBER;
} else {
- yyerror(0, 0, 0, 0, "bad number\n");
+ yyerror(0, 0, 0, 0, 0, "bad number\n");
yylval.v.has_error = 1;
return NUMBER;
}
yylval.v.has_error = 0;
return NUMBER;
} else {
- yyerror(0, 0, 0, 0, "bad number\n");
+ yyerror(0, 0, 0, 0, 0, "bad number\n");
yylval.v.has_error = 1;
return NUMBER;
}
yylval.v.has_error = 0;
return NUMBER;
} else {
- yyerror(0, 0, 0, 0, "bad number\n");
+ yyerror(0, 0, 0, 0, 0, "bad number\n");
yylval.v.has_error = 1;
return NUMBER;
}
int yyerror(__attribute__((unused)) long long *result,
__attribute__((unused)) double *dresult,
__attribute__((unused)) int *has_error,
+ __attribute__((unused)) int *units_specified,
__attribute__((unused)) int *bye, const char *msg);
extern int yylex(void);
%parse-param { long long *result }
%parse-param { double *dresult }
%parse-param { int *has_error }
+%parse-param { int *units_specified }
%parse-param { int *bye }
%type <v> expression
}
| expression '/' expression {
if ($3.ival == 0)
- yyerror(0, 0, 0, 0, "divide by zero");
+ yyerror(0, 0, 0, 0, 0, "divide by zero");
else
$$.ival = $1.ival / $3.ival;
if ($3.dval < 1e-20 && $3.dval > -1e-20)
- yyerror(0, 0, 0, 0, "divide by zero");
+ yyerror(0, 0, 0, 0, 0, "divide by zero");
else
$$.dval = $1.dval / $3.dval;
if ($3.has_dval || $1.has_dval)
else
$$.dval = $1.ival * $2.ival;
$$.has_error = $1.has_error || $2.has_error;
+ *units_specified = 1;
}
| expression '%' expression {
if ($1.has_dval || $3.has_dval)
- yyerror(0, 0, 0, 0, "modulo on floats");
+ yyerror(0, 0, 0, 0, 0, "modulo on floats");
if ($3.ival == 0)
- yyerror(0, 0, 0, 0, "divide by zero");
+ yyerror(0, 0, 0, 0, 0, "divide by zero");
else {
$$.ival = $1.ival % $3.ival;
$$.dval = $$.ival;
lexer_read_offset = 0;
}
-int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
+int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval,
+ double implied_units)
{
- int rc, bye = 0, has_error = 0;
+ int rc, units_specified = 0, bye = 0, has_error = 0;
setup_to_parse_string(buffer);
- rc = yyparse(ival, dval, &has_error, &bye);
+ rc = yyparse(ival, dval, &has_error, &units_specified, &bye);
yyrestart(NULL);
if (rc || bye || has_error) {
*ival = 0;
*dval = 0;
has_error = 1;
}
+ if (!units_specified) {
+ *ival = (int) ((double) *ival * implied_units);
+ *dval = *dval * implied_units;
+ }
return has_error;
}
int yyerror(__attribute__((unused)) long long *result,
__attribute__((unused)) double *dresult,
__attribute__((unused)) int *has_error,
+ __attribute__((unused)) int *units_specified,
__attribute__((unused)) int *bye,
__attribute__((unused)) const char *msg)
{
#include "../y.tab.h"
extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
- double *dval);
+ double *dval, double implied_units);
int main(int argc, char *argv[])
{
rc = strlen(buffer);
if (rc > 0 && buffer[rc - 1] == '\n')
buffer[rc - 1] = '\0';
- rc = evaluate_arithmetic_expression(buffer, &result, &dresult);
+ rc = evaluate_arithmetic_expression(buffer, &result, &dresult, 1.0);
if (!rc) {
printf("%lld (%20.20lf)\n", result, dresult);
} else {
}
extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
- double *dval);
+ double *dval, double implied_units);
#ifdef CONFIG_ARITHMETIC
/*
* original number parsing code. Once sufficiently sure that the arithmetic
* code is always getting the right answers, these can be removed.
*/
-static void verify_exp_parser_float(const char *str)
+static void verify_exp_parser_float(const char *str, double implied_units)
{
long long ival;
double dval, tmpval;
if (sscanf(str, "%lf", &tmpval) != 1)
return;
- if (evaluate_arithmetic_expression(str, &ival, &dval) != 0) {
+ if (evaluate_arithmetic_expression(str, &ival, &dval, implied_units) != 0) {
log_info("Arithmetic failed on '%s'\n", str);
return;
}
int rc;
long long ival;
double dval;
+ double implied_units = 1.0;
- rc = evaluate_arithmetic_expression(str, &ival, &dval);
+ if (is_seconds)
+ implied_units = 1000000.0;
+
+ rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units);
if (!rc) {
if (ival != val)
log_info("Arithmetic failed on '%s', expected %lld, got %lld\n",
double dval;
if (str[0] == '(') {
- rc = evaluate_arithmetic_expression(str, &ival, &dval);
+ rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0);
if (!rc) {
*val = dval;
return 1;
}
} else {
- verify_exp_parser_float(str);
+ verify_exp_parser_float(str, 1.0);
}
#endif
return 1 == sscanf(str, "%lf", val);
#ifdef CONFIG_ARITHMETIC
long long ival;
double dval;
+ double implied_units = 1.0;
#endif
len = strlen(str);
return 1;
#ifdef CONFIG_ARITHMETIC
+ if (is_seconds)
+ implied_units = 1000000.0;
if (str[0] == '(')
- rc = evaluate_arithmetic_expression(str, &ival, &dval);
+ rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units);
if (str[0] == '(' && !rc) {
if (!kilo && is_seconds)
*val = ival / 1000000LL;