summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2016-01-10 13:28:27 +0000
committerAndy Williams <andy@andywilliams.me>2016-01-10 13:28:27 +0000
commit98e6b941ef031228d2412d5c940961ab2e088f44 (patch)
tree7eb7af4c3b600249c1948de1d41c2061d747ddf9
parent7989510b4c97ae89b68589156e0ef57f7296d72b (diff)
[commandline] add an eval option
Pass it through to the main equate eval engine and output result.
-rw-r--r--src/calc.h4
-rw-r--r--src/calc.y17
-rw-r--r--src/calc_parse.c17
-rw-r--r--src/main.c15
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 @@
1#ifndef _EQUATE_CALC_H 1#ifndef _EQUATE_CALC_H
2#define _EQUATE_CALC_H 2#define _EQUATE_CALC_H
3 3
4#include <Eina.h>
5
4/* Functions the gui calls */ 6/* Functions the gui calls */
5void equate_clear(void); 7void equate_clear(void);
6int equate_append(char *str); 8int equate_append(char *str);
7double equate_eval(void); 9double equate_eval(void);
8const char *equate_string_get(void); 10const char *equate_string_get(void);
11Eina_Bool equate_ok(void);
12
9 13
10/* Fonctions type. */ 14/* Fonctions type. */
11typedef double (*func_t) (double); 15typedef 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; }
53%% 53%%
54#include "calc_lex.c" 54#include "calc_lex.c"
55 55
56static Eina_Bool yyerror_found;
57
56void 58void
57yyerror(const char *s) 59yyerror(const char *s)
58{ /* Called by yyparse on error */ 60{ /* Called by yyparse on error */
61 yyerror_found = EINA_TRUE;
59 printf("%s\n", s); 62 printf("%s\n", s);
60} 63}
61 64
@@ -68,6 +71,7 @@ yyresult(void)
68void 71void
69equate_clear(void) 72equate_clear(void)
70{ 73{
74 yyerror_found = EINA_FALSE;
71 yy_scan_string("0"); 75 yy_scan_string("0");
72 yyparse(); 76 yyparse();
73 tmp[0] = '\0'; 77 tmp[0] = '\0';
@@ -103,7 +107,18 @@ equate_eval(void)
103 tmp[0] = '\0'; 107 tmp[0] = '\0';
104 return yyresult(); 108 return yyresult();
105} 109}
106const char * equate_string_get(void) { return(tmp); } 110
111Eina_Bool
112equate_ok(void)
113{
114 return !yyerror_found;
115}
116
117const char *
118equate_string_get(void)
119{
120 return(tmp);
121}
107 122
108struct init { 123struct init {
109 char *fname; 124 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:
1264 1264
1265#include "calc_lex.c" 1265#include "calc_lex.c"
1266 1266
1267static Eina_Bool yyerror_found;
1268
1267void 1269void
1268yyerror(const char *s) 1270yyerror(const char *s)
1269{ /* Called by yyparse on error */ 1271{ /* Called by yyparse on error */
1272 yyerror_found = EINA_TRUE;
1270 printf("%s\n", s); 1273 printf("%s\n", s);
1271} 1274}
1272 1275
@@ -1279,6 +1282,7 @@ yyresult(void)
1279void 1282void
1280equate_clear(void) 1283equate_clear(void)
1281{ 1284{
1285 yyerror_found = EINA_FALSE;
1282 yy_scan_string("0"); 1286 yy_scan_string("0");
1283 yyparse(); 1287 yyparse();
1284 tmp[0] = '\0'; 1288 tmp[0] = '\0';
@@ -1314,7 +1318,18 @@ equate_eval(void)
1314 tmp[0] = '\0'; 1318 tmp[0] = '\0';
1315 return yyresult(); 1319 return yyresult();
1316} 1320}
1317const char * equate_string_get(void) { return(tmp); } 1321
1322Eina_Bool
1323equate_ok(void)
1324{
1325 return !yyerror_found;
1326}
1327
1328const char *
1329equate_string_get(void)
1330{
1331 return(tmp);
1332}
1318 1333
1319struct init { 1334struct init {
1320 char *fname; 1335 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 = {
305 "A calculator written with Enlightenment Foundation Libraries", 305 "A calculator written with Enlightenment Foundation Libraries",
306 EINA_TRUE, 306 EINA_TRUE,
307 { 307 {
308 ECORE_GETOPT_STORE_DEF_STR('e', "eval", "Evaluate expression and print result",
309 NULL),
308// ECORE_GETOPT_LICENSE('L', "license"), 310// ECORE_GETOPT_LICENSE('L', "license"),
309 ECORE_GETOPT_COPYRIGHT('C', "copyright"), 311 ECORE_GETOPT_COPYRIGHT('C', "copyright"),
310 ECORE_GETOPT_VERSION('V', "version"), 312 ECORE_GETOPT_VERSION('V', "version"),
@@ -318,8 +320,10 @@ elm_main(int argc, char **argv)
318{ 320{
319 int args; 321 int args;
320 Eina_Bool quit_option = EINA_FALSE; 322 Eina_Bool quit_option = EINA_FALSE;
323 char *eval_option = NULL;
321 324
322 Ecore_Getopt_Value values[] = { 325 Ecore_Getopt_Value values[] = {
326 ECORE_GETOPT_VALUE_STR(eval_option),
323// ECORE_GETOPT_VALUE_BOOL(quit_option), 327// ECORE_GETOPT_VALUE_BOOL(quit_option),
324 ECORE_GETOPT_VALUE_BOOL(quit_option), 328 ECORE_GETOPT_VALUE_BOOL(quit_option),
325 ECORE_GETOPT_VALUE_BOOL(quit_option), 329 ECORE_GETOPT_VALUE_BOOL(quit_option),
@@ -333,6 +337,17 @@ elm_main(int argc, char **argv)
333 EINA_LOG_CRIT("Could not parse arguments."); 337 EINA_LOG_CRIT("Could not parse arguments.");
334 return 1; 338 return 1;
335 } 339 }
340 else if (eval_option)
341 {
342 double result;
343
344 equate_append(eval_option);
345 result = equate_eval();
346 if (equate_ok())
347 printf("%.10g\n", result);
348
349 return !equate_ok();
350 }
336 else if (quit_option) 351 else if (quit_option)
337 { 352 {
338 return 0; 353 return 0;