A binary expression combines exactly two subexpressions—its left operand and right operand—through one operator. It evaluates to a value, so it can be nested inside another expression or used as the computed value of an attribute. This makes it a compact building block for detailed business logic: calculations, conditions, and outcomes based on other values. section 11.0 section 11.6
Model a binary expression as an AST object with three pieces of data: operator, left operand, and right operand. Infix notation places the operator between operand projections: left operand, operator, right operand. For example, “discount of rental price before discount” has `of` as its operator and two value-producing operands; 20% of $60 evaluates to $12. section 11.1
Figure 11.3 An ORD of the AST representing the subexpression “discount of rental price before discount,” including the attributes involved
Inspect the AST shape: the Binary Operation contains its two operands, while referenced attributes remain separate targets rather than being copied into the expression. section 11.1
Implement a binary expression across the DSL’s key aspects:
- Add a Binary Operation concept with operator, left operand, and right operand properties. section 11.1
- Project each operand as an expression choice, allowing an Attribute Reference, Number, or another Binary Operation; render the operator with a dropdown. section 11.2
- Generate code by recursively generating both operands and inserting the operator; treat `of` as multiplication by 0.01 and the right operand. section 11.4
- Traverse the full expression AST when finding referenced attributes, because nested binary expressions can contain multiple references. section 11.4
- Validate operand types for each operator, reporting an issue when an operand has an incompatible type. section 13.2
Computing a discounted rental price
Mark `rental price after discount` as computed rather than initially entered. Replace its prior value with a Binary Operation whose left operand is the `discount` reference, operator is `of`, and right operand is the `rental price before discount` reference. The generated getter evaluates the expression, preventing app users from manually setting the derived price. section 11.0 section 11.2 section 11.4
Binary expressions compose because each operand can itself be an expression. Their arity—two contained subexpressions—is the key structural characteristic. Composition permits arbitrarily large expression trees, but visible notation must later account for order of operations, precedence, and associativity so editing and interpretation remain familiar. section 11.6 section 1.2.3 section 12.0
