Commit ae7fbc79 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

support integer and floating point suffixes use correct type for integer

2006-09-02  Jürg Billeter  <j@bitron.ch>

	* vala/scanner.l: support integer and floating point suffixes
	* vala/valasemanticanalyzer.vala: use correct type for integer and
	  floating point literals
	* vala/valaintegerliteral.val, vala/valarealliteral.vala: add
	  get_type_name method
	* vapi/glib-2.0.vala: add string to int64 conversion method

svn path=/trunk/; revision=118
parent be3441b3
2006-09-02 Jürg Billeter <j@bitron.ch>
* vala/scanner.l: support integer and floating point suffixes
* vala/valasemanticanalyzer.vala: use correct type for integer and
floating point literals
* vala/valaintegerliteral.val, vala/valarealliteral.vala: add
get_type_name method
* vapi/glib-2.0.vala: add string to int64 conversion method
2006-09-02 Jürg Billeter <j@bitron.ch>
* vala/valasemanticanalyzer.vala: use IntegerType, FloatingType, and
......
......@@ -43,11 +43,13 @@ static gboolean file_comment = FALSE;
space [ \t\n]*
ident [[:alnum:]_]+
decimal_integer_literal (0|[1-9][[:digit:]]*)
real_literal [[:digit:]]+"."[[:digit:]]*
real_literal [[:digit:]]+"."[[:digit:]]*{real_suffix}?
hex_digit [[:digit:]A-fa-f]
octal_digit [0-7]
octal_integer_literal 0{octal_digit}+
hexadecimal_integer_literal 0x{hex_digit}+
integer_suffix L|LL|U|UL|ULL
real_suffix F
single_character [^\'\\]
single_string_literal_character [^\"\\]
simple_escape_sequence \\[\'\"\?\\abfnrtv]
......@@ -56,7 +58,7 @@ character ({single_character}|{simple_escape_sequence})
string_literal_character ({single_string_literal_character}|{simple_escape_sequence})
character_literal \'{character}*\'
string_literal \"{string_literal_character}*\"
integer_literal ({decimal_integer_literal}|{hexadecimal_integer_literal}|{octal_integer_literal})
integer_literal ({decimal_integer_literal}|{hexadecimal_integer_literal}|{octal_integer_literal}){integer_suffix}?
literal ({integer_literal}|{real_literal}|{character_literal}|{string_literal})
%%
......
......@@ -50,4 +50,54 @@ public class Vala.IntegerLiteral : Literal {
public override ref string! to_string () {
return value;
}
/**
* Returns the type name of the value this literal represents.
*
* @return the name of literal type
*/
public string! get_type_name () {
string number = value;
int l = 0;
while (number.has_suffix ("L")) {
l++;
number = number.ndup (number.size () - 1);
}
bool u = false;
if (number.has_suffix ("U")) {
u = true;
number = number.ndup (number.size () - 1);
}
int64 n = number.to_int64 ();
if (!u && n > 0x7fffffff) {
// value doesn't fit into signed 32-bit
l = 2;
} else if (u && n > 0xffffffff) {
// value doesn't fit into unsigned 32-bit
l = 2;
}
if (l == 0) {
if (u) {
return "uint";
} else {
return "int";
}
} else if (l == 1) {
if (u) {
return "ulong";
} else {
return "long";
}
} else {
if (u) {
return "uint64";
} else {
return "int64";
}
}
}
}
......@@ -46,4 +46,17 @@ public class Vala.RealLiteral : Literal {
public override void accept (CodeVisitor! visitor) {
visitor.visit_real_literal (this);
}
/**
* Returns the type name of the value this literal represents.
*
* @return the name of literal type
*/
public string! get_type_name () {
if (value.has_suffix ("F")) {
return "float";
}
return "double";
}
}
......@@ -608,12 +608,12 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
public override void visit_integer_literal (IntegerLiteral! expr) {
expr.static_type = new TypeReference ();
expr.static_type.data_type = (DataType) root_symbol.lookup ("int").node;
expr.static_type.data_type = (DataType) root_symbol.lookup (expr.get_type_name ()).node;
}
public override void visit_real_literal (RealLiteral! expr) {
expr.static_type = new TypeReference ();
expr.static_type.data_type = (DataType) root_symbol.lookup ("double").node;
expr.static_type.data_type = (DataType) root_symbol.lookup (expr.get_type_name ()).node;
}
public override void visit_string_literal (StringLiteral! expr) {
......
......@@ -115,11 +115,17 @@ public struct uint32 {
[CCode (cname = "gint64", cheader_filename = "glib.h", type_id = "G_TYPE_INT64", marshaller_type_name = "INT64")]
[IntegerType (rank = 14)]
public struct int64 {
[InstanceLast ()]
[CCode (cname = "g_strdup_printf")]
public ref string! to_string (string! format = "%lli");
}
[CCode (cname = "guint64", cheader_filename = "glib.h", type_id = "G_TYPE_UINT64", marshaller_type_name = "UINT64")]
[IntegerType (rank = 15)]
public struct uint64 {
[InstanceLast ()]
[CCode (cname = "g_strdup_printf")]
public ref string! to_string (string! format = "%llu");
}
[CCode (cname = "float", cheader_filename = "glib.h", type_id = "G_TYPE_FLOAT", marshaller_type_name = "FLOAT")]
......@@ -157,7 +163,7 @@ public struct unichar {
}
[ReferenceType (dup_function = "g_strdup", free_function = "g_free", type_id = "G_TYPE_STRING", ref_function = "g_strdup")]
[CCode (cname = "char", cheader_filename = "string.h,glib.h", type_id = "G_TYPE_STRING", marshaller_type_name = "STRING")]
[CCode (cname = "char", cheader_filename = "stdlib.h,string.h,glib.h", type_id = "G_TYPE_STRING", marshaller_type_name = "STRING")]
public struct string {
[CCode (cname = "g_strstr")]
public string str (string! needle);
......@@ -208,6 +214,8 @@ public struct string {
[CCode (cname = "atoi")]
public int to_int ();
[CCode (cname = "strtoll")]
public int64 to_int64 (out string endptr = null, int _base = 0);
[CCode (cname = "strlen")]
public long size ();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment