This chapter covers:
- How to encode ASTs (represented as object-relation diagrams) as values in code
- How to use Node.js interactively to construct ASTs in code
- How to interact with an encoded AST
- How to traverse encoded ASTs algorithmically
In the previous chapter you learned what an AST is, and how to represent one using an object-relation diagram. In this chapter, you’ll learn how to put this representation into code, as in-memory data, and to “work with” that encoded AST: inspect it, alter it, and traverse it. These skills will form the basis of the Domain IDE’s code base. You’ll probably also encounter these notions more generally outside of this book.
In this section, we’re going to learn how to turn ASTs, that are notated in object-relation diagram style we introduced in chapter 2, into actual data that a computer can work with. Since we’re going to use JavaScript, we’ll be turning an AST into a JavaScript value. This process is called encoding: the AST is turned into JavaScript code that produces a JavaScript value faithfully representing the AST.
Let’s turn the AST that corresponds to our example DSL content describing the “Rental” record type into code that produces a JavaScript value representing it. For convenience, let’s re-iterate our example DSL content: the specification of a record type called “Rental”, which is intended to capture the details of a single car rental.