markw65 / prettier-extension-monkeyc

A VSCode extension for Garmin MonkeyC
MIT License
11 stars 1 forks source link

Decimal floats not folding #6

Closed danielfomin96 closed 1 year ago

danielfomin96 commented 1 year ago

Hello,

Firstly, thank you very much for this project. After starting to work with your extension, I don't think I can ever write monkey C without it again.

Upon inspection of the optimized code I found that literal expressions that contain floats in decimal form do not fold. Is this known or even anticipated? Since floats are used in many projects and probably have a great impact on the performance of the program this feature would be well received.

Example:

        function test(){
            const t = 10;
            const fl = 0.5;
            const c = 5;

            var test1 = t * fl * c;
            var test2 = t * c * fl;

            return test1 + test2;
        }
---------------------------------------------------
Optimized to:
    function test() {
      var test1 = 10 * 0.5 * 5;
      var test2 = 50 * 0.5;

      return test1 + test2;
    }
----------------------------------------------------
Expected:
    function test() {
      var test1 = 25;
      var test2 = 25;

      return test1 + test2;
    }
markw65 commented 1 year ago

I was just a little conservative when I first wrote the constant folding code. It also turns out that none of my projects have any examples of this, so I've never been motivated to go back and fix it!

But yes, should be fairly straightforward. I'll try to get it into the next release.

danielfomin96 commented 1 year ago

Thank you very much, this is much appreciated!

markw65 commented 1 year ago

Fixed by fa155792ff2e9a53a2a9dff0fe28ac16e1e074fd (the real fix was in @mark65/monkeyc-optimizer)