This week I set out to solve my parser woes from my last post. I worked really hard to build a library that would match my needs. In the end I came up with a project called pebble_parser. Its a similar parser combinator library to pom but with a better syntax and it works on iterators rather than arrays.
My project for this week was to build a BrainF**k compiler. Its a simple language with very little syntax. I figured it would be a great way to test out my parser without having too hard a project to complete.
There are not many differences between this post and my last. I am going to quickly point out the parts of interest and leave the rest as an exercise for the reader.
The lexer is very similar to last time. Using logos and mainly focusing on basic tokens.
In a similar sense the runtime is simple and similar to the last one as well. It is recursive and holds some common references. The big difference is the looping structure and block evaluation. As you can see here we use existing structures within rust to simply execute as needed.
Finally there is the parser. Here is where I began to work with full intentions of ending up with a cleaner parser that contained better error handling. Unfortunately, what I ended up with was more or less the same as last time. I like the method calls more than operator overloading, but the parser is the same speed, has bad error handling, and is generally the same thing in a different syntax.
Like the last project this one works fine. It will run any BrainF*** program that you want. However the implementation is not an improvement over last week.
Parser libraries like pom and pebble_parser are not the correct approach for any big compiler work. The source code of the compiler is much better at handling complex worflows than libraries. For that reason I wont be releasing pebble_parser as I don’t think that its the correct solution. I’m now thinking that using logos is enough to build a toolchain. My next parser will be hand built and I will see if that works out better.