View previous topic :: View next topic |
Author |
Message |
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Mon Oct 21, 2002 4:29 pm Post subject: IF command - Syntax Checker |
|
|
A little VDS helper utility which can check a script for IF commands that may not have been closed with an END.
Quite new, so it may still have a bug or two
Greetz
Dr. Dread
Code: |
title IF syntax check
if %1
goto go
end
dialog CREATE,IF syntax check .. by Dr. Dread,-1,0,400,75
dialog ADD,TEXT,T1,10,10,,,Filename:
dialog ADD,EDIT,Filename,8,70,285,19
dialog ADD,BUTTON,Browse,8,360,30,20,...
dialog ADD,BUTTON,CheckIt,40,70,80,24,Check script,DEFAULT
dialog SHOW
:evloop
wait event
goto @event()
:Browsebutton
%a = @filedlg("Script Files (*.DSC)|*.DSC",Select Script File)
if %a
dialog set,Filename,%a
end
goto evloop
:CheckItbutton
if @not(@file(@dlgtext(Filename)))
warn "The file entered does not exist,"@cr()"please enter a valid script filename."
goto evloop
end
:go
list create,1
list create,2
if %1
if @equal(%1,Untitled.dsc)
warn You must save the file to disk before using this utility!
stop
else
list loadfile,1,%1
end
else
list loadfile,1,@dlgtext(Filename)
end
%e = %F
repeat
%a = @trim(@item(1))
if @equal(@substr(%a,1,3),"if ")@equal(@substr(%a,1,3),"end")
list add,2,"("line @succ(@index(1))")" %a
end
%%dummy = @next(1)
until @equal(@index(1),@count(1))
list close,1
list create,3
list seek,2,0
gosub check
list savefile,3,IFSynTax.txt
list close,2
list close,3
shell open,IFSynTax.txt
wait 2
file delete,IFSynTax.txt
:close
exit
rem ##### SUBs #####
:check
%%open = 0
%%countIF = 0
%%countEND = 0
list create,4
repeat
%a = @next(2)
%b = @item(2)
if @both(@greater(@pos(") if",%a),0),@greater(@pos(") end",%b),0))
rem We have a complete if-end command
list add,3,%a
list add,3,%b
%a = @next(2)
%%countIF = @succ(%%countIF)
%%countEND = @succ(%%countEND)
else
rem This statement is not complete yet
if @greater(@pos(") if",%a),0)
%%open = @succ(%%open)
%%countIF = @succ(%%countIF)
list add,3,%a
rem Add also to list of uncompleted IFs
list add,4,%a
else
rem Check whether an IF was opened - then we can end it now
%%countEND = @succ(%%countEND)
if @greater(%%open,0)
%%open = @pred(%%open)
list add,3,%a
rem Delete from our list of uncompleted IFs
list seek,4,@pred(@count(4))
list delete,4
end
end
end
until @equal(@index(2),@count(2))
if @greater(@count(4),0)@not(@equal(%%countIF,%%countEND))
if @greater(@count(4),0)
list seek,4,0
end
list add,3,
list add,3,****** IF/END MISMATCH *****
list add,3,.... %%countIF IF commands detected
list add,3,.... %%countEND END commands detected
list add,3,****** LIST OF SUSPECTS *****
repeat
list add,3,* @next(4)
until @equal(@index(4),@count(4))
list add,3,*****************************
else
list add,3,
list add,3,****** EVERYTHING SEEMS OK *****
list add,3,.... %%countIF IF commands detected
list add,3,.... %%countEND END commands detected
end
list close,4
exit
|
EDIT1: Now the script can be compiled and put in the Tools folder. So you can run it directly from VDS' Tools menu.
EDIT2: Fixed small problem when trying to run this on a script not saved to disk. _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing
Last edited by Dr. Dread on Wed Mar 12, 2003 7:45 pm; edited 4 times in total |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Tue Oct 22, 2002 3:21 pm Post subject: |
|
|
Hey Dread, here's the prog I had goin'...
[EDIT] My code checker is moved here:
http://www.vdsworld.com/forum/viewtopic.php?p=8776#8776
It checks IF/END/ELSE/ELSIF, REPEAT/UNTIL, WHILE/WEND
and GOSUB commands (also displays subprocedure "EXIT"
commands to accompany GOSUB).
I worked on it some more, but still haven't figured how to
pinpoint a command that's missing its' counterpart (such
as which IF is missing an END, etc.) when they're nested.
Clicking a "Line = " number on the right list will seek to that
line in the code (left list).
EDIT - Remedied a counting error in subroutines that exit early
by an IF/ELSE etc. Also has option of counting code pairs only
(IF/END etc.), or counting pairs AND tracing thru to check nesting
depth.
NOTE: Nesting depth may not be 100% accurate due to the
situation described above. The trace looks for the first exit
encountered in a subroutine, and if an early exit by IF etc.
is encountered, it will return to the GOSUB line that called it.
I don't have a practical way to remedy this, but for the most
part (hopefully) it shouldn't be a problem.
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Last edited by Mac on Mon Mar 17, 2003 5:13 pm; edited 3 times in total |
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Tue Oct 22, 2002 7:19 pm Post subject: |
|
|
Hey Mac!
Nice prog there! Smooth interface, too ..
I agree with ya.. It's quite tough to figure out exactly which IF is missing an END when they are nested.
My syntax checker falls short there also; I keep a list of IFs opened and then remove the latest when
an END is found. I chose to leave ELSE out of consideration as it does not have much effect on the IF/END
construction as such.
It works fairly well but when IF commands are nested, it may point to the wrong IF line when an END has been
left out. Probably not much to do about it - the VDS command interpreter must have the same problem: if an IF
construct has been opened it must take the first END that it reaches, no matter what follows.
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Tue Oct 22, 2002 11:57 pm Post subject: |
|
|
Thanks Dread.
Yeah, I guess if it was easy to match the missing IF/ENDs
then VDS would catch them as an error with a line number...
BTW, the help file mentions there's a max nesting of 9 deep
for GOSUB and REPEAT. Does anybody know if there's a
limit on IF/END nesting? I've run a test 12 deep with no
problems...
Also, wondering about WHILE/WEND nesting, although I don't
have it in VDS3...
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Oct 30, 2002 12:51 pm Post subject: |
|
|
Remedied a counting error in subroutines that exit early by
an IF/ELSE etc. Also now has option of counting code pairs only,
or counting pairs AND tracing thru to check nesting depth. See
notes at the bottom of the post for more details.
Thanks to Larry for drawing my attention to the problem.
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
cnodnarb Professional Member
Joined: 11 Sep 2002 Posts: 762 Location: Rockeledge, GA
|
Posted: Wed Dec 11, 2002 1:34 am Post subject: |
|
|
I don't think that there is a limit on nesting if ends, but I usually throw gosubs in if my code gets confusing due to if end nesting.
Probably un-needed example below for clarity.
Code: |
if %%event
gosub checkapple
else
end
goto evloop
:checkapple
if %%apple
%%bananas = true
else
%%bananas = false
end
exit
|
NodNarb
PS If only everyone did this all the time your scripts would work beautifully! |
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Mar 12, 2003 7:46 pm Post subject: |
|
|
Fixed an error when trying to run the program through the Tools menu on a script not yet saved
to disk. See EDIT2 in org posting.
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|