feat: add divide (/) operator to the Tx3 language#340
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduces the
/infix operator across grammar, AST, parsing, analysis, lowering, and thev1beta0spec. Mirrors the recently-shipped*operator.tx3.pest):data_div = { "/" }added todata_infix. No lookahead needed — pest stripsWHITESPACE/COMMENT(//,/*,///) before matching an infix token, so a bare/is unambiguous.DivOp { lhs, rhs, span }+DataExpr::DivOp.div_op_parse;data_divjoined to the same Pratt.op(...)level asdata_mulso*and/share precedence and are left-associative (8 / 4 * 2→(8/4)*2).Analyzableimpl; lowers toir::BuiltInOp::Div.//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 / IntandAnyAsset / Int(scale down). Non-commutative:Int / AnyAssetinvalid. 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
DivTIR variant lands in tx3-lang/tir; this PR's lowering target requires that release. Bumptx3-tirhere once tir is published.🤖 Generated with Claude Code