惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
腾讯CDC
博客园_首页
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗

博客园 - Wisdom-zh

【关注】“诋毁” 一下博客园的模板 混淆器【修正版】 发一个混淆器, Nuva 写的 Win32Ole 演示【控制 Word】 Win32Api 演示【控制任务栏】 通用数据浏览示例 方便的 XML 数据绑定 Swap 函数 延迟计算 短路运算 语法兼容 之 运算符总结 这是什么语言的代码 Nuva 语法兼容 之 控制结构总结 设计模式 之 状态机模式 Nuva 示例代码(每日一帖)之 数据架构提取 Nuva 示例代码(每日一帖)之 ShowMessage Nuva 示例代码(每日一帖)之 异常处理 Nuva 示例代码(每日一帖)之 语法兼容 挑战 XSLT 最擅长的 XML 文档转换
Nuva 示例代码(每日一帖)之 源代码统计
Wisdom-zh · 2006-09-14 · via 博客园 - Wisdom-zh
<..========================================================
==                                                       ==
==                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 = ''
assign(htmlText)
  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
  foreach(fileName = System.File.Find(FilePattern, '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.Load(fileName)
    // 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 CountCode(Index, Length, Value)
      codeCount ++
      if( IsComment(Value) )
        commentCount++
      elseif(not Value.RegexIsMatch('\w+')) // [^\s] or \w+
        blankCount++
      end if
    end function
        
    /*
      Detect comment
    */
    function IsComment(lineText)
      result = true
      if(lineText.RegexIsMatch('^\s*//'))
        // do nothing
      elseif(inComment)
        inComment = not lineText.RegexIsMatch('\*/')
      elseif(lineText.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 = Form1()
f.Show()

System.App.Run()
.>


<..
【简介】
    本例是一个实用的源代码统计程序。本例的程序针对给定的目录和文件通配符,对每个文件进行代码统计,统计指标包括代码行数、空行数、注释行数等。然后将其按照 Html 的形式显示出来。

【看点】
    1、本例演示了一个源代码统计程序,通过对每个文件的分析,统计出源代码行数、空行数以及注释行数。
       本例的重点在于注释的分析,见 IsComment 函数。
      
       其他的代码分析请参见<<每日一帖>>中的其他示例分析。
      
    2、本例最后通过一个 Html 窗口将统计结果显示出来,演示了 Nuva 语言的 GUI 构造过程。

【扩展】
    本例是一个源代码统计程序,可以对其进行扩充,从而统计出更多的信息。
..>