diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/PPrinter.java | 737 | ||||
| -rw-r--r-- | misc/PrintFilter.java | 15 | 
2 files changed, 752 insertions, 0 deletions
diff --git a/misc/PPrinter.java b/misc/PPrinter.java new file mode 100644 index 0000000..eff2357 --- /dev/null +++ b/misc/PPrinter.java @@ -0,0 +1,737 @@ +package misc; + +import syntaxtree.*; +import visitor.*; +import java.util.*; + +/** + * Provides default methods which visit each node in the tree in depth-first + * order.  Your visitors may extend this class. + */ +public class PPrinter<R,A> extends GJDepthFirst<R,A> { + +    private int offset; + +    private void printNode(Node n, A argu) { +        for (int i=0; i < this.offset; ++i) +            PrintFilter.print(".", false); +        PrintFilter.print(n.getClass().getSimpleName(), true); +        ++this.offset; +    } + +    // +    // User-generated visitor methods below +    // + +    /** +     * f0 -> MainClass() +     * f1 -> ( TypeDeclaration() )* +     * f2 -> <EOF> +     */ +    public R visit(Goal n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "class" +     * f1 -> Identifier() +     * f2 -> "{" +     * f3 -> "public" +     * f4 -> "static" +     * f5 -> "void" +     * f6 -> "main" +     * f7 -> "(" +     * f8 -> "String" +     * f9 -> "[" +     * f10 -> "]" +     * f11 -> Identifier() +     * f12 -> ")" +     * f13 -> "{" +     * f14 -> ( VarDeclaration() )* +     * f15 -> ( Statement() )* +     * f16 -> "}" +     * f17 -> "}" +     */ +    public R visit(MainClass n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        n.f6.accept(this, argu); +        n.f7.accept(this, argu); +        n.f8.accept(this, argu); +        n.f9.accept(this, argu); +        n.f10.accept(this, argu); +        n.f11.accept(this, argu); +        n.f12.accept(this, argu); +        n.f13.accept(this, argu); +        n.f14.accept(this, argu); +        n.f15.accept(this, argu); +        n.f16.accept(this, argu); +        n.f17.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> ClassDeclaration() +     *       | ClassExtendsDeclaration() +     */ +    public R visit(TypeDeclaration n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "class" +     * f1 -> Identifier() +     * f2 -> "{" +     * f3 -> ( VarDeclaration() )* +     * f4 -> ( MethodDeclaration() )* +     * f5 -> "}" +     */ +    public R visit(ClassDeclaration n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "class" +     * f1 -> Identifier() +     * f2 -> "extends" +     * f3 -> Identifier() +     * f4 -> "{" +     * f5 -> ( VarDeclaration() )* +     * f6 -> ( MethodDeclaration() )* +     * f7 -> "}" +     */ +    public R visit(ClassExtendsDeclaration n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        n.f6.accept(this, argu); +        n.f7.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> Type() +     * f1 -> Identifier() +     * f2 -> ";" +     */ +    public R visit(VarDeclaration n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "public" +     * f1 -> Type() +     * f2 -> Identifier() +     * f3 -> "(" +     * f4 -> ( FormalParameterList() )? +     * f5 -> ")" +     * f6 -> "{" +     * f7 -> ( VarDeclaration() )* +     * f8 -> ( Statement() )* +     * f9 -> "return" +     * f10 -> Expression() +     * f11 -> ";" +     * f12 -> "}" +     */ +    public R visit(MethodDeclaration n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        n.f6.accept(this, argu); +        n.f7.accept(this, argu); +        n.f8.accept(this, argu); +        n.f9.accept(this, argu); +        n.f10.accept(this, argu); +        n.f11.accept(this, argu); +        n.f12.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> FormalParameter() +     * f1 -> ( FormalParameterRest() )* +     */ +    public R visit(FormalParameterList n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> Type() +     * f1 -> Identifier() +     */ +    public R visit(FormalParameter n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "," +     * f1 -> FormalParameter() +     */ +    public R visit(FormalParameterRest n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> ArrayType() +     *       | BooleanType() +     *       | IntegerType() +     *       | Identifier() +     */ +    public R visit(Type n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "int" +     * f1 -> "[" +     * f2 -> "]" +     */ +    public R visit(ArrayType n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "boolean" +     */ +    public R visit(BooleanType n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "int" +     */ +    public R visit(IntegerType n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> Block() +     *       | AssignmentStatement() +     *       | ArrayAssignmentStatement() +     *       | IfStatement() +     *       | WhileStatement() +     *       | PrintStatement() +     */ +    public R visit(Statement n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "{" +     * f1 -> ( Statement() )* +     * f2 -> "}" +     */ +    public R visit(Block n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> Identifier() +     * f1 -> "=" +     * f2 -> Expression() +     * f3 -> ";" +     */ +    public R visit(AssignmentStatement n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> Identifier() +     * f1 -> "[" +     * f2 -> Expression() +     * f3 -> "]" +     * f4 -> "=" +     * f5 -> Expression() +     * f6 -> ";" +     */ +    public R visit(ArrayAssignmentStatement n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        n.f6.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "if" +     * f1 -> "(" +     * f2 -> Expression() +     * f3 -> ")" +     * f4 -> Statement() +     * f5 -> "else" +     * f6 -> Statement() +     */ +    public R visit(IfStatement n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        n.f6.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "while" +     * f1 -> "(" +     * f2 -> Expression() +     * f3 -> ")" +     * f4 -> Statement() +     */ +    public R visit(WhileStatement n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "System.out.println" +     * f1 -> "(" +     * f2 -> Expression() +     * f3 -> ")" +     * f4 -> ";" +     */ +    public R visit(PrintStatement n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> AndExpression() +     *       | CompareExpression() +     *       | PlusExpression() +     *       | MinusExpression() +     *       | TimesExpression() +     *       | ArrayLookup() +     *       | ArrayLength() +     *       | MessageSend() +     *       | PrimaryExpression() +     */ +    public R visit(Expression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "&&" +     * f2 -> PrimaryExpression() +     */ +    public R visit(AndExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "<" +     * f2 -> PrimaryExpression() +     */ +    public R visit(CompareExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "+" +     * f2 -> PrimaryExpression() +     */ +    public R visit(PlusExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "-" +     * f2 -> PrimaryExpression() +     */ +    public R visit(MinusExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "*" +     * f2 -> PrimaryExpression() +     */ +    public R visit(TimesExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "[" +     * f2 -> PrimaryExpression() +     * f3 -> "]" +     */ +    public R visit(ArrayLookup n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "." +     * f2 -> "length" +     */ +    public R visit(ArrayLength n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> PrimaryExpression() +     * f1 -> "." +     * f2 -> Identifier() +     * f3 -> "(" +     * f4 -> ( ExpressionList() )? +     * f5 -> ")" +     */ +    public R visit(MessageSend n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        n.f5.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> Expression() +     * f1 -> ( ExpressionRest() )* +     */ +    public R visit(ExpressionList n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "," +     * f1 -> Expression() +     */ +    public R visit(ExpressionRest n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> IntegerLiteral() +     *       | TrueLiteral() +     *       | FalseLiteral() +     *       | Identifier() +     *       | ThisExpression() +     *       | ArrayAllocationExpression() +     *       | AllocationExpression() +     *       | NotExpression() +     *       | BracketExpression() +     */ +    public R visit(PrimaryExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> <INTEGER_LITERAL> +     */ +    public R visit(IntegerLiteral n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "true" +     */ +    public R visit(TrueLiteral n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "false" +     */ +    public R visit(FalseLiteral n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> <IDENTIFIER> +     */ +    public R visit(Identifier n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "this" +     */ +    public R visit(ThisExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "new" +     * f1 -> "int" +     * f2 -> "[" +     * f3 -> Expression() +     * f4 -> "]" +     */ +    public R visit(ArrayAllocationExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        n.f4.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "new" +     * f1 -> Identifier() +     * f2 -> "(" +     * f3 -> ")" +     */ +    public R visit(AllocationExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        n.f3.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "!" +     * f1 -> Expression() +     */ +    public R visit(NotExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    /** +     * f0 -> "(" +     * f1 -> Expression() +     * f2 -> ")" +     */ +    public R visit(BracketExpression n, A argu) { +        this.printNode(n, argu); +        R _ret=null; +        n.f0.accept(this, argu); +        n.f1.accept(this, argu); +        n.f2.accept(this, argu); +        --this.offset; +        return _ret; +    } + +    public R visit(NodeToken n, A argu) { +        for (int i=0; i < this.offset; ++i) +            PrintFilter.print(".", false); +        PrintFilter.print(n.getClass().getSimpleName() + +			  " => " + +			  n.toString(), true); +        R _ret=null; +        return _ret; +    } + +} diff --git a/misc/PrintFilter.java b/misc/PrintFilter.java new file mode 100644 index 0000000..971ef04 --- /dev/null +++ b/misc/PrintFilter.java @@ -0,0 +1,15 @@ +package misc; + + +public class PrintFilter { + +    public static void print(String message, boolean newline) { +        boolean debug = true; +        if (debug) { +            System.out.print(message); +            if (newline) +                System.out.println(); +        } +    } + +}  | 
