[gcalctool/gcalctool-newui] Fix up some new behaviour



commit 45b0f168a7613cb3affee8f7512ab9f63b811c06
Author: Robert Ancell <robert ancell gmail com>
Date:   Sun May 31 11:55:22 2009 +0200

    Fix up some new behaviour
---
 src/display.c            |   33 ++-------------------------------
 src/mp-equation-lexer.l  |   10 +++++++++-
 src/mp-equation-parser.y |   23 ++++++++++++++++-------
 src/unittest.c           |    4 +++-
 4 files changed, 30 insertions(+), 40 deletions(-)

diff --git a/src/display.c b/src/display.c
index bda956b..85a3804 100644
--- a/src/display.c
+++ b/src/display.c
@@ -638,40 +638,11 @@ display_solve(GCDisplay *display)
     const char *text;
     int error;
     MPNumber result;
-    const char *message = NULL, *c;
-    char *o, *clean_text, result_text[MAXLINE];
+    const char *message = NULL;
+    char result_text[MAXLINE];
     
     text = display_get_text(display);
-    clean_text = malloc(sizeof(char) * (strlen(text) + 1 + 100)); // FIXME: Needs to be growable e.g. Sqrt
-    for(c = text, o = clean_text; *c; c++) {
-        struct substitution
-        {
-            const char *a, *b;
-        } *s;
-        static struct substitution substitutions[] =
-        {
-            {"µ", "u"},
-            {NULL, NULL}
-        };
-        
-        for(s = substitutions; s->a != NULL; s++) {
-            if(strncmp(c, s->a, strlen(s->a)) == 0) {
-                strcpy(o, s->b);
-                c += strlen(s->a) - 1;
-                o += strlen(s->b);
-                break;
-            }
-        }
-        if(s->a == NULL) {
-            //assert((*c & 0x80) == 0);
-            *o = *c;
-            o++;
-        }
-    }
-    *o = '\0';
-    
     error = mp_equation_parse(text, &result);
-    free(clean_text);
     
     switch (error) {
     case 0:
diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l
index 0938973..ab6b5a8 100644
--- a/src/mp-equation-lexer.l
+++ b/src/mp-equation-lexer.l
@@ -56,6 +56,15 @@ BIN_NUM{BIN}+|{BIN}*{DECIMAL}{BIN}*
 "-"|"â??" {return tSUBTRACT;}
 "*"|"Ã?" {return tMULTIPLY;}
 "/"|"÷" {return tDIVIDE;}
+"m" {return tMILLI;}
+"µ"|"u" {return tMICRO;}
+"n" {return tNANO;}
+"p" {return tPICO;}
+"f" {return tFEMTO;}
+"k" {return tKILO;}
+"M" {return tMEGA;}
+"G" {return tGIGA;}
+"T" {return tTERA;}
 "abs"|"Abs"|"ABS" {return tABS_FUNC;}
 "|" {return tABS;}
 "acosh"|"Acosh"|"ACOSH" {return tACOSH;}
@@ -103,7 +112,6 @@ yylval->integer = atoi(yytext+1);
 return tREG;
 }
 
-
 {DEC_NUM}{EXP}{DEC_NUM} {
 if (_mp_equation_get_extra(yyscanner)->base == 16) REJECT;
 if (strlen(yytext) > MAX_DIGITS) yyextra->error = -PARSER_ERR_TOO_LONG_NUMBER;
diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y
index 937ad95..07dabee 100644
--- a/src/mp-equation-parser.y
+++ b/src/mp-equation-parser.y
@@ -49,6 +49,15 @@
 %token tSUBTRACT
 %token tMULTIPLY
 %token tDIVIDE
+%token tMILLI
+%token tMICRO
+%token tNANO
+%token tPICO
+%token tFEMTO
+%token tKILO
+%token tMEGA
+%token tGIGA
+%token tTERA
 %token tABS
 %token tABS_FUNC
 %token tACOS
@@ -151,9 +160,9 @@ value:
 exp: 
   term {mp_set_from_mp(&$1, &$$);}
   
-| exp tROOT exp {MPNumber t; mp_sqrt(&$3, &t); mp_multiply(&$1, &t, &$$);}
-| exp tROOT3 exp {MPNumber t; mp_root(&$3, 3, &t); mp_multiply(&$1, &t, &$$);}
-| exp tROOT4 exp {MPNumber t; mp_root(&$3, 4, &t); mp_multiply(&$1, &t, &$$);}
+| exp tROOT term {MPNumber t; mp_sqrt(&$3, &t); mp_multiply(&$1, &t, &$$);}
+| exp tROOT3 term {MPNumber t; mp_root(&$3, 3, &t); mp_multiply(&$1, &t, &$$);}
+| exp tROOT4 term {MPNumber t; mp_root(&$3, 4, &t); mp_multiply(&$1, &t, &$$);}
 
 | exp tADD exp {mp_add(&$1, &$3, &$$);}
 | exp tSUBTRACT exp {mp_subtract(&$1, &$3, &$$);}
@@ -198,12 +207,12 @@ exp:
 term:
   number {mp_set_from_mp(&$1, &$$);}
 | rcl {mp_set_from_mp(&$1, &$$);}
-| tROOT exp {mp_sqrt(&$2, &$$);}
-| tROOT3 exp {mp_root(&$2, 3, &$$);}
-| tROOT4 exp {mp_root(&$2, 4, &$$);}
+| tROOT term {mp_sqrt(&$2, &$$);}
+| tROOT3 term {mp_root(&$2, 3, &$$);}
+| tROOT4 term {mp_root(&$2, 4, &$$);}
 | term tDIVIDE term {mp_divide(&$1, &$3, &$$);}
 | term tMULTIPLY term {mp_multiply(&$1, &$3, &$$);}
-| tABS term tABS {mp_abs(&$2, &$$);} 
+| tABS exp tABS {mp_abs(&$2, &$$);} 
 | 'e' '^' term {mp_epowy(&$3, &$$);} 
 | term '!' {mp_factorial(&$1, &$$);}
 | term '%' {mp_divide_integer(&$1, 100, &$$);}
diff --git a/src/unittest.c b/src/unittest.c
index ccf3621..dc31b8c 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -173,6 +173,7 @@ test_parser()
     test("4^(3^2)", "262144", 0);    
     test("(4^3)^2", "4096", 0);
     test("â??4", "2", 0);
+    test("â??4-2", "0", 0);    
     test("â??8", "2", 0);
     test("â??16", "2", 0);
     test("â??(2+2)", "2", 0);
@@ -197,7 +198,8 @@ test_parser()
     test("Frac(-3.2)", "-0.2", 0);
 
     test("|1|", "1", 0);
-    test("|-1|", "1", 0);    
+    test("|-1|", "1", 0);
+    test("|3-5|", "2", 0);    
     test("Abs(1)", "1", 0);
     test("Abs(-1)", "1", 0);
     



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]