[gnome-calculator/60-split-out-a-backend-library] gcalc: add support for terms as variables
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/60-split-out-a-backend-library] gcalc: add support for terms as variables
- Date: Thu, 3 Jan 2019 16:58:24 +0000 (UTC)
commit 1f8269d4cc7ccbd6bf69aad66d7c65d026879e82
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Thu Jan 3 09:40:07 2019 -0600
gcalc: add support for terms as variables
gcalc/gcalc-parser.vala | 28 +++++++++++++++++++++-------
tests/gcalc-main-interfaces.vala | 31 ++++++++++++++++++++++++++++++-
2 files changed, 51 insertions(+), 8 deletions(-)
---
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 634566b7..160734af 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -153,12 +153,9 @@ public class GCalc.Parser : Object {
expected.add(Vala.TokenType.PLUS);
expected.add(Vala.TokenType.MINUS);
message ("Adding new variable named: '%s'", (current as Variable).name);
- } else if (current is Polynomial) {
- var t = new GTerm ();
- t.expressions.add (v);
- current.expressions.add (t);
- current_parent = current;
- current = t;
+ } else if (current is Operator && current_parent is Term && top_parent is Polynomial) {
+ current_parent.expressions.add (v);
+ current = v;
expected.clear ();
}
}
@@ -212,13 +209,30 @@ public class GCalc.Parser : Object {
top_parent = current_parent;
current_parent = current;
current = opp;
- } else if (current is Constant && current_parent is Term && top_parent is Polynomial) {
+ expected.clear ();
+ } else if ((current is Constant || current is Variable)
+ && 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;
+ expected.clear ();
+ }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);
+ var t2 = new GTerm ();
+ exp.expressions.add (t2);
+ t2.expressions.add (opp);
+ current = opp;
+ current_parent = t2;
+ top_parent = exp;
+ expected.clear ();
}
break;
case Vala.TokenType.DIV:
diff --git a/tests/gcalc-main-interfaces.vala b/tests/gcalc-main-interfaces.vala
index 99d43475..cc0b279d 100644
--- a/tests/gcalc-main-interfaces.vala
+++ b/tests/gcalc-main-interfaces.vala
@@ -106,7 +106,7 @@ class Tests {
warning ("Error: %s", error.message);
}
});
- Test.add_func ("/gcalc/parser/binaryoperator/plus/constant",
+ Test.add_func ("/gcalc/parser/term/plus/constant",
()=>{
try {
var parser = new Parser ();
@@ -135,6 +135,35 @@ class Tests {
warning ("Error: %s", error.message);
}
});
+ Test.add_func ("/gcalc/parser/term/plus/variables",
+ ()=>{
+ 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);
+ 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 c1 = t1.expressions.get_item (0) as Variable;
+ 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 c2 = t2.expressions.get_item (1) as Variable;
+ assert (c2 != 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]