vala r2302 - in trunk: . gobject tests/basic-types vala



Author: juergbi
Date: Fri Jan  9 14:42:02 2009
New Revision: 2302
URL: http://svn.gnome.org/viewvc/vala?rev=2302&view=rev

Log:
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


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

Modified: trunk/gobject/valaccodebasemodule.vala
==============================================================================
--- trunk/gobject/valaccodebasemodule.vala	(original)
+++ trunk/gobject/valaccodebasemodule.vala	Fri Jan  9 14:42:02 2009
@@ -2354,7 +2354,20 @@
 	}
 
 	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) {

Modified: trunk/tests/basic-types/floats.test
==============================================================================
--- trunk/tests/basic-types/floats.test	(original)
+++ trunk/tests/basic-types/floats.test	Fri Jan  9 14:42:02 2009
@@ -1,94 +1,62 @@
 
 Program: test
 
-using GLib;
+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);
+}
 
-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", 
-
-				infinity > 0 ? "positive" : 
-				infinity < 0 ? "negative" : "none");
-		}
-	}
+void main () {
+	test_double ();
 }

Modified: trunk/vala/valascanner.vala
==============================================================================
--- trunk/vala/valascanner.vala	(original)
+++ trunk/vala/valascanner.vala	Fri Jan  9 14:42:02 2009
@@ -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 @@
 		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 @@
 			}
 			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 '{':



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