Created
September 16, 2016 12:34
-
-
Save UplinkCoder/0957dff195a39fde6695f9f20a36055b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio; | |
| import std.algorithm; | |
| import std.range; | |
| import std.string; | |
| import std.file; | |
| import std.math; | |
| string visitor_boilerplate = ` | |
| module ddmd.asttypename; | |
| import ddmd.attrib; | |
| import ddmd.aliasthis; | |
| import ddmd.aggregate; | |
| import ddmd.complex; | |
| import ddmd.cond; | |
| import ddmd.ctfeexpr; | |
| import ddmd.dclass; | |
| import ddmd.declaration; | |
| import ddmd.denum; | |
| import ddmd.dimport; | |
| import ddmd.declaration; | |
| import ddmd.dstruct; | |
| import ddmd.dsymbol; | |
| import ddmd.dtemplate; | |
| import ddmd.dversion; | |
| import ddmd.expression; | |
| import ddmd.func; | |
| import ddmd.denum; | |
| import ddmd.dimport; | |
| import ddmd.dmodule; | |
| import ddmd.mtype; | |
| import ddmd.typinf; | |
| import ddmd.identifier; | |
| import ddmd.init; | |
| import ddmd.doc; | |
| import ddmd.root.rootobject; | |
| import ddmd.statement; | |
| import ddmd.staticassert; | |
| import ddmd.visitor; | |
| string astTypeName(Expression node) | |
| { | |
| scope tsv = new AstTypeNameVisitor; | |
| node.accept(tsv); | |
| return tsv.typeName; | |
| } | |
| string astTypeName(Dsymbol node) | |
| { | |
| scope tsv = new AstTypeNameVisitor; | |
| node.accept(tsv); | |
| return tsv.typeName; | |
| } | |
| string astTypeName(Statement node) | |
| { | |
| scope tsv = new AstTypeNameVisitor; | |
| node.accept(tsv); | |
| return tsv.typeName; | |
| } | |
| extern(C++) final class AstTypeNameVisitor : Visitor | |
| { | |
| alias visit = super.visit; | |
| public : | |
| string typeName; | |
| `; | |
| struct Entry | |
| { | |
| uint level; | |
| string nodeName; | |
| // populated after populate was called | |
| uint parentIdx; | |
| uint numberOfChildren; | |
| uint importantParentIdx; | |
| uint enumIdx; | |
| } | |
| Entry[] entries; | |
| //string[] importantParents["DSymbol","Type","Expression","Statement","TemplateDeclaration"]; | |
| void main() { | |
| string ch_txt = readText("ch.txt"); | |
| auto lines = ch_txt.splitLines; | |
| foreach(i,line;lines) | |
| { | |
| auto level = countTabs(line); | |
| line = line[level .. $]; | |
| if (!line.length) continue; | |
| auto nameString = line.split(' ')[0]; | |
| if(nameString.length) | |
| entries ~= Entry(level, nameString); | |
| } | |
| writeln("The DMD class hierachy has ", entries.length, " members"); | |
| foreach(i; 0 .. entries.length) | |
| findParentIdx(cast(uint)i); | |
| uint typeCount[6]; | |
| foreach(i, entry;entries) | |
| { | |
| // if (canFind()) writeln(entry); | |
| typeCount[entry.level-1]++; | |
| } | |
| writeln(typeCount/*[].map!(i => cast(int)(log2(i)+0.5))*/); | |
| writeln(visitor_boilerplate); | |
| foreach(entry;entries) | |
| { | |
| // if(!entry.numberOfChildren) | |
| { | |
| writeln(""); | |
| writeln(" override void visit(" ~ entry.nodeName ~ " node)"); | |
| writeln(" {"); | |
| writeln(" typeName = \"" ~ entry.nodeName ~ "\";"); | |
| writeln(" }"); | |
| } | |
| } | |
| } | |
| uint findParentIdx(uint entryIdx) | |
| { | |
| auto entryLevel = entries[entryIdx].level; | |
| if(entryLevel > 1) foreach_reverse(i, entry; entries[0 .. entryIdx]) | |
| { | |
| if(entryLevel > entry.level) | |
| { | |
| entries[entryIdx].parentIdx = cast(uint)i; | |
| ++entries[i].numberOfChildren; | |
| return cast(uint)i; | |
| } | |
| } | |
| return 0; | |
| } | |
| uint countTabs(ref string line) | |
| { | |
| int numTabs; | |
| while(line.length && line[numTabs] == '\t') {++numTabs;} | |
| return numTabs; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment