Tsonnet #28 - Debugging gets pretty (printed)
derive show; // compiler does the rest
Welcome to the Tsonnet series!
If you’re not following along, check out how it all started in the first post of the series.
In the previous post, I reorganized function parameters and centralized error messages for consistency:
The project is getting more complex and not having an option to debug the AST is becoming cumbersome. We’ll quickly explore how to improve this.
deriving show
Sometimes we want to see a data type in the console in its raw form. We could implement a function to do that ourselves, but it’s repetitive and boring. Enter ppx_deriving.show:
This saves you from manually writing boilerplate code to convert your types to strings.
Then we annotate the expr and other types, and it automatically generates the show and pp (pretty-printer) functions:
As you might have noticed, we can customize the functions for specific types. Neat!
This is required for our next step: printing the AST.
--debug-ast
We start by adding a new parameter:
Instead of passing multiple options to the library, let’s condense the configuration parameters in a Config module.
It has this interface:
And this concrete implementation:
Now every function call has a clean config record to rely on:
As always, cram tests FTW--they function as automated tests with a nice touch of documentation:
Error messages
Some leftovers -- untamed error messages from previous changes:
Conclusion
With ppx_deriving.show and a new --debug-ast flag, debugging Tsonnet just got a whole lot easier. And consolidating configuration into a Config module keeps the API clean as more options get added.
The error message cleanup was long overdue. Having all error strings centralized in Error.Msg makes them easier to maintain and ensures consistency between the parser, type checker, and interpreter. One less thing to worry about when adding new features!
The entire diff can be seen here.









