]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
support element access for strings test element access for strings
authorJuerg Billeter <j@bitron.ch>
Wed, 11 Jul 2007 15:06:57 +0000 (15:06 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Wed, 11 Jul 2007 15:06:57 +0000 (15:06 +0000)
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

ChangeLog
gobject/valacodegenerator.vala
tests/test-019.out
tests/test-019.vala
vala/valasemanticanalyzer.vala

index 1a64d04d1524d38177072a300af1df5e7c71389d..dd9e99d7eac5b7405e8795dc8a1e4d9824fc0bda 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+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,
index 3d1ae542bc7ddea7a34b3a0b6583aa84a931e37c..4191377f23e155e43b21031cc04ef1cfca817ef6 100644 (file)
@@ -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");
index 3cad519f4c7cd9ba6621b37a74b22d8f0ec3b983..b57b47c27c610e5b7af5095464bc046324724ffe 100644 (file)
@@ -1 +1 @@
-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
index 50fb1595720d0b3590e4cdc1d944ea62c36c1efc..bf906a93f8644dc51069eaff4d4cd260e00b0b57 100644 (file)
@@ -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;
        }
index 1a1b61e7d29f6647b8e0f607aded85cfc8be55ce..4ee568e746cb032b5f7ba17354a89befc5f7fb4b 100644 (file)
@@ -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 ()));