[vala/wip/transform: 22/93] Add more to_string() methods to expressions



commit 852e859108cd848ea464019b0c632c9e42f7628d
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 ++++++++++++++
 vala/valanamespace.vala               |    8 ++++++++
 vala/valasymbol.vala                  |    4 ++++
 9 files changed, 97 insertions(+), 0 deletions(-)
---
diff --git a/vala/valaaddressofexpression.vala b/vala/valaaddressofexpression.vala
index b537174..8d0f026 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 a433157..f5d6062 100644
--- a/vala/valaarraycreationexpression.vala
+++ b/vala/valaarraycreationexpression.vala
@@ -127,6 +127,24 @@ public class Vala.ArrayCreationExpression : Expression {
                return true;
        }
 
+       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 52648b7..1847632 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 39cbdd4..474039a 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 01888f7..5ebc21a 100644
--- a/vala/valaconditionalexpression.vala
+++ b/vala/valaconditionalexpression.vala
@@ -122,6 +122,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 0bd5ca3..8343d98 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 c1f97ca..8d05feb 100644
--- a/vala/valainitializerlist.vala
+++ b/vala/valainitializerlist.vala
@@ -106,6 +106,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) {
diff --git a/vala/valanamespace.vala b/vala/valanamespace.vala
index 47dd4ee..c253205 100644
--- a/vala/valanamespace.vala
+++ b/vala/valanamespace.vala
@@ -523,4 +523,12 @@ public class Vala.Namespace : Symbol {
 
                return !error;
        }
+
+       public override string to_string () {
+               if (name == null) {
+                       return "(root namespace)";
+               } else {
+                       return "namespace %s".printf (name);
+               }
+       }
 }
diff --git a/vala/valasymbol.vala b/vala/valasymbol.vala
index 3dcd820..a6ef1aa 100644
--- a/vala/valasymbol.vala
+++ b/vala/valasymbol.vala
@@ -467,6 +467,10 @@ public abstract class Vala.Symbol : CodeNode {
        public virtual void add_destructor (Destructor d) {
                Report.error (d.source_reference, "unexpected declaration");
        }
+
+       public override string to_string () {
+               return get_full_name();
+       }
 }
 
 public enum Vala.SymbolAccessibility {


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