Our parser now works as it should, and you could use it in any kind of application to offer very flexible customization options to the end user. However, the parser does not work very efficiently. In general, parsing expressions are computationally expensive, and in most use cases, it is reasonable to assume that the actual expressions that you're working with do not change with every request (or at least, are evaluated more often than they are changed).
Because of this, we can optimize the parser's performance by adding a caching layer to our interpreter. Of course, we cannot cache the actual evaluation results of an expression; after all, these could change when they are interpreted with different variables.
What we're going to do in this section is add a compiler feature to our parser. For each parsed expression, our parser generates an AST that represents the structure of this expression. You can now use this syntax tree to translate the...