Recent

Author Topic: Making the semicolon useless  (Read 10408 times)

circular

  • Hero Member
  • *****
  • Posts: 3331
    • Personal webpage
Making the semicolon useless
« on: April 02, 2020, 10:53:57 am »
The semicolon is useless most of the time. Though sometimes, it makes a difference. So I am thinking that we could simply avoid these cases and remove the semicolon completely. It seems that many people agree, though we can make it a compiler option so that anyone is free to do as they want.

Here are the only uses we have found of the semicolon.

Code: Delphi  [Select][+][-]
  1. if sometest then ;
To do nothing if the test happens. But one could simply write in this rare case the follwing:
Code: Pascal  [Select][+][-]
  1. if sometest then begin end
Also for the loop, the same thing applies.
Code: Delphi  [Select][+][-]
  1. for i := 1 to 10 do ;

Another case, that is arguably more significant is the following:
Code: Delphi  [Select][+][-]
  1. var
  2.  a: integer = 1;
  3. begin
  4.   case a of
  5.     1, 2: if a = 2 then
  6.               writeln(2);
  7.   else
  8.     writeln(1);
  9.   end;
  10. end;
  11.  
In this case, one can use the otherwise keyword to avoid the ambiguity:
Code: Pascal  [Select][+][-]
  1. var
  2.  a: integer = 1
  3. begin
  4.   case a of
  5.     1, 2: if a = 2 then
  6.               writeln(2)
  7.   otherwise
  8.     writeln(1)
  9.   end
  10. end
  11.  

What could help to transition towards a Pascal without semicolon would be to have a compiler switch to forbid the use of else instead of otherwise in a case statement and to forbid empty statements so that people would need to write begin end in these rare occurences.

After that, we could transition to an optional semicolon, and then to remove it completely.

An exception could be kept though to have multiple statements on one line.
Code: Delphi  [Select][+][-]
  1. begin
  2.   a := 1; b := 2
  3. end

What do you think?
Conscience is the debugger of the mind

440bx

  • Hero Member
  • *****
  • Posts: 1882
Re: Making the semicolon useless
« Reply #1 on: April 02, 2020, 11:11:45 am »
What could help to transition towards a Pascal without semicolon would be to have a compiler switch to forbid the use of else instead of otherwise in a case statement and to forbid empty statements so that people would need to write begin end in these rare occurences.

After that, we could transition to an optional semicolon, and then to remove it completely.

An exception could be kept though to have multiple statements on one line.
Code: Delphi  [Select][+][-]
  1. begin
  2.   a := 1; b := 2
  3. end

What do you think?
Here are some thoughts.  The exception you mentioned creates an inconsistency, sometimes the semicolon would be required, other times it wouldn't be. 

The other cases you mention, such as disallowing "else" in "case" statements and requiring the use of "begin/end" to create an empty statement can be problematic for existing code.

Personally, not only I don't mind the semicolon, I like the instant visualization it provides to the end of the statement. 

I'd rather see some delimited statements being implemented, particularly something such as, "if/then/else/endif".  That gets rid of the "begin/end" for "if" statements and gets rid of "else" association problem and, that wouldn't cause any problems with existing code (at least none that I can think of as I type this.)

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

circular

  • Hero Member
  • *****
  • Posts: 3331
    • Personal webpage
Re: Making the semicolon useless
« Reply #2 on: April 02, 2020, 11:22:03 am »
If the exception is an issue, I don't mind having no semicolon in this case.

Regarding existing code, precisely the idea is that you could enable the switch to make the code unambiguous regarding the semicolon and that it would not compile anymore. Then after fixing the very few cases that need an update, you could then remove all semicolons.

Using begin end for an empty statement may be cumbersome, so maybe we could write nil for example to express an empty statement.

The idea is to close the gap between semicolon or not, so that in the end it becomes equivalent to have it or not.
Conscience is the debugger of the mind

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8463
  • FPC developer.
Re: Making the semicolon useless
« Reply #3 on: April 02, 2020, 11:58:05 am »
Well, for me the incompatibility of it all is a killer. Even if you regard it is as feature it will split the pascal codebases yet again over silly dialect lines. (see all discussions about the local variable declaration of Delphi for this)

But to remain constructive, let's forget that for now;

But why not try to modify e.g. fcl-passrc to implement what you want rather than useless discussion? Just for a try out? The fcl-passrc is much more readable than the compiler parser.

I also want to reiterate the error handling point again, but I whipped up an example as illustration. Each time you change  fundamental (block-statement structure) of the language you have to rethink error handling.

Consider this

Code: [Select]

procedure aa(j:integer);  begin  end;
procedure bb; begin end;
procedure cc; begin end;
function yy(j:integer):integer; begin end;
function zz(j:integer):integer; begin end;

begin
 aa(yy(zz(33));
   bb();
   cc();
 end.

Now, the call to aa is unbalanced. Without a semicolon, bb would probably be parsed as part of the parameter construction of aa, and errors would be something like missing operator for on the bb  line. (aa bb has no + or so between them)

While now you get

Quote
Fatal: Syntax error, ")" expected but ";" found
 
on the right line (the aa() line with the unbalanced ), and an unrelated line bb() is not relevant. The message also conveys that a ) was expected, so is to the point.

« Last Edit: April 02, 2020, 12:00:08 pm by marcov »

kupferstecher

  • Sr. Member
  • ****
  • Posts: 390
Re: Making the semicolon useless
« Reply #4 on: April 02, 2020, 12:44:18 pm »
Now, the call to aa is unbalanced. Without a semicolon, bb would probably be parsed as part of the parameter construction of aa, and errors would be something like missing operator for on the bb  line.

While now you get
Quote
Fatal: Syntax error, ")" expected but ";" found
Omitting the semicolon would mean that a linebreak implicitly finishs the statement, so for the error handling this wouldn't be more difficult, but of course it results in a lot of places in the compiler code that needs adaption.
Because of the linebreak-sensitivity a new concept for multiline statements would be needed. In Fortran (90?) e.g. the character '&' is used to tell the compiler that the statement will go on in the next line.

--
There is currently one semicolon-thing in FPC that feels very odd to me:

Code: Pascal  [Select][+][-]
  1. if true then
  2.   aStatement // <- A completed statement without semicolon
  3. else
  4.   bStatement;

The missing semicolon makes more sense when a begin..end block is used instead.

Code: Pascal  [Select][+][-]
  1. if true then begin
  2.   aStatement; // <- A completed statement without semicolon
  3. end else
  4.   bStatement;

In a "if/then/elseif/else/endif"-syntax there wouldn't be such an issue.

MarkMLl

  • Hero Member
  • *****
  • Posts: 925
Re: Making the semicolon useless
« Reply #5 on: April 02, 2020, 12:58:46 pm »
> The semicolon is useless most of the time. Though sometimes, it makes a difference.

It /always/ makes a difference, because it's a defined part of Pascal's syntax. End of discussion as far as Pascal's concerned.

If OP wants to change it- and note that I'm not saying "fix" here, then it's down to him to define and implement a new compiler mode which either has relaxed checking (and I suspect that he'd find this extraordinarily difficult) or with a language variant closer to Modula-2 /ALGOL-68/Ada.

MarkMLl


Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8463
  • FPC developer.
Re: Making the semicolon useless
« Reply #6 on: April 02, 2020, 01:09:46 pm »
Omitting the semicolon would mean that a linebreak implicitly finishs the statement, so for the error handling this wouldn't be more difficult, but of course it results in a lot of places in the compiler code that needs adaption.
Because of the linebreak-sensitivity a new concept for multiline statements would be needed. In Fortran (90?) e.g. the character '&' is used to tell the compiler that the statement will go on in the next line.

Or backslash like C macros. Maybe you could also (ab)use indentation like Python   O:-)

Anyway, whatever solution you choose, parsing will be very different, and it is more to illustrate that it is more than a "simple if check" in the compiler to let the semicolon fall away. You need to rethink a lot of things.

And then you are not even forward compatible anymore (as in being able to compile arbitrary pascal code, since you first need to insert a lot of &'s), let alone backwards compatible.


This is why if you really mean this, you need to build a proof of concept. For everything, including errorhandling.

And pas2js/fcl-passrc is maybe a cleaner source to base that on than the compiler

« Last Edit: April 02, 2020, 03:04:42 pm by marcov »

Bart

  • Hero Member
  • *****
  • Posts: 3808
    • Bart en Mariska's Webstek
Re: Making the semicolon useless
« Reply #7 on: April 02, 2020, 02:08:47 pm »
But to remain constructive, let's forget that for now;

Pun intended?

Bart

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8463
  • FPC developer.
Re: Making the semicolon useless
« Reply #8 on: April 02, 2020, 02:18:53 pm »
But to remain constructive, let's forget that for now;

Pun intended?

No, accidental.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1116
    • formatio reticularis
Re: Making the semicolon useless
« Reply #9 on: April 02, 2020, 02:23:41 pm »
Why should we? Even some languages of the C family, e.g. S and R, use the semicolon.

The main problem of dropping the semicolon is the loss of backwards-compatibilty. Moreover, I don't see a useful point in replacing a single character by "begn end".
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.8 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

MarkMLl

  • Hero Member
  • *****
  • Posts: 925
Re: Making the semicolon useless
« Reply #10 on: April 02, 2020, 03:18:31 pm »
The main problem of dropping the semicolon is the loss of backwards-compatibilty. Moreover, I don't see a useful point in replacing a single character by "begn end".

The fact that OP suggests  then begin end  rather than simply  then end  suggests that he hasn't done his homework. For what it's worth, I'd be broadly in favour of an alternative mode supporting  if then else end  etc. (i.e. the ALGOL-68/Modula-2/Ada syntax) as an alternative mode, since Object Pascal is already half way there with things like  try finally end  not to mention e.g. record and object definitions. But the semicolon has an important function, even if a mandatory end made it easier for a parser to be more tolerant of sloppy placement.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

kupferstecher

  • Sr. Member
  • ****
  • Posts: 390
Re: Making the semicolon useless
« Reply #11 on: April 02, 2020, 08:26:19 pm »
This is why if you really mean this, you need to build a proof of concept. For everything, including errorhandling.
Just in case you overlooked, I'm not the TE and I wouldn't even vote for such a change. Some time before I had a different view on it, but compatibility is really essential.

Zoran

  • Hero Member
  • *****
  • Posts: 1564
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Making the semicolon useless
« Reply #12 on: April 02, 2020, 09:25:04 pm »
In my opinion, semicolon is of great help to code readability.
Isn't it a great feature that you can easily see where the statement ends (set aside that it is a great help to compiler).

It would be even better if semi-colon were statement terminator, as in C, not only separator; so it would be mandatory always, even in front of "end" and "else". But, to be honest, it doesn't matter much.

Uh, and I do hope you wouldn't want line endings to be significant in any way.


circular

  • Hero Member
  • *****
  • Posts: 3331
    • Personal webpage
Re: Making the semicolon useless
« Reply #13 on: April 02, 2020, 09:29:36 pm »
I also want to reiterate the error handling point again, but I whipped up an example as illustration. Each time you change  fundamental (block-statement structure) of the language you have to rethink error handling.
Ok now that you gave an example I see what you are thinking about. Line endings.

Well that's not a problem. Without semicolons this would be:
Code: Delphi  [Select][+][-]
  1.  
  2. procedure aa(j:integer)  begin end
  3. procedure bb begin end
  4. procedure cc begin end
  5. function yy(j:integer):integer begin end
  6. function zz(j:integer):integer begin end
  7.  
  8. begin
  9.  aa(yy(zz(33))
  10.    bb()
  11.    cc()
  12.  end
  13.  
bb would not be considered as a parameter in anyway. Following a ")" by an identifier is not a continuation of instruction, it is in implicit end of instruction.
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 1457
Re: Making the semicolon useless
« Reply #14 on: April 02, 2020, 09:39:48 pm »
Hi!

Without semicolons it looks " naked". That will be forbidden in the USA.

Yes I agree with Zoran - the semicolons increase the readability.

Winni

 

TinyPortal © 2005-2018