[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented adding grouped 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: implemented adding grouped terms
- Date: Fri, 4 Jan 2019 20:16:22 +0000 (UTC)
commit cb550157e882a261097b947fe867449b3b9664c5
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Fri Jan 4 13:28:54 2019 -0600
gcalc: implemented adding grouped terms
gcalc/gcalc-gexpression.vala | 6 +++++-
gcalc/gcalc-gterm.vala | 8 -------
gcalc/gcalc-parser.vala | 23 ++++++++++++++++++--
gcalc/gcalc-term.vala | 7 ++++++
tests/gcalc-parsing.vala | 49 ++++++++++++++++++++++++++++++++++++++++++
tests/gcalc-solving-basic.vala | 19 ++++++++++++++++
6 files changed, 101 insertions(+), 11 deletions(-)
---
diff --git a/gcalc/gcalc-gexpression.vala b/gcalc/gcalc-gexpression.vala
index 8238e9bf..5ca735c6 100644
--- a/gcalc/gcalc-gexpression.vala
+++ b/gcalc/gcalc-gexpression.vala
@@ -27,7 +27,11 @@ public class GCalc.GExpression : Object, Expression {
public weak Expression parent { get; set; }
public ExpressionContainer expressions { get { return exps; } }
public new virtual string to_string () {
- return "";
+ string s = "";
+ foreach (Expression e in expressions) {
+ s += e.to_string ();
+ }
+ return s;
}
public new virtual Result solve () {
var e = new GErrorResult ("Invalid expression");
diff --git a/gcalc/gcalc-gterm.vala b/gcalc/gcalc-gterm.vala
index 92849ae2..b96c45c5 100644
--- a/gcalc/gcalc-gterm.vala
+++ b/gcalc/gcalc-gterm.vala
@@ -30,13 +30,5 @@ public class GCalc.GTerm : GExpression, Term {
}
return res;
}
- // Expression
- public override string to_string () {
- string s = "";
- foreach (Expression e in expressions) {
- s += e.to_string ();
- }
- return s;
- }
}
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 8d23e6a0..54049a48 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -249,6 +249,16 @@ public class GCalc.Parser : Object {
expected.add (Vala.TokenType.INTEGER_LITERAL);
expected.add (Vala.TokenType.REAL_LITERAL);
expected.add (Vala.TokenType.CLOSE_PARENS);
+ } else if (current is Operator && current_parent is Term && top_parent is Polynomial) {
+ var g = new GGroup ();
+ current_parent.expressions.add (g);
+ var exp = new GPolynomial ();
+ g.expressions.add (exp);
+ var t = new GTerm ();
+ exp.expressions.add (t);
+ current = t;
+ current_parent = exp;
+ top_parent = g;
}
break;
case Vala.TokenType.CLOSE_PARENS:
@@ -266,8 +276,8 @@ public class GCalc.Parser : Object {
par = par.parent;
}
if (foundp) {
- current = par.parent; // Term
- current_parent = current.parent;
+ current = par;
+ current_parent = par.parent; // Term
top_parent = current_parent.parent;
}
break;
@@ -360,6 +370,15 @@ public class GCalc.Parser : Object {
current = opp;
current_parent = t;
expected.clear ();
+ } else if (current is Group && current_parent is Term && top_parent is Polynomial) {
+ // New term
+ var t = new GTerm ();
+ t.expressions.add (opp);
+ top_parent.expressions.add (t);
+ current = opp;
+ current_parent = t;
+ top_parent = current_parent.parent;
+ expected.clear ();
} else if (current is Variable && current_parent == null) {
// New Polynomial
var exp = new GPolynomial ();
diff --git a/gcalc/gcalc-term.vala b/gcalc/gcalc-term.vala
index 9d604317..bbd5d6e8 100644
--- a/gcalc/gcalc-term.vala
+++ b/gcalc/gcalc-term.vala
@@ -37,6 +37,7 @@ public interface GCalc.Term : Object, Expression {
Operator current_operator = null;
bool first = true;
foreach (Expression e in expressions) {
+ message ("Evaluation Expression in term: %s", e.to_string ());
if (e is Operator) {
if (!(e is Minus || e is Plus) && first) {
throw new TermError.INVALID_OPERATOR ("Incorrect position for operator in expression");
@@ -66,6 +67,12 @@ public interface GCalc.Term : Object, Expression {
}
}
}
+ } else if (e is Group) {
+ var ev = ((Group) e).evaluate ();
+ if (current == null) {
+ current = ev;
+ first = false;
+ }
}
}
if (current == null) {
diff --git a/tests/gcalc-parsing.vala b/tests/gcalc-parsing.vala
index 6ab031e1..04b21d91 100644
--- a/tests/gcalc-parsing.vala
+++ b/tests/gcalc-parsing.vala
@@ -563,6 +563,55 @@ class Tests {
message ("Correctly catched grouping error: %s", error.message);
}
});
+ Test.add_func ("/gcalc/parser/term/parenthesis/grouping",
+ ()=>{
+ 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 () == 2);
+ var t1 = p.expressions.get_item (0) as Term;
+ assert (t1 != null);
+ assert (t1.expressions.get_n_items () == 1);
+ var g1 = t1.expressions.get_item (0) as Group;
+ assert (g1 != null);
+ assert (g1.closed);
+ assert (g1.expressions.get_n_items () == 1);
+ var p1 = g1.expressions.get_item (0) as Polynomial;
+ assert (p1 != null);
+ assert (p1.expressions.get_n_items () == 1);
+ var t11 = p1.expressions.get_item (0) as Term;
+ assert (t11 != null);
+ assert (t11.expressions.get_n_items () == 1);
+ var c1 = t11.expressions.get_item (0) as Constant;
+ assert (c1 != null);
+ var t2 = p.expressions.get_item (1) as Term;
+ assert (t2 != null);
+ assert (t2.expressions.get_n_items () == 2);
+ var plus = t2.expressions.get_item (0) as Plus;
+ assert (plus != null);
+ var g2 = t2.expressions.get_item (1) as Group;
+ assert (g2 != null);
+ assert (g2.closed);
+ assert (g2.expressions.get_n_items () == 1);
+ var p2 = g2.expressions.get_item (0) as Polynomial;
+ assert (p2 != null);
+ assert (p2.expressions.get_n_items () == 1);
+ var t21 = p1.expressions.get_item (0) as Term;
+ assert (t21 != null);
+ assert (t21.expressions.get_n_items () == 1);
+ var c2 = t21.expressions.get_item (0) as Constant;
+ assert (c2 != null);
+ } catch (GLib.Error error) {
+ warning ("Error: %s", error.message);
+ }
+ });
return Test.run ();
}
}
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index a8e38410..4037d2f9 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -314,6 +314,25 @@ class Tests {
warning ("Error: %s", e.message);
}
});
+ Test.add_func ("/gcalc/solve/group/multiple/2",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("(2)+(3)", eqman);
+ assert (eqman.equations.get_n_items () == 1);
+ var eq = eqman.equations.get_item (0) as MathEquation;
+ assert (eq != null);
+ var e = eq.expressions.get_item (0) as Polynomial;
+ assert (e != null);
+ var res = e.evaluate () as Constant;
+ assert (res != null);
+ message ("Constant Result: %s", res.to_string ());
+ assert (res.real () == 5.0);
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ });
return Test.run ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]