Recent

Author Topic: Simplifying compound control structures  (Read 471 times)

JackD

  • Newbie
  • Posts: 4
Simplifying compound control structures
« on: December 08, 2022, 10:29:21 am »
I love Pascal and have been programming in it for 30 years, but the one thing about it which irritates me is the ambiguity of compound control statements, especially in regards to if-then. This blog post sums it up :

Pascal’s syntactic ambiguity and C’s ineptitude

More generally, I'm taking about dispensing with begin-end in control statements, as Wirth did with Modula-2 and Oberon. It makes for clearer and much more readable code which has a pleasing symmetry about it.

I have no idea how much work would be involved to implement this, and it's probably been suggested before, but I'm interested in responses.

Handoko

  • Hero Member
  • *****
  • Posts: 4764
  • My goal: build my own game engine using Lazarus
Re: Simplifying compound control structures
« Reply #1 on: December 08, 2022, 10:38:38 am »
Multiple level if then else statement will be much clearer:

Code: Pascal  [Select][+][-]
  1. if cond1 then
  2. begin
  3.   if cond2 then
  4.     do_something1
  5.   else
  6.     do_something2
  7. end;

or

Code: Pascal  [Select][+][-]
  1. if cond1 then
  2.   case cond2 of
  3.     True:  do_something1;
  4.     False: do_something2;
  5.   end;

Begin-end block and case statement are very useful to make the code clearer.
« Last Edit: December 08, 2022, 10:48:16 am by Handoko »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10484
  • FPC developer.
Re: Simplifying compound control structures
« Reply #2 on: December 08, 2022, 10:56:28 am »
While the M2 syntax is mostly IMHO better, there is a reason this hasn't happened yet. Compatibility is simply more important than microsyntax improvements that are mostly generated by IDEs nowadays anyway.

But if you want to invent a new language, go ahead.

JackD

  • Newbie
  • Posts: 4
Re: Simplifying compound control structures
« Reply #3 on: December 08, 2022, 10:59:04 am »
Hi Handoko,

I agree, if we have to make do with what we have. But neither are as clear as :

Code: Pascal  [Select][+][-]
  1. if cond1 then
  2.   if cond2 then
  3.     foo
  4.   else
  5.     bah
  6.   end
  7. end;

Actually, I don't use either of your suggestions, but this:

Code: Pascal  [Select][+][-]
  1. if cond1 then begin
  2.   if cond2 then begin
  3.     foo
  4.   end else begin
  5.     bah
  6.   end
  7. end;

JackD

  • Newbie
  • Posts: 4
Re: Simplifying compound control structures
« Reply #4 on: December 08, 2022, 11:02:30 am »
Quote
While the M2 syntax is mostly IMHO better, there is a reason this hasn't happened yet.

"yet"? So there is some hope, then?  :D

Compatibility is simply more important...

Yes, I suspected it would be.

Quote
But if you want to invent a new language, go ahead.

I think I'll stick with Pascal. There's no such thing as a perfect language, and it's good enough.  :)
« Last Edit: December 08, 2022, 11:04:07 am by JackD »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10484
  • FPC developer.
Re: Simplifying compound control structures
« Reply #5 on: December 08, 2022, 11:51:38 am »
Quote
While the M2 syntax is mostly IMHO better, there is a reason this hasn't happened yet.

"yet"? So there is some hope, then?  :D

Yes, you can use Modula-2  :)


MarkMLl

  • Hero Member
  • *****
  • Posts: 5862
Re: Simplifying compound control structures
« Reply #6 on: December 08, 2022, 01:11:41 pm »
More generally, I'm taking about dispensing with begin-end in control statements, as Wirth did with Modula-2 and Oberon. It makes for clearer and much more readable code which has a pleasing symmetry about it.

You need to take this in context. Pascal was a hurried derivative of ALGOL-W, which was syntactically close to ALGOL-60 where the rule was that an if statement (etc.) controlled a single subsequent statement. That form was also consistent with the if /expression/, which selected one of two possible subsequent expressions for evaluation.

The dangling-else issue was identified and fixed in ALGOL-68, and as a member of the committee Wirth cannot claim ignorance of both the issue and its solution.

There are two corollaries to this. The first is that if Wirth had had even minimal exposure to COBOL he should have realised how problematic the various declaration sections were, and the extent to which they were already derided. As such, I would argue that CONST, TYPE, VAR and the rest should similarly require a BEGIN-END if they applied to multiple declarations.

The second is that the extremely uncomfortable mix of styles in Pascal "as currently spoken", where an if statement /requires/ a BEGIN-END to govern more than one subsequent statement but a CASE-ELSE-END is self-closing, is largely Borland's doing. However it would be unfair to absolve Wirth of all responsibility, since RECORD-END and REPEAT-UNTIL are his responsibility.

The bottom line is that we've inherited a Godawful mess.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 4872
  • Compiler Developer
Re: Simplifying compound control structures
« Reply #7 on: December 08, 2022, 10:20:58 pm »
I have no idea how much work would be involved to implement this, and it's probably been suggested before, but I'm interested in responses.

Yes, such things have been suggested before and as before the answer will always be the same: there won't be any changes regarding this.

 

TinyPortal © 2005-2018