Skip to content

Parser gives AND and OR the same precedence, but the device evaluates AND first #1994

Open

Description

@jack1590

The parser gives and and or the same precedence and groups them left to right. On the device, and binds tighter than or (Roku's precedence table lists them as AND > OR). So for

x = a or b and c

the device evaluates a or (b and c), but BrighterScript's AST is (a or b) and c:

const { Parser } = require('brighterscript');
const p = Parser.parse('sub main()\n  x = a or b and c\nend sub');
const expr = p.ast.statements[0].func.body.statements[0].value;
const show = e => e.left ? `(${show(e.left)} ${e.operator.text} ${show(e.right)})` : e.name?.text ?? e.token?.text;
console.log(show(expr)); // ((a or b) and c)

The cause is in Parser.boolean(), which matches TokenKind.And and TokenKind.Or in one loop.

Plain transpiling is unaffected, because the original tokens are written back out as they were. It bites plugins that rebuild expressions from the AST. With rooibos 5.17.0 code coverage on, every operand of an and/or gets wrapped in a function call, and the wrapping follows the AST grouping. So this:

if content = invalid or content.video = invalid and isReady then

is instrumented as (content = invalid or content.video = invalid) and isReady. That evaluates content.video when content is invalid, and the coverage build crashes where the normal build doesn't. We worked around it by adding explicit parentheses to the two places in our code that relied on the device's grouping.

Splitting boolean() into an or level that calls an and level would match the device.

Versions: brighterscript 0.69.13, rooibos-roku 5.17.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions