[gnome-calculator/60-split-out-a-backend-library] gcalc: fix parsing integer constants
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/60-split-out-a-backend-library] gcalc: fix parsing integer constants
- Date: Wed, 2 Jan 2019 23:17:02 +0000 (UTC)
commit fececc406917e62b8f884ea7b55c003f71ea88a8
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Wed Jan 2 16:23:07 2019 -0600
gcalc: fix parsing integer constants
gcalc/gcalc-gconstant.vala | 2 +-
gcalc/gcalc-parser.vala | 18 +++++++++++++++---
tests/gcalc-main-interfaces.vala | 17 +++++++++++++----
3 files changed, 29 insertions(+), 8 deletions(-)
---
diff --git a/gcalc/gcalc-gconstant.vala b/gcalc/gcalc-gconstant.vala
index f33a1d22..4658db80 100644
--- a/gcalc/gcalc-gconstant.vala
+++ b/gcalc/gcalc-gconstant.vala
@@ -19,7 +19,7 @@
* Daniel Espinosa <esodan gmail com>
*/
public class GCalc.GConstant : GExpression, Constant {
- private MPFR.Real real_value;
+ private MPFR.Real real_value = MPFR.Real (1000);
construct {
real_value.set_zero ();
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 6c633744..316a9a78 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -27,7 +27,13 @@ public class GCalc.Parser : Object {
SourceFileType type = SourceFileType.NONE;
var sf = new SourceFile (context, type, "gcalc://", str);
var scanner = new Vala.Scanner (sf);
- var lines = str.split ("\n");
+ string[] lines;
+ if ("\n" in str) {
+ lines = str.split ("\n");
+ } else {
+ lines = new string [0];
+ lines[0] = str;
+ }
Vala.TokenType token = Vala.TokenType.NONE;
var expected = new Gee.ArrayList<Vala.TokenType> ();
Expression current = null;
@@ -35,11 +41,16 @@ public class GCalc.Parser : Object {
while (token != Vala.TokenType.EOF) {
Vala.SourceLocation begin, end;
token = scanner.read_token (out begin, out end);
+ if (token == Vala.TokenType.EOF) {
+ break;
+ }
string n = token.to_string ();
n = n.replace ("`", "");
n = n.replace ("'", "");
- string l = lines[begin.line];
- n = l.substring (begin.column, end.column - begin.column);
+ string l = lines[begin.line - 1];
+ message ("Token: '%s' : Line: %d Column begin: %d Column end: %d Text: %s", n, begin.line,
begin.column, end.column, l);
+ n = l.substring (begin.column - 1, end.column - begin.column + 1);
+ message ("Token text: %s", n);
if (expected.size != 0 && !expected.contains (token)) {
throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected expression");
}
@@ -96,6 +107,7 @@ public class GCalc.Parser : Object {
var iexp = new GConstant.@double (double.parse (n));
if (current == null) {
current = new GPolynomial ();
+ eq.expressions.add (current);
}
if (current is Polynomial) {
current.expressions.add (iexp);
diff --git a/tests/gcalc-main-interfaces.vala b/tests/gcalc-main-interfaces.vala
index e342d87d..48f36de7 100644
--- a/tests/gcalc-main-interfaces.vala
+++ b/tests/gcalc-main-interfaces.vala
@@ -23,12 +23,21 @@ class Tests {
{
GLib.Intl.setlocale (GLib.LocaleCategory.ALL, "");
Test.init (ref args);
- Test.add_func ("/gcalc/solver/initial",
+ Test.add_func ("/gcalc/parser/constant",
()=>{
try {
- var solv = new GCalc.GSolver () as Solver;
- //var res = solv.solve ("4+4");
- //message (res.to_string ());
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("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 c = p.expressions.get_item (0) as Constant;
+ assert (c != null);
} catch (GLib.Error error) {
warning ("Error: %s", error.message);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]