Chapter 8. Code Generation

So far in my discussion of implementing DSLs, I’ve talked about parsing some DSL text, usually with the aim of populating a Semantic Model, and putting interesting behavior in that model. In many cases, once we can populate the Semantic Model our work is done—we can just execute the Semantic Model to get the behavior we’re looking for.

While executing the Semantic Model directly is usually the easiest thing to do, there are plenty of times when you can’t do that. You may need your DSL-specified logic to execute in a very different environment, one where it’s difficult or impossible to build a Semantic Model or a parser. It’s in these situations that you can reach for code generation. By using code generation, you ...

Get Domain Specific Languages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.