Fix Calculator rewriting for nested pow calls

SA
2d ago
+13-4

Summary

Fixes #4785.

The pow/log/ln workaround regexes combined .NET balancing groups with RegexOptions.RightToLeft. When matching right-to-left, the balancing-group match for input such as pow(sqrt(4),3) stops at the nested call's closing parenthesis, so PowMatchEvaluator receives pow(sqrt(4) rather than the complete call, cannot find a top-level comma, and leaves the expression unchanged. It then reaches Mages' broken pow, which swaps its arguments (FlorianRappl/Mages#132), producing 3^2 = 9.

Removing RightToLeft lets the balancing-group regex capture the complete outer call. The existing replacement loops then iteratively rewrite nested pow/log/ln calls, including pow(pow(2,2),3), while preserving the existing argument trimming and top-level-comma handling. PowMatchEvaluator now emits ((a)^(b)) so compound arguments keep their grouping in the rewritten exponent expression.

Added Calculator regression cases for:

  • pow(sqrt(4),3) -> 8
  • pow(min(2,3),3) -> 8
  • pow(2,sqrt(9)) -> 8
  • pow(pow(2,2),3) -> 64
  • pow(1+sqrt(4),3) -> 27
  • pow(2,1+1) -> 4
  • whitespace in pow( 2 , 3 )
  • nested unary rewriting with log(sqrt(10000)) and ln(sqrt(e))

Verification

Passed:

  • dotnet build Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj -c Release -p:EnableWindowsTargeting=true — 0 errors (only the existing SourceLink warning)
  • dotnet format /home/ubuntu/repos/Flow.Launcher/Flow.Launcher.sln whitespace --verify-no-changes --no-restore --include /home/ubuntu/repos/Flow.Launcher/Plugins/Flow.Launcher.Plugin.Calculator/Main.cs /home/ubuntu/repos/Flow.Launcher/Plugins/Flow.Launcher.Plugin.Calculator/MainRegexHelper.cs /home/ubuntu/repos/Flow.Launcher/Flow.Launcher.Test/Plugins/CalculatorTest.cs
  • dotnet run --project /home/ubuntu/calculator-harness/calculator-harness.csproj — 13/13 assertions passed. The temporary Linux console harness loaded the built plugin assembly's generated GetPowRegex/GetLogRegex/GetLnRegex by reflection, applied the same replacement loops and match evaluators, and evaluated the rewrites with Mages.Core:
    • pow(2,3) -> 8
    • pow(sqrt(4),3) -> 8
    • pow(min(2,3),3) -> 8
    • pow(2,sqrt(9)) -> 8
    • pow(pow(2,2),3) -> 64
    • pow(1+sqrt(4),3) -> 27
    • pow(2,1+1) -> 4
    • pow(log(100),3) -> 8
    • pow( 2 , 3 ) -> 8
    • pow(2,3)+pow(4,2) -> 24
    • log(sqrt(10000)) -> 2
    • ln(e) -> 1
    • ln(sqrt(e)) -> 0.5

Before the fix, the same harness reproduced pow(sqrt(4),3) => 9, pow(min(2,3),3) => 9, and pow(pow(2,2),3) => 81.

I also attempted the requested project test command:

dotnet test Flow.Launcher.Test --filter CalculatorPluginTest -p:EnableWindowsTargeting=true

On this Linux machine the Windows-targeted test project does not compile because unrelated projects fail generated-localization references (CS0103: The name 'Localize' does not exist) in Flow.Launcher.Plugin.Shell, Flow.Launcher.Plugin.Explorer, and Flow.Launcher.Infrastructure. The Calculator plugin itself builds successfully with EnableWindowsTargeting=true.