liveBook · Manning

Parentheses expression

As explained in Building User-Friendly DSLs

A DSL object or expression that explicitly groups a subexpression so it is processed as a unit and has the value and type of that subexpression.

A parentheses expression is a DSL object that explicitly groups a subexpression, causing it to be processed as a unit and to inherit the value and type of that subexpression. section 12.2 section 13.1

Implementation uses parentheses in several connected places:

  • The expression AST wraps a subexpression such as 2 + 3 so it is processed before the surrounding operation. section 12.2
  • The projection may insert virtual, show-only parentheses when nested binary operations would otherwise hide the AST’s meaning. section 12.2
  • The code generator applies the same precedence test when emitting executable expressions, without changing the underlying AST. section 12.2
  • The type system returns the type of the Parentheses object’s subexpression, preserving the grouped expression’s semantic type. section 13.1

Grouping 2 + 3

For 1 * (2 + 3), the Parentheses object wraps the 2 + 3 subexpression. The grouping makes that addition occur before multiplication and lets the projection and generated code display the intended order. section 12.2

Figure 12.13 The AST representing the expression 1 * (2 + 3), “fixed” by wrapping 2 + 3 in parentheses
figure

Inspect the AST representation that fixes 1 * (2 + 3) by wrapping 2 + 3 in a Parentheses object. section 12.2

How Building User-Friendly DSLs covers it

How deep it goes
Explains AST structure, precedence checks, generated output, and type propagation; defers advanced type inference such as Hindley–Milner.
Examples
Uses 1 * (2 + 3), virtual display parentheses, generated JavaScript parentheses, and a rental price expression.
What it assumes
Assumes abstract syntax trees and binary operations while teaching parentheses-specific precedence handling and type propagation.
What you can do after
Builds it from scratch by adding AST grouping, precedence-aware projection and generation, and subexpression-based type computation.
How it connects
Connects parentheses expressions to abstract syntax trees, operator precedence, binary operations, code generation, projections, and type systems.

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.