Skip to content

feat: add divide (/) operator to the Tx3 language#340

Merged
scarmuega merged 1 commit into
mainfrom
feat/divide-operator
Jun 10, 2026
Merged

feat: add divide (/) operator to the Tx3 language#340
scarmuega merged 1 commit into
mainfrom
feat/divide-operator

Conversation

@scarmuega

Copy link
Copy Markdown
Contributor

Summary

Introduces the / infix operator across grammar, AST, parsing, analysis, lowering, and the v1beta0 spec. Mirrors the recently-shipped * operator.

  • Grammar (tx3.pest): data_div = { "/" } added to data_infix. No lookahead needed — pest strips WHITESPACE/COMMENT (//, /*, ///) before matching an infix token, so a bare / is unambiguous.
  • AST: DivOp { lhs, rhs, span } + DataExpr::DivOp.
  • Parsing: div_op_parse; data_div joined to the same Pratt .op(...) level as data_mul so * and / share precedence and are left-associative (8 / 4 * 2(8/4)*2).
  • Analysis / lowering: structural Analyzable impl; lowers to ir::BuiltInOp::Div.
  • Spec: §3.7 (token + // gotcha), §4.4–4.5 (production + precedence row), §5.5.1 (operand-type table, truncation, divide-by-zero error).

Semantics

Integer division truncating toward zero. Int / Int and AnyAsset / Int (scale down). Non-commutative: Int / AnyAsset invalid. Divide-by-zero is an error.

Tests

Added div_op, div_binds_tighter_than_add, div_binds_like_mul. Full suite: 174 passed + 4 doc-tests.

Dependency note

The Div TIR variant lands in tx3-lang/tir; this PR's lowering target requires that release. Bump tx3-tir here once tir is published.

🤖 Generated with Claude Code

Add `/` as a first-class infix operator at the same precedence as `*`
(left-associative, tighter than `+`/`-`). Mirrors the `*` rollout
end-to-end: grammar token `data_div`, `DivOp` AST node, `div_op_parse`
joined to the existing multiplicative Pratt level, structural analysis,
and lowering to `ir::BuiltInOp::Div`.

Spec amended (§3.7 lexical, §4.4-4.5 grammar/precedence, §5.5.1 type
system): integer division truncating toward zero, `AnyAsset / Int`
scaling, non-commutative, divide-by-zero is an error. The `//` and `/*`
comment tokens take precedence over a bare `/`, so `a//b` lexes as `a`
plus a line comment — documented in §3.7.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@scarmuega scarmuega merged commit 45a4ad8 into main Jun 10, 2026
6 checks passed
@scarmuega scarmuega deleted the feat/divide-operator branch June 10, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant