[gnome-calculator/60-split-out-a-backend-library] gcalc: porting solver to Expression



commit d76322b4a3a244617e7e4f4a8f1ebdcae0d5bf30
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Wed Jan 2 15:29:35 2019 -0600

    gcalc: porting solver to Expression
    
    Parsing fixes has been included.

 gcalc/gcalc-error-result.vala           |  3 ---
 gcalc/gcalc-gerror-result.vala          | 29 +++++++++++++++++++++++++++++
 gcalc/gcalc-gmath-equation-manager.vala |  2 ++
 gcalc/gcalc-gresult.vala                |  7 ++++++-
 gcalc/gcalc-gsolver.vala                | 15 +++++++++++++--
 gcalc/gcalc-math-equation-manager.vala  |  1 +
 gcalc/gcalc-parser.vala                 | 18 +++++++++++++++---
 gcalc/meson.build                       |  1 +
 8 files changed, 67 insertions(+), 9 deletions(-)
---
diff --git a/gcalc/gcalc-error-result.vala b/gcalc/gcalc-error-result.vala
index a44ddb80..c4fef72c 100644
--- a/gcalc/gcalc-error-result.vala
+++ b/gcalc/gcalc-error-result.vala
@@ -19,9 +19,6 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 public interface GCalc.ErrorResult : Object {
-  public abstract uint start { get; }
-  public abstract uint end { get; }
-  public abstract uint representation_base { get; }
   public abstract string to_string ();
 }
 
diff --git a/gcalc/gcalc-gerror-result.vala b/gcalc/gcalc-gerror-result.vala
new file mode 100644
index 00000000..61aca945
--- /dev/null
+++ b/gcalc/gcalc-gerror-result.vala
@@ -0,0 +1,29 @@
+/* gcalc-gerror-result.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.GErrorResult : Object {
+  private string msg = "";
+  public GErrorResult (string msg) {
+    this.msg = msg;
+  }
+  // ErrorResult
+  public string to_string () { return msg; }
+}
+
diff --git a/gcalc/gcalc-gmath-equation-manager.vala b/gcalc/gcalc-gmath-equation-manager.vala
index 5bfc3c4e..104aaea7 100644
--- a/gcalc/gcalc-gmath-equation-manager.vala
+++ b/gcalc/gcalc-gmath-equation-manager.vala
@@ -21,7 +21,9 @@
 public class GCalc.GMathEquationManager : Object, MathEquationManager {
   ExpressionContainer _equations = new ExpressionContainer ();
   ExpressionContainer _functions = new ExpressionContainer ();
+  ExpressionContainer _variables = new ExpressionContainer ();
   public ExpressionContainer equations { get { return _equations; } }
   public ExpressionContainer functions { get { return _functions; } }
+  public ExpressionContainer variables { get { return _variables; } }
 }
 
diff --git a/gcalc/gcalc-gresult.vala b/gcalc/gcalc-gresult.vala
index 48b47bbe..5cb4d29b 100644
--- a/gcalc/gcalc-gresult.vala
+++ b/gcalc/gcalc-gresult.vala
@@ -23,8 +23,13 @@ public class GCalc.GResult : Object {
   private ErrorResult _error;
   public GResult (Expression exp) {
     _expression = exp;
+    _error = null;
   }
-  public bool is_valid { get { return expression != null; } }
+  public GResult.with_error (Expression exp, ErrorResult error) {
+    _expression = exp;
+    _error = error;
+  }
+  public bool is_valid { get { return _error == null; } }
   public string to_string () {
     return expression.to_string ();
   }
diff --git a/gcalc/gcalc-gsolver.vala b/gcalc/gcalc-gsolver.vala
index 61aabc50..ffaea6d2 100644
--- a/gcalc/gcalc-gsolver.vala
+++ b/gcalc/gcalc-gsolver.vala
@@ -18,9 +18,20 @@
  * Authors:
  *      Daniel Espinosa <esodan gmail com>
  */
+using GCalc;
+
 public class GCalc.GSolver : Object, Solver {
   public Result solve (string str) throws GLib.Error {
-    var e = new GExpression () as Expression;
-    return new GResult (e) as Result;
+    var p = new Parser ();
+    var em = new GMathEquationManager ();
+    Result res;
+    try {
+      p.parse (str, em);
+      res = new GResult ((Expression) em.equations.get_item (0)) as Result; // FIXME: This should return a 
constant object
+    } catch (GLib.Error e) {
+      var err = new GErrorResult (e.message);
+      res = new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result; // FIXME: 
This should return a constant object
+    }
+    return res;
   }
 }
diff --git a/gcalc/gcalc-math-equation-manager.vala b/gcalc/gcalc-math-equation-manager.vala
index 97b085c1..49d7cc55 100644
--- a/gcalc/gcalc-math-equation-manager.vala
+++ b/gcalc/gcalc-math-equation-manager.vala
@@ -21,5 +21,6 @@
 public interface GCalc.MathEquationManager : Object {
   public abstract ExpressionContainer equations { get; }
   public abstract ExpressionContainer functions { get; }
+  public abstract ExpressionContainer variables { get; }
 }
 
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index a37aa197..6c633744 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -31,6 +31,7 @@ public class GCalc.Parser : Object {
     Vala.TokenType token = Vala.TokenType.NONE;
     var expected = new Gee.ArrayList<Vala.TokenType> ();
     Expression current = null;
+    var eq = new GMathEquation ();
     while (token != Vala.TokenType.EOF) {
       Vala.SourceLocation begin, end;
       token = scanner.read_token (out begin, out end);
@@ -55,22 +56,25 @@ public class GCalc.Parser : Object {
             current = f;
             expected.clear ();
             expected.add(Vala.TokenType.OPEN_PARENS);
-          } else if (n.down () == "def" && !(current is Function)) {
+          } else if (n.down () == "def" && current is Function) {
             throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected function definition expression");
           } else {
             var v = new GVariable (n) as Expression;
-            if (eqman.equations.find (v) == null) {
-              eqman.equations.add (v);
+            if (eqman.variables.find_named (n) == null) {
+              eqman.variables.add (v);
             }
             if (current == null) {
               var ex = new GPolynomial ();
               current = ex;
+              ex.expressions.add (v);
+              eq.expressions.add (ex);
               expected.clear ();
               expected.add(Vala.TokenType.ASSIGN);
             }
             if (current is Assign) {
               var ex = new GPolynomial ();
               current.expressions.add (ex);
+              ex.expressions.add (v);
               expected.clear ();
               expected.add (Vala.TokenType.IDENTIFIER);
               expected.add (Vala.TokenType.STAR);
@@ -85,7 +89,14 @@ public class GCalc.Parser : Object {
           break;
         case Vala.TokenType.INTEGER_LITERAL:
         case Vala.TokenType.REAL_LITERAL:
+          double res = 0;
+          if (!double.try_parse (n, out res)) {
+            throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected expression for a constant");
+          }
           var iexp = new GConstant.@double (double.parse (n));
+          if (current == null) {
+            current = new GPolynomial ();
+          }
           if (current is Polynomial) {
             current.expressions.add (iexp);
             expected.clear ();
@@ -185,6 +196,7 @@ public class GCalc.Parser : Object {
         case Vala.TokenType.HASH:
           break;
       }
+      eqman.equations.add (eq);
     }
   }
 }
diff --git a/gcalc/meson.build b/gcalc/meson.build
index 80508bc2..92fee1ee 100644
--- a/gcalc/meson.build
+++ b/gcalc/meson.build
@@ -48,6 +48,7 @@ sources = files([
        'gcalc-gexpression.vala',
        'gcalc-gassign.vala',
        'gcalc-gconstant.vala',
+       'gcalc-gerror-result.vala',
        'gcalc-gfunction.vala',
        'gcalc-gmath-equation.vala',
        'gcalc-gmath-equation-manager.vala',


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