001    aspect PrettyPrint {
002         /** 
003          * Print AST
004          */
005         public String ASTNode.printAST() {
006                 StringBuilder sb = new StringBuilder();
007                 printAST(sb);
008                 return sb.toString();
009         }
010        public void ASTNode.printAST(StringBuilder sb) {
011                printAST(sb, 0);
012        }
013        public void ASTNode.printAST(StringBuilder sb, int t) {
014                for (int j = 0; j < t; j++) {
015                        sb.append((j%2==0) ? " |" : " !");
016                }
017                sb.append(getClass().getName() + "\n");
018                for (int i = 0; i < getNumChild(); i++) {
019                        getChild(i).printAST(sb, t+1);
020                }
021        }
022    
023        /**
024         * Pretty print 
025         */
026        public String Program.prettyPrint() {
027                StringBuilder sb = new StringBuilder();
028                getBlock().prettyPrint(sb, 0);
029                return sb.toString();
030        }
031        public void Block.prettyPrint(StringBuilder sb, int t) {
032                sb.append("{\n");
033                for (BlockStmt bs : getBlockStmts()) {
034                        bs.prettyPrint(sb, t+1);
035                }
036                sb.append(getIndent(t)).append("}\n");
037        }
038    
039        abstract void BlockStmt.prettyPrint(StringBuilder sb, int t);
040        public void ClassDecl.prettyPrint(StringBuilder sb, int t) {
041                sb.append(getIndent(t)).append("class ").append(getName());
042                if (hasSuperclass()) {
043                        sb.append(" extends ").append(getSuperclass());
044                }
045                sb.append(" ");
046                getBody().prettyPrint(sb, t);
047        }
048        public void VarDecl.prettyPrint(StringBuilder sb, int t) {
049                sb.append(getIndent(t)).append(getType()).append(" ");
050                sb.append(getName()).append(";\n");
051        }
052        public void AssignStmt.prettyPrint(StringBuilder sb, int t) {
053                sb.append(getIndent(t)).append(getVariable()).append(" = ");
054                sb.append(getValue()).append(";\n");    
055        }
056        public void WhileStmt.prettyPrint(StringBuilder sb, int t) {
057                sb.append(getIndent(t)).append("while (").append(getCondition()).append(")\n");
058                getBody().prettyPrint(sb, t+1);
059        }
060        public void PrimitiveDecl.prettyPrint(StringBuilder sb, int t) { }
061        public void UnknownDecl.prettyPrint(StringBuilder sb, int t) { }
062    
063    
064        /**
065         * Expressions
066         */ 
067        public String IdUse.toString() {
068                return getName();
069        }
070        public String Dot.toString() {
071                return getObjectReference() + "." + getIdUse();
072        }
073        public String BooleanLiteral.toString() {
074                return getValue();
075        }
076    
077    
078        public String ASTNode.getIndent(int t) {
079                String s = "";
080                for (int i = 0; i < t; i++) {
081                        s += "\t";
082                }
083                return s;
084        }
085    }