#193261 - 2009-03-30 08:37 PM
Enhancing my Sanity
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
(the UDF, that is...)
This post illustrates where Sanity can be helpful. I have recently enhanced the UDF to alert for unterminated command pairs (If/EndIf, Do/Until, While/Loop, and Select/EndSelect) and had been scouring the board for examples of "insane" code. The raw code in the above post has over 250 levels of unterminated IF statements.
The question I have is - what is a REASONABLE level of open If, While, Do, and Select statements? I currently permit 40 levels in the UDF, after which the UDF breaks down and cries (at the condition of your code, and the insanity of it all, not that it can't keep up). Actually, Sanity simply aborts with a "Too many open levels- check your code!" error.
So - is 40 open If statements a reasonable limit? I apply the same limit to the other command-pairs, even though I belive that 40 is too excessive (by about 38!) for nested loops and selects. Personally, I've never used more than 4-5 nested levels of If or loops, even in the complex code projects, but I want to provide something that is useful to all.
The new Sanity will generate an additional report, something like this: Searching for available UDFs.......................
187 UDFs located in 112 files.
No UDFs needed for this generation!
Generation of test2.kix is complete!
000001: Main
000005: If $X = 1
000007: EndIf
000009: If $X = 1
000010: While $X < 2
000011: If $X = 1
000013: Loop
- WARNING: Unterminated If when closing While!
000014: EndIf
000015: EndIf
000020: Function: X1
000022: If $X = 1
000024: EndIf
000026: While/Loop
000029: While $X < 10
000031: Loop
000033: While $X < 10
000038: EndFunction X1
- WARNING: Unterminated While when closing function!
000041: Function: X2
- WARNING: Open If, While, Do or Select when defining function.
000043: If/EndIf
000046: Select
000051: EndSelect
- WARNING: Unterminated While when closing Select!
000053: Select
000067: EndFunction X2
- WARNING: Unterminated Select when closing function!
000069: Function: x3
- WARNING: Open If, While, Do or Select when defining function.
000071: If $X = 1
WARNING: Missing EndFunction!
WARNING: 1 Unterminated If statement(s)!
WARNING: 1 Unterminated While statement(s)!
WARNING: 1 Unterminated Select statement(s)!
8 warnings generated, 73 lines processed. This output was from KGen. You can see that it indents the code fragments by 1 space per level, making it easy to catch the mismatched items. The actual line containing the If or While is displayed in the report. Between that and the line number it should be easy to locate in your source file.
Comments? Suggestions?
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#193299 - 2009-04-01 10:41 AM
Re: Enhancing my Sanity
[Re: Glenn Barnas]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Aren't you just pushing the open construct onto a LIFO/push-down stack and popping it off when a matching close construct is found?
If so it should be trivial to make the stacked item a compound of the open construct and the source line number.
No issue with nested counts either, other than memory/string limits for the stack size.
From your example:
000001: Main
000005: If $X = 1
000007: EndIf
000009: If $X = 1
000010: While $X < 2
000011: If $X = 1
000013: Loop
- WARNING: Unterminated If when closing While!
If you are stacking the constructs then your stack would look like this at the point that you hit the Loop:
[0] 00011:IF
[1] 00010:WHILE
[2] 00009:IF
This makes it easy to track back and issue a message like:
000001: Main
000005: If $X = 1
000007: EndIf
000009: If $X = 1
000010: While $X < 2
000011: If $X = 1
000013: Loop
- WARNING: Loop without While
Possible unterminated "IF" at line 000011
Possible match with "WHILE" at line 000010
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1013 anonymous users online.
|
|
|