[commandline] add an eval option

Pass it through to the main equate eval engine and output result.
This commit is contained in:
Andy Williams 2016-01-10 13:28:27 +00:00
parent 7989510b4c
commit 98e6b941ef
4 changed files with 51 additions and 2 deletions

View File

@ -1,11 +1,15 @@
#ifndef _EQUATE_CALC_H
#define _EQUATE_CALC_H
#include <Eina.h>
/* 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);

View File

@ -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;

View File

@ -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;

View File

@ -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;