Recent

Author Topic: Ann: DeCoperators  (Read 2365 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19119
  • Glad to be alive.
Re: Ann: DeCoperators
« Reply #15 on: April 14, 2026, 09:23:24 am »
Created a Lazarus plugin:
You can either use it through the menu or use crtl-shift-q

THIS IS A MAJOR UPGRADE and now BETA 1:
- The utility skips comments and literals
- The console app is renamed.
- The Lazarus plugin supports the same.

Given:
Code: Pascal  [Select][+][-]
  1. program testcoperators;
  2. {$ifdef fpc}{$mode objfpc}{$endif}
  3. var
  4.   a,b,c,d:integer;
  5.   e,f:double;
  6.   s:string;
  7. begin
  8.   // comment a+=1
  9.   a:=10;b:=10;c:=10;d:=10;e:=10.0;f:=10;
  10.   { more
  11.     a -=1
  12.     comment }
  13.   a+=a;a -= a; e/=f;a+=a;
  14.   (* even
  15.   a*=1
  16.    more comment *)
  17.   s:='a line with some calculation like a-=a in the middle';
  18.   c *= d;a += a;
  19. end.

Both the console version and the plugin produce:
Code: Pascal  [Select][+][-]
  1. program testcoperators;
  2. {$ifdef fpc}{$mode objfpc}{$endif}
  3. var
  4.   a,b,c,d:integer;
  5.   e,f:double;
  6.   s:string;
  7. begin
  8.   // comment a+=1
  9.   a:=10;b:=10;c:=10;d:=10;e:=10.0;f:=10;
  10.   { more
  11.     a -=1
  12.     comment }
  13.   a := a + a;a := a - a; e := e / f;a := a + a;
  14.   (* even
  15.   a*=1
  16.    more comment *)
  17.   s:='a line with some calculation like a-=a in the middle';
  18.   c := c * d;a := a + a;
  19. end.

The utility won't convert parts where {$defines} do not apply, only what is current.

The Lazarus plugin works on a selection in the whole file, the console app works on a whole pascal unit or program.
Multi-line strings are not yet supported.(but are not yet in a release...)

Plz test and let me know.

https://1drv.ms/v/c/d4e10f4b00df53d8/IQAoRu4buuA9QoQFmEest17cAe_dTfYqYgVQLihxa581Ovs?e=aiQfJm

« Last Edit: April 14, 2026, 02:18:01 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

creaothceann

  • Sr. Member
  • ****
  • Posts: 350
Re: Ann: DeCoperators
« Reply #16 on: April 15, 2026, 11:52:43 am »
This whole issue reminds me of France trying to keep English words out of their language.

440bx

  • Hero Member
  • *****
  • Posts: 6462
Re: Ann: DeCoperators
« Reply #17 on: April 15, 2026, 12:11:37 pm »
This whole issue reminds me of France trying to keep English words out of their language.
Which is a bit amusing considering that roughly 40% of the words in the English language have their origin in French.    French used to be the language spoken by the English high class/royalty, it was the "kewl" thing until France and England engaged in a war (this happened a few centuries ago, don't remember exactly when.)  There are still quite a few words that are _identical_, the only difference is their pronunciation (pronunciation being one of those words :) )

Just an amusing factoid.
 
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 929
  • Behold, I bring salvation - FPC Unleashed
Re: Ann: DeCoperators
« Reply #18 on: April 15, 2026, 12:34:19 pm »
Now make a "compactor" - the reverse tool. Takes verbose Pascal and compresses it into the shortest readable form possible. Bonus points if it uses Unleashed syntax :D

Given:

Code: Pascal  [Select][+][-]
  1. program VerboseExample;
  2. {$mode objfpc}
  3. var
  4.   I: Integer;
  5.   Sum: Integer;
  6.   Items: array of Integer;
  7.   S: String;
  8. begin
  9.   Sum := 0;
  10.   SetLength(Items, 5);
  11.   for I := 0 to High(Items) do
  12.   begin
  13.     Items[I] := I * 2;
  14.     Sum := Sum + Items[I];
  15.   end;
  16.   if Sum > 10 then
  17.     S := 'big'
  18.   else
  19.     S := 'small';
  20.   WriteLn(S, ': ', Sum);
  21. end.
  22.  

Becomes:

Code: Pascal  [Select][+][-]
  1. program CompactExample;
  2. {$mode unleashed}
  3. begin
  4.   var Items: array of Integer := [0, 2, 4, 6, 8];
  5.   var Sum := 0;
  6.   for var I := 0 to High(Items) do Sum += Items[I];
  7.   WriteLn(if Sum > 10 then 'big' else 'small', ': ', Sum);
  8. end.
  9.  
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

440bx

  • Hero Member
  • *****
  • Posts: 6462
Re: Ann: DeCoperators
« Reply #19 on: April 15, 2026, 12:47:05 pm »
That would be a very difficult compactor to implement because not only it has to use the more compact syntax but, it also has to interpret the Pascal code it is compacting to reach the "becomes" target.

Specifically, initializing the array of integers requires the compactor to interpret and execute the lines "SetLength(....)" and "Items[I] := I * 2;".  That's quite a bit different than simply replacing one set of syntactic elements with another more compact one.

Who knows, with the proper training, maybe A.I (Claude) might be able to do it.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19119
  • Glad to be alive.
Re: Ann: DeCoperators
« Reply #20 on: April 15, 2026, 01:29:40 pm »
Now make a "compactor" - the reverse tool. Takes verbose Pascal and compresses it into the shortest readable form possible. Bonus points if it uses Unleashed syntax :D
Well, you have the code. Anything to reverse it can use the same parser.... :)

Anyway: this serves at least three issues:
- solve unwanted feature, added by mistake
- produce code that is easier to be accepted by the core team if it involves a compiler patch.
- stick to normal pascal syntax.

Otherwise I am just your average party spoiler, you know I provided refused patches... - on the same subject - and have a 15+ year history of "fixing" this and bug reports :-[ :-X  )

I might write more "Undo" tools, because I like it.
You  know, but just in case: I don't care either way. I support new language features.

Back in the days I wrote a "feature" that changed all compiled code to Pascal byte arrays - fixed length - and that still worked after compilation if no limits were reached.. 
« Last Edit: April 15, 2026, 01:45:54 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: Ann: DeCoperators
« Reply #21 on: April 17, 2026, 03:39:19 am »
Why didn't anyone read my comment, which was one of the first?

There's already a "JCF" package with similar functionality (for formatting). I'm sure such an add-on would definitely be useful for many users, as it's installed by default and has the popular "Ctrl+D" shortcut.

There's probably already a codebase there for implementing similar functionality (e.g. string/comment processing).

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: Ann: DeCoperators
« Reply #22 on: April 17, 2026, 03:43:13 am »
Created a Lazarus plugin:
You can either use it through the menu or use crtl-shift-q
...
Plz test and let me know.

This package doesn't actually allow to undo changes, because after selecting a line and pressing [Ctrl+Shift+Q], the [Ctrl+Z] shortcut behaves erratically.

By the way, undo works correctly in the JCF package ))

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: Ann: DeCoperators
« Reply #23 on: April 17, 2026, 03:56:00 am »
That would be a very difficult compactor to implement because not only it has to use the more compact syntax but, it also has to interpret the Pascal code it is compacting to reach the "becomes" target.

Specifically, initializing the array of integers requires the compactor to interpret and execute the lines "SetLength(....)" and "Items[I] := I * 2;".  That's quite a bit different than simply replacing one set of syntactic elements with another more compact one.

Who knows, with the proper training, maybe A.I (Claude) might be able to do it.

This task is indeed more complex, but CodeTools can handle it easily (IDE uses it). Any package can use it.

By the way, I was surprised to see that JCF doesn't depend on CodeTools. I don't know how it's implemented.

Anyway, I'm interested in @DomingoGP's opinion on this topic (I see he supports JCF), so I sent him a PM.

Fibonacci

  • Hero Member
  • *****
  • Posts: 929
  • Behold, I bring salvation - FPC Unleashed
Re: Ann: DeCoperators
« Reply #24 on: April 17, 2026, 03:59:13 am »
Why didn't anyone read my comment, which was one of the first?

There's already a "JCF" package with similar functionality (for formatting). I'm sure such an add-on would definitely be useful for many users, as it's installed by default and has the popular "Ctrl+D" shortcut.

There's probably already a codebase there for implementing similar functionality (e.g. string/comment processing).

I'm actually working on a new formatter. It will also handle the Unleashed syntax extensions. I tried JCF in the past and spent some time configuring it, but I never managed to get the formatted code to look the way I wanted.

I plan to make it as configurable as possible, so everyone can set it up to their own taste.

As for "de-coperators" - honestly, it's a silly idea, and I'd guess the actual user count is zero, maybe @Thaddy himself uses it (though I doubt it, perhaps once in a blue moon). If it should exist at all, it definitely shouldn't be a standalone tool whose only job is swapping C-style operators for Pascal-style ones. It belongs as part of a larger formatter, exactly as you suggested - an addition to JCF. I may (emphasis on "may") add such an option too, working both ways: stripping C-operators, and converting what can be converted into C-operators. Everyone picks the style they prefer.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: Ann: DeCoperators
« Reply #25 on: April 17, 2026, 06:19:07 am »
I'm actually working on a new formatter. It will also handle the Unleashed syntax extensions. I tried JCF in the past and spent some time configuring it, but I never managed to get the formatted code to look the way I wanted.

I'd like to clarify: are you talking only about the features used for your FPC fork, or about general needs? It would be great if you made improvements to JCF itself - it would benefit a wider range of users.

As for the fork, it would certainly be better to make it a separate package. Theoretically, it could be an extension package for JCF (depending on it), if it had such interfaces. I have no idea if this is possible or necessary, just a thought.

440bx

  • Hero Member
  • *****
  • Posts: 6462
Re: Ann: DeCoperators
« Reply #26 on: April 17, 2026, 06:37:39 am »
I'm actually working on a new formatter. It will also handle the Unleashed syntax extensions.

I plan to make it as configurable as possible, so everyone can set it up to their own taste.
Just to mention an idea I've had in the past that you may find useful.   I've given some thought about writing a program that would separate FPC's front end (scanner, parser and IR) from the back end (code generator, optimizer, linker, etc.)

Ideally, this separated front end would produce a fully tokenized representation of the source even including comments along with internal representation that would include all scopes, types, variables and even constants.  The goal is to have a complete representation of the source code without any information loss which would in turn allow manipulating the original source as well as analyzing it.  Obviously a very ambitious project.

Since the scanner and parser would be from the compiler itself, that would ensure the tool matched the compiler because that part should not be modified other than possibly add interfaces to it.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: Ann: DeCoperators
« Reply #27 on: April 17, 2026, 06:45:25 am »
The CodeTools package can "read" Pascal code. Perhaps that's what you mean? I don't know its code, but it's obviously supposed to do something similar.

Regarding compiler integration, it's been discussed elsewhere that there's a fundamental difference between how a compiler reads code and how an IDE reads it. As far as I remember, an IDE should be tolerant of unfinished code and ignore some errors, while a compiler should be as fast as possible.

But this is definitely off-topic, it's better to create a separate thread...

440bx

  • Hero Member
  • *****
  • Posts: 6462
Re: Ann: DeCoperators
« Reply #28 on: April 17, 2026, 06:58:44 am »
But this is definitely off-topic, it's better to create a separate thread...
I agree, it is off topic.  I just wanted to mention/plant the idea.  If someone is interested in elaborating the idea they can create a separate thread and I'll contribute to it in the measure I can.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 929
  • Behold, I bring salvation - FPC Unleashed
Re: Ann: DeCoperators
« Reply #29 on: April 17, 2026, 07:14:59 am »
@n7800, @440bx:

It will handle standard Pascal syntax plus the Unleashed extensions. Whether the code being formatted contains Unleashed-specific constructs or not doesn't matter - it formats whatever it gets.

The code is fully standalone and independent - it has its own scanner and parser, no dependency on anything from FPC. It can be compiled as a CLI tool, a GUI app, a DLL, WASM (so the formatter will be available online on a web page, no download or install needed), as a Lazarus package, embedded into an IDE, or whatever else.

I don't plan to touch JCF.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

 

TinyPortal © 2005-2018