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

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
Currently the compiler, when sitting on a ":" can determine the difference between a single ":" and a ":=" by looking at the next token but, if ":=:" was also allowed then the compiler would have to look two tokens ahead to determine it is dealing with a ":=:" instead of just an assignment ":=".

Good luck defending this argument when it comes to the opening parentheses of variable pairs for swapping. The parser already has a lot of work determining what the opening bracket refers to, so don’t add yet another layer of context. Besides, specifying the same pair of swap variable identifiers twice is pure redundancy and obfuscation of something that can and should be much simpler. Anyway, it doesn't matter, because using the alternative syntax adds another layer of context to the assignment operator anyway.

Besides, the language's syntax should be tailored to people's needs, not to the complexity of the parser. Using a more complex syntax at the expense of users, just to avoid having to develop the parser, seems like a case of misplaced priorities.

Quote
That would add unnecessary complexity to the parser and would definitely impact performance enough that it might actually be noticeable in most cases because ":" and ":=" are, very, very common.

There isn't much evidence to suggest that the introduction of a new operator would significantly reduce compilation performance. Without testing (e.g. in an experimental branch) and profiling the compilation of a reasonably large project, this is just speculation.

However, it follows that the parser will have much more work to do with the alternative syntax, because not only will there be many more tokens in the swap expression, but it will also have to check the order of variables in both pairs (which isn’t necessary in the case of the operator).
« Last Edit: May 28, 2026, 08:25:37 pm by flowCRANE »
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
Below is a comparison of the two proposed swap syntaxes—the first relates to the proposed operator, while the second is the syntax familiar from other programming languages. The comparison focuses on the number of tokens in the expression and the general checks the parser must perform to recognize the expression as a valid swap. The comparison does not take into account the optional semicolon at the end of the expression, as its presence depends on where the swap expression is located in the code.

Operator — A :=: B
  • three tokens: identifier, operator, identifier
  • identifiers must refer to variables
  • identifiers must refer to two different variables
  • the data types of both variables must be swappable

Alternative — (A, B) := (B, A)
  • eleven tokens: parenthesis, identifier, comma, identifier, parenthesis, operator, parenthesis, identifier, comma, identifier, parenthesis
  • all four identifiers must refer to variables
  • identifiers in a given pair must refer to two different variables
  • identifiers in the first pair must refer to the same variables as in the second pair
  • identifiers in the first pair must be in the reverse order of those in the second pair
  • the data types of both/four variables must be swappable

It should now be clear that parsing the alternative swap syntax involves much more work, and on top of that, it comes at the cost of a syntax that is far less user-friendly and has more opportunities for errors.
« Last Edit: May 28, 2026, 08:52:25 pm by flowCRANE »
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

440bx

  • Hero Member
  • *****
  • Posts: 6628
Good luck defending this argument when it comes to the opening parentheses of variable pairs for swapping.
I don't needs a single iota of luck to support that argument, all that's needed is to understand how LL(1) scanning and parsing works.

In the land of bad ideas ":=:" is in the top 3.

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

H₂SO₄

  • Hero Member
  • *****
  • Posts: 518
[...] or similar types when dealing with some specific data types (e.g., swapping the values of variables of type Byte and Word should be valid).

Please don't, or at the very least not without a compiler warning.

I'm also not a fan of  :=:  - I think the tuple pattern destructuring approach that @Fibonacci already demonstrated is both much more elegant from a language design perspective and also less error-prone (try to find the rogue  :=:  :P  A := B; C := D; E := F; G := H; I := J; K := L; M :=: N; O := P; Q := R; S := T;)

Besides, you could always do the following:

Code: Pascal  [Select][+][-]
  1. {$mode unleashed}
  2. {$modeswitch implicitspecialization}
  3.  
  4. procedure Exchange<T>(var A, B: T); inline;
  5. var
  6.   Temp: T;
  7. begin
  8.   Temp := B;
  9.   B := A;
  10.   A := B;
  11. end;
  12.  
  13. procedure Foo(var X1, Y1, Z1, X2, Y2, Z2: Single);
  14. begin
  15.   Exchange(X1, X2);
  16.   Exchange(Y1, Y2);
  17.   Exchange(Z1, Z2);
  18. end;

No changes to the language required!

440bx

  • Hero Member
  • *****
  • Posts: 6628
Alternative — (A, B) := (B, A)
  • eleven tokens: parenthesis, identifier, comma, identifier, parenthesis, operator, parenthesis, identifier, comma, identifier, parenthesis
  • all four identifiers must refer to variables
  • identifiers in a given pair must refer to two different variables
  • identifiers in the first pair must refer to the same variables as in the second pair
  • identifiers in the first pair must be in the reverse order of those in the second pair
  • the data types of both/four variables must be swappable

It should now be clear that parsing the alternative swap syntax involves much more work, and on top of that, it comes at the cost of a syntax that is far less user-friendly and has more opportunities for errors.
Quite likely one of the absolute worst habits programmers have inherited from C and C++ is the obsession with typing less at the expense of clarity and solid syntax.

At least the alternative gives a hint as to what the statement does.  Typing a few more characters ?... not everyone out there is a one-and-a-half fingers typist.   It takes a lot longer to figure out, by finding and searching the manual, that :=: means swap than the time it takes to type a few more characters that makes the intent clearer for anyone who reads the statement.

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
String Interpolation - design review before it lands

Before I finalize string interpolation and merge it, I want to put the design in front of you. If anyone has objections, syntax concerns, or additions worth considering - now is the time, not after it's in main.

Full spec

https://github.com/fpc-unleashed/freepascal/blob/feat/strinterp/unleashed/docs/string-interpolation.md

The short version

A $ before a string literal opens an interpolated string. Braces delimit Pascal expressions, everything else is text. Each placeholder is type-checked, formatted, and concatenated at compile time - no manual + chains, no IntToStr, no Format calls.

Code: Pascal  [Select][+][-]
  1. var
  2.   name: string = 'Alice';
  3.   age: integer = 30;
  4. begin
  5.   WriteLn($'Hello {name}, you are {age} years old.');
  6.   // Hello Alice, you are 30 years old.
  7. end.

Placeholder forms

Code: Pascal  [Select][+][-]
  1. {expr}            // auto-format by type
  2. {expr:width}      // Pascal-style width (like Write(expr:width))
  3. {expr:width:prec} // width + precision for floats
  4. {expr as 'mask'}  // format mask

Format masks

The as 'mask' form dispatches by type + mask shape:

Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. WriteLn($'pi = {Pi as '%.2f'}');           // Format()        -> pi = 3.14
  4. WriteLn($'pi = {Pi as '0.00'}');           // FormatFloat()   -> pi = 3.14
  5. WriteLn($'today = {Now as 'yyyy-mm-dd'}'); // FormatDateTime()-> 2026-05-29
  6. var n := $beef;
  7. WriteLn($'n hex = {n as 'x4'}');           // IntToHex()      -> BEEF
  8. WriteLn($'n10 = [{n:10}]');                // Write width     -> [     48879]
  9. var c := ExceptClass.Create('just a test');
  10. WriteLn($'class: {c}');                    // calls .ToString  -> Exception: just a test

Masks that need a specific unit (e.g. SysUtils for Format / FormatFloat / IntToHex) emit a clear compile error naming the function and the unit if it's not in scope.

Code: Text  [Select][+][-]
  1. project1.lpr(15,23) Error: String interpolation format requires function "FORMAT" in unit "SYSUTILS" - add it to uses clause
  2. project1.lpr(16,23) Error: String interpolation format requires function "FORMATFLOAT" in unit "SYSUTILS" - add it to uses clause
  3. project1.lpr(17,27) Error: String interpolation format "yyyy-mm-dd" is not supported for this type
  4. project1.lpr(19,25) Error: String interpolation format requires function "INTTOHEX" in unit "SYSUTILS" - add it to uses clause



Try it

Grab it with the Unleashed Installer - pick the feat/strinterp branch and it builds the compiler + IDE for you. Throw some weird strings at it and see what breaks before I lock the syntax down.



Speak now. Once it's in main I'd rather not break the syntax on people who start using it.
« Last Edit: May 29, 2026, 05:12:43 pm by Fibonacci »
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
Quite likely one of the absolute worst habits programmers have inherited from C and C++ is the obsession with typing less at the expense of clarity and solid syntax.

This isn’t about C/C++ or any other language. The proposed operator and its syntax are dictated solely by the clarity and readability of the swap operation itself. And in my opinion, there simply isn’t a simpler syntax for swapping that would fit so well with the current Pascal syntax.

So this isn’t some obsession with limiting the number of characters to be written, but rather a proposal driven by the desire to increase the verbosity of the swap operation. And this would be achieved through an explicitly separate operator, rather than the complicated and lengthy syntax of tuples. A separate operator makes even more sense because tuples can be disabled, making their syntax for performing a swap unavailable.

Besides, of course, having less code to write to express a given operation is always a disadvantage. Pascal itself has many such shorthands, and the unleashed dialect has even more. If you don’t like the new operator, that’s fine, but don’t be hypocrites.

Quote
At least the alternative gives a hint as to what the statement does.

And how long will it take to analyze and understand the meaning of such a highly complex expression?

Quote
Typing a few more characters ?... not everyone out there is a one-and-a-half fingers typist.

Not just a few, but even dozens more characters—not everyone out there uses single-letter-identifiers (actually most of Pascal programmers don't use them as this is a very bad practice). I gave you an example of swapping field values between two records (practical use, from my game source code) and this ridiculous tuple syntax produces expression wider that half of my entire monitor screen—very long, hard to read, hard to understand.

And it’s not just about the extra characters to type, but also the number of places where a mistake can be made (the same pairs of variables on both sides, their order reversed) which, in the case of a separate operator, simply do not exist. To me, it’s ridiculous that no one seems to see the absurdity of the alternative syntax.

Quote
It takes a lot longer to figure out, by finding and searching the manual, that :=: means swap than the time it takes to type a few more characters that makes the intent clearer for anyone who reads the statement.

That’s simply not true; it’s just gaslighting, an attempt to make me believe that black is white. A short expression, as short and simple as an ordinary assignment, with an explicit separate operator having only one specific meaning, requires no analysis at all, only knowledge of the existence and purpose of that operator. Knowledge of the operators available in the language is an absolutely fundamental skill for a programmer.

You see the :=: operator and immediately know it’s a swap, and you see it right away because it’s the second token in a three-token expression, not enclosed in any parentheses. But when you see tuple syntax, you won’t know if it’s a swap until you analyze the entire, long, and complex expression, comparing the variables in the left tuple with those in the right tuple and the order in which they’re listed. Besides, the syntax of tuples isn’t clear enough to immediately know that using it for a swap is valid, so you’ll have to consult the documentation anyway to be sure.

Besides, as I mentioned earlier, tuples can be disabled via a directive, in which case the swap option will not be available at all. That’s why a separate operator comes out on top once again.
« Last Edit: May 29, 2026, 02:22:34 pm by flowCRANE »
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Quite likely one of the absolute worst habits programmers have inherited from C and C++ is the obsession with typing less at the expense of clarity and solid syntax.

This isn’t about C/C++ or any other language. The proposed operator and its syntax are dictated solely by the clarity and readability of the swap operation itself. And in my opinion, there simply isn’t a simpler syntax for swapping that would fit so well with the current Pascal syntax.

So let's start with the beginning. Why does it have to be an operator and is that better than a pseudo procedural intrinsic? (other than racking up an operator poster like Perl)

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
It doesn't have to be an operator, but an operator would provide simplicity, clarity and verbosity comparable to a standard assignment (especially since it would also serve as an assignment, albeit a double one). It's a very elegant solution, independent of tuples that may be switched off. In addition, a separate operator would explicitly inform the compiler of the intention to swap, which the compiler could optimize into a zero-cost instruction.

Intrinsic would also be independent of tuples, but what about its name? Swap is already taken. Any suggestions?
« Last Edit: May 29, 2026, 02:48:12 pm by flowCRANE »
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

440bx

  • Hero Member
  • *****
  • Posts: 6628
This isn’t about C/C++ or any other language.
I think it is.  Some programmers are so used to it they don't even realize it.

long live ==, ||, && and all their single character variations that are a disgustingly common source of bugs but, hey! they save the programmer from typing 1 extra character... that's really what counts.  Absolutely!!  design to save a character, that's what designing is all about.  Not to mention the inconvenients in scanning and parsing that ":=:" creates... why should anyone care ???
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
I think it is.  Some programmers are so used to it they don't even realize it.

Some programmers are, but not me. If I preferred C/C++ syntax, I wouldn't use Pascal.
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
It doesn't have to be an operator, but an operator would provide simplicity, clarity and verbosity comparable to a standard assignment (especially since it would also serve as an assignment, albeit a double one).

Unfounded opinion

Quote
It's a very elegant solution, independent of tuples that may be switched off.

I don't understand what tuples have to do with it. Could you elaborate.

Quote
In addition, a separate operator would explicitly inform the compiler of the intention to swap, which the compiler could optimize into a zero-cost
instruction.

That is what an intrinsic can also do.

Quote
Intrinsic would also be independent of tuples, but what about its name? Swap is already taken. Any suggestions?

Don't care. _Swap() or SwapIntrinsic() for all I care. It really doesn't matter much. Even that is more intuitive than :=:

flowCRANE

  • Hero Member
  • *****
  • Posts: 1010
Unfounded opinion

Sure, greater visibility of the operation, and simpler and shorter syntax compared to tuple syntax—in fact, unfounded.

Why do we need a symmetric difference operator that shortens the expression by half and immediately reveals the expression’s purpose, when we can achieve the same thing using basic operators? After all, Foo >< Bar does the same as (Foo + Bar) - (Foo * Bar). But no one in their right mind would want to write the second version because of its complexity, since the dedicated operator immediately makes the expression's purpose clear and provides a short, simple and easy to understand syntax.

A dedicated swap operator would be just as useful, clear, and distinct in its functionality as the one for the symmetric difference of sets.

Quote
I don't understand what tuples have to do with it. Could you elaborate.

The syntax (A, B) := (B, A) is used for assigning anonymous tuples, and tuples are currently supported by the unleashed dialect. However, tuples can be disabled using a dedicated compiler directive, in which case this syntax for swapping will no longer be available.

Quote
Don't care. _Swap() or SwapIntrinsic() for all I care. It really doesn't matter much. Even that is more intuitive than :=:

And you should care, because a swap has many contexts, so the name of the new intrinsic should clearly explain what it’s for and fit with the generally accepted Pascal naming convention. "Don’t care" is a strange statement coming from a compiler developer.

My candidate for such intrinsic would be SwapVar or SwapVars (SwapValues also looks good).
« Last Edit: May 29, 2026, 05:40:03 pm by flowCRANE »
Lazarus 4.8 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Unfounded opinion

Sure, greater visibility of the operation, and simpler and shorter syntax compared to tuple syntax—in fact, unfounded.

Still opinion based, an no facts to support it.

Quote
Why do we need a symmetric difference operator that shortens the expression by half and immediately reveals the expression’s purpose, when we can achieve the same thing using basic operators? After all, Foo >< Bar does the same as (Foo + Bar) - (Foo * Bar). But no one in their right mind would want to write the second version because of its complexity, since the dedicated operator immediately makes the expression's purpose clear and provides a short, simple and easy to understand syntax.

You are diverging from the topic. Why is it an explicit case for an operator in the first place? Not necessarily relating to decisions made in 1968.


Quote
Quote
I don't understand what tuples have to do with it. Could you elaborate.

The syntax (A, B) := (B, A) is used for assigning anonymous tuples, and tuples are currently supported by the unleashed dialect

Ok. Well, a swap operation (operator or intrinsic) requires write access (lvalue) on both sides. But I don't see a reason why if  (A,B) :=: (B,A) would be different from swapintrinsic( (A,B), (B,A) );

In short, it doesn't make the case for an operator. At all.

Quote
And you should care, because a swap has many contexts, so the name of the new intrinsic should clearly explain what it’s for and fit with the generally accepted Pascal naming convention. "Don’t care" is a strange statement coming from a compiler developer.

Well, I'm not. I'm a RTL developer that picked up a thing of two after being on a list with compiler developers for over 25 years. I regularly tease Pascaldragon (who is the end all for dialect matters nowadays) to finally clean out the mode OBJFPC cobwebs.

Quote
My candidate for such intrinsic would be SwapVar or SwapVars (SwapValues also looks good).

SwapValues would be the best so far.

creaothceann

  • Sr. Member
  • ****
  • Posts: 481
Or Exchange, which would fit neatly with InterlockedExchange, InterlockedCompareExchange etc.

 

TinyPortal © 2005-2018