]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Fix some type suffix issues with real literals
authorJürg Billeter <j@bitron.ch>
Fri, 9 Jan 2009 14:42:02 +0000 (14:42 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Fri, 9 Jan 2009 14:42:02 +0000 (14:42 +0000)
2009-01-09  Jürg Billeter  <j@bitron.ch>

* vala/valascanner.vala:
* gobject/valaccodebasemodule.vala:

Fix some type suffix issues with real literals

* tests/basic-types/floats.test:

Improve float tests

svn path=/trunk/; revision=2302

ChangeLog
gobject/valaccodebasemodule.vala
tests/basic-types/floats.test
vala/valascanner.vala

index 252c545d0b9cae1a09d1de2f65353bc4b03612b1..834613b1232885015f5ecc1017f1a9e0fbcaf794 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2009-01-09  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valascanner.vala:
+       * gobject/valaccodebasemodule.vala:
+
+       Fix some type suffix issues with real literals
+
+       * tests/basic-types/floats.test:
+
+       Improve float tests
+
 2009-01-09  Jürg Billeter  <j@bitron.ch>
 
        * tests/Makefile.am:
index 3f883bf5ebb7df2ecd5c53b5ff73798693c9519e..2cb23a7f99b1f6408223fade611fc2bb8beb3af5 100644 (file)
@@ -2354,7 +2354,20 @@ public class Vala.CCodeBaseModule : CCodeModule {
        }
 
        public override void visit_real_literal (RealLiteral expr) {
-               expr.ccodenode = new CCodeConstant (expr.value);
+               string c_literal = expr.value;
+               if (c_literal.has_suffix ("d") || c_literal.has_suffix ("D")) {
+                       // there is no suffix for double in C
+                       c_literal = c_literal.substring (0, c_literal.length - 1);
+               }
+               if (!("." in c_literal || "e" in c_literal || "E" in c_literal)) {
+                       // C requires period or exponent part for floating constants
+                       if ("f" in c_literal || "F" in c_literal) {
+                               c_literal = c_literal.substring (0, c_literal.length - 1) + ".f";
+                       } else {
+                               c_literal += ".";
+                       }
+               }
+               expr.ccodenode = new CCodeConstant (c_literal);
        }
 
        public override void visit_string_literal (StringLiteral expr) {
index da7e6c07cd22ac1bea84d2a95bcc99d22d405761..6a4ac7d6cc3c9fc06a44159e6626c6c823177543 100644 (file)
@@ -1,94 +1,62 @@
 
 Program: test
 
-using GLib;
-
-class Maman.Foo : Object {
-       const float[] FLOAT_TESTS = { 
-               float.EPSILON, 0.0F, 1.0F, 
-               -float.INFINITY,
-               float.INFINITY,
-               float.NAN
-       };
-
-       const double[] DOUBLE_TESTS = { 
-               double.EPSILON, 0.0, 1.0, 
-               -double.INFINITY,
-               double.INFINITY,
-               double.NAN
-       };
-
-       static void main (string[] args) {
-               stdout.printf (
-                       "float: range=%s...%s\n" +
-                       "       digits=%s(%s), exp=%s..%s(%s..%s)\n" +
-                       "       epsilon=%s, infinity=%s/%s, nan=%s\n",
-
-                       float.MIN.to_string (), 
-                       float.MAX.to_string (),
-
-                       float.MANT_DIG.to_string (),
-                       float.DIG.to_string (),
-                       float.MIN_EXP.to_string (), 
-                       float.MAX_EXP.to_string (),
-                       float.MIN_10_EXP.to_string (), 
-                       float.MAX_10_EXP.to_string (),
-
-                       float.EPSILON.to_string (),
-                       float.INFINITY.to_string (),
-                       (-float.INFINITY).to_string (),
-                       float.NAN.to_string ());
-
-               for (int i = 0; i < 6; i++) { // XXX use foreach
-                       float value = FLOAT_TESTS[i];
-                       int infinity = value.is_infinity ();
-
-                       stdout.printf (
-                               "float(%g): nan=%s, finite=%s, normal=%s, infinity=%s\n", 
-                               
-                               value,
-                               value.is_nan () ? "true" : "false", 
-                               value.is_finite () ? "true" : "false",
-                               value.is_normal () ? "true" : "false", 
-
-                               infinity > 0 ? "positive" : 
-                               infinity < 0 ? "negative" : "none");
-               }
-
-               stdout.printf (
-                       "double: range=%s...%s\n" +
-                       "        digits=%s(%s), exp=%s..%s(%s..%s)\n" +
-                       "        epsilon=%s, infinity=%s/%s, nan=%s\n",
-
-                       double.MIN.to_string (), 
-                       double.MAX.to_string (),
-
-                       double.MANT_DIG.to_string (),
-                       double.DIG.to_string (),
-                       double.MIN_EXP.to_string (), 
-                       double.MAX_EXP.to_string (),
-                       double.MIN_10_EXP.to_string (), 
-                       double.MAX_10_EXP.to_string (),
-
-                       double.EPSILON.to_string (),
-                       double.INFINITY.to_string (),
-                       (-double.INFINITY).to_string (),
-                       double.NAN.to_string ());
-
-               for (int i = 0; i < 6; i++) { // XXX use foreach
-                       double value = DOUBLE_TESTS[i];
-                       int infinity = value.is_infinity ();
-
-                       stdout.printf(
-                               "double(%g): nan=%s, finite=%s, normal=%s, infinity=%s\n", 
-                               
-                               value,
-                               value.is_nan () ? "true" : "false", 
-                               value.is_finite () ? "true" : "false",
-                               value.is_normal () ? "true" : "false", 
+void test_double () {
+       // declaration and initialization
+       double d = 42d;
+       assert (d == 42d);
+
+       // assignment
+       d = 23d;
+       assert (d == 23d);
+
+       // access
+       double e = d;
+       assert (e == 23d);
+
+       // +
+       d = 42d + 23d;
+       assert (d == 65d);
+
+       // -
+       d = 42d - 23d;
+       assert (d == 19d);
+
+       // *
+       d = 42d * 23d;
+       assert (d == 966d);
+
+       // /
+       d = 42d / 23d;
+       assert (d > 1.8);
+       assert (d < 1.9);
+
+       // equality and relational
+       d = 42d;
+       assert (d == 42d);
+       assert (d != 50d);
+       assert (d < 42.5);
+       assert (!(d < 41.5));
+       assert (d <= 42d);
+       assert (!(d <= 41.5));
+       assert (d >= 42d);
+       assert (!(d >= 42.5));
+       assert (d > 41.5);
+       assert (!(d > 42.5));
+
+       // to_string
+       string s = d.to_string ();
+       assert (s == "42");
+
+       // ensure that MIN and MAX are valid values
+       d = double.MIN;
+       assert (d == double.MIN);
+       assert (d < double.MAX);
+       d = double.MAX;
+       assert (d == double.MAX);
+       assert (d > double.MIN);
+}
 
-                               infinity > 0 ? "positive" : 
-                               infinity < 0 ? "negative" : "none");
-               }
-       }
+void main () {
+       test_double ();
 }
index 390f8161298514d32346a83dbb2717fbd07b869b..9891d52df5151c4bfd952762f66f1772c536eb2b 100644 (file)
@@ -1,6 +1,6 @@
 /* valascanner.vala
  *
- * Copyright (C) 2008  Jürg Billeter
+ * Copyright (C) 2008-2009  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -333,6 +333,93 @@ public class Vala.Scanner {
                return TokenType.IDENTIFIER;
        }
 
+       TokenType read_number () {
+               var type = TokenType.INTEGER_LITERAL;
+
+               // integer part
+               if (current < end - 2 && current[0] == '0'
+                   && current[1] == 'x' && current[2].isxdigit ()) {
+                       // hexadecimal integer literal
+                       current += 2;
+                       while (current < end && current[0].isxdigit ()) {
+                               current++;
+                       }
+               } else {
+                       // decimal number
+                       while (current < end && current[0].isdigit ()) {
+                               current++;
+                       }
+               }
+
+               // fractional part
+               if (current < end - 1 && current[0] == '.' && current[1].isdigit ()) {
+                       type = TokenType.REAL_LITERAL;
+                       current++;
+                       while (current < end && current[0].isdigit ()) {
+                               current++;
+                       }
+               }
+
+               // exponent part
+               if (current < end && current[0].tolower () == 'e') {
+                       type = TokenType.REAL_LITERAL;
+                       current++;
+                       if (current < end && (current[0] == '+' || current[0] == '-')) {
+                               current++;
+                       }
+                       while (current < end && current[0].isdigit ()) {
+                               current++;
+                       }
+               }
+
+               // type suffix
+               if (current < end) {
+                       bool real_literal = (type == TokenType.REAL_LITERAL);
+
+                       switch (current[0]) {
+                       case 'l':
+                       case 'L':
+                               if (type == TokenType.INTEGER_LITERAL) {
+                                       current++;
+                                       if (current < end && current[0].tolower () == 'l') {
+                                               current++;
+                                       }
+                               }
+                               break;
+                       case 'u':
+                       case 'U':
+                               if (type == TokenType.INTEGER_LITERAL) {
+                                       current++;
+                                       if (current < end && current[0].tolower () == 'l') {
+                                               current++;
+                                               if (current < end && current[0].tolower () == 'l') {
+                                                       current++;
+                                               }
+                                       }
+                               }
+                               break;
+                       case 'f':
+                       case 'F':
+                       case 'd':
+                       case 'D':
+                               type = TokenType.REAL_LITERAL;
+                               current++;
+                               break;
+                       }
+
+                       if (!real_literal && is_ident_char (current[0])) {
+                               // allow identifiers to start with a digit
+                               // as long as they contain at least one char
+                               while (current < end && is_ident_char (current[0])) {
+                                       current++;
+                               }
+                               type = TokenType.IDENTIFIER;
+                       }
+               }
+
+               return type;
+       }
+
        public TokenType read_token (out SourceLocation token_begin, out SourceLocation token_end) {
                space ();
 
@@ -363,56 +450,7 @@ public class Vala.Scanner {
                        }
                        type = TokenType.IDENTIFIER;
                } else if (current[0].isdigit ()) {
-                       while (current < end && current[0].isdigit ()) {
-                               current++;
-                       }
-                       type = TokenType.INTEGER_LITERAL;
-                       if (current < end && current[0].tolower () == 'l') {
-                               current++;
-                               if (current < end && current[0].tolower () == 'l') {
-                                       current++;
-                               }
-                       } else if (current < end && current[0].tolower () == 'u') {
-                               current++;
-                               if (current < end && current[0].tolower () == 'l') {
-                                       current++;
-                                       if (current < end && current[0].tolower () == 'l') {
-                                               current++;
-                                       }
-                               }
-                       } else if (current < end - 1 && current[0] == '.' && current[1].isdigit ()) {
-                               current++;
-                               while (current < end && current[0].isdigit ()) {
-                                       current++;
-                               }
-                               if (current < end && current[0].tolower () == 'e') {
-                                       current++;
-                                       if (current < end && (current[0] == '+' || current[0] == '-')) {
-                                               current++;
-                                       }
-                                       while (current < end && current[0].isdigit ()) {
-                                               current++;
-                                       }
-                               }
-                               if (current < end && current[0].tolower () == 'f') {
-                                       current++;
-                               }
-                               type = TokenType.REAL_LITERAL;
-                       } else if (current < end && current == begin + 1
-                                  && begin[0] == '0' && begin[1] == 'x' && begin[2].isxdigit ()) {
-                               // hexadecimal integer literal
-                               current++;
-                               while (current < end && current[0].isxdigit ()) {
-                                       current++;
-                               }
-                       } else if (current < end && is_ident_char (current[0])) {
-                               // allow identifiers to start with a digit
-                               // as long as they contain at least one char
-                               while (current < end && is_ident_char (current[0])) {
-                                       current++;
-                               }
-                               type = TokenType.IDENTIFIER;
-                       }
+                       type = read_number ();
                } else {
                        switch (current[0]) {
                        case '{':