[vala/wip/transform: 25/99] Add more to_string() methods to expressions



commit d731ff5237848a91cc159e3873abc0fb503a9955
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jan 3 13:03:53 2012 +0100

    Add more to_string() methods to expressions

 vala/valaaddressofexpression.vala     |    4 ++++
 vala/valaarraycreationexpression.vala |   18 ++++++++++++++++++
 vala/valaassignment.vala              |   21 +++++++++++++++++++++
 vala/valacastexpression.vala          |   10 ++++++++++
 vala/valaconditionalexpression.vala   |    4 ++++
 vala/valaelementaccess.vala           |   14 ++++++++++++++
 vala/valainitializerlist.vala         |   14 ++++++++++++++
 7 files changed, 85 insertions(+), 0 deletions(-)
---
diff --git a/vala/valaaddressofexpression.vala b/vala/valaaddressofexpression.vala
index 81123ee..7b4cdc5 100644
--- a/vala/valaaddressofexpression.vala
+++ b/vala/valaaddressofexpression.vala
@@ -62,6 +62,10 @@ public class Vala.AddressofExpression : Expression {
                inner.accept (visitor);
        }
 
+       public override string to_string () {
+               return "&" + inner.to_string ();
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (inner == old_node) {
                        inner = new_node;
diff --git a/vala/valaarraycreationexpression.vala b/vala/valaarraycreationexpression.vala
index 3b52748..43135b3 100644
--- a/vala/valaarraycreationexpression.vala
+++ b/vala/valaarraycreationexpression.vala
@@ -113,6 +113,24 @@ public class Vala.ArrayCreationExpression : Expression {
                return false;
        }
 
+       public override string to_string () {
+               var builder = new StringBuilder ("new ");
+               builder.append_printf ("%s[", element_type.to_string ());
+               bool first = true;
+               foreach (var size in sizes) {
+                       if (first) {
+                               builder.append (size.to_string ());
+                               first = false;
+                       } else {
+                               builder.append_printf (", %s", size.to_string ());
+                       }
+               }
+               if (initializer_list != null) {
+                       builder.append (initializer_list.to_string ());
+               }
+               return builder.str;
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                for (int i = 0; i < sizes.size; i++) {
                        if (sizes[i] == old_node) {
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index ba8d577..e394a48 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -84,6 +84,27 @@ public class Vala.Assignment : Expression {
                right.accept (visitor);
        }
 
+       public string get_operator_string () {
+               switch (_operator) {
+               case AssignmentOperator.SIMPLE: return "=";
+               case AssignmentOperator.BITWISE_OR: return "|=";
+               case AssignmentOperator.BITWISE_AND: return "&=";
+               case AssignmentOperator.BITWISE_XOR: return "^=";
+               case AssignmentOperator.ADD: return "+=";
+               case AssignmentOperator.SUB: return "-=";
+               case AssignmentOperator.MUL: return "*=";
+               case AssignmentOperator.DIV: return "/=";
+               case AssignmentOperator.PERCENT: return "%=";
+               case AssignmentOperator.SHIFT_LEFT: return "<<=";
+               case AssignmentOperator.SHIFT_RIGHT: return ">>=";
+               default: assert_not_reached ();
+               }
+       }
+
+       public override string to_string () {
+               return left.to_string () + get_operator_string () + right.to_string ();
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (left == old_node) {
                        left = new_node;
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index c7a72b6..8bca197 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -93,6 +93,16 @@ public class Vala.CastExpression : Expression {
                }
        }
 
+       public override string to_string () {
+               if (is_non_null_cast) {
+                       return "(!)"+inner.to_string ();
+               } else if (is_silent_cast) {
+                       return "%s as %s".printf (inner.to_string (), type_reference.to_string ());
+               } else {
+                       return "(%s) %s".printf (type_reference.to_string (), inner.to_string ());
+               }
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (inner == old_node) {
                        inner = new_node;
diff --git a/vala/valaconditionalexpression.vala b/vala/valaconditionalexpression.vala
index 486ecbd..7a4fdae 100644
--- a/vala/valaconditionalexpression.vala
+++ b/vala/valaconditionalexpression.vala
@@ -112,6 +112,10 @@ public class Vala.ConditionalExpression : Expression {
                false_expression.get_used_variables (collection);
        }
 
+       public override string to_string () {
+               return "%s ? %s : %s".printf (condition.to_string, true_expression.to_string (), 
false_expression.to_string ());
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (condition == old_node) {
                        condition = new_node;
diff --git a/vala/valaelementaccess.vala b/vala/valaelementaccess.vala
index b879ffc..83bf106 100644
--- a/vala/valaelementaccess.vala
+++ b/vala/valaelementaccess.vala
@@ -75,6 +75,20 @@ public class Vala.ElementAccess : Expression {
                }
        }
 
+       public override string to_string () {
+               var s = "%s[";
+               bool first = true;
+               foreach (var index in indices) {
+                       if (first) {
+                               s += index.to_string ();
+                               first = false;
+                       } else {
+                               s += ", %s".printf (index.to_string ());
+                       }
+               }
+               return s+"]";
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                if (container == old_node) {
                        container = new_node;
diff --git a/vala/valainitializerlist.vala b/vala/valainitializerlist.vala
index edd9857..6108946 100644
--- a/vala/valainitializerlist.vala
+++ b/vala/valainitializerlist.vala
@@ -96,6 +96,20 @@ public class Vala.InitializerList : Expression {
                return true;
        }
 
+       public override string to_string () {
+               var builder = new StringBuilder ("{");
+               bool first = true;
+               foreach (var initializer in initializers) {
+                       if (first) {
+                               builder.append (initializer.to_string ());
+                               first = false;
+                       } else {
+                               builder.append_printf (", %s", initializer.to_string ());
+                       }
+               }
+               return builder.str;
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                for (int i = 0; i < initializers.size; i++) {
                        if (initializers[i] == old_node) {


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