r/Compilers • u/Usual_Office_1740 • 1d ago
Serialization unit tests for a Crafting Interpreters clox bytecode vm?
I'm working my way through the C portion of Crafting Interpreters with my own modern C++ implementation. I’ve just finished chapter 21 and am about to implement local variables. So I can now begin to write basic scripts or programs in my version of the language.
I've never done anything with C++ serialization. I expect it will work similar to the Rust Serde crate or the C++ fmt library. Standard library containers will already have serialization support and I would need to implement a type trait or function of some kind for my custom types. I would do this for my Opcode's enum class and then I could have an expected serialized bytecode output, hopefully generated at compile time, that is easily written and updated. I could compare this to what my compiler generates. As I progress I could use tested good code output to generate more serialized tests and then I would only have to check new sections of the opcode output against hand written serialized bytecode.
Is this a viable approach for testing compiler output. Do I even need a serialization library? Could I just add constexpr to my code and reorganize the translation units so that I can generate compile time bytecode. What advice could more experienced developers offer? Any tips or suggestions would be appreciated.
Thanks.