https://kotlinlang.org logo
s

Sam Stone

07/09/2023, 8:20 AM
Is the order of precedence for logical operators the same as in Java? Is there a program someone has written to show what is the order of execution for a statement like
Copy code
a == b && c || a == d && !e
❤️ 1
you could possibly use https://github.com/kotlinx/ast to parse and print the groupings of the expression but I'm not aware of anything pre-built
✔️ 1
Copy code
$ kotlin parse.main.kts 'a == b && c || a == d && !e'
packageHeader
functionDeclaration
  FUN >>>fun<<< (DEFAULT_TOKEN_CHANNEL)
  WS >>> <<< (HIDDEN)
  simpleIdentifier
    Identifier >>>main<<< (DEFAULT_TOKEN_CHANNEL)
  functionValueParameters
    LPAREN >>>(<<< (DEFAULT_TOKEN_CHANNEL)
    RPAREN >>>)<<< (DEFAULT_TOKEN_CHANNEL)
  WS >>> <<< (HIDDEN)
  functionBody
    block
      LCURL >>>{<<< (DEFAULT_TOKEN_CHANNEL)
      NL >>>\n<<< (DEFAULT_TOKEN_CHANNEL)
      statements
        statement
          expression
            disjunction
              conjunction
                equality
                  comparison
                    genericCallLikeComparison
                      infixOperation
                        elvisExpression
                          infixFunctionCall
                            rangeExpression
                              additiveExpression
                                multiplicativeExpression
                                  asExpression
                                    prefixUnaryExpression
                                      postfixUnaryExpression
                                        primaryExpression
                                          simpleIdentifier
                                            Identifier >>>a<<< (DEFAULT_TOKEN_CHANNEL)
                  WS >>> <<< (HIDDEN)
                  equalityOperator
                    EQEQ >>>==<<< (DEFAULT_TOKEN_CHANNEL)
                  WS >>> <<< (HIDDEN)
                  comparison
                    genericCallLikeComparison
                      infixOperation
                        elvisExpression
                          infixFunctionCall
                            rangeExpression
                              additiveExpression
                                multiplicativeExpression
                                  asExpression
                                    prefixUnaryExpression
                                      postfixUnaryExpression
                                        primaryExpression
                                          simpleIdentifier
                                            Identifier >>>b<<< (DEFAULT_TOKEN_CHANNEL)
                WS >>> <<< (HIDDEN)
                CONJ >>>&&<<< (DEFAULT_TOKEN_CHANNEL)
                WS >>> <<< (HIDDEN)
                equality
                  comparison
                    genericCallLikeComparison
                      infixOperation
                        elvisExpression
                          infixFunctionCall
                            rangeExpression
                              additiveExpression
                                multiplicativeExpression
                                  asExpression
                                    prefixUnaryExpression
                                      postfixUnaryExpression
                                        primaryExpression
                                          simpleIdentifier
                                            Identifier >>>c<<< (DEFAULT_TOKEN_CHANNEL)
              WS >>> <<< (HIDDEN)
              DISJ >>>||<<< (DEFAULT_TOKEN_CHANNEL)
              WS >>> <<< (HIDDEN)
              conjunction
                equality
                  comparison
                    genericCallLikeComparison
                      infixOperation
                        elvisExpression
                          infixFunctionCall
                            rangeExpression
                              additiveExpression
                                multiplicativeExpression
                                  asExpression
                                    prefixUnaryExpression
                                      postfixUnaryExpression
                                        primaryExpression
                                          simpleIdentifier
                                            Identifier >>>a<<< (DEFAULT_TOKEN_CHANNEL)
                  WS >>> <<< (HIDDEN)
                  equalityOperator
                    EQEQ >>>==<<< (DEFAULT_TOKEN_CHANNEL)
                  WS >>> <<< (HIDDEN)
                  comparison
                    genericCallLikeComparison
                      infixOperation
                        elvisExpression
                          infixFunctionCall
                            rangeExpression
                              additiveExpression
                                multiplicativeExpression
                                  asExpression
                                    prefixUnaryExpression
                                      postfixUnaryExpression
                                        primaryExpression
                                          simpleIdentifier
                                            Identifier >>>d<<< (DEFAULT_TOKEN_CHANNEL)
                WS >>> <<< (HIDDEN)
                CONJ >>>&&<<< (DEFAULT_TOKEN_CHANNEL)
                WS >>> <<< (HIDDEN)
                equality
                  comparison
                    genericCallLikeComparison
                      infixOperation
                        elvisExpression
                          infixFunctionCall
                            rangeExpression
                              additiveExpression
                                multiplicativeExpression
                                  asExpression
                                    prefixUnaryExpression
                                      unaryPrefix
                                        prefixUnaryOperator
                                          excl
                                            EXCL_NO_WS >>>!<<< (DEFAULT_TOKEN_CHANNEL)
                                      postfixUnaryExpression
                                        primaryExpression
                                          simpleIdentifier
                                            Identifier >>>e<<< (DEFAULT_TOKEN_CHANNEL)
        semis
          NL >>>\n<<< (DEFAULT_TOKEN_CHANNEL)
      RCURL >>>}<<< (DEFAULT_TOKEN_CHANNEL)
👍 1
k

Klitos Kyriacou

07/09/2023, 4:37 PM
What's an easy way of remembering the
||
vs
&&
precedence? For me, I treat them like
+
and
*
. We all know the precedence of those arithmetic operators since junior school. If you treat booleans as numbers, with 0 being false and non-zero being true (as is the case with C), then:
||
is like
+
: true or false = 1 + 0 = 1 = true
&&
is like
*
: true and false = 1 * 0 = 0 = false
e

ephemient

07/09/2023, 4:41 PM
strictly speaking,
||
is saturating add, which doesn't have an operator, while wrapping add (
+
) is `xor`… but that's the right analogy in general
also note that the infix functions
and
or
xor
etc. do not have separate precedence from each other, because of how infix functions work in Kotlin. but you should very rarely (if ever) see those used on
Boolean