Recent

Author Topic: [Solved] Weird 'case' block which must not compile  (Read 1236 times)

Seenkao

  • Full Member
  • ***
  • Posts: 152
Re: Weird 'case' block which must not compile
« Reply #15 on: April 16, 2021, 09:09:56 am »
look at this post :
https://forum.lazarus.freepascal.org/index.php/topic,49082.msg355332.html#msg355332
I can't put a version on it, but it certainly predates FPC v3. The rationale is to fix this sort of thing:

Code: Pascal  [Select][+][-]
  1.           CASE model OF
  2.             hpm4952: IF NOT sendRsreExpectAcc THEN
  3.                        RAISE Exception.Create('sendRsreExpectAcc() in tryBlockCount()')
  4.                      ELSE BEGIN END
  5.           ELSE
  6.             IF NOT sendTrbrExpectAcc THEN
  7.               RAISE Exception.Create('sendTrbrExpectAcc() in tryBlockCount()')
  8.           END
  9.  

С моей точки зрения это недоработка программиста, а не оператора case. Для того чтоб этого избежать, достаточно поставить begin-end.
google translate: From my point of view, this is a programmer's flaw, not the case operator. To avoid this, just put begin-end.

lucamar

  • Hero Member
  • *****
  • Posts: 3915
Re: Weird 'case' block which must not compile
« Reply #16 on: April 16, 2021, 09:19:24 am »
I can't put a version on it, but it certainly predates FPC v3. The rationale is to fix this sort of thing:

Code: Pascal  [Select][+][-]
  1.           CASE model OF
  2.             hpm4952: IF NOT sendRsreExpectAcc THEN
  3.                        RAISE Exception.Create('sendRsreExpectAcc() in tryBlockCount()')
  4.                      ELSE BEGIN END
  5.           ELSE
  6.             IF NOT sendTrbrExpectAcc THEN
  7.               RAISE Exception.Create('sendTrbrExpectAcc() in tryBlockCount()')
  8.           END

If you didn't have that dummy else begin end then the case statement's else would be misparsed without warning. Use otherwise and there isn't that risk.

Though we all know that's a programmers error; no dummy "else begin end" is needed, just ";" in the correct places:

Code: Pascal  [Select][+][-]
  1.   CASE model OF
  2.   hpm4952:
  3.     IF NOT sendRsreExpectAcc THEN
  4.       RAISE Exception.Create(
  5.           'sendRsreExpectAcc() in tryBlockCount()');
  6.   ELSE
  7.     IF NOT sendTrbrExpectAcc THEN
  8.       RAISE Exception.Create(
  9.           'sendTrbrExpectAcc() in tryBlockCount()');
  10.   END;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2563
Re: Weird 'case' block which must not compile
« Reply #17 on: April 16, 2021, 09:26:37 am »
google translate: From my point of view, this is a programmer's flaw, not the case operator. To avoid this, just put begin-end.

You are obviously entitled to your point of view. But the purpose of a high-level language is to make life easier for programmers, and the purpose of a compiler is to generate reliable code. Hence if there is something in the language design which regularly trips up programmers and can't easily be detected as an error by a compiler then it's realistic to change the language definition, particularly if this can be done without breaking backwards compatibility.

Part of the rationale for the moderately strong typing introduced by Wirth in Pascal was to help programmers write reliable code. Programmers who don't like having the compiler check things on their behalf should perhaps stick to K&R C, since even in the C/C++ ecosystem there is a long-term trend to tighten up the language definition.

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.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 2563
Re: Weird 'case' block which must not compile
« Reply #18 on: April 16, 2021, 09:32:12 am »
Though we all know that's a programmers error; no dummy "else begin end" is needed, just ";" in the correct places:

Noting that ; is a separator, not a terminator: I'm pretty sure that there's at least one extra in your refactored example. However right now I'm not even starting to think about that one, since it you go back to Turbo Pascal there was at least one situation where an "obviously correct" semicolon was reported as an error.

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.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Seenkao

  • Full Member
  • ***
  • Posts: 152
Re: Weird 'case' block which must not compile
« Reply #19 on: April 16, 2021, 09:45:42 am »
You are obviously entitled to your point of view. But the purpose of a high-level language is to make life easier for programmers, and the purpose of a compiler is to generate reliable code. Hence if there is something in the language design which regularly trips up programmers and can't easily be detected as an error by a compiler then it's realistic to change the language definition, particularly if this can be done without breaking backwards compatibility.
Боюсь вас огорчить, но вы не облегчили жизнь программисту. Вы сделали какую-то работу за него. Но! От проблемы не ушли! Я вам точно могу сказать, что найдутся "умные" люди, которые запихают case в case. И ваша проблема снова проявится.
google translate: I'm afraid to upset you, but you did not make life easier for the programmer. You did some work for him. But! The problem has not gone away! I can tell you for sure that there are "smart" people who will cram case into case. And your problem will show up again.

440bx

  • Hero Member
  • *****
  • Posts: 2310
Re: Weird 'case' block which must not compile
« Reply #20 on: April 16, 2021, 10:09:27 am »
google translate: From my point of view, this is a programmer's flaw, not the case operator. To avoid this, just put begin-end.
This isn't a matter of viewpoint.  Allowing "else" in the case statement introduces a flaw in the grammar.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
Re: [Solved] Weird 'case' block which must not compile
« Reply #21 on: April 16, 2021, 10:33:58 am »
It isn't a flaw as such, but a recent change from last year regarding ISO, where the else block is mandatory to close the case statement. I already complained on the bug tracker that this also spilled to other modes (e.g. objfpc and Delphi, where else is optional) but it was closed by Jonas as "no change required".
« Last Edit: April 16, 2021, 10:40:35 am by Thaddy »

440bx

  • Hero Member
  • *****
  • Posts: 2310
Re: [Solved] Weird 'case' block which must not compile
« Reply #22 on: April 16, 2021, 11:18:02 am »
It isn't a flaw as such,
It is a flaw in the grammar.  In the following code snippet
Code: Pascal  [Select][+][-]
  1. var
  2.   a: integer = 1;
  3.  
  4. begin
  5. case a of
  6.   1, 2: if a = 2 then
  7.           writeln(2);
  8.         else
  9.           writeln(1);
  10. end;            
  11. end.
the compiler cannot decide (without the semicolon) if the "else" belongs to the "if" or the "case" and, to make matters worse, in that code snippet, the semicolon ";" is acting as a statement _terminator_ instead of _separator_.  In Pascal an "else" should never be preceded by a semicolon.

Allowing the "else" in a "case" statement is another one of those Borland-isms someone thought of between a bagel and a cup of coffee.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Seenkao

  • Full Member
  • ***
  • Posts: 152
Re: Weird 'case' block which must not compile
« Reply #23 on: April 16, 2021, 11:23:32 am »
This isn't a matter of viewpoint.  Allowing "else" in the case statement introduces a flaw in the grammar.
Вы сами можете сделать вложенные if-else-if-else...if-else... и ваш код, в лучшем случае будет работать не правильно. В худшем выдаст ошибку. Можете проверить. Без begin-end такие места кода могут быть абсолютно не читабельны.
И я уже писал выше, что подобным функционалом для case, вы не избавились от проблемы - вы её отложили на другое время.
google translate: You yourself can make nested if-else-if-else...if-else... and your code, at best, will not work correctly. At worst, it will give an error. You can check. Without begin-end, such places in the code can be absolutely unreadable.
And I already wrote above that with similar functionality for case, you did not get rid of the problem - you postponed it for another time.

Seenkao

  • Full Member
  • ***
  • Posts: 152
Re: [Solved] Weird 'case' block which must not compile
« Reply #24 on: April 16, 2021, 11:28:44 am »
It is a flaw in the grammar.  In the following code snippet
Code: Pascal  [Select][+][-]
  1. var
  2.   a: integer = 1;
  3.  
  4. begin
  5. case a of
  6.   1, 2: if a = 2 then
  7.           writeln(2);
  8.         else
  9.           writeln(1);
  10. end;            
  11. end.
Учитывая, что вы пишите об else - вы должны понимать, что вы пишите это для всех условий!
А значит вы утверждаете что else - это недостаток грамматики.  :)
google translate: Considering that you are writing about else - you must understand that you are writing this for all conditions!
So you are claiming that else is a lack of grammar. :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2563
Re: [Solved] Weird 'case' block which must not compile
« Reply #25 on: April 16, 2021, 11:43:50 am »
Allowing the "else" in a "case" statement is another one of those Borland-isms someone thought of between a bagel and a cup of coffee.

In fairness, Wirth allows it in Modula-2 (probably predating Turbo Pascal) and since a CASE statement must handle all possibilities it is heavily used. However (a) the case separator becomes | rather than ; and (b) since IF-THEN must be explicitly terminated by END there is no ambiguity.

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.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9286
  • FPC developer.
Re: [Solved] Weird 'case' block which must not compile
« Reply #26 on: April 16, 2021, 11:47:02 am »
In Pascal an "else" should never be preceded by a semicolon.

Well, it should if you want it to be the outer else of a nested if block. But yeah, dangling else is a problem of language that allow a choice between one or multiple line blocks.

Thaddy

  • Hero Member
  • *****
  • Posts: 10786
Re: [Solved] Weird 'case' block which must not compile
« Reply #27 on: April 16, 2021, 12:31:33 pm »
Ah, missed the semicolon, indeed dangling else.

 

TinyPortal © 2005-2018