Line count, version 3

Post Reply
User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Line count, version 3

Post by Dutchman »

This program counts lines of code in a file.
Comments and lines in included files {such as this} are not taken into account.
Set Filename$ to the name of the file to be analyzed.
If test=1 then the program prints its own code lines.

Code: Select all

REM Line count, by Dutchman, july 2015, Version 3
' program counts user-defined functions and
' lines of code, exclusive comments!
' Code in included files is not taken into account!

'---- Input filename
Filename$="/Examples/Games/MAZER.txt" REM user setting

'---- Options
test=0 '1=testmode
OPTION BASE 1
SET OUTPUT FONT SIZE 12

'---- Main
IF test THEN Filename$="Line count.txt"
CALL LineCount(FileName$)
PRINT "'"&FileName$&"' has "&STR$(LineCount.text,"#")&" lines."
PRINT ,LineCount.code;" lines of code, exclusive include-files and comments."
IF LineCount.functions>0 THEN PRINT ,LineCount.functions;" user-defined functions."
END
'
/* this section
should not be
interpreted as command lines
*/Functions:/*this will also be skipped */
DEF LineCount(id$)
IF NOT FILE_EXISTS(id$) THEN
  PRINT "File '";id$;"' not found."
  STOP
ENDIF
text=0 ' number of textlines
code=0 ' number of code lines
functions=0 ' number of functions
WHILE NOT FILE_END(Id$)
  FILE Id$ READLINE Line$ ! text+=1
SkipEmpty:
  IF Line$="" THEN NextLine
'---- find and cut "/*" section
  cut=INSTR(Line$,"/*",1)
  IF cut>0 THEN
     skipped=INSTR(Line$,"*/",1)
     Line$=LEFT$(Line$,cut-1)
  ENDIF
'---- extract command lines
  SPLIT Line$ TO List$,n WITH "!"
  '---- check empty lines and comments
  FOR i=1 TO n
    SPLIT LOWSTR$(LTRIM$(List$(i))) TO w$,j WITH " "
    IF j>0 THEN c$=LEFT$(w$(1),1)
    IF j=0 OR c$="""" OR c$="'" OR w$(1)="rem" THEN
    ELSE
      code+=1 ! IF .test THEN PRINT ,List$(i)
      IF j>1 AND w$(1)&w$(2)="enddef" THEN functions+=1
    ENDIF
  NEXT i
'---- find and skip to "*/"
  IF skipped<0 THEN
    DO
      IF NOT FILE_END(Id$) THEN 
        FILE Id$ READLINE Line$ ! text+=1
        skipped=INSTR(Line$,"*/",1)
      ENDIF
    UNTIL skipped>0
  Line$=RIGHT$(Line$,LEN(Line$)-skipped-1)
  GOTO SkipEmpty 'check rest
  ENDIF
NextLine:
END WHILE
END DEF
Last edited by Dutchman on Sun Jul 19, 2015 12:54 pm, edited 2 times in total.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Line count, version 2

Post by Dutchman »

The program did not extract sections between "/*" and "*/".
That has been corrected in version 2.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Line count, version 3

Post by Dutchman »

The program counts now also the number of user-defined subroutines.

Post Reply