Read my book

I wrote books about Webpack and React. Check them out!

Sunday, August 30, 2009

Implementation of a simple Logoish language

I'm currently reading The Pragmatic Programmer. The book contains these nice little exercises. Exercise 5 was about implementing a parser for a logoish language that looks like

So quite simple, eh? Besides writing a parser the exercise stated that it should be easy to extend the language. Anyway to cut the story short I went ahead and wrote one in Python:

It's quite a bit different than the proposed solution written in C (procedural, table-based version) but works for this simple case. I think it should be relatively easy to extend. I thought about deriving the operation syntax automatically based on the class name (just pick the first letter) but this might be a nightmare on the long run. Obviously it would be cool to hook it up with some drawing library but that's better done later.

I cleaned up the code by moving separate parse function inside the Parser class. It's neater this way.