Skip to content

Add Pattern Matching Support#3

Open
hasumikin wants to merge 16 commits intomasterfrom
pattern_matching
Open

Add Pattern Matching Support#3
hasumikin wants to merge 16 commits intomasterfrom
pattern_matching

Conversation

@hasumikin
Copy link
Member

This PR implements pattern matching (case/in syntax) for the mruby-compiler2, bringing Ruby 2.7+ pattern matching capabilities to PicoRuby.

Changes

  • Code generation (src/codegen_prism.inc): 683 lines implementing:

    • Pattern matching compilation logic
    • Array pattern deconstruction
    • Guard clause evaluation
    • Pattern matching control flow
    • Symbol additions (include/mrc_presym.inc): Added necessary presymbols for pattern matching operations
  • New test suite (compiler_test/pattern_match_test.rb): comprehensive tests covering:

    • Value patterns (integers, strings, symbols, booleans, nil)
    • Variable patterns and wildcards
    • Array patterns with exact matching
    • Alternative patterns (| operator)
    • Guard clauses (if/unless conditions)
    • Multiple in clauses with else fallback

Some tests are pending until mruby/c supports pattern matching mrubyc/mrubyc#257

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements Ruby 2.7+ case/in pattern matching in the PicoRuby mruby-compiler2 code generator, along with required presymbol additions and a new compiler-level test suite to validate pattern compilation behavior.

Changes:

  • Add pattern-matching code generation (case/in, guards, alternation, array/hash/find patterns) in src/codegen_prism.inc.
  • Extend presymbol table with deconstruct, deconstruct_keys, and size for pattern-matching runtime calls.
  • Add compiler_test/pattern_match_test.rb covering core pattern matching behaviors (with some cases marked pending for mruby/c).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.

File Description
src/codegen_prism.inc Adds the main pattern matching compilation implementation and PM_CASE_MATCH_NODE handling.
include/mrc_presym.inc Adds presymbols used by the new codegen (deconstruct, deconstruct_keys, size).
compiler_test/pattern_match_test.rb Adds a new test suite for pattern matching across literals, variables, arrays, alternation, pin, and guards (with pending section).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI commented Feb 10, 2026

@hasumikin I've opened a new pull request, #4, to work on those changes. Once the pull request is ready, I'll request review from you.

@hasumikin hasumikin force-pushed the pattern_matching branch 2 times, most recently from 3417cfd to bfa030a Compare February 12, 2026 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants