Skip to content

Language: match expression (PHP 8) #143

Description

@PurHur

Problem

match ($x) { ... } is not compiled (Expr_Match maps to #143 in UnsupportedRegistry). Routers and modern PHP use match for clean path dispatch; 003-MiniWebApp uses switch today (#96 ✅) but future refactors may prefer match.

Goal

match with literal / int / string arms and a default arm (no guard expressions in v1):

echo match ($method) {
    'GET' => 'get',
    'POST' => 'post',
    default => 'other',
};

VM + JIT + AOT parity with Zend for simple arms.

Implementation hints

Layer Files Notes
Compiler lib/Compiler.php compileExpr Lower to chained compare + jump or reuse #96 switch machinery
CFG php-cfg Expr_Match arms One block per arm; default as fall-through
VM lib/VM.php New opcode or desugar to TYPE_SWITCH
JIT lib/JIT.php Mirror switch LLVM branch tree
Lint lib/Lint/UnsupportedRegistry.php Remove Expr_Match → 143 when done

Tests

Acceptance criteria

docker run --rm -i -w /compiler php-compiler:22.04-dev bash -lc '
  tar -xf - && php bin/vm.php -r "echo match (2) { 1 => \"a\", 2 => \"b\", default => \"c\" };"
'

Prints b. JIT/AOT compile the same snippet without throw.

Dependencies

Verification

./script/ci-local.sh --filter VMTest — local/Docker only (#394 GHA off).

Links

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions