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

support element access for strings test element access for strings

2007-07-11  Juerg Billeter  <j@bitron.ch>

	* vala/valasemanticanalyzer.vala, gobject/valacodegenerator.vala:
	  support element access for strings
	* tests/test-019.vala, tests/test-019.out: test element access for
	  strings

	Fixes bug 443523

svn path=/trunk/; revision=346
parent bf8e760d
2007-07-11 Jürg Billeter <j@bitron.ch>
* vala/valasemanticanalyzer.vala, gobject/valacodegenerator.vala:
support element access for strings
* tests/test-019.vala, tests/test-019.out: test element access for
strings
Fixes bug 443523
2007-07-11 Jürg Billeter <j@bitron.ch>
* ccode/valaccodefragment.vala, ccode/valaccodenode.vala,
......
......@@ -1677,8 +1677,24 @@ public class Vala.CodeGenerator : CodeVisitor {
int rank = indices.length ();
if (rank == 1) {
/* FIXME: had to add Expression cast due to possible compiler bug */
expr.ccodenode = new CCodeElementAccess ((CCodeExpression)expr.container.ccodenode, (CCodeExpression)((Expression)indices.first ().data).ccodenode);
var ccontainer = (CCodeExpression) expr.container.ccodenode;
// FIXME had to add Expression cast due to possible compiler bug
var cindex = (CCodeExpression) ((Expression) indices.first ().data).ccodenode;
if (expr.container.static_type.data_type == string_type.data_type) {
// access to unichar in a string
var coffsetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_utf8_offset_to_pointer"));
coffsetcall.add_argument (ccontainer);
coffsetcall.add_argument (cindex);
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_utf8_get_char"));
ccall.add_argument (coffsetcall);
expr.ccodenode = ccall;
} else {
// access to element in an array
expr.ccodenode = new CCodeElementAccess (ccontainer, cindex);
}
} else {
expr.error = true;
Report.error (expr.source_reference, "Arrays with more then one dimension are not supported yet");
......
Element access: 1 2 3 4 5 6 7 8 9
Element access: 1 2 3 4 5 6 7 8 9 10 11 12 13
......@@ -5,7 +5,7 @@ class Maman.Foo {
stdout.printf (" 2");
var sa = "a,b,c,d".split (",");
var i = 3;
int i = 3;
stdout.printf (" 3");
......@@ -22,7 +22,23 @@ class Maman.Foo {
stdout.printf (" 7");
}
stdout.printf (" 8");
string bar = "efgh";
counter = 0;
if (bar[inc()] == 'e') {
stdout.printf (" 8");
}
if (bar[inc()] == 'f') {
stdout.printf (" 9");
}
if (bar[2] == 'g') {
stdout.printf (" 10");
}
if (bar[i] == 'h') {
stdout.printf (" 11");
}
stdout.printf (" 12");
}
public int inc () {
......@@ -35,7 +51,7 @@ class Maman.Foo {
var foo = new Foo ();
foo.run ();
stdout.printf (" 9\n");
stdout.printf (" 13\n");
return 0;
}
......
......@@ -46,6 +46,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
TypeReference int_type;
TypeReference uint_type;
TypeReference ulong_type;
TypeReference unichar_type;
TypeReference type_type;
DataType pointer_type;
DataType initially_unowned_type;
......@@ -83,6 +84,9 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
ulong_type = new TypeReference ();
ulong_type.data_type = (DataType) root_symbol.lookup ("ulong").node;
unichar_type = new TypeReference ();
unichar_type.data_type = (DataType) root_symbol.lookup ("unichar").node;
// TODO: don't require GLib namespace in semantic analyzer
var glib_ns = root_symbol.lookup ("GLib");
if (glib_ns != null) {
......@@ -1458,6 +1462,14 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
expr.static_type = (TypeReference) args.data;
} else if (expr.container.static_type.data_type == string_type.data_type) {
if (expr.get_indices ().length () != 1) {
expr.error = true;
Report.error (expr.source_reference, "Element access with more than one dimension is not supported for strings");
return;
}
expr.static_type = unichar_type;
} else {
expr.error = true;
Report.error (expr.source_reference, "The expression `%s' does not denote an Array".printf (expr.container.static_type.to_string ()));
......
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