Author Topic: Quick code summaries  (Read 1955 times)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2646
    • View Profile
Quick code summaries
« on: 2016-Sep-15 »
I document the old fashioned way, that is, via text files.

This little program extracts the bits I document for a directory of GBAS files and writes them to a new subdir within that directory when its done.

It extracts from each GBAS file...

Constants
Globals
Global Functions
Types
Type Properties
Type Methods

Sorts them and writes them to a .TXT file.

See end for an example.

Code: (glbasic) [Select]
// --------------------------------- //
// Project: GLBDocumenter
// Start: Wednesday, August 24, 2016
// IDE Version: 14.371


GLOBAL dir$ = "C:/Coding Folder/Projects/GLBasic/plasma/" // Scan dir
GLOBAL docsDir$ = "GLBDocs" // Where to store generated text files
GLOBAL sorting% = TRUE // Use alphabetic sorting

GLOBAL files[] AS TFile // Holds all the file data

TYPE TFile
filename$
dir$
globals$[]
constants$[]
functions$[]
types[] AS TType
text$[]
ENDTYPE

TYPE TType
name$
properties$[]
methods$[]
ENDTYPE

// Do it

ReadInFiles( files[], dir$ )
ProcessFiles( files[] )
WriteFileData( files[] )

END


INLINE
/*-------------------------------------------------------------------------------------*\
| |
|                                     READ A Dir                                        |
| |
\*-------------------------------------------------------------------------------------*/
ENDINLINE
// Scan a dir reading in all the samples
FUNCTION ReadDir: dir$ = "", ext$ = "GBAS"
LOCAL aFile AS TFile, gbasFileList$[]
aFile.dir$ = dir$
IF dir$="" THEN dir$ = GETCURRENTDIR$()
LOCAL saveCurrentDir$ = GETCURRENTDIR$()
SETCURRENTDIR( dir$ )
GETFILELIST( "*.*", gbasFileList$[] )
FOREACH filename$ IN gbasFileList$[]
IF UCASE$ ( RIGHT$( filename$, 4) ) = UCASE$( ext$ )
aFile.filename$ = filename$
DIMPUSH files[], aFile
DEBUG "File: " + aFile.filename$ + "\n"
ENDIF
NEXT
SETCURRENTDIR( saveCurrentDir$ )
ENDFUNCTION



INLINE
/*-------------------------------------------------------------------------------------*\
| |
|                                     Read In Text                                      |
| |
\*-------------------------------------------------------------------------------------*/
ENDINLINE
// Scan a dir reading in all the samples
FUNCTION ReadInFiles: files[] AS TFile, dir$
LOCAL f = GENFILE(), a$, t$[], n%, rv#
ReadDir( dir$ )
IF dir$="" THEN dir$=GETCURRENTDIR$()
LOCAL saveCurrentDir$=GETCURRENTDIR$()
SETCURRENTDIR(dir$)
IF NOT ( DOESDIREXIST( docsDir$ ) ) THEN SHELLCMD("cmd.exe /c mkdir " + docsDir$, FALSE, FALSE, rv#)
FOREACH file IN files[]
OPENFILE(f, file.dir$ + file.filename$, TRUE)
WHILE NOT ( ENDOFFILE( f ) )
READLINE f, a$
// Cheack and split ";"
IF SPLITSTR( a$, t$[], ";" ) > 0
FOREACH a$ IN t$[]
DIMPUSH file.text$[], a$
NEXT
ELSE
DIMPUSH file.text$[], a$
ENDIF
WEND
CLOSEFILE f
NEXT
SETCURRENTDIR( saveCurrentDir$ )
ENDFUNCTION



INLINE
/*-------------------------------------------------------------------------------------*\
| |
|                                     Process Files                                     |
| |
\*-------------------------------------------------------------------------------------*/
ENDINLINE
// Scan a dir reading in all the samples
FUNCTION ProcessFiles: files[] AS TFile
LOCAL n%, t$[], inType%, inFunc%,typeStart%, inInline%, typ AS TType
FOREACH f IN files[]
//DEBUG "\n\nFILE: \"" + f.filename$ + "\"\n"
inType% = FALSE; inFunc% = FALSE
FOREACH a$ IN f.text$[]
n% = SPLITSTR( a$, t$[], ", \t" )
IF LEN( t$[] ) = 0 THEN CONTINUE
IF LEFT$( t$[0], 2 ) = "//" THEN CONTINUE
SELECT t$[0]

CASE "TYPE"
inType% = TRUE
typeStart% = TRUE
DIMPUSH f.types[], typ
f.types[ -1 ].name$ = t$[1]
//DEBUG "\n TYPE: "+f.types[ -1 ].name$ + "\n"

CASE "ENDTYPE"
inType% = FALSE
typeStart% = FALSE

CASE "FUNCTION"
inFunc% = TRUE
typeStart% = FALSE
IF inType%
DIMPUSH f.types[ -1 ].methods$[], Func$( a$ )
//DEBUG " m:"+f.types[ -1 ].methods$[-1]+"\n"
ELSE
DIMPUSH f.functions$[], Func$( a$ )
//DEBUG " f:"+f.functions$[-1]+"\n"
ENDIF

CASE "ENDFUNCTION"
inFunc% = FALSE

CASE "GLOBAL"
DIMPUSH f.globals$[], LTRIM$( MID$( a$, INSTR( a$, "GLOBAL" ) + 6, -1 ) , " \t" )
//DEBUG " G:"+f.globals$[-1]+"\n"

CASE "CONSTANT"
DIMPUSH f.constants$[], LTRIM$( MID$( a$, INSTR( a$, "CONSTANT" ) + 8, -1 ) , " \t" )
//DEBUG " C:"+f.constants$[-1]+"\n"

CASE "//"

CASE "INLINE"
inInline% = TRUE

CASE "ENDINLINE"
inInline% = FALSE

DEFAULT
IF inType% AND typeStart% AND NOT ( inInline% )
DIMPUSH f.types[ -1 ].properties$[], LTRIM$( a$ , " \t" )
//DEBUG " P:" + f.types[ -1 ].properties$[ -1 ] + "\n"
ENDIF
ENDSELECT
NEXT
//
NEXT
ENDFUNCTION



INLINE
/*-------------------------------------------------------------------------------------*\
| |
|                                Process a Function Call                                |
| |
\*-------------------------------------------------------------------------------------*/
ENDINLINE
// Scan a dir reading in all the samples
FUNCTION Func$: a$
LOCAL b$ = LTRIM$( MID$( a$, INSTR( a$, "FUNCTION" ) + 8, -1 ) , " \t" )
b$ = REPLACE$ (b$, ":"," ( " )
IF INSTR( b$, "//" ) = -1
b$ = b$ + " )"
ELSE
b$ = REPLACE$ (b$, "//"," )  //" )
ENDIF
b$ = REPLACE$( b$, " (  )", "()" )
b$ = REPLACE$( b$, "(  ", "( " )
b$ = REPLACE$( b$, "  )", " )" )
RETURN b$
ENDFUNCTION


INLINE
/*-------------------------------------------------------------------------------------*\
| |
|                                Process File Data                                      |
| |
\*-------------------------------------------------------------------------------------*/
ENDINLINE

// Scan a dir reading in all the samples
FUNCTION WriteFileData: files[] AS TFile
LOCAL n%, f%, r
FOREACH file IN files[]
// Open file
OPENFILE( f%, file.dir$ + docsDir$ + "/" + file.filename$ + ".txt", FALSE)
WRITELINE f%, "FILENAME: " + file.dir$ + file.filename$
// Constants
WRITELINE f%,""; WRITELINE f%, "CONSTANTS:"
IF sorting% THEN SORTARRAY file.constants$[], ADDRESSOF(compare)
FOREACH c$ IN file.constants$[]
WRITELINE f%, "\t" + c$
NEXT
// Globals
WRITELINE f%,""; WRITELINE f%, "GLOBALS:"
IF sorting% THEN SORTARRAY file.globals$[], ADDRESSOF(compare)
FOREACH g$ IN file.globals$[]
WRITELINE f%, "\t" + g$
NEXT
// Global Functions
WRITELINE f%,""; WRITELINE f%, "GLOBAL FUNCTIONS:"
IF sorting% THEN SORTARRAY file.functions$[], ADDRESSOF(compare)
FOREACH f$ IN file.functions$[]
WRITELINE f%, "\t" + f$
NEXT
// Types
WRITELINE f%,""; WRITELINE f%, "TYPES:"
FOREACH t IN file.types[]
WRITELINE f%, "\t" + t.name$ + ":"
// Properties
WRITELINE f%, "\t\tPROPERTIES:"
IF sorting% THEN SORTARRAY t.properties$[], ADDRESSOF(compare)
FOREACH p$ IN t.properties$[]
WRITELINE f%, "\t\t\t." + p$
NEXT
// Methods
WRITELINE f%, "\t\tMETHODS:"
IF sorting% THEN SORTARRAY t.methods$[], ADDRESSOF(compare)
FOREACH m$ IN t.methods$[]
WRITELINE f%, "\t\t\t." + m$
NEXT
NEXT
WRITELINE f%,""
CLOSEFILE f%
NEXT
ENDFUNCTION


INLINE
/*-------------------------------------------------------------------------------------*\
| |
|                                     Sort Function                                     |
| |
\*-------------------------------------------------------------------------------------*/
ENDINLINE

FUNCTION compare: BYREF a$, BYREF b$
   IF ASC( LCASE$( a$ ) ) < ASC( LCASE$( b$ ) ) THEN RETURN -1
   IF ASC( LCASE$( a$ ) ) > ASC( LCASE$( b$ ) ) THEN RETURN 1
   RETURN 0
ENDFUNCTION


An example of the output.

Code: (glbasic) [Select]
FILENAME: C:/Coding Folder/Projects/GLBasic/plasma/2Dmesh.gbas

CONSTANTS:
ANITYPE_2D_ROTATE = 0
RENDERFX_2D_QUICK = 2
RENDERFX_2D_NORMAL = 0
RENDERFX_2D_COLOURED = 1

GLOBALS:
meshes2D AS T2DMeshes

GLOBAL FUNCTIONS:

TYPES:
T2DMeshes:
PROPERTIES:
.batch AS TVBeam
.models AS T2DModels
.m[] AS T2DMesh
.total2DMeshes%
METHODS:
.Add ( m AS T2DMesh )
.Destroy()
.Del ( mNum% )
.New()
.Render ( batch AS TVBeam, useModels AS TModels )
.SetUp()
.Update()
T2DMesh:
PROPERTIES:
.aniRotSpd#
.aniType%
.baseRadius# // Radius of unscaled Mesh
.drawFlag% = TRUE
.isInterpolatedFlag% = TRUE
.isRotatededFlag% = TRUE
.isScaledFlag% = TRUE
.lineAlpha% = 255
.lineColour% = 0xffffff
.lineWidth# = 8
.lineCombined% = 0xffffff00
.matrix#[]
.mdlNum%
.oldScl AS TVec2
.oldPos AS TVec2
.oldRot AS TVec2
.pos AS TVec2
.rot AS TVec2
.radius# // Radius of scaled 2DMesh(based on baseRadius)
.realTimeScalingFlag% = FALSE
.renderFX%
.scl AS TVec2
.vAniRotDir AS TVec2
METHODS:
.Animate()
.AniRotate()
.CacheTransform()
.CalcRadius ( )  // TODO ( Wrong!
.Destroy()
.Hide()
.RenderColoured ( batch AS TVBeam, use2DModels AS T2DModels = models2D )
.RenderColouredOLD ( batch AS TVBeam, use2DModels AS T2DModels = models2D )
.RenderNormal ( batch AS TVBeam, use2DModels AS T2DModels = models2D )
.Render ( batch AS TVBeam, use2DModels AS T2DModels = models2D )
.RenderColoured2 ( batch AS TVBeam, use2DModels AS T2DModels = models2D )
.RenderQuick ( batch AS TVBeam, use2DModels AS T2DModels = models2D )
.sRotateX ( x# )
.sRotateY ( y# )
.sMove ( x#, y# )
.SetUp ( use2DModels AS T2DModels = models2D )
.sRotate ( x#, y# )
.sScale ( x#, y# )
.SetRadius ( baseRadius# )
.Show()
.Set()
.sMoveTo ( x#, y# )
.sTurn ( x#, y# )
.sScaleRel ( x#, y# )
.Update()
.vMoveTo ( pos AS TVec2 )
.vRotate ( rot AS TVec2 )
.vTurn ( rot AS TVec2 )
.vMove ( pos AS TVec2 )
.vScaleRel ( scl AS TVec2 )
.vScale ( scl AS TVec2 )
« Last Edit: 2016-Sep-15 by bigsofty »
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)