363
|
1 /++
|
|
2 Author: Aziz Köksal
|
|
3 License: GPL3
|
|
4 +/
|
|
5 module cmd.Statistics;
|
|
6 import dil.Token;
|
|
7 import dil.File;
|
|
8 import dil.Lexer;
|
|
9 import std.stdio;
|
|
10
|
|
11 struct Statistics
|
|
12 {
|
|
13 uint whitespaceCount;
|
|
14 uint wsTokenCount;
|
|
15 uint keywordCount;
|
|
16 uint identCount;
|
|
17 uint numberCount;
|
|
18 uint commentCount;
|
|
19 }
|
|
20
|
|
21 void statistics(string fileName)
|
|
22 {
|
|
23 auto sourceText = loadFile(fileName);
|
|
24 auto lx = new Lexer(sourceText, fileName);
|
|
25
|
|
26 auto token = lx.getTokens();
|
|
27 char* end = lx.text.ptr;
|
|
28
|
|
29 Statistics stats;
|
|
30 // Traverse linked list.
|
|
31 while (token.type != TOK.EOF)
|
|
32 {
|
|
33 token = token.next;
|
|
34
|
|
35 // Count whitespace characters
|
|
36 if (end != token.start)
|
|
37 {
|
|
38 stats.whitespaceCount += token.start - end;
|
|
39 }
|
|
40
|
|
41 switch (token.type)
|
|
42 {
|
|
43 case TOK.Identifier:
|
|
44 stats.identCount++;
|
|
45 break;
|
|
46 case TOK.Comment:
|
|
47 stats.commentCount++;
|
|
48 break;
|
|
49 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64,
|
|
50 TOK.Float32, TOK.Float64, TOK.Float80,
|
|
51 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80:
|
|
52 stats.numberCount++;
|
|
53 break;
|
|
54 default:
|
|
55 if (token.isKeyword)
|
|
56 stats.keywordCount++;
|
|
57 }
|
|
58
|
|
59 if (token.isWhitespace)
|
|
60 stats.wsTokenCount++;
|
|
61
|
|
62 end = token.end;
|
|
63 }
|
|
64 writefln("Whitespace character count: %s\n"
|
|
65 "Whitespace token count: %s\n"
|
|
66 "Keyword count: %s\n"
|
|
67 "Identifier count: %s\n"
|
|
68 "Number count: %s\n"
|
|
69 "Comment count: %s\n"
|
|
70 "Lines of code: %s",
|
|
71 stats.whitespaceCount,
|
|
72 stats.wsTokenCount,
|
|
73 stats.keywordCount,
|
|
74 stats.identCount,
|
|
75 stats.numberCount,
|
|
76 stats.commentCount,
|
|
77 lx.loc);
|
|
78 }
|