public inbox for frysk@sourceware.org
 help / color / mirror / Atom feed
From: Stan Cox <scox@redhat.com>
To: Frysk List <frysk@sourceware.org>
Subject: Re: Use StringBuilder instead of PrintWriter for type display
Date: Mon, 10 Dec 2007 22:00:00 -0000	[thread overview]
Message-ID: <1197323542.10805.196.camel@multics.rdu.redhat.com> (raw)
In-Reply-To: <4759EB1B.7040007@redhat.com>

> 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());
     }


  reply	other threads:[~2007-12-10 22:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-07 21:43 Stan Cox
2007-12-07 23:11 ` Andrew Cagney
2007-12-08  0:53   ` Andrew Cagney
2007-12-10 22:00     ` Stan Cox [this message]
2007-12-10 22:08       ` Stan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1197323542.10805.196.camel@multics.rdu.redhat.com \
    --to=scox@redhat.com \
    --cc=frysk@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).