Skip to content

Collections: pattern matching on List and Map #62

@MelbourneDeveloper

Description

@MelbourneDeveloper

Scope

Implements Phase 5 of docs/plans/collections.md. Three pattern forms on lists, one on maps:

  • Empty list pattern [] — guard osprey_list_length(xs) == 0
  • Fixed-length pattern [x, y] — guard length, bind via osprey_list_get
  • Head/tail pattern [head, ...tail] — guard length ≥ N, bind head positions and a sub-list via a new osprey_list_drop(n) runtime call
  • Subset map pattern { "key": binding, ... } — guard contains(key) for each listed key, bind via get

Required work

  1. Grammar update in compiler/osprey.g4 — new pattern productions.
  2. make regenerate-parser.
  3. New AST nodes: EmptyListPattern, FixedListPattern, HeadTailListPattern, SubsetMapPattern.
  4. Builder support in compiler/internal/ast/builder_match.go.
  5. Match codegen branches in compiler/internal/codegen/ to emit the guards and bindings.
  6. Tested examples covering each pattern form in compiler/examples/tested/basics/lists/.

Acceptance

  • Spec examples classify(xs) and analyze(p) from spec/0004-TypeSystem.md compile and produce expected output.
  • All four pattern shapes have an example in examples/tested/basics/lists/ with .expectedoutput.
  • make ci green.

Origin

docs/plans/collections.md Phase 5 (deferred — required grammar + parser regen + AST + builder + match-codegen across multiple files).

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