[gcalctool] Make CE parser part of MP library



commit b6ccfca5090268283ec86f26c6a6595487f79b61
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon May 11 11:31:54 2009 +1000

    Make CE parser part of MP library
---
 src/Makefile.am                                 |   47 ++++++------
 src/calctool.c                                  |    2 +-
 src/display.c                                   |    2 +-
 src/functions.c                                 |    2 +-
 src/gtk.c                                       |    2 +-
 src/{ce_parser.y => mp-equation-parser.y}       |   32 ++++----
 src/{ce_tokeniser.l => mp-equation-tokeniser.l} |    8 +-
 src/{ce_parser.c => mp-equation.c}              |    4 +-
 src/{ce_parser.h => mp-equation.h}              |   57 ++++++++------
 src/parser.c                                    |   91 -----------------------
 src/parser.h                                    |   49 ------------
 src/parser_mac.h                                |   36 ---------
 src/unittest.c                                  |    2 +-
 13 files changed, 79 insertions(+), 255 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 5a43285..ef757fa 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,25 +30,22 @@ gcalctool_SOURCES = \
 	mp-convert.c \
 	mp-internal.h \
 	mp-trigonometric.c \
+	mp-equation.c \
+	mp-equation.h \
+	mp-equation.tab.h \	
 	financial.c \
 	financial.h \
-	parser.c \
-	parser.h \
-	parser_mac.h \
 	register.c \
 	register.h \
 	gtk.c \
 	ui.h \
 	unittest.c \
-	unittest.h \
-	ce_parser.c \
-	ce_parser.h \
-	ce_parser.tab.h
+	unittest.h
 	
 CLEANFILES = \
-	ce_parser.tab.h \
-	ce_parser.tab.c \
-	lex.ce.c \
+	mp-equation-parser.tab.h \
+	mp-equation-parser.tab.c \
+	mp-equation-lexer.c \
 	libparser.a
 
 gcalctool_LDADD = \
@@ -58,26 +55,26 @@ gcalctool_LDADD = \
 	libparser.a
 
 libparser.a: \
-	lex.ce.o\
-	ce_parser.tab.o\
-	ce_parser.o
+	mp-equation-lexer.o\
+	mp-equation-parser.tab.o\
+	mp-equation.o
 	$(AR) r libparser.a $^
 	$(RANLIB) libparser.a
 
-ce_parser.o: ce_parser.c ce_parser.h calctool.h
-	$(COMPILE) -c $(INCLUDES) -o $@ $(srcdir)/ce_parser.c
+mp-equation.o: mp-equation.c mp-equation.h calctool.h
+	$(COMPILE) -c $(INCLUDES) -o $@ $(srcdir)/mp-equation.c
 
-lex.ce.o: lex.ce.c calctool.h
-	$(COMPILE) -c $(INCLUDES) -o $@ lex.ce.c
+mp-equation-lexer.o: mp-equation-lexer.c calctool.h
+	$(COMPILE) -c $(INCLUDES) -o $@ mp-equation-lexer.c
 
-ce_parser.tab.o: ce_parser.tab.c
-	$(COMPILE) -c $(INCLUDES) -o $@ ce_parser.tab.c
+mp-equation-parser.tab.o: mp-equation-parser.tab.c
+	$(COMPILE) -c $(INCLUDES) -o $@ mp-equation-parser.tab.c
 
-ce_parser.tab.c: ce_parser.y calctool.h
-	bison -d -p ce -d $(srcdir)/ce_parser.y
+mp-equation-parser.tab.c: mp-equation-parser.y calctool.h
+	bison -d -p ce -d $(srcdir)/mp-equation-parser.y
 
-lex.ce.c: ce_parser.tab.c ce_tokeniser.l
-	$(LEX) -Pce $(srcdir)/ce_tokeniser.l
+mp-equation-lexer.c: mp-equation-parser.tab.c mp-equation-tokeniser.l
+	$(LEX) -Pce -o $@ $(srcdir)/mp-equation-tokeniser.l
 
 code-format: 
 	ls *[ch] | xargs indent -nbad -bap -bbo -nbc -br\
@@ -96,8 +93,8 @@ uninstall-local:
 	&& rm -f "$(DESTDIR)$(bindir)/gnome-calculator"
 
 EXTRA_DIST = \
-	ce_parser.y \
-	ce_tokeniser.l
+	mp-equation-parser.y \
+	mp-equation-tokeniser.l
 
 test: gcalctool
 	./gcalctool -u
diff --git a/src/calctool.c b/src/calctool.c
index af346e8..ee72b16 100644
--- a/src/calctool.c
+++ b/src/calctool.c
@@ -35,7 +35,7 @@
 #include "ui.h"
 #include "mp.h"
 #include "register.h"
-#include "ce_parser.h"
+#include "mp-equation.h"
 
 time_t time();
 
diff --git a/src/display.c b/src/display.c
index 108ea96..11de36a 100644
--- a/src/display.c
+++ b/src/display.c
@@ -30,7 +30,7 @@
 #include "mp.h"
 #include "functions.h"
 #include "ui.h"
-#include "ce_parser.h" // For ce_parse()
+#include "mp-equation.h" // For ce_parse()
 #include "register.h"
 
 static const char *display_types[] = { "ENG", "FIX", "SCI", NULL };
diff --git a/src/functions.c b/src/functions.c
index e889aa4..f15cd9e 100644
--- a/src/functions.c
+++ b/src/functions.c
@@ -33,7 +33,7 @@
 #include "register.h"
 #include "mp.h"
 #include "display.h"
-#include "ce_parser.h"
+#include "mp-equation.h"
 #include "ui.h"
 
 typedef enum {
diff --git a/src/gtk.c b/src/gtk.c
index 7635b3e..ffe4cb5 100644
--- a/src/gtk.c
+++ b/src/gtk.c
@@ -38,7 +38,7 @@
 #include "dsdefs.h"
 #include "functions.h"
 #include "financial.h"
-#include "ce_parser.h"
+#include "mp-equation.h"
 #include "display.h"
 #include "get.h"
 #include "register.h"
diff --git a/src/ce_parser.y b/src/mp-equation-parser.y
similarity index 90%
rename from src/ce_parser.y
rename to src/mp-equation-parser.y
index 7e0294c..114a5fa 100644
--- a/src/ce_parser.y
+++ b/src/mp-equation-parser.y
@@ -24,12 +24,10 @@
 #include <stdlib.h> 
 #include <math.h>
 #include <errno.h>
+
 #include "calctool.h"
 #include "register.h"
-#include "display.h"
-#include "parser.h"
-#include "parser_mac.h"
-#include "ce_parser.h"
+#include "mp-equation.h"
 
 %}
 
@@ -99,7 +97,7 @@
 
 statement: 
   seq
-| value {ret(&$1);}
+| value { mp_set_from_mp(&$1, &parser_state.ret); parser_state.flags |= ANS; }
 | error {
   yyclearin; 
   reset_ce_tokeniser();
@@ -131,12 +129,12 @@ udf:
 ;
 
 value: 
-  exp {cp(&$1, &$$);}
+  exp {mp_set_from_mp(&$1, &$$);}
 | tPI %prec HIGH {mp_get_pi(&$$);} 
 ;
 
 exp: 
-  term {cp(&$1, &$$);}
+  term {mp_set_from_mp(&$1, &$$);}
 
 | exp '+' exp {mp_add(&$1, &$3, &$$);}
 | exp '-' exp {mp_subtract(&$1, &$3, &$$);}
@@ -179,8 +177,8 @@ exp:
 
 
 term:
-  number {cp(&$1, &$$);}
-| rcl {cp(&$1, &$$);}
+  number {mp_set_from_mp(&$1, &$$);}
+| rcl {mp_set_from_mp(&$1, &$$);}
 | term '/' term {mp_divide(&$1, &$3, &$$);}
 | term '*' term {mp_multiply(&$1, &$3, &$$);}
 | 'e' '^' term {mp_epowy(&$3, &$$);} 
@@ -195,17 +193,17 @@ term:
     mp_not(&$2, v->wordlen, &$$);
 }
 | '-' term %prec NEG {mp_invert_sign(&$2, &$$);}
-| '+' term %prec POS {cp(&$2, &$$);}
+| '+' term %prec POS {mp_set_from_mp(&$2, &$$);}
 | term '^' term {mp_xpowy(&$1, &$3, &$$);}
 
-| func {cp(&$1, &$$);}
-| reg {cp(&$1, &$$);}
+| func {mp_set_from_mp(&$1, &$$);}
+| reg {mp_set_from_mp(&$1, &$$);}
 
-| parenthesis {cp(&$1, &$$);}
+| parenthesis {mp_set_from_mp(&$1, &$$);}
 ;
 
 parenthesis:
-  '(' exp ')' {cp(&$2, &$$);}
+  '(' exp ')' {mp_set_from_mp(&$2, &$$);}
   ;
 
 reg: 
@@ -263,9 +261,9 @@ rcl:
   ;
 
 number:
-  tNUMBER {cp(&$1, &$$);}
+  tNUMBER {mp_set_from_mp(&$1, &$$);}
 | tANS {
-  cp(display_get_answer(&v->display), &$$);
+  mp_set_from_mp(display_get_answer(&v->display), &$$);
 }
 ;
 
@@ -278,7 +276,7 @@ int ceerror(char *s)
 
 #if 0
 
-| '(' lexp ')' {cp(&$2, &$$);}
+| '(' lexp ')' {mp_set_from_mp(&$2, &$$);}
 
 | term term {mp_multiply(&$1, &$2, &$$);}
 
diff --git a/src/ce_tokeniser.l b/src/mp-equation-tokeniser.l
similarity index 96%
rename from src/ce_tokeniser.l
rename to src/mp-equation-tokeniser.l
index 5cdd237..e568280 100644
--- a/src/ce_tokeniser.l
+++ b/src/mp-equation-tokeniser.l
@@ -26,12 +26,10 @@
 #include <locale.h>
 #include <string.h>
 #include <sys/types.h>
+
 #include "calctool.h"
-#include "display.h"
-#include "mp.h"
-#include "functions.h" /* FIXME: Needed for gc_strdup() */
-#include "ce_parser.h"
-#include "ce_parser.tab.h"
+#include "mp-equation.h"
+#include "mp-equation-parser.tab.h"
 %}
 
 %option noinput
diff --git a/src/ce_parser.c b/src/mp-equation.c
similarity index 95%
rename from src/ce_parser.c
rename to src/mp-equation.c
index e4a0e8e..bc4a00a 100644
--- a/src/ce_parser.c
+++ b/src/mp-equation.c
@@ -19,11 +19,11 @@
  *  02111-1307, USA.
  */
 
-#include "ce_parser.h"
+#include "mp-equation.h"
 #include "limits.h"
 #include "calctool.h"
 
-/* TODO: This file is almost identical to lr-parser. */
+struct parser_state parser_state;
 
 int 
 ce_parse_(const char *expression, MPNumber *result, int flags)
diff --git a/src/ce_parser.h b/src/mp-equation.h
similarity index 65%
rename from src/ce_parser.h
rename to src/mp-equation.h
index e07f59a..f369b3a 100644
--- a/src/ce_parser.h
+++ b/src/mp-equation.h
@@ -28,39 +28,46 @@
 #include <math.h>
 #include <assert.h>
 #include <errno.h>
-#include "calctool.h"
-#include "parser.h"
-#include "parser_mac.h"
 
-/* CE = Common expression */
+#include "mp.h"
 
-extern struct parser_state parser_state;
+#define PARSER_MIN(a, b) (a < b) ? a : b;
 
-int celex();
-int ceerror();                   /* dummy definition TODO: this is a douple */
-int ceparse();                   /* dummy definition. */
-int ceerror(char *s);
-int ce_parse(const char *expression, MPNumber *result);
-int ce_udf_parse(const char *expression);
+#define YY_INPUT(buf, result, max) {\
+    int l = strlen(parser_state.buff);\
+    int remaining = l - parser_state.i;\
+    int c = PARSER_MIN(remaining, max);\
+    memcpy(buf, parser_state.buff + parser_state.i, c);\
+    parser_state.i += c;\
+    result = (c) ? c : YY_NULL;\
+}
 
-void reset_ce_tokeniser();
+#define ANS 1
 
-/* UTILITY NEEDED BECAUSE GNU SOURCE NOT ALWAYS AVAILABLE. */
+#define PARSER_ERR_INVALID_BASE     10000
+#define PARSER_ERR_TOO_LONG_NUMBER  10001
+#define PARSER_ERR_BITWISEOP        10002
+#define PARSER_ERR_MODULUSOP        10003
+#define PARSER_ERR_OVERFLOW         10004
 
-static inline char *
-ce_strndup(char *str, int len)
-{
-    char *dup;
+struct parser_state {
+    int flags;
+    char *buff;
+    int i;
+    int error;
+    MPNumber ret;
+    int ncount;
+};
 
-    if (len < 1) {
-        return(NULL); 
-    }
+extern struct parser_state parser_state;
 
-    dup = malloc(len+1);
-    memset(dup, 0, len+1);
-    strncpy(dup, str, len);
+int ce_parse(const char *expression, MPNumber *result);
+int ce_udf_parse(const char *expression);
 
-    return(dup);
-}
+int celex();
+int ceerror();                   /* dummy definition TODO: this is a douple */
+int ceparse();                   /* dummy definition. */
+int ceerror(char *s);
+void reset_ce_tokeniser();
 
 #endif
diff --git a/src/parser.c b/src/parser.c
deleted file mode 100644
index 3e0e902..0000000
--- a/src/parser.c
+++ /dev/null
@@ -1,91 +0,0 @@
-
-/*  $Header$
- *
- *  Copyright (C) 2004-2008 Sami Pietila
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2, or (at your option)
- *  any later version.
- *           
- *  This program is distributed in the hope that it will be useful, but 
- *  WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
- *  General Public License for more details.
- *           
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- *  02111-1307, USA.
- */
-
-#include <assert.h>
-#include <string.h>
-#include <errno.h>
-#include <ctype.h>
-
-#include "parser.h"
-#include "calctool.h"
-
-struct parser_state parser_state;
-
-
-void 
-cp(const MPNumber *s, MPNumber *t)
-{
-    mp_set_from_mp(s, t);
-}
-
-
-void 
-ret(const MPNumber *s)     /* Copy result value. */
-{
-    mp_set_from_mp(s, &parser_state.ret);
-    parser_state.flags |= ANS;
-}
-
-void
-check_numbase(char *num)
-{
-    int i;
-
-    for (i = 0; num[i]; i++) {
-	char l = num[i];
-
-	if (l == '.' || 
-	    l == ',' || 
-	    l == 'e' || 
-	    l == '+' || 
-	    l == ' ') continue;
-
-	switch (v->base) {
-	case BIN:
-	    if ((l < '0') || (l > '1')) {
-		 parser_state.error = -PARSER_ERR_INVALID_BASE;
-		 return;		
-	    }
-	    break;
-	case OCT:
-	    if ((l < '0') || (l > '7')) {
-		 parser_state.error = -PARSER_ERR_INVALID_BASE;
-		 return;		
-	    }
-	    break;
-	case DEC:
-	    if (!isdigit(l)) {
-		 parser_state.error = -PARSER_ERR_INVALID_BASE;
-		 return;		
-	    }
-
-	    break;
-	case HEX:
-	    if (!isxdigit(l)) {
-		 parser_state.error = -PARSER_ERR_INVALID_BASE;
-		 return;		
-	    }
-	    break;
-	default:
-	    assert(0);      /* unknown base. */
-	}
-    }
-}
diff --git a/src/parser.h b/src/parser.h
deleted file mode 100644
index 019e0e7..0000000
--- a/src/parser.h
+++ /dev/null
@@ -1,49 +0,0 @@
-
-/*  $Header$
- *
- *  Copyright (C) 2004-2008 Sami Pietila
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2, or (at your option)
- *  any later version.
- *           
- *  This program is distributed in the hope that it will be useful, but 
- *  WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
- *  General Public License for more details.
- *           
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- *  02111-1307, USA.
- */
-
-#ifndef PARSER_H
-#define PARSER_H
-
-#include "mp.h"
-
-#define ANS 1
-
-#define PARSER_ERR_INVALID_BASE			10000
-#define PARSER_ERR_TOO_LONG_NUMBER 		10001
-#define PARSER_ERR_BITWISEOP		    	10002
-#define PARSER_ERR_MODULUSOP		    	10003
-#define PARSER_ERR_OVERFLOW			10004
-
-struct parser_state {
-    int flags;
-    char *buff;
-    int i;
-    int error;
-    MPNumber ret;
-    int ncount;
-};
-
-void cp(const MPNumber *s, MPNumber *t);
-void ret(const MPNumber *s);
-
-void check_numbase(char *num);
-
-#endif /*PARSER_H*/
diff --git a/src/parser_mac.h b/src/parser_mac.h
deleted file mode 100644
index 84cf159..0000000
--- a/src/parser_mac.h
+++ /dev/null
@@ -1,36 +0,0 @@
-
-/*  $Header$
- *
- *  Copyright (C) 2004-2008 Sami Pietila
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2, or (at your option)
- *  any later version.
- *
- *  This program is distributed in the hope that it will be useful, but
- *  WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *  General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- *  02111-1307, USA.
- */
-
-#ifndef PARSER_MAC_H
-#define PARSER_MAC_H
-
-#define PARSER_MIN(a, b) (a < b) ? a : b;
-
-#define YY_INPUT(buf, result, max) {\
-    int l = strlen(parser_state.buff);\
-    int remaining = l - parser_state.i;\
-    int c = PARSER_MIN(remaining, max);\
-    memcpy(buf, parser_state.buff + parser_state.i, c);\
-    parser_state.i += c;\
-    result = (c) ? c : YY_NULL;\
-}
-
-#endif /*PARSER_MAC_H*/
diff --git a/src/unittest.c b/src/unittest.c
index a01721e..9e82f7b 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -27,7 +27,7 @@
 #include "display.h"
 #include "functions.h"
 #include "calctool.h"
-#include "ce_parser.h"
+#include "mp-equation.h"
 
 static int fails = 0;
 



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