chapter eleven

11 Implementing expressions: binary operations

 

This chapter covers:

  • Why, how, and for what expressions are very useful
  • Why binary operations are especially useful
  • How to represent expressions with binary operations as ASTs
  • How to implement binary operations across all key aspects of a DSL

Looking at our example DSL content, we notice that the attributes of the “Rental” record type should be related more intricately than they currently are.

Figure 11.1. The relation between the values of the four attributes of the “Rental” record type.
rental price arithmetical relation

The value of rental price after discount should be calculated from the values of rental price before discount and discount. Unfortunately, we can’t express such relations yet: the DSL doesn’t have any concept with the appropriate notation, and the corresponding meaning. Leaving it up to the user of the generated Web app to set the value of rental price after discount to a value governed by the arithmetical relation above, is unfair.

11.1 Representing expressions as DSL content

11.1.1 Changing the structure

11.2 Changing the projection

11.3 Changing the runtime

11.4 Changing the code generator

11.5 Intermezzo: expressions in general

11.5.1 Characterizing expressions by arity

11.5.2 Compositionality and expressiveness

11.5.3 Characterizing operations by operator

11.6 Summary