GitHub – antirez/picol: A Tcl interpreter in 500 lines of code Skip to content You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert antirez / picol Public Notifications You must be signed in to change notification settings Fork 11 Star 127 A Tcl interpreter in 500 lines of code 127 stars 11 forks Branches Tags Activity Star Notifications You must be signed in to change notification settings antirez/picol main Branches Tags Go to file Code Open more actions menu Folders and files Name Name Last commit message Last commit date Latest commit History 3 Commits 3 Commits README.md README.md picol.c picol.c View all files Repository files navigation 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 ). To run a program stored in a file, use: picol filename.tcl . Probably the parser could be rewritten in order to take less space, currently it takes almost 250 lines of code: this is too much and leaves little room for all the rest. On the other side, it’s a decent example about writing parsers by hand. A Raw list of the supported features: Interpolation, as seen above. You can also write “2+2 = [+ 2 2]” or “My name is: $foobar” . Procedures, with return. Like Tcl if return is missing the result of the last command executed is returned. If , if .. else .. , while with break and continue . Recursion. Variables inside procedures are limited in scope like Tcl, i.e. there are real call frames in Picol. The following other commands: set + – * / == != > < >= <= puts . This is an example of programs Picol can run: proc fib {x} { if {== $x 0} { return 0 } if {== $x 1} { return 1 } return [+ [fib [- $x 1]] [fib [
Source: Hacker News | Original Link