[gnome-calculator/60-split-out-a-backend-library] gcalc: added support for pasing division in terms
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/60-split-out-a-backend-library] gcalc: added support for pasing division in terms
- Date: Thu, 3 Jan 2019 17:23:27 +0000 (UTC)
commit 94ddc31cd481a062d792463513433804abe8ce0c
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Thu Jan 3 11:13:13 2019 -0600
gcalc: added support for pasing division in terms
_build/gcalc/namespace-info.vala | 24 ++++++++++
gcalc/gcalc-division.vala | 22 +++++++++
gcalc/gcalc-gdivision.vala | 26 +++++++++++
gcalc/gcalc-gmultiply.vala | 2 +-
gcalc/gcalc-parser.vala | 47 +++++++++++--------
gcalc/meson.build | 2 +
tests/gcalc-main-interfaces.vala | 98 ++++++++++++++++++++++++++++++++++++++++
7 files changed, 200 insertions(+), 21 deletions(-)
---
diff --git a/_build/gcalc/namespace-info.vala b/_build/gcalc/namespace-info.vala
new file mode 100644
index 00000000..230b2167
--- /dev/null
+++ b/_build/gcalc/namespace-info.vala
@@ -0,0 +1,24 @@
+/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
+/* Attr.vala
+ *
+ * Copyright (C) 2018 Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library 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
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors:
+ * Daniel Espinosa <esodan gmail com>
+ */
+[CCode (gir_namespace = "GCalc", gir_version = "1", cheader_filename = "gcalc/gcalc.h")]
+namespace GCalc {}
diff --git a/gcalc/gcalc-division.vala b/gcalc/gcalc-division.vala
new file mode 100644
index 00000000..68d29bee
--- /dev/null
+++ b/gcalc/gcalc-division.vala
@@ -0,0 +1,22 @@
+/* gcalc-division.vala
+ *
+ * Copyright (C) 2018 Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Daniel Espinosa <esodan gmail com>
+ */
+public interface GCalc.Division : Object, Expression, Operator, BinaryOperator {}
+
diff --git a/gcalc/gcalc-gdivision.vala b/gcalc/gcalc-gdivision.vala
new file mode 100644
index 00000000..9832cd0b
--- /dev/null
+++ b/gcalc/gcalc-gdivision.vala
@@ -0,0 +1,26 @@
+/* gcalc-gplus.vala
+ *
+ * Copyright (C) 2018 Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Daniel Espinosa <esodan gmail com>
+ */
+public class GCalc.GDivision : GExpression, Operator, BinaryOperator, Division {
+ public override string to_string () {
+ return "/";
+ }
+}
+
diff --git a/gcalc/gcalc-gmultiply.vala b/gcalc/gcalc-gmultiply.vala
index 50d2a004..384b092c 100644
--- a/gcalc/gcalc-gmultiply.vala
+++ b/gcalc/gcalc-gmultiply.vala
@@ -20,7 +20,7 @@
*/
public class GCalc.GMultiply : GExpression, Operator, BinaryOperator, Multiply {
public override string to_string () {
- return "+";
+ return "*";
}
}
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 175b3bcd..f64575c8 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -185,32 +185,16 @@ public class GCalc.Parser : Object {
break;
case Vala.TokenType.STAR:
var op = new GMultiply ();
- if (current is Operator) {
- throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected expression for a multiply
operator");
- }
- if ((current is Constant || current is Variable)
- && current_parent is Term && top_parent is Polynomial) {
- current_parent.expressions.add (op);
- current = op;
- } else if (current is Variable && current_parent == null) {
- // New Polynomial
- var exp = new GPolynomial ();
- eq.expressions.add (exp);
- var t = new GTerm ();
- exp.expressions.add (t);
- t.expressions.add (current);
- t.expressions.add (op);
- current = op;
- current_parent = t;
- top_parent = exp;
- expected.clear ();
- }
+ process_term_operator (op, eq);
break;
case Vala.TokenType.PLUS:
var opp = new GPlus ();
process_operator (opp, eq);
break;
case Vala.TokenType.DIV:
+ var op = new GDivision ();
+ process_term_operator (op, eq);
+ break;
case Vala.TokenType.MINUS:
var opp = new GMinus ();
process_operator (opp, eq);
@@ -352,6 +336,29 @@ public class GCalc.Parser : Object {
expected.clear ();
}
}
+ private void process_term_operator (Operator op, GMathEquation eq) throws GLib.Error {
+ if (current is Operator) {
+ throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected expression for a multiply operator");
+ }
+ if ((current is Constant || current is Variable)
+ && current_parent is Term && top_parent is Polynomial) {
+ current_parent.expressions.add (op);
+ current = op;
+ expected.clear ();
+ } else if (current is Variable && current_parent == null) {
+ // New Polynomial
+ var exp = new GPolynomial ();
+ eq.expressions.add (exp);
+ var t = new GTerm ();
+ exp.expressions.add (t);
+ t.expressions.add (current);
+ t.expressions.add (op);
+ current = op;
+ current_parent = t;
+ top_parent = exp;
+ expected.clear ();
+ }
+ }
}
public errordomain GCalc.ParserError {
diff --git a/gcalc/meson.build b/gcalc/meson.build
index 62a6d29c..bbc268f8 100644
--- a/gcalc/meson.build
+++ b/gcalc/meson.build
@@ -42,6 +42,7 @@ sources = files([
'gcalc-assign.vala',
'gcalc-binary-operator.vala',
'gcalc-constant.vala',
+ 'gcalc-division.vala',
'gcalc-expression.vala',
'gcalc-expression-container.vala',
'gcalc-function.vala',
@@ -49,6 +50,7 @@ sources = files([
'gcalc-gexpression.vala',
'gcalc-gassign.vala',
'gcalc-gconstant.vala',
+ 'gcalc-gdivision.vala',
'gcalc-gerror-result.vala',
'gcalc-gfunction.vala',
'gcalc-gmath-equation.vala',
diff --git a/tests/gcalc-main-interfaces.vala b/tests/gcalc-main-interfaces.vala
index af8030f8..a91c70c5 100644
--- a/tests/gcalc-main-interfaces.vala
+++ b/tests/gcalc-main-interfaces.vala
@@ -394,6 +394,7 @@ class Tests {
assert (c2 != null);
var t2 = p.expressions.get_item (1) as Term;
assert (t2 != null);
+ assert (t2.expressions.get_n_items () == 4);
var plus = t2.expressions.get_item (0) as Plus;
assert (plus != null);
var c3 = t2.expressions.get_item (1) as Constant;
@@ -406,6 +407,103 @@ class Tests {
warning ("Error: %s", error.message);
}
});
+ Test.add_func ("/gcalc/parser/term/division/constant",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("1/1", eqman);
+ assert (eqman.equations.get_n_items () == 1);
+ var eq = eqman.equations.get_item (0) as MathEquation;
+ assert (eq != null);
+ assert (eq.expressions.get_n_items () == 1);
+ var p = eq.expressions.get_item (0) as Polynomial;
+ assert (p != null);
+ assert (p.expressions.get_n_items () == 1);
+ var t1 = p.expressions.get_item (0) as Term;
+ assert (t1 != null);
+ assert (t1.expressions.get_n_items () == 3);
+ var c1 = t1.expressions.get_item (0) as Constant;
+ assert (c1 != null);
+ var m = t1.expressions.get_item (1) as Division;
+ assert (m != null);
+ var c2 = t1.expressions.get_item (2) as Constant;
+ assert (c2 != null);
+ } catch (GLib.Error error) {
+ warning ("Error: %s", error.message);
+ }
+ });
+ Test.add_func ("/gcalc/parser/term/division/variable",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("A/B", eqman);
+ assert (eqman.equations.get_n_items () == 1);
+ var eq = eqman.equations.get_item (0) as MathEquation;
+ assert (eq != null);
+ assert (eq.expressions.get_n_items () == 1);
+ var p = eq.expressions.get_item (0) as Polynomial;
+ assert (p != null);
+ message ("Terms: %u", p.expressions.get_n_items ());
+ assert (p.expressions.get_n_items () == 1);
+ var t1 = p.expressions.get_item (0) as Term;
+ assert (t1 != null);
+ assert (t1.expressions.get_n_items () == 3);
+ var c1 = t1.expressions.get_item (0) as Variable;
+ assert (c1 != null);
+ var m = t1.expressions.get_item (1) as Division;
+ assert (m != null);
+ var c2 = t1.expressions.get_item (2) as Variable;
+ assert (c2 != null);
+ } catch (GLib.Error error) {
+ warning ("Error: %s", error.message);
+ }
+ });
+ Test.add_func ("/gcalc/parser/term/complex/multiply-division/constant-variable",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("-1/B+3*A/5", eqman);
+ assert (eqman.equations.get_n_items () == 1);
+ var eq = eqman.equations.get_item (0) as MathEquation;
+ assert (eq != null);
+ assert (eq.expressions.get_n_items () == 1);
+ var p = eq.expressions.get_item (0) as Polynomial;
+ assert (p != null);
+ message ("Terms: %u", p.expressions.get_n_items ());
+ assert (p.expressions.get_n_items () == 2);
+ var t1 = p.expressions.get_item (0) as Term;
+ assert (t1 != null);
+ assert (t1.expressions.get_n_items () == 4);
+ var minus = t1.expressions.get_item (0) as Minus;
+ assert (minus != null);
+ var c1 = t1.expressions.get_item (1) as Constant;
+ assert (c1 != null);
+ var m1 = t1.expressions.get_item (2) as Division;
+ assert (m1 != null);
+ var c2 = t1.expressions.get_item (3) as Variable;
+ assert (c2 != null);
+ var t2 = p.expressions.get_item (1) as Term;
+ assert (t2 != null);
+ assert (t2.expressions.get_n_items () == 6);
+ var plus = t2.expressions.get_item (0) as Plus;
+ assert (plus != null);
+ var c3 = t2.expressions.get_item (1) as Constant;
+ assert (c3 != null);
+ var m2 = t2.expressions.get_item (2) as Multiply;
+ assert (m2 != null);
+ var c4 = t2.expressions.get_item (3) as Variable;
+ assert (c4 != null);
+ var m3 = t2.expressions.get_item (4) as Division;
+ assert (m3 != null);
+ var c5 = t2.expressions.get_item (5) as Constant;
+ assert (c5 != null);
+ } catch (GLib.Error error) {
+ warning ("Error: %s", error.message);
+ }
+ });
return Test.run ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]