Nuva 示例代码(每日一帖)之 源代码统计
<..========================================================
== ==
== Macrobject Nuva Samples ==
== ==
== Copyright (c) 2004-2006 Macrobject Software ==
== ==
== ALL RIGHTS RESERVED ==
== ==
== http://www.macrobject.com ==
== ==
========================================================..>
<.
var FilePattern = System.Path.ProjectPath ~ '..\*.nuva*'
var IncludeSubDir = true
var htmlText = ''
assignhtmlText var FileCount = 0
var CountCode = 0
var CountBlank = 0
var CountComment = 0
.>
<style>
td {font-size: 10.5pt}
</style>
<table align=center border=1 borderColor=#CCCCCC cellspacing=0>
<tr align=center>
<td width=30>No</td>
<td width=60>Code</td>
<td width=60>Blank</td>
<td width=60>Comment</td>
<td>FileName</td>
</tr>
<.
// Count code for each file
foreachfileName = System.File.FindFilePattern, 'F', IncludeSubDir | not fileName.EndsWith'.nuvaproj' FileCount++
var codeCount = 0
var blankCount = 0
var commentCount = 0
var inComment = false
// Load file for text
var fileText = \n ~ System.File.LoadfileName // Count code use Regex Expression
fileText.RegexMatch'\n[^\n]*', Function = 'CountCode' .>
<tr>
<td align=center>[.=FileCount.]</td>
<td align=right>[.=codeCount.]</td>
<td align=right>[.=blankCount.]</td>
<td align=right>[.=commentCount.]</td>
<td>[.=fileName.]</td>
</tr>
<.
// Do Count: Value <- for each Line
function CountCodeIndex, Length, Value codeCount ++
if IsCommentValue commentCount++
elseifnot Value.RegexIsMatch'\w+' // [^\s] or \w+
blankCount++
end if
end function
/*
Detect comment
*/
function IsCommentlineText result = true
iflineText.RegexIsMatch'^\s*//' // do nothing
elseifinComment inComment = not lineText.RegexIsMatch'\*/' elseiflineText.RegexIsMatch'/\*' inComment = not lineText.RegexIsMatch'\*/' else
result = false
end if
end function
CountCode += codeCount
CountBlank += blankCount
CountComment += commentCount
end foreach
.>
<tr>
<td align=center>Count</td>
<td align=right>[.=CountCode.]</td>
<td align=right>[.=CountBlank.]</td>
<td align=right>[.=CountComment.]</td>
<td>[.=CountCode-CountBlank-CountComment.]</td>
</tr>
</table>
<.
end assign
class Form1 = System.Ui.HtmlForm Caption = 'Code Count'
Width = 800
Height = 600
function Create ? htmlText
end function
end class
var f = Form1f.ShowSystem.App.Run.>
<..
【简介】
本例是一个实用的源代码统计程序。本例的程序针对给定的目录和文件通配符,对每个文件进行代码统计,统计指标包括代码行数、空行数、注释行数等。然后将其按照 Html 的形式显示出来。
【看点】
1、本例演示了一个源代码统计程序,通过对每个文件的分析,统计出源代码行数、空行数以及注释行数。
本例的重点在于注释的分析,见 IsComment 函数。
其他的代码分析请参见<<每日一帖>>中的其他示例分析。
2、本例最后通过一个 Html 窗口将统计结果显示出来,演示了 Nuva 语言的 GUI 构造过程。
【扩展】
本例是一个源代码统计程序,可以对其进行扩充,从而统计出更多的信息。
..>