[gcalctool] Fix remaining variable powers
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gcalctool] Fix remaining variable powers
- Date: Thu, 18 Mar 2010 10:59:46 +0000 (UTC)
commit df3135552a16817e72159ebacc480aafc89d5778
Author: Robert Ancell <robert ancell gmail com>
Date: Thu Mar 18 21:59:14 2010 +1100
Fix remaining variable powers
src/mp-equation-lexer.l | 12 +++++++++++-
src/mp-equation-parser.y | 19 +++++++++++++------
src/unittest.c | 3 ++-
3 files changed, 26 insertions(+), 8 deletions(-)
---
diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l
index 1c660fe..1172aa0 100644
--- a/src/mp-equation-lexer.l
+++ b/src/mp-equation-lexer.l
@@ -92,7 +92,17 @@ IN [iI][nN]
{SUP_NUM} {yylval->integer = super_atoi(yytext); return tSUPNUM;}
{NSUP_NUM} {yylval->integer = super_atoi(yytext); return tNSUPNUM;}
{SUB_NUM} {yylval->integer = sub_atoi(yytext); return tSUBNUM;}
-{VARIABLE} {yylval->name = strdup(yytext); return tVARIABLE;}
+{VARIABLE} {\
+ MPEquationParserState *state = _mp_equation_get_extra(yyscanner);\
+ if (state->function_is_defined(state, yytext)) {\
+ yylval->name = strdup(yytext);\
+ return tFUNCTION;\
+ }\
+ else {\
+ yylval->name = strdup(yytext);\
+ return tVARIABLE;\
+ }\
+}
[ \t\n]
. {return *yytext;}
diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y
index dce2f23..a882695 100644
--- a/src/mp-equation-parser.y
+++ b/src/mp-equation-parser.y
@@ -153,7 +153,7 @@ static void do_conversion(yyscan_t yyscanner, const MPNumber *x, const char *x_u
%left tMULTIPLY tDIVIDE tMOD MULTIPLICATION
%left tNOT
%left tROOT tROOT3 tROOT4
-%left <name> tVARIABLE
+%left <name> tVARIABLE tFUNCTION
%right <integer> tSUBNUM tSUPNUM tNSUPNUM
%left BOOLEAN_OPERATOR
%left PERCENTAGE
@@ -161,7 +161,7 @@ static void do_conversion(yyscan_t yyscanner, const MPNumber *x, const char *x_u
%right '^' '!' '|'
%left tIN
-%type <int_t> exp variable
+%type <int_t> exp variable term
%start statement
%%
@@ -206,14 +206,21 @@ exp:
variable:
- tVARIABLE exp {if (!get_function(yyscanner, $1, &$2, &$$)) YYABORT; free($1);}
-| tVARIABLE tSUPNUM {MPNumber t; if (!get_variable(yyscanner, $1, $2, &$$)) YYABORT; free($1);}
-| tVARIABLE tSUPNUM exp {if (!get_function(yyscanner, $1, &$3, &$$)) YYABORT; mp_xpowy_integer(&$$, $2, &$$); free($1);}
+ term {mp_set_from_mp(&$1, &$$);}
+| tFUNCTION exp {if (!get_function(yyscanner, $1, &$2, &$$)) YYABORT; free($1);}
+| tFUNCTION tSUPNUM exp {if (!get_function(yyscanner, $1, &$3, &$$)) YYABORT; mp_xpowy_integer(&$$, $2, &$$); free($1);}
+| tVARIABLE exp {set_error(yyscanner, PARSER_ERR_UNKNOWN_FUNCTION, $1); free($1); YYABORT;}
+| tVARIABLE tSUPNUM exp {set_error(yyscanner, PARSER_ERR_UNKNOWN_FUNCTION, $1); free($1); YYABORT;}
| tSUBNUM tROOT exp {mp_root(&$3, $1, &$$);}
| tROOT exp {mp_sqrt(&$2, &$$);}
| tROOT3 exp {mp_root(&$2, 3, &$$);}
| tROOT4 exp {mp_root(&$2, 4, &$$);}
-| tVARIABLE {if (!get_variable(yyscanner, $1, 1, &$$)) YYABORT; free($1);}
+;
+
+term:
+ tVARIABLE {if (!get_variable(yyscanner, $1, 1, &$$)) YYABORT; free($1);}
+| tVARIABLE tSUPNUM {if (!get_variable(yyscanner, $1, $2, &$$)) YYABORT; free($1);}
+| term term {mp_multiply(&$1, &$2, &$$);}
;
%%
diff --git a/src/unittest.c b/src/unittest.c
index f7bcba0..e3eb2a6 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -229,6 +229,7 @@ test_parser()
test("z", "", PARSER_ERR_UNKNOWN_VARIABLE);
test("2y", "6", 0);
test("y2", "", PARSER_ERR_UNKNOWN_FUNCTION);
+ test("y(2)", "", PARSER_ERR_UNKNOWN_FUNCTION);
test("y²", "9", 0);
test("2y²", "18", 0);
test("xÃ?y", "6", 0);
@@ -241,7 +242,7 @@ test_parser()
test("2x²y", "24", 0);
test("2xy²", "36", 0);
test("2x²y²", "72", 0);
- test("x²yx²y", "324", 0);
+ test("x²yx²y", "144", 0);
test("Ï?", "3.141592654", 0);
test("e", "2.718281828", 0);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]