Skip to content

Conversation

Copy link

Copilot AI commented Sep 1, 2025

This PR implements basic support for Python 3.10+ match statements (structural pattern matching) in Skulpt. The implementation covers fundamental pattern matching capabilities including literal patterns and wildcard patterns.

What's implemented

The following match statement syntax is now supported:

match x:
    case 1:
        print("one")
    case 2: 
        print("two")
    case 5:
        print("five")
    case _:  # wildcard pattern
        print("other")

String pattern matching also works:

match s:
    case "hello":
        print("greeting")
    case "world":
        print("planet")
    case _:
        print("unknown")

Implementation details

  • Grammar: Added match_stmt and case_block productions to Grammar.txt
  • AST: Added Match and match_case node types to Python.asdl
  • Parser: Implemented astForMatchStmt() to construct AST nodes from parse trees
  • Symbol table: Added symbol table visitor support for match statements
  • Compiler: Implemented cmatch() to generate JavaScript code with proper control flow
  • Tests: Added comprehensive test coverage in test/run/t950.py

Current limitations

This initial implementation focuses on basic literal pattern matching. More advanced features like tuple destructuring, variable binding in patterns, and guard clauses are not yet implemented but could be added in future PRs.

The match and case keywords are properly implemented as soft keywords as defined in the keyword module.

Testing

The implementation passes all existing tests and includes new test coverage. Basic match statement functionality works correctly with proper control flow and pattern matching semantics.

Fixes #3.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits September 1, 2025 04:17
…and compilation

Co-authored-by: acbart <897227+acbart@users.noreply.github.com>
Co-authored-by: acbart <897227+acbart@users.noreply.github.com>
Copilot AI changed the title [WIP] Support match keyword Implement basic match statement support for Python 3.10+ structural pattern matching Sep 1, 2025
Copilot AI requested a review from acbart September 1, 2025 04:20
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.

Support match keyword

2 participants