[fpc-devel] Semicolon before else

Aleksa Todorovic alexione at gmail.com
Tue Jan 26 01:08:45 CET 2010

Hi, all!

I used to work in Pascal (Delphi/fpc) for 10 years, and later had to
go to C++ (because of my day job). Now, I'm back to fpc, and one thing
I really miss is ability to have semicolon before else.

So, here's (attached) patch for fpc that makes it possible. The only
problematic place for this syntax change is inside case statement,
when last branch is single if statement, for example:

case p of
'1': do_1;
'2' if (q > 0) then do_2;
else do_3;

It is not clear if else should be part of branch '2' (part of if
statement), or else branch for case. Fortunately, there is also
'otherwise' (I bet only few out there know about it, I've discovered
it this night!) which can be used instead of else inside case, which
can be used to clarify intent, so that default behavior could be to
interpret else as part of if statement.

Note that attached patch has lots of bad points:
- besides compiling fpc itself with this change (and changing some
else-s to otherwise-s), as well as some of my projects, I haven't
officially tested it
- from design point of view, it's very 'dirty' (I mess with scanner
internal state from statement parser - ouch, that hurts!)
- because of possible problems, else as part of case should be
deprecated, or discouraged, or there should at least be warning if
last branch before else is single if statement
- this patch only applies to else branch of if statement, not one
inside case (because, as stated before, you can use keyword
'otherwise' inside case)
- I'm pretty sure there are still some else-s inside case-s inside
compiler/rtl/fcl which introduce bugs, I just didn't hit those places;
the proper solution here is to either add begin/end, or change code to
use 'otherwise'

Anyway, as far as I can see - it works!

Hope someone will find it useful. Or not :-)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: semicolon_before_else.patch
Type: application/octet-stream
Size: 3659 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20100126/b285abf4/attachment.obj>

More information about the fpc-devel mailing list