Splitties / WffDsl

Kotlin DSL to generate XML based Watch Face Format for Wear OS : https://developer.android.com/training/wearables/wff/watch-face
Apache License 2.0
16 stars 4 forks source link

Expressions with parenthesis are not parsed correctly #8

Closed lucianbc closed 7 months ago

lucianbc commented 7 months ago

Hi! Very good job for developing this. I like the operator overloading feature, but I found a problem where expressions have parenthesis. The following snippet

    expression(name = "my_expr") {
      (SourceType.Time.HOUR_1_24 `==` 1) `||` (SourceType.Time.HOUR_1_24 `==` 13)
    }

generates

<Expression name="my_expr">[HOUR_1_24] == 1 || [HOUR_1_24] == 13</Expression>

while it was expected to generate

<Expression name="my_expr">([HOUR_1_24] == 1) || ([HOUR_1_24] == 13)</Expression>

The first one, without parenthesis, is not evaluated correctly. This happens with all of the other operators. One solution would be to add parenthesis for all operator arguments. I don't know if it is possible to overload the () operator (not invoke).

LouisCAD commented 7 months ago

Hi! It's unfortunately not possible to override parentheses behavior in Kotlin, but that's okay, you only need to prefix them with p so it becomes a function call, which will carry the parentheses over into the arithmetic expression.

LouisCAD commented 7 months ago

Here's an example from tests: https://github.com/Splitties/WffDsl/blob/983af762fb1ee575eafd9763b9bf198455e739f5/core/src/test/kotlin/splitties/wff/ArithmeticExpressionTest.kt#L26

lucianbc commented 7 months ago

that's a nice solution. I missed that utility. Thanks!