[gcalctool/gcalctool-newui2] ...
- From: Robert Ancell <rancell src gnome org>
- To: svn-commits-list gnome org
- Subject: [gcalctool/gcalctool-newui2] ...
- Date: Tue, 30 Jun 2009 01:44:22 +0000 (UTC)
commit d500d031a11be3e0101979c275b76ae19f5e2666
Author: Robert Ancell <robert ancell gmail com>
Date: Tue Jun 30 11:28:36 2009 +1000
...
data/gcalctool.ui | 9 ++++---
src/gtk.c | 14 +++++++----
src/mp-binary.c | 2 +
src/mp-equation-lexer.l | 14 +++-------
src/mp-equation-parser.y | 60 ++++++++++------------------------------------
src/mp-equation.c | 26 +++++++------------
src/mp-equation.h | 5 ++-
src/unittest.c | 4 +++
8 files changed, 50 insertions(+), 84 deletions(-)
---
diff --git a/data/gcalctool.ui b/data/gcalctool.ui
index c22f40b..2be22e4 100644
--- a/data/gcalctool.ui
+++ b/data/gcalctool.ui
@@ -302,7 +302,8 @@
</object>
</child>
<signal name="populate_popup" handler="buffer_populate_popup_cb"/>
- <signal name="button_release_event" handler="mouse_button_cb"/>
+ <signal name="button_release_event" handler="middle_click_paste_cb"/>
+ <signal name="paste_clipboard" handler="paste_cb"/>
</object>
</child>
</object>
@@ -3042,7 +3043,7 @@
<property name="use_underline">True</property>
<property name="focus_on_click">False</property>
<child internal-child="accessible">
- <object class="AtkObject" id="calc_fractional_portion_button1-atkobject">
+ <object class="AtkObject" id="calc_fractional_portion_button-atkobject">
<property name="AtkObject::accessible-name" translatable="yes">Integer portion</property>
</object>
</child>
@@ -3089,6 +3090,7 @@
<property name="receives_default">True</property>
<property name="border_width">3</property>
<property name="focus_on_click">False</property>
+ <signal name="clicked" handler="set_subscript_cb"/>
<child>
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
@@ -3097,7 +3099,6 @@
<property name="use_markup">True</property>
</object>
</child>
- <signal name="clicked" handler="set_subscript_cb"/>
</object>
<packing>
<property name="left_attach">3</property>
@@ -3115,6 +3116,7 @@
<property name="receives_default">True</property>
<property name="border_width">3</property>
<property name="focus_on_click">False</property>
+ <signal name="clicked" handler="set_superscript_cb"/>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
@@ -3122,7 +3124,6 @@
<property name="use_markup">True</property>
</object>
</child>
- <signal name="clicked" handler="set_superscript_cb"/>
</object>
<packing>
<property name="left_attach">4</property>
diff --git a/src/gtk.c b/src/gtk.c
index 7454287..60a7a50 100644
--- a/src/gtk.c
+++ b/src/gtk.c
@@ -1413,14 +1413,18 @@ main_window_key_press_cb(GtkWidget *widget, GdkEventKey *event)
/* Clear display */
if ((event->keyval == GDK_Escape && state == 0) ||
(event->keyval == GDK_BackSpace && state == GDK_CONTROL_MASK) ||
- (event->keyval == GDK_Delete && state == GDK_SHIFT_MASK))
- {
+ (event->keyval == GDK_Delete && state == GDK_SHIFT_MASK)) {
do_button(FN_CLEAR, 0);
return TRUE;
}
- // FIXME: Give focus to entry if it loses focus but something typed?
-
+ /* Solve */
+ if ((event->keyval == GDK_Return && state == 0) ||
+ (event->keyval == GDK_KP_Enter && state == 0)) {
+ do_button(FN_CALCULATE, 0);
+ return TRUE;
+ }
+
for (c = allowed_input; *c; c++) {
// FIXME: event->string deprecated
if (strncmp(event->string, c, 1) == 0) {
@@ -1491,7 +1495,7 @@ on_paste(GtkClipboard *clipboard, const gchar *text, gpointer data)
G_MODULE_EXPORT
gboolean
-mouse_button_cb(GtkWidget *widget, GdkEventButton *event)
+middle_click_paste_cb(GtkWidget *widget, GdkEventButton *event)
{
if (event->button == 2)
gtk_clipboard_request_text(gtk_clipboard_get(X.primary_atom),
diff --git a/src/mp-binary.c b/src/mp-binary.c
index 153a4bd..8e36fd0 100644
--- a/src/mp-binary.c
+++ b/src/mp-binary.c
@@ -1,3 +1,5 @@
+#include <stdio.h>
+
#include "mp.h"
#include "mp-internal.h"
#include "calctool.h" // FIXME: Used for MAX_DIGITS
diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l
index 89e5bb8..9a7be18 100644
--- a/src/mp-equation-lexer.l
+++ b/src/mp-equation-lexer.l
@@ -33,7 +33,6 @@
#include <string.h>
#include <sys/types.h>
-#include "calctool.h"
#include "mp-equation.h"
#include "mp-equation-parser.h"
@@ -88,6 +87,7 @@ OCT_NUM {OCT}+{OCT_SUFFIX}|{OCT}*{DECIMAL}{OCT}+{OCT_SUFFIX}
BIN_NUM {BIN}+{BIN_SUFFIX}|{BIN}*{DECIMAL}{BIN}+{BIN_SUFFIX}
SUP_NUM {SUPER_DIGITS}+
SUB_NUM {SUB_DIGITS}+
+NUMBER {BIN_NUM}|{OCT_NUM}|{DEC_NUM}|{HEX_NUM}
%%
@@ -95,8 +95,8 @@ SUB_NUM {SUB_DIGITS}+
"-"|"â??" {return tSUBTRACT;}
"*"|"Ã?" {return tMULTIPLY;}
"/"|"÷" {return tDIVIDE;}
-"abs"|"Abs"|"ABS" {return tABS_FUNC;}
"|" {return tABS;}
+"abs"|"Abs"|"ABS" {return tABS_FUNC;}
"sin"|"Sin"|"SIN" {return tSIN;}
"cos"|"Cos"|"COS" {return tCOS;}
"tan"|"Tan"|"TAN" {return tTAN;}
@@ -111,8 +111,6 @@ SUB_NUM {SUB_DIGITS}+
"tanh�¹"|"atanh"|"Atanh"|"ATANH" {return tATANH;}
"â?§"|"and"|"And"|"AND" {return tAND;}
"ans"|"Ans"|"ANS" {return tANS;}
-"clr"|"Clr"|"CLR" {return tCLR;}
-"eng"|"Eng"|"ENG" {return tEXP;}
"frac"|"Frac"|"FRAC" {return tFRAC;}
"int"|"Int"|"INT" {return tINT;}
"ln"|"Ln"|"LN" {return tLN;}
@@ -120,6 +118,7 @@ SUB_NUM {SUB_DIGITS}+
"mod"|"Mod"|"MOD" {return tMOD;}
"¬"|"~" {return tNOT;}
"â?¨"|"or"|"Or"|"OR" {return tOR;}
+"e" {return tEULERS;}
"Ï?"|"pi"|"Pi"|"PI" {return tPI;}
"rand"|"Rand"|"RAND" {return tRAND;}
"rcl"|"Rcl"|"RCL" {return tRCL;}
@@ -140,12 +139,7 @@ return tREG;
{SUP_NUM} { yylval->integer = super_atoi(yytext); return tSUPNUM; }
{SUB_NUM} { yylval->integer = sub_atoi(yytext); return tSUBNUM; }
-
-{BIN_NUM}|{OCT_NUM}|{DEC_NUM}|{HEX_NUM} {
-if (strlen(yytext) > MAX_DIGITS) yyextra->error = -PARSER_ERR_TOO_LONG_NUMBER;
-mp_set_from_string(yytext, &yylval->int_t);
-return tNUMBER;
-}
+{NUMBER} {mp_set_from_string(yytext, &yylval->int_t); return tNUMBER;}
[ \t\n]
. {return *yytext; }
diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y
index dce713d..5d18d18 100644
--- a/src/mp-equation-parser.y
+++ b/src/mp-equation-parser.y
@@ -26,8 +26,6 @@
#include <math.h>
#include <errno.h>
-#include "calctool.h" // FIXME: Can be removed soon
-#include "register.h" // FIXME: Can be removed soon
#include "mp-equation.h"
#include "mp-equation-parser.h"
#include "mp-equation-lexer.h"
@@ -72,6 +70,7 @@
%token t2S
%token tNOT
%token tOR
+%token tEULERS
%token tPI
%token tRAND
%token tRCL
@@ -91,57 +90,19 @@
%token <int_t> tNUMBER
%token <integer> tREG tSUBNUM tSUPNUM
-%token NEG
-
-%type <int_t> exp rcl value term func number
+%type <int_t> exp rcl term func number constant
%start statement
%left tADD tSUBTRACT tMULTIPLY tDIVIDE
%left MED
%left LNEG
-%left NEG
-%left POS
-%right '^'
-%right '!'
-%right '%'
-%right tSQUARED tCUBED
+%right '^' '!' '%' tSQUARED tCUBED
%left HIGH
%%
statement:
- seq
-| value { mp_set_from_mp(&$1, &(_mp_equation_get_extra(yyscanner))->ret); (_mp_equation_get_extra(yyscanner))->have_result = 1; }
-| error {
- (_mp_equation_get_extra(yyscanner))->error = -EINVAL;
- YYABORT;
-}
-;
-
-seq:
- udf
-| seq udf
-;
-
-udf:
- value '=' {
- display_set_number(&v->display, &$1);
- }
-| value '=' tSTO '(' tNUMBER ')' {
- int val = mp_cast_to_int(&$5);
- register_set_value(val, &$1);
-}
-| value tSTO '(' tNUMBER ')' {
- int val = mp_cast_to_int(&$4);
- register_set_value(val, &$1);
-}
-| tCLR {
- display_clear(&v->display);
-}
-;
-
-value:
- exp {mp_set_from_mp(&$1, &$$);}
+ exp { mp_set_from_mp(&$1, &(_mp_equation_get_extra(yyscanner))->ret); (_mp_equation_get_extra(yyscanner))->have_result = 1; }
;
exp:
@@ -191,8 +152,8 @@ exp:
term:
number {mp_set_from_mp(&$1, &$$);}
-| 'e' {mp_get_eulers(&$$);}
-| tPI {mp_get_pi(&$$);}
+| constant {mp_set_from_mp(&$1, &$$);}
+| number constant {mp_multiply(&$1, &$2, &$$);}
| rcl {mp_set_from_mp(&$1, &$$);}
| tSUBNUM tROOT term {mp_root(&$3, $1, &$$);}
| tROOT term {mp_sqrt(&$2, &$$);}
@@ -217,7 +178,7 @@ term:
| term '^' term {mp_xpowy(&$1, &$3, &$$);}
| term func {mp_multiply(&$1, &$2, &$$);}
| func {mp_set_from_mp(&$1, &$$);}
-| tREG {mp_set_from_mp(register_get_value($1), &$$);}
+| tREG {mp_set_from_mp(_mp_equation_get_extra(yyscanner)->get_variable($1), &$$);}
| '(' exp ')' {mp_set_from_mp(&$2, &$$);}
;
@@ -269,7 +230,7 @@ func:
rcl:
tRCL '(' tNUMBER ')' {
int val = mp_cast_to_int(&$3);
- mp_set_from_mp(register_get_value(val), &$$);
+ mp_set_from_mp(_mp_equation_get_extra(yyscanner)->get_variable(val), &$$);
}
;
@@ -278,4 +239,9 @@ number:
| tANS {mp_set_from_mp(&_mp_equation_get_extra(yyscanner)->ans, &$$);}
;
+constant:
+ tEULERS {mp_get_eulers(&$$);}
+| tPI {mp_get_pi(&$$);}
+;
+
%%
diff --git a/src/mp-equation.c b/src/mp-equation.c
index 42b1d57..d25c0ca 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -21,13 +21,20 @@
#include "mp-equation.h"
#include "calctool.h"
+#include "register.h"
#include "mp-equation-parser.h"
#include "mp-equation-lexer.h"
extern int _mp_equation_parse(yyscan_t yyscanner);
+static const MPNumber *
+get_variable(int index)
+{
+ return register_get_value(index);
+}
+
int
-mp_equation_parse_(const char *expression, MPNumber *result, int need_result)
+mp_equation_parse(const char *expression, MPNumber *result)
{
int ret = 0;
MPEquationParserState state;
@@ -41,6 +48,7 @@ mp_equation_parse_(const char *expression, MPNumber *result, int need_result)
state.base = v->base;
state.wordlen = v->wordlen;
state.angle_units = v->ttype;
+ state.get_variable = get_variable;
mp_set_from_mp(display_get_answer(&v->display), &state.ans);
v->math_error = 0;
@@ -56,7 +64,7 @@ mp_equation_parse_(const char *expression, MPNumber *result, int need_result)
if (ret)
return ret;
- if ((need_result != 0) != (state.have_result != 0))
+ if (!state.have_result)
return -EINVAL;
if (v->math_error)
@@ -67,20 +75,6 @@ mp_equation_parse_(const char *expression, MPNumber *result, int need_result)
}
-int
-mp_equation_parse(const char *expression, MPNumber *result)
-{
- return(mp_equation_parse_(expression, result, 1));
-}
-
-
-int
-mp_equation_udf_parse(const char *expression)
-{
- MPNumber t;
- return mp_equation_parse_(expression, &t, 0);
-}
-
int _mp_equation_error(void *yylloc, MPEquationParserState *state, char *text)
{
return 0;
diff --git a/src/mp-equation.h b/src/mp-equation.h
index 871e347..e610c0b 100644
--- a/src/mp-equation.h
+++ b/src/mp-equation.h
@@ -49,14 +49,15 @@ typedef struct {
/* TRUE if have a result */
int have_result;
+
+ /* Function to get variable values */
+ const MPNumber *(*get_variable)(int value);
/* Value returned from parser */
MPNumber ret;
} MPEquationParserState;
int mp_equation_parse(const char *expression, MPNumber *result);
-int mp_equation_udf_parse(const char *expression);
-
int _mp_equation_error(void *yylloc, MPEquationParserState *state, char *text);
#endif
diff --git a/src/unittest.c b/src/unittest.c
index 0a02a51..dd4cf5d 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -110,7 +110,9 @@ test_parser()
test("1.00", "1", 0);
test("1.01", "1.01", 0);
test("Ï?", "3.141592654", 0);
+ test("2Ï?", "6.283185307", 0);
test("e", "2.718281828", 0);
+ test("2e", "5.436563657", 0);
test("0+0", "0", 0);
test("1+1", "2", 0);
@@ -175,6 +177,8 @@ test_parser()
test("2^0", "1", 0);
test("2^1", "2", 0);
test("2^2", "4", 0);
+ test("2�¹", "0.5", 0);
+ test("2â?»", "", -20001);
test("2^â??1", "0.5", 0);
test("2^(â??1)", "0.5", 0);
test("â??10^2", "â??100", 0);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]