Skip to content

Commit

Permalink
Merge pull request #6851 from WalterBright/printast
Browse files Browse the repository at this point in the history
refactor: add printast.d
merged-on-behalf-of: Daniel Murphy <yebblies@gmail.com>
  • Loading branch information
dlang-bot authored Jun 3, 2017
2 parents 34c24aa + 1d85c99 commit 6c9afb9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 46 deletions.
42 changes: 1 addition & 41 deletions src/ddmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import ddmd.nspace;
import ddmd.opover;
import ddmd.optimize;
import ddmd.parse;
import ddmd.printast;
import ddmd.root.ctfloat;
import ddmd.root.file;
import ddmd.root.filename;
Expand Down Expand Up @@ -2603,18 +2604,6 @@ extern (C++) abstract class Expression : RootObject
return buf.extractString();
}

/********************
* Print AST data structure in a nice format.
* Params:
* indent = indentation level
*/
void printAST(int indent = 0)
{
foreach (i; 0 .. indent)
printf(" ");
printf("%s %s\n", Token.toChars(op), type ? type.toChars() : "");
}

final void error(const(char)* format, ...) const
{
if (type != Type.terror)
Expand Down Expand Up @@ -6468,14 +6457,6 @@ extern (C++) class SymbolExp : Expression
{
v.visit(this);
}

override void printAST(int indent)
{
Expression.printAST(indent);
foreach (i; 0 .. indent + 2)
printf(" ");
printf(".var: %s\n", var ? var.toChars() : "");
}
}

/***********************************************************
Expand Down Expand Up @@ -7878,12 +7859,6 @@ extern (C++) class UnaExp : Expression
{
v.visit(this);
}

override void printAST(int indent)
{
Expression.printAST(indent);
e1.printAST(indent + 2);
}
}

extern (C++) alias fp_t = UnionExp function(Loc loc, Type, Expression, Expression);
Expand Down Expand Up @@ -8205,13 +8180,6 @@ extern (C++) abstract class BinExp : Expression
{
v.visit(this);
}

override void printAST(int indent)
{
Expression.printAST(indent);
e1.printAST(indent + 2);
e2.printAST(indent + 2);
}
}

/***********************************************************
Expand Down Expand Up @@ -9528,14 +9496,6 @@ extern (C++) final class DelegateExp : UnaExp
{
v.visit(this);
}

override void printAST(int indent)
{
UnaExp.printAST(indent);
foreach (i; 0 .. indent + 2)
printf(" ");
printf(".func: %s\n", func ? func.toChars() : "");
}
}

/***********************************************************
Expand Down
3 changes: 0 additions & 3 deletions src/ddmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ class Expression : public RootObject

void print();
const char *toChars();
virtual void printAST(int ident = 0);
void error(const char *format, ...) const;
void warning(const char *format, ...) const;
void deprecation(const char *format, ...) const;
Expand Down Expand Up @@ -704,7 +703,6 @@ class UnaExp : public Expression
Expression *resolveLoc(Loc loc, Scope *sc);

void accept(Visitor *v) { v->visit(this); }
void printAST(int ident);
};

typedef UnionExp (*fp_t)(Type *, Expression *, Expression *);
Expand All @@ -731,7 +729,6 @@ class BinExp : public Expression
Expression *reorderSettingAAElem(Scope *sc);

void accept(Visitor *v) { v->visit(this); }
void printAST(int ident);
};

class BinAssignExp : public BinExp
Expand Down
90 changes: 90 additions & 0 deletions src/ddmd/printast.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (c) 1999-2017 by Digital Mars, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(DMDSRC _printast.d)
*/

module ddmd.printast;

import core.stdc.stdio;

import ddmd.expression;
import ddmd.tokens;
import ddmd.visitor;

/********************
* Print AST data structure in a nice format.
* Params:
* e = expression AST to print
* indent = indentation level
*/
void printAST(Expression e, int indent = 0)
{
scope PrintASTVisitor pav = new PrintASTVisitor(indent);
e.accept(pav);
}

private:

extern (C++) final class PrintASTVisitor : Visitor
{
alias visit = super.visit;

int indent;

extern (D) this(int indent)
{
this.indent = indent;
}

override void visit(Expression e)
{
printIndent(indent);
printf("%s %s\n", Token.toChars(e.op), e.type ? e.type.toChars() : "");
}

override void visit(StructLiteralExp e)
{
printIndent(indent);
printf("%s %s, %s\n", Token.toChars(e.op), e.type ? e.type.toChars() : "", e.toChars());
}

override void visit(SymbolExp e)
{
visit(cast(Expression)e);
printIndent(indent + 2);
printf(".var: %s\n", e.var ? e.var.toChars() : "");
}

override void visit(UnaExp e)
{
visit(cast(Expression)e);
printAST(e.e1, indent + 2);
}

override void visit(BinExp e)
{
visit(cast(Expression)e);
printAST(e.e1, indent + 2);
printAST(e.e2, indent + 2);
}

override void visit(DelegateExp e)
{
visit(cast(Expression)e);
printIndent(indent + 2);
printf(".func: %s\n", e.func ? e.func.toChars() : "");
}

static void printIndent(int indent)
{
foreach (i; 0 .. indent)
putc(' ', stdout);
}
}


2 changes: 1 addition & 1 deletion src/posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ FRONT_SRCS=$(addsuffix .d, $(addprefix $D/,access aggregate aliasthis apply argt
hdrgen impcnvtab imphint init inline inlinecost intrange \
json lib link mars mtype nogc nspace objc opover optimize parse sapply \
sideeffect statement staticassert target traits visitor \
typinf utils statement_rewrite_walker statementsem staticcond safe blockexit asttypename))
typinf utils statement_rewrite_walker statementsem staticcond safe blockexit asttypename printast))

LEXER_SRCS=$(addsuffix .d, $(addprefix $D/, console entity errors globals id identifier lexer tokens utf))

Expand Down
2 changes: 1 addition & 1 deletion src/win32.mak
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ FRONT_SRCS=$D/access.d $D/aggregate.d $D/aliasthis.d $D/apply.d $D/argtypes.d $D
$D/impcnvtab.d $D/init.d $D/inline.d $D/inlinecost.d $D/intrange.d $D/json.d $D/lib.d $D/link.d \
$D/mars.d $D/mtype.d $D/nogc.d $D/nspace.d $D/objc.d $D/opover.d $D/optimize.d $D/parse.d \
$D/sapply.d $D/sideeffect.d $D/statement.d $D/staticassert.d $D/target.d \
$D/safe.d $D/blockexit.d $D/asttypename.d \
$D/safe.d $D/blockexit.d $D/asttypename.d $D/printast.d \
$D/traits.d $D/utils.d $D/visitor.d $D/libomf.d $D/scanomf.d $D/typinf.d \
$D/libmscoff.d $D/scanmscoff.d $D/statement_rewrite_walker.d $D/statementsem.d $D/staticcond.d

Expand Down

0 comments on commit 6c9afb9

Please sign in to comment.