Picol is a Tcl-alike interpreter in 500 lines of code that I released 15th of March 2007. Recentely I looked at the source code and realized this was a better C programming example compared to what I recalled, so I'm putting this on GitHub to archive it, together with the main points of the original article.
Rules
When I built this code, I had some rule in mind:
I wanted to use more or less my normal C style. In Picol you'll find normal C spacing and even comments.
I wanted to write an interpreter with a design similar to a real one. One of the few useful things you can do with Picol is to learn how to write a Tcl interpreter if you are a newbie programmer, I guess, so the point was to write a simple to understand program, not just a short program.
The resulting interpreter should be able to run some kind of non trivial program: to just set few vars and print hello world was not an option.
The resulting interpreter: Picol
The parser is very similar to the Tcl one, Picol supports interpolation as well, for example you can write:
set a " pu " set b {ts} $a$b " Hello World! "
Note that Picol has an interactive shell! so just launch it without arguments to start to play (to compile the code use gcc -O2 -Wall -o picol picol.c ).
... continue reading