changeset 672:d422e5f2f3ea

Added '--asttable' option to command 'statistics'.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Fri, 18 Jan 2008 00:30:14 +0100
parents f1325a4506de
children 64fec49651cf
files trunk/src/cmd/ASTStats.d trunk/src/cmd/Statistics.d trunk/src/dil/ast/DefaultVisitor.d trunk/src/main.d
diffstat 4 files changed, 89 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/cmd/ASTStats.d	Thu Jan 17 22:57:36 2008 +0100
+++ b/trunk/src/cmd/ASTStats.d	Fri Jan 18 00:30:14 2008 +0100
@@ -5,7 +5,11 @@
 module cmd.ASTStats;
 
 import dil.ast.DefaultVisitor;
-import dil.ast.Node;
+import dil.ast.Node,
+       dil.ast.Declaration,
+       dil.ast.Statement,
+       dil.ast.Expression,
+       dil.ast.Types;
 
 class ASTStats : DefaultVisitor
 {
@@ -18,5 +22,35 @@
     return table;
   }
 
-  // TODO: add visit methods.
+  // Override dispatch functions.
+override:
+  Declaration visitD(Declaration n)
+  {
+    table[n.kind]++;
+    return super.visitD(n);
+  }
+
+  Statement visitS(Statement n)
+  {
+    table[n.kind]++;
+    return super.visitS(n);
+  }
+
+  Expression visitE(Expression n)
+  {
+    table[n.kind]++;
+    return super.visitE(n);
+  }
+
+  TypeNode visitT(TypeNode n)
+  {
+    table[n.kind]++;
+    return super.visitT(n);
+  }
+
+  Node visitN(Node n)
+  {
+    table[n.kind]++;
+    return super.visitN(n);
+  }
 }
--- a/trunk/src/cmd/Statistics.d	Thu Jan 17 22:57:36 2008 +0100
+++ b/trunk/src/cmd/Statistics.d	Fri Jan 18 00:30:14 2008 +0100
@@ -7,6 +7,8 @@
 import dil.File;
 import dil.lexer.Lexer;
 import dil.lexer.Token;
+import dil.parser.Parser;
+import dil.ast.NodesEnum;
 import cmd.ASTStats;
 import common;
 
@@ -20,13 +22,16 @@
   uint commentCount;    /// Counter for comments.
   uint tokenCount;      /// Counter for all tokens produced by the Lexer.
   uint linesOfCode;     /// Number of lines.
-  uint[] tokensTable;   /// Table of counters for all token types.
+  uint[] tokensTable;   /// Table of counters for all token kinds.
+  uint[] nodesTable;    /// Table of counters for all node kinds.
 
-  static Statistics opCall(bool allocateTokensTable)
+  static Statistics opCall(bool allocateTokensTable, bool allocateNodesTable = false)
   {
     Statistics s;
     if (allocateTokensTable)
       s.tokensTable = new uint[TOK.MAX];
+    if (allocateNodesTable)
+      s.nodesTable = new uint[classNames.length];
     return s;
   }
 
@@ -42,16 +47,18 @@
     this.linesOfCode     += s.linesOfCode;
     foreach (i, count; s.tokensTable)
       this.tokensTable[i] += count;
+    foreach (i, count; s.nodesTable)
+      this.nodesTable[i] += count;
   }
 }
 
-void execute(string[] filePaths, bool printTokensTable)
+void execute(string[] filePaths, bool printTokensTable, bool printNodesTable)
 {
   Statistics[] stats;
   foreach (filePath; filePaths)
-    stats ~= getStatistics(filePath, printTokensTable);
+    stats ~= getStatistics(filePath, printTokensTable, printNodesTable);
 
-  auto total = Statistics(printTokensTable);
+  auto total = Statistics(printTokensTable, printNodesTable);
 
   foreach (i, ref stat; stats)
   {
@@ -107,22 +114,49 @@
   if (printTokensTable)
   {
     Stdout("Table of tokens:").newline;
-    Stdout.formatln(" {,10} | {}", "Count", "Token type");
+    Stdout.formatln(" {,10} | {}", "Count", "Token kind");
     Stdout("-----------------------------").newline;
     foreach (i, count; total.tokensTable)
       Stdout.formatln(" {,10} | {}", count, Token.toString(cast(TOK)i));
-    Stdout("// End of table.").newline;
+    Stdout("// End of tokens table.").newline;
+  }
+
+  if(printNodesTable)
+  {
+    Stdout("Table of nodes:").newline;
+    Stdout.formatln(" {,10} | {}", "Count", "Node kind");
+    Stdout("-----------------------------").newline;
+    foreach (i, count; total.nodesTable)
+      Stdout.formatln(" {,10} | {}", count, classNames[i]);
+    Stdout("// End of nodes table.").newline;
   }
 }
 
-Statistics getStatistics(string filePath, bool printTokensTable)
+Statistics getStatistics(string filePath, bool printTokensTable, bool printNodesTable)
 {
+  // Create a new record.
+  auto stats = Statistics(printTokensTable);
+
   auto sourceText = loadFile(filePath);
-  auto lx = new Lexer(sourceText, filePath);
-  lx.scanAll();
+  Parser parser;
+  Lexer lx;
+  if (printNodesTable)
+  {
+    parser = new Parser(sourceText, filePath);
+    auto rootNode = parser.start();
+    // Count nodes.
+    stats.nodesTable = (new ASTStats).count(rootNode);
+    lx = parser.lexer;
+  }
+  else
+  {
+    lx = new Lexer(sourceText, filePath);
+    lx.scanAll();
+  }
+
   auto token = lx.firstToken();
 
-  auto stats = Statistics(printTokensTable);
+  // Count tokens.
   // Lexer creates HEAD + Newline, which are not in the source text.
   // No token left behind!
   stats.tokenCount = 2;
--- a/trunk/src/dil/ast/DefaultVisitor.d	Thu Jan 17 22:57:36 2008 +0100
+++ b/trunk/src/dil/ast/DefaultVisitor.d	Fri Jan 18 00:30:14 2008 +0100
@@ -76,7 +76,7 @@
         value && visitE(value);
     }
     static if (is(D == DebugDeclaration) || is(D == VersionDeclaration))
-      visitD(d.decls),
+      d.decls && visitD(d.decls),
       d.elseDecls && visitD(d.elseDecls);
     static if (is(D == StaticIfDeclaration))
       visitE(d.condition),
--- a/trunk/src/main.d	Thu Jan 17 22:57:36 2008 +0100
+++ b/trunk/src/main.d	Fri Jan 18 00:30:14 2008 +0100
@@ -143,12 +143,15 @@
   case "stats", "statistics":
     char[][] filePaths;
     bool printTokensTable;
+    bool printNodesTable;
     foreach (arg; args[2..$])
-      if (arg == "--table")
+      if (arg == "--toktable")
         printTokensTable = true;
+      else if (arg == "--asttable")
+        printNodesTable = true;
       else
         filePaths ~= arg;
-    cmd.Statistics.execute(filePaths, printTokensTable);
+    cmd.Statistics.execute(filePaths, printTokensTable, printNodesTable);
     break;
   case "tok", "tokenize":
     char[] filePath;
@@ -324,7 +327,8 @@
   dil stat file.d [file2.d, ...] [Options]
 
 Options:
-  --table         : print the count of all types of tokens in a table.
+  --toktable      : print the count of all kinds of tokens in a table.
+  --asttable      : print the count of all kinds of nodes in a table.
 
 Example:
   dil stat src/dil/Parser.d src/dil/Lexer.d";