Skip to content

Add flooring division (//) and modulus (%) operators#273

Open
AaronWebster wants to merge 1 commit into
masterfrom
emboss/divmod-operators
Open

Add flooring division (//) and modulus (%) operators#273
AaronWebster wants to merge 1 commit into
masterfrom
emboss/divmod-operators

Conversation

@AaronWebster

@AaronWebster AaronWebster commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Part 1 of 3 of the integer division/modulus series (base: master), following
doc/design_docs/division_and_modulus.md.

Adds integer // (flooring division) and % (flooring modulus) to the Emboss
expression language.

What this does

  • Front end: ///% tokens; the grammar splits the multiplicative tier
    into a *-chain and a ///%-chain that may not be mixed without
    parentheses (per the design doc); type checking; and expression-bounds
    computation.
  • Semantics: flooring, matching Python 3 (-7 // 2 == -4, -7 % 2 == 1).
    Constant folding reuses Python's ///%; the runtime FlooringQuotient /
    FlooringRemainder helpers convert C++'s truncating / and % for signed
    types (unsigned needs no conversion).
  • Division by zero: this initial rollout does not yet track an undefined
    value through the type system, so a divisor whose range includes zero is
    rejected at compile time. constant_value() folds ///% totally (a zero
    divisor yields no value instead of raising), so the diagnostic is reported
    cleanly rather than as a traceback.
  • Docs, unit tests (tokenizer, grammar, bounds, constraints, constant-folding,
    runtime arithmetic), an integration schema + golden, and formatter coverage.

Series

  1. This PR — the operators, with strict division-by-zero rejection.
  2. Relax to allow possibly-zero divisors, propagating an undefined value.
  3. IsComplete() returns true for a division-by-zero size.

Supersedes the stale WIP #263.

bazel test //... is green.


PR chain (merge in order): #273 (this) → #274#275

Adds integer `//` (flooring division) and `%` (flooring modulus) to the
Emboss expression language, following doc/design_docs/division_and_modulus.md.

- Front end: `//`/`%` tokens; grammar splits the multiplicative tier into
  a `*`-chain and a `//`/`%`-chain that may not be mixed without
  parentheses (per the design doc); type check; and expression bounds.
- Semantics: flooring, matching Python 3 (`-7 // 2 == -4`, `-7 % 2 == 1`).
  Constant folding reuses Python's `//`/`%`; the runtime FlooringQuotient
  / FlooringRemainder helpers convert C++'s truncating `/` and `%` for
  signed types (unsigned needs no conversion).
- Division by zero: this initial rollout does not yet track an
  `undefined` value through the type system, so a divisor whose range
  includes zero is rejected at compile time. constant_value() folds
  `//`/`%` totally (a zero divisor yields no value instead of raising),
  so the diagnostic is reported cleanly rather than as a traceback.
- Docs, unit tests (tokenizer, grammar, bounds, constraints,
  constant-folding, runtime arithmetic), an integration schema and
  golden, and formatter coverage.
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