From 98e6b941ef031228d2412d5c940961ab2e088f44 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Sun, 10 Jan 2016 13:28:27 +0000 Subject: [PATCH] [commandline] add an eval option Pass it through to the main equate eval engine and output result. --- src/calc.h | 4 ++++ src/calc.y | 17 ++++++++++++++++- src/calc_parse.c | 17 ++++++++++++++++- src/main.c | 15 +++++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/calc.h b/src/calc.h index cd8e190..e0ff6a6 100644 --- a/src/calc.h +++ b/src/calc.h @@ -1,11 +1,15 @@ #ifndef _EQUATE_CALC_H #define _EQUATE_CALC_H +#include + /* Functions the gui calls */ void equate_clear(void); int equate_append(char *str); double equate_eval(void); const char *equate_string_get(void); +Eina_Bool equate_ok(void); + /* Fonctions type. */ typedef double (*func_t) (double); diff --git a/src/calc.y b/src/calc.y index 5d95698..d025726 100644 --- a/src/calc.y +++ b/src/calc.y @@ -53,9 +53,12 @@ exp: NUM { $$ = $1; } %% #include "calc_lex.c" +static Eina_Bool yyerror_found; + void yyerror(const char *s) { /* Called by yyparse on error */ + yyerror_found = EINA_TRUE; printf("%s\n", s); } @@ -68,6 +71,7 @@ yyresult(void) void equate_clear(void) { + yyerror_found = EINA_FALSE; yy_scan_string("0"); yyparse(); tmp[0] = '\0'; @@ -103,7 +107,18 @@ equate_eval(void) tmp[0] = '\0'; return yyresult(); } -const char * equate_string_get(void) { return(tmp); } + +Eina_Bool +equate_ok(void) +{ + return !yyerror_found; +} + +const char * +equate_string_get(void) +{ + return(tmp); +} struct init { char *fname; diff --git a/src/calc_parse.c b/src/calc_parse.c index 88cf797..bf7bc2c 100644 --- a/src/calc_parse.c +++ b/src/calc_parse.c @@ -1264,9 +1264,12 @@ yyreturn: #include "calc_lex.c" +static Eina_Bool yyerror_found; + void yyerror(const char *s) { /* Called by yyparse on error */ + yyerror_found = EINA_TRUE; printf("%s\n", s); } @@ -1279,6 +1282,7 @@ yyresult(void) void equate_clear(void) { + yyerror_found = EINA_FALSE; yy_scan_string("0"); yyparse(); tmp[0] = '\0'; @@ -1314,7 +1318,18 @@ equate_eval(void) tmp[0] = '\0'; return yyresult(); } -const char * equate_string_get(void) { return(tmp); } + +Eina_Bool +equate_ok(void) +{ + return !yyerror_found; +} + +const char * +equate_string_get(void) +{ + return(tmp); +} struct init { char *fname; diff --git a/src/main.c b/src/main.c index 1f86b2f..ed3c94b 100644 --- a/src/main.c +++ b/src/main.c @@ -305,6 +305,8 @@ static const Ecore_Getopt optdesc = { "A calculator written with Enlightenment Foundation Libraries", EINA_TRUE, { + ECORE_GETOPT_STORE_DEF_STR('e', "eval", "Evaluate expression and print result", + NULL), // ECORE_GETOPT_LICENSE('L', "license"), ECORE_GETOPT_COPYRIGHT('C', "copyright"), ECORE_GETOPT_VERSION('V', "version"), @@ -318,8 +320,10 @@ elm_main(int argc, char **argv) { int args; Eina_Bool quit_option = EINA_FALSE; + char *eval_option = NULL; Ecore_Getopt_Value values[] = { + ECORE_GETOPT_VALUE_STR(eval_option), // ECORE_GETOPT_VALUE_BOOL(quit_option), ECORE_GETOPT_VALUE_BOOL(quit_option), ECORE_GETOPT_VALUE_BOOL(quit_option), @@ -333,6 +337,17 @@ elm_main(int argc, char **argv) EINA_LOG_CRIT("Could not parse arguments."); return 1; } + else if (eval_option) + { + double result; + + equate_append(eval_option); + result = equate_eval(); + if (equate_ok()) + printf("%.10g\n", result); + + return !equate_ok(); + } else if (quit_option) { return 0;