Skip to content

Allow chaining conditions in if using &&/|| #3336

@jprochazk

Description

@jprochazk

I was surprised to find that as of v1.51, the following does not work:

arch := if os() == "macos" && arch() == "aarch64" { "arm64" } else { arch() }
print-arch:
	echo {{ arch }}

According to the GRAMMAR.md, this is not valid syntax, but only because equality cannot exist outside of an if condition.

Last mention of conditional logic I found was here: #696 - has the situation changed?

To support this, the grammar would be updated to allow equality to appear as an expression outside an if condition, and if condition would become an arbitrary expression, or only disjunct if that's too much.

  expression    : disjunct || expression
                | disjunct

- disjunct      : conjunct && disjunct
-               | conjunct
+ disjunct      : condition && disjunct
+               | condition

+ condition     : conjunct '==' conjunct
+               | conjunct '!=' conjunct
+               | conjunct '=~' conjunct
+               | conjunct

- conjunct      : 'if' condition '{' expression '}' 'else' '{' expression '}'
+ conjunct      : 'if' expression '{' expression '}' 'else' '{' expression '}'
                | 'assert' '(' condition ',' expression ')'
                | '/' expression
                | value '/' expression
                | value '+' expression
                | value

- condition     : expression '==' expression
-               | expression '!=' expression
-               | expression '=~' expression

&&/|| would have higher precedence, so a == b && c == d would parse as (a == b) && (c == d).

I'd be happy to take a stab at implementing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions