Skip to content

Language: Ternary operator (?:) in compiler pipeline #114

Description

@PurHur

Problem

PHPCfg\Parser::parseExpr_Ternary() lowers ?: to CFG branches, but lib/Compiler.php has no handler for the resulting ops—any script using ternaries hits Unsupported expression.

Ternaries are ubiquitous in web apps (defaults, inline HTML flags, routing).

Goal

$x = $cond ? $a : $b compiles on VM, JIT, and AOT.

Implementation hints

  1. Reproduce: bin/print.php -r '$x = $a ? $b : $c;' — note CFG block structure (JumpIf, assign blocks).
  2. lib/Compiler.php: extend compileExpr() (or dedicated helper) for the ternary CFG op php-cfg emits after parseExpr_Ternary() — likely not a single Op\Expr\* node; trace with bin/print.php before guessing opcode names.
  3. Lowering pattern: evaluate condition → TYPE_JUMPIF to true/false blocks → compile each arm → phi/assign merge to target variable (mirror compileSwitchStmt block linking).
  4. lib/VM.php: both arms must produce compatible stack types; document mixed string/int behavior vs Zend.
  5. lib/JIT.php: LLVM branch + Builder::phi at merge block for SSA values.
  6. Lint: add registry entry until done (Lint: Register Phase-2 blockers in UnsupportedRegistry (ternary, .=, break) #258 pattern) if lint reports opaque errors.
  7. Tests: test/compliance/cases/language/ternary.phpt (string, int, nested); optional test/compliance/cases/language/ternary_mixed.phpt.

Acceptance criteria

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  php bin/vm.php -r '$a=1; echo ($a>0)?"yes":"no";'
docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  php bin/jit.php -r '$a=1; echo ($a>0)?"yes":"no";'

Both print yes. phpc lint accepts compliant ternary scripts.

Verification (local / Docker only)

./script/ci-local.sh --filter ternary

Dependencies

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