chapter eight

8 Generating code from the AST

 

This chapter covers:

  • How the build system relates to the Domain IDE and the Runtime
  • Why make a reference implementation of the Runtime first
  • Implementing a code generator using templates
  • Deriving templates iteratively from the reference implementation’s code
  • Composing templates conveniently, with declarative indentation

In the previous chapters we started working on a Domain IDE for the domain experts to capture their business knowledge with in the form of DSL content. We said in chapter 1 that this DSL content should not just be “pretty pictures” or mere documentation, but should be precise enough to build software from. In this chapter, we’re going to do exactly that: generate a Runtime from DSL content written using the Domain IDE.

Figure 8.1. Using a DSL-based approach to develop a Runtime using a Domain IDE.
mentalModel DSL based approach it4

Our example DSL and its example content captures how to compute a rental price based on what data input by business stakeholders. The Runtime for this DSL content could be as simple as a Web app that displays a form in which business stakeholders input data about a rental.

Let’s have a better look at the Runtime, in particular at how its codebase is organized in general.

Figure 8.2. The organization of the Runtime’s codebase.
organization codebase Runtime

The Runtime’s codebase can be divided into the following parts:

8.1 Integrating the build system with the software development process

8.2 Using a reference implementation

8.3 A Runtime for “Rental”

8.4 Implementing a code generator using templates

8.4.1 Iterating from reference source to template

8.4.2 Composing template functions

8.4.3 Indenting declaratively

8.4.4 Finishing the code generator

8.4.5 Alternative approaches for writing templates

8.5 Summary