This is the mail archive of the frysk@sourceware.org mailing list for the frysk project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Use StringBuilder instead of PrintWriter for type display


> wouldn't passing in the middle part

Here is a variation that works top down, middle out instead of bottom
up left to right.  With the middle out approach, in
some situations it is necessary to go from the middle to the beginning
and end of the token, instead of the beginning and end of the entire
declaration.  For example: int (*ptr_to_func) (const int), where the
const applies to a parameter and not to the return value.  So in those
cases a temporary string buffer is used.   (This does not currently use
a specialized class.)  The diffs are comparing to the bottom up
approach.  (Both patch sets pass TestTypeEntry)


##### Similar to bottom up approach

--- /home/scox/accu/save/ArrayType.java 2007-12-07 18:15:43.000000000
-0500
+++ frysk/value/ArrayType.java  2007-12-10 11:18:30.000000000 -0500
@@ -276,9 +276,11 @@ public class ArrayType
     }
 
     public void toPrint(StringBuilder stringBuilder, int indent) {
-       type.toPrint(stringBuilder, indent);
-       stringBuilder.append(" ");
-       stringBuilder.append("%s");
+       StringBuilder tmpStringBuilder = new StringBuilder();
+       type.toPrint(tmpStringBuilder, indent);
+       if (indent == 0)
+           tmpStringBuilder.append(" ");
+       stringBuilder.insert(0, tmpStringBuilder);
        for(int i = 0; i < this.dimension.length; i++) {
            stringBuilder.append("[");
            stringBuilder.append(dimension[i]);

##### Similar to bottom up approach but uses temporary
memberStringBuilder for struct members

--- /home/scox/accu/save/CompositeType.java     2007-12-09
20:37:17.000000000 -0500
+++ frysk/value/CompositeType.java      2007-12-10 16:06:37.000000000
-0500
@@ -275,7 +275,7 @@ public abstract class CompositeType
        writer.print("}");
     }
 
-    public void toPrint(StringBuilder stringBuilder, int indentation) {
+    public void toPrint(StringBuilder stringBuilderParm, int
indentation) {
        if (indentation == 0)
            indentation = 2;
        String indentPrefix = "";
@@ -283,6 +283,7 @@ public abstract class CompositeType
            indentPrefix = indentPrefix + " ";
 
        // {class,union,struct} NAME
+       StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getPrefix());
        if (getName() != null && getName().length() > 0) {
            stringBuilder.append(" ");
@@ -314,6 +315,7 @@ public abstract class CompositeType
        Access previousAccess = null;
        stringBuilder.append(" {\n");
 
+       StringBuilder memberStringBuilder = new StringBuilder();
        while (member != null) {
            if (member.access != previousAccess) {
                previousAccess = member.access;
@@ -323,13 +325,11 @@ public abstract class CompositeType
                    stringBuilder.append(":\n");
                }
            }
-           stringBuilder.append(indentPrefix);
-           member.type.toPrint (stringBuilder, indentation + 2);
-           if (! toPrintFormat(stringBuilder, "%s", member.name)) {
-               if (stringBuilder.charAt(stringBuilder.length() - 1) !=
'*')
-                   stringBuilder.append(" ");
-               stringBuilder.append(member.name);
-           }
+           memberStringBuilder.delete(0, memberStringBuilder.length());
+           memberStringBuilder.append(" " + member.name);
+           member.type.toPrint(memberStringBuilder, indentation + 2);
+           memberStringBuilder.insert(0, indentPrefix);
+           stringBuilder.append(memberStringBuilder);
            if (member.bitSize > 0) {
                stringBuilder.append(":");
                stringBuilder.append(member.bitSize);
@@ -344,6 +344,7 @@ public abstract class CompositeType
        for (int indent = 1; indent <= indentation - 2; indent++)
            stringBuilder.append(" ");
        stringBuilder.append("}");
+       stringBuilderParm.insert(0, stringBuilder);
     }
     
     public Value member(Value var1, String member)


##### Similar to bottom up approach but uses tmpStringBuilder for
parameters

+       StringBuilder tmpStringBuilder = new StringBuilder();
+       if (stringBuilder.charAt(0) == ' ')
+           stringBuilder.deleteCharAt(0);
        if (returnType == null) 
-           stringBuilder.append("void");
+           tmpStringBuilder.insert(0, "void");
        else
-           returnType.toPrint(stringBuilder, 0);
-       stringBuilder.append(" ");
-       stringBuilder.append("%s");
+           returnType.toPrint(tmpStringBuilder, 0);
+       tmpStringBuilder.append(" ");
+       stringBuilder.insert(0, tmpStringBuilder);
        stringBuilder.append(" (");
        for (int i = 0; i < this.parmTypes.size(); i++) {
-           ((Type)this.parmTypes.get(i)).toPrint(stringBuilder, 0);
-           stringBuilder.append((String)this.parmNames.get(i));
+           tmpStringBuilder.delete(0, tmpStringBuilder.length());
+           ((Type)this.parmTypes.get(i)).toPrint(tmpStringBuilder, 0);
+           tmpStringBuilder.append((String)this.parmNames.get(i));
+           stringBuilder.append(tmpStringBuilder);
            if (i < this.parmTypes.size() - 1)
                // Not last arg
                stringBuilder.append(",");
        }
        stringBuilder.append(")");
     }


##### Similar to bottom up but needs to specialize the (*x) cases a
      bit more

--- /home/scox/accu/save/PointerType.java       2007-12-09
20:36:51.000000000 -0500
+++ frysk/value/PointerType.java        2007-12-10 15:03:34.000000000
-0500
@@ -118,21 +118,25 @@ public class PointerType extends Integer
 
     public void toPrint(StringBuilder stringBuilder, int indent) {
        // For handling int (*x)[2]
-       if (type instanceof ArrayType) {
-           ((ArrayType)type).toPrint(stringBuilder, indent);
-           toPrintFormat(stringBuilder, "%s", "(*%s)");
-       }
-       else if (type instanceof FunctionType) {
-           ((FunctionType)type).toPrint(stringBuilder, indent);
-           toPrintFormat(stringBuilder, "%s", "(*%s)");
+       if (type instanceof ArrayType || type instanceof FunctionType) {
+           if (indent > 0 && stringBuilder.length() > 0
+                   && stringBuilder.charAt(0) == ' ') {
+               stringBuilder.deleteCharAt(0);
+               stringBuilder.insert(0, " (*");
+           }
+           else
+               stringBuilder.insert(0, "(*");
+           stringBuilder.append(")");
+           type.toPrint(stringBuilder, indent);
        }
        else {
+           if (indent > 0 && stringBuilder.length() > 0
+                   && stringBuilder.charAt(0) == ' ')
+               stringBuilder.deleteCharAt(0);
+           stringBuilder.insert(0, "*");
+           if (! (type instanceof PointerType))
+               stringBuilder.insert(0, " ");
            type.toPrint(stringBuilder, indent);
-           if (! toPrintFormat(stringBuilder, "*%s", "**%s")) {
-               if (stringBuilder.charAt(stringBuilder.length() - 1) !=
'*')
-                   stringBuilder.append(" ");
-               stringBuilder.append("*");
-           }
        }


##### The other files are similar to bottom up approach but use insert
      instead of append, e.g.

--- /home/scox/accu/save/ArithmeticType.java    2007-12-06
14:20:13.000000000 -0500
+++ frysk/value/ArithmeticType.java     2007-12-10 09:42:16.000000000
-0500
@@ -70,7 +70,7 @@ public abstract class ArithmeticType
     }
 
     public void toPrint(StringBuilder stringBuilder, int indent) {
-       stringBuilder.append(getName());
+       stringBuilder.insert(0, getName());
     }



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]