zertovitch / hac

HAC Ada Compiler - a small, quick Ada compiler fully in Ada
http://hacadacompiler.sf.net/
122 stars 11 forks source link

Prefer short circuit and membership test #11

Closed pjljvandelaar closed 1 year ago

pjljvandelaar commented 1 year ago

Dear HAC developers, Dear Gautier de Montmollin,

Thinking about you request to change

if Code = SP_Open or Code = SP_Create or Code = SP_Append then

into

if Code in SP_Open | SP_Create | SP_Append then

I realized why we didn't have this find and replace pattern.

On my codebases, we always started with preferring short circuit operators. See for more info adaic

When the code is changed and uses short circuit operators, we apply the find-and-replace patterns to prefer membership tests.

So I exactly applied these two steps in succession on the hac archive. And e.g. src/apps/hac_pkg.adb benefits from both!

Hopes this helps in making hac even better and easier to maintain!

Greetings, Pierre

P.S. A pretty printer is applied to every rewrite to ensure that no warnings are introduced (such as line too long). However not all your code seems to adhere to the pretty print settings as specified in your project file.

Problem detected and solved by Rejuvenation-Ada crate vote for Rejuvenation-Ada as The 2022 Ada Crate Of The Year

zertovitch commented 1 year ago

Integrated a good part of the suggest changes. Thanks! Commit: https://github.com/zertovitch/hac/commit/83188104d9e02e18e0912cd7d70cfa0eaf543067.

For cases like b1 or b2 where b1 and b2 are simple boolean variables, I have left the full evaluation. The shortcuts introduce a branch (CPU-wise), or said otherwise, a hidden "if .. then .." which is not always a performance gain. For cases where safety matters like suggested in the Ada 95 Quality and Style Guide, I already use the shortcut form.