Skip to content

Implimenting issue #887 - Adding an optional if or for at the end of a comp function. #901

Open
ad4mf06 wants to merge 2 commits intoevhub:developfrom
ad4mf06:feature/pattern-matching-comprehensions-887
Open

Implimenting issue #887 - Adding an optional if or for at the end of a comp function. #901
ad4mf06 wants to merge 2 commits intoevhub:developfrom
ad4mf06:feature/pattern-matching-comprehensions-887

Conversation

@ad4mf06
Copy link

@ad4mf06 ad4mf06 commented Feb 24, 2026

Summary

This PR implements issue #887 by adding support for optional if and for clauses at the end of pattern-matching comprehensions.

Changes

grammar.py (line 1969)

Added .Optional(comp_iter) at the end of match_comp_for, allowing an optional if or for clause to be appended to a pattern-matching comprehension.

compiler.py (line 4597)

Modified the match_comp_expr_handle function to handle the optional extra comprehension clauses:

  • If len(match_for_group) == 2: the group contains only matches and iterable (no extra clauses).
  • If len(match_for_group) > 2: the third element and beyond are joined into extra_comp_clauses, which are appended to the generated Python comprehension expression.

Extra clauses are also handled for expr_setnames context: any variables introduced by for clauses in extra_comp_clauses are extracted via regex and removed from new_names to avoid false positives in scope analysis.

tests/src/cocotest/agnostic/primary_2.coco

Added a comprehensive test suite for pattern-matching comprehensions covering:

  • Basic match comprehension with CompPair and CompTriple data types
  • Extra for clauses after the pattern match
  • for + if combinations on loop variables
  • if filtering on pattern variables (single and multiple conditions)
  • Dict, set, and generator comprehensions with pattern matching
  • Combined conditions using and

@evhub
Copy link
Owner

evhub commented Feb 26, 2026

I think it's a bit confusing for only the first comprehension to have the ability to be a pattern-matching comprehension. My preference here is to either stick with the current implementation that only allows one comprehension, or to support arbitrary mixing and matching of standard and pattern-matching comprehensions (the ideal, but annoying to implement).

@ad4mf06
Copy link
Author

ad4mf06 commented Mar 2, 2026

Thanks for the feedback! I've updated the PR to implement the ideal option — arbitrary mixing and matching of standard and pattern-matching comprehensions.

The approach works by adding two new grammar rules (inner_match_comp_for with explicit match keyword, and implicit_inner_match_comp_for without) to comp_iter, which is the recursive clause rule. This means pattern-matching for
clauses can now appear in any position and in any combination with standard for/if clauses:

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants