Author Topic: Unleashed Pascal (async/await, parallel for, match, string interpolation & more)  (Read 70320 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Just a remark, the heavy use of underscore _ on unleashed: can it break this code that is for example used in some frameworks like spring4d? I use it too, for enumerator classes/records and type helpers.
It is in quite common use in the Delphi community.
Code: Pascal  [Select][+][-]
  1. type
  2.   T_ = class
  3.   end;
  4. var
  5.   _:T_
  6. begin
  7. end.
« Last Edit: June 02, 2026, 09:22:48 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
dxgettext also uses _, though that doesn't work with FPC yet (as FPC doesn't use windows resourcestring tables)

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
This example? No.

You can name a variable, function, procedure, type - whatever - _ and call it, use it normally.

"Heavy use"... it's only used in tuples and match, as far as I remember. And only when it stands alone - not as a prefix or suffix. So T_, _foo, foo_ are all ordinary identifiers; only a bare _ in a tuple/match position is the discard.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Yes. Therefor I consider this wrong, although it compiles:
Code: Pascal  [Select][+][-]
  1. {$mode unleashed}
  2. type
  3.   T_ = type string;
  4. var
  5.   x:integer = 50;
  6.   _:T_ = 'Test';
  7. begin
  8.   match all x of
  9.     0..4: writeln('one');
  10.     2: writeln('two');
  11.     2..3: writeln('three');
  12.     _:writeln(_);  // <--- ambiguous underscores
  13.   end;
  14.   readln;
  15. end.
 
In my opinion it should not. else is accepted, so enforce else (or otherwise)

But that does not work for fall through:
Code: Pascal  [Select][+][-]
  1. {$mode unleashed}
  2. type
  3.   T_ = type string;
  4. var x:integer = 3;
  5.   _:T_ = 'Fail';
  6. begin
  7.   match all x of
  8.     0..4: writeln('one');
  9.     2: writeln('two');
  10.     2..3: writeln('three');
  11.     otherwise writeln(_); // should MOT print, but does.
  12.   end;
  13.   readln;
  14. end.
The code should stop fall through before else/otherwise,_and only execute those if no match is found.
Looks like a bug to me... Because that is what the syntax tells you: match all x, otherwise otherwise.
Solution is a simple boolean flag: if any match is found flag becomes true, otherwise when false execute else,_,otherwise.
« Last Edit: June 02, 2026, 10:01:08 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
This would be conform the default: switch in C:
Default Case: The default case is optional and executes if no matching case is found.
In C it is not part of the fall through if any match found.

Since the feature is modelled after this C construct (see discussion) this is a bug.
Can add a C example if you want, but you probably know that.

C99 accepts the default too if no breaks are used.
So the behavior is correct., although not very intuitive.

Rests the case of the underscore.
« Last Edit: June 02, 2026, 10:19:15 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Code: Pascal  [Select][+][-]
  1. {$mode unleashed}
  2.  
  3. begin
  4.   var x := 33;
  5.   var s := match x of
  6.     0..4: 'one';
  7.        2: 'two';
  8.     2..3: 'three';
  9.        //_: 'fail 1'; end; // gz, you found a bug
  10.   else 'fail 2';           // if both "_" and "else" exist at the same time
  11.  
  12.   //Error: Compilation raised exception internally
  13.   //Debug: An unhandled exception occurred at $00000001001ECC73:
  14.   //Debug: EAccessViolation: Access violation
  15.   //Debug:   $00000001001ECC73  append_else,  line 461 of pstatmnt.pas
  16.   //Debug:   $00000001001EC181  match_statement,  line 754 of pstatmnt.pas
  17.   //Debug:   $00000001001F752B  statement_expr,  line 4481 of pstatmnt.pas
  18.  
  19.   writeln('s = ', s);
  20.  
  21.   readln;
  22. end.

Code: Pascal  [Select][+][-]
  1. {$mode unleashed}
  2.  
  3. begin
  4.   var x := 3;
  5.   match all x of
  6.     0..4: writeln('one');
  7.        2: writeln('two');
  8.     2..3: writeln('three');
  9.        _: writeln('match anything'); // prints, maybe should not? _/else/otherwise not allowed inside "match all"?
  10.   end; //else writeln('fail');       // else not possible here, maybe should add?
  11.  
  12.   writeln;
  13.   writeln('** another match all ***');
  14.   writeln;
  15.  
  16.   match all x of
  17.     0..4: writeln('one');
  18.        2: writeln('two');
  19.     2..3: begin writeln('three'); leave; end; // but doesnt it solve the problem?
  20.        _: writeln('match anything');
  21.   end; //else writeln('fail');
  22.  
  23.   readln;
  24. end.

Will fix the first bug, but on the second sample I need more feedback.
« Last Edit: June 02, 2026, 11:21:13 am by Fibonacci »
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6628
@Fibonacci,

I just want to caution you about using "else" in the match statement because it can cause ambiguities when used in an "if" due to the "else" being common to both.  In those cases it is usually necessary to interpret the semicolon that is usually a separator as a statement terminator to eliminate the ambiguity, this reinterpretation of the semicolon is inconsistent with the rest of the language.  Using "otherwise" instead of "else" usually solves the problem in a more elegant and predictable way.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
So your suggestion is to disallow "else" in the statement-expression scenario?
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6628
no, just using "otherwise" instead of "else" to cover the cases that are not explicitly covered.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
But otherwise is a straight alias for else in the compiler. (as now is underscore it seems).
Otherwise is used by some instead of else. (especially those that come from ISO Pascal or MacPas)
Be careful not to damage the parse tree if you decide so.
If so, while fall through does in that case conform to C syntax, all three would be valid if ignored.
I propose to change to Default, although that is also an intrinsic, that would solve the ambiguity of the current syntax.

Then the syntax would be:
Code: Pascal  [Select][+][-]
  1. Match all x of
  2. 1:;
  3. 2:;
  4. 3:;
  5. default:; // executes on fall through
  6. end;
  7.  
  8. Match all x of
  9. 1:;
  10. 2:;
  11. 3:;
  12. otherwise {or else or _}:; // does not execute on fall through
  13. end;
                                             
Any "programmer" that knows only one programming language is not a programmer

440bx

  • Hero Member
  • *****
  • Posts: 6628
A Russian forum member who wrote his own Pascal compiler identified the problem with using "else" in the "case" statement.   Unfortunately, I cannot find his post and give him due credit because I don't remember his user name and he has not been active in quite some time.

However, the post at:
https://forum.lazarus.freepascal.org/index.php/topic,73869.msg580554.html#msg580554
shows the problem he originally noted.

HTH.

ETA:

Finally found the author of the Pascal compiler forum member who identified the problem, his username is @vtereshkov and the post where he showed the problem is:
https://forum.lazarus.freepascal.org/index.php/topic,49082.msg355332.html#msg355332

« Last Edit: June 02, 2026, 02:15:20 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
no, just using "otherwise" instead of "else" to cover the cases that are not explicitly covered.

How about case in a statement expression, where else is allowed?

Code: Pascal  [Select][+][-]
  1. var z := 1;
  2. var d := case z of
  3.   0: 'a'
  4. else 'b';
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6628
How about case in a statement expression, where else is allowed?
I haven't done a full syntactic analysis but, "else" is problematic in any construct that may appear inside an "if" statement because the "if" can also have an "else", that opens the door to making it difficult or counter-intuitive who owns the "else".  Look at the sample urls I posted, they clearly show the problem.  Personally, I'd use "otherwise", much safer for any statement that may be in an "if"/"else" construct.

All of the above said, the only real solution is to carefully analyze the EBNF grammar to ensure it is conflict-free.  IOW, I believe you'd be safe using "otherwise" instead of "else" but, there may be some case that I'm not currently seeing where "otherwise" could have the same problem as "else" since "otherwise" would also be used by different statements.

Everything I'm saying here is with the objective of letting you know that "else" could be a problem and, since it seems to be allowed in the "else" part of an "if" statement, it really looks like it would be a problem but, the final determination is yours to make.  To me, it looks like a problem.



FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1125
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Look at the sample urls I posted, they clearly show the problem.

That actually proves how much coding style matters. Indentation - and whether a semicolon is there or not (or where) - changes how the code behaves; the similar-looking code can do different things.

You can't do much about how someone writes their code - the only cure is running it through a code formatter.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6628
This has nothing to do with coding style or formatting of the code.  That's an ambiguity in the grammar.  Ambiguities in a grammar should not exist.  The "else" in the "case" statement introduced a flaw in the grammar.   The grammar should have been corrected but, correcting it would have broken existing code, the result, the broken grammar survives.

The only thing I'm telling you is, one flaw in the grammar is more than enough, the last thing you should do is add more flaws to it which, the additional use of "else" is extremely likely to cause.
« Last Edit: June 02, 2026, 02:17:58 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018