Skip to content

Add support for ternary operator block - #11687

Open
Dmitriy Antipov (THEb0nny) wants to merge 4 commits into
microsoft:masterfrom
THEb0nny:feature/logic-ternary
Open

Dmitriy Antipov (THEb0nny) wants to merge 4 commits into
microsoft:masterfrom
THEb0nny:feature/logic-ternary

Conversation

@THEb0nny

@THEb0nny Dmitriy Antipov (THEb0nny) commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR adds support for the ternary operator block (logic_ternary) in MakeCode:

  • Block Layout: Single-line inline layout (if %1 then %2 else %3) with round reporter output shape.
  • Compiler: Compiles to JavaScript ternary expressions (cond ? then : else) with operator-precedence-aware parenthesization for nested ternaries and higher-precedence parent expressions. Also cleans up unnecessary as any casts on relational operators (<, <=, >, >=) in the condition, while preserving necessary ones for literal equality checks (TS2367).
  • Decompiler: Decompiles TypeScript conditional expressions (a ? b : c) into logic_ternary blocks.
  • Type Checker & Inference: Infers Boolean for the condition input and unifies return types of both THEN and ELSE branches in both infer() and returnType().
  • Target Configuration: Controlled via "ternaryBlock": true in pxtarget.json under runtime (placed in the Logic category with weight 30, matching the pattern of breakBlock in Loops). Even when the flag is disabled or absent, existing or decompiled ternary blocks still render and compile cleanly.
  • Documentation: Added a reference guide at /blocks/logic/ternary with interactive block examples and linked it from the Logic overview.
  • Robustness: Added defensive guards when looking up Blockly.Blocks definitions in initLogic().
  • Tests: Added decompiler test case and baseline in tests/decompile-test.

Tests

  • Ran testdecompiler: 166/166 passing.
  • Ran testlang: 59/59 passing.
  • Ran testtutorials: 25/25 passing.
  • Tested locally in MakeCode target with "ternaryBlock": true and "ternaryBlock": false.

Screenshot

Screenshot_14 Screenshot_15

Remove extra blank lines before the JavaScript and Python syntax examples.
@THEb0nny

Copy link
Copy Markdown
Contributor Author
BrickCode.for.EV3.-.Google.Chrome.2026-10-24.20-49-57-compressed.mp4

- Guard Blockly.Blocks lookup in initLogic to avoid potential TypeError if not registered

- Unify and return the type of THEN and ELSE branches in returnType for logic_ternary

- Make stripAsAny in compileTernary resilient to whitespace variations
@THEb0nny

Copy link
Copy Markdown
Contributor Author

Pushed an update with additional polish and robustness improvements:

  • Block initialization guard: Added a defensive check for Blockly.Blocks[logicTernaryId] in initLogic to prevent potential runtime TypeErrors if Blockly built-in blocks aren't yet loaded.
  • Type inference: Updated returnType for logic_ternary in typeChecker.ts to compute and return the unified type of THEN and ELSE branches (union(t1, t2) -> find(t1)) when both are present.
  • Compiler: Hardened stripAsAny in compileTernary against whitespace variations. (Note: as any is intentionally preserved for equality operators == / != on literal operands to comply with TypeScript's non-overlapping literal comparison checks TS2367, matching the existing pattern in compileArithmetic).
  • All tests passing (testdecompiler: 166/166, testlang: 59/59, testtutorials: 25/25).

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.

1 participant