Introducing Flea - A tiny Lisp written in Ruby
Flea is a tiny Lisp interpreter implemented in Ruby. Flea is designed to be an example of how simple it can be to bootstrap the core of a small flexible language. Flea essentially defines an informal subset of Scheme, just enough to be fun and interesting.
Here is a version of the classic ‘guess-the-number’ game implemented using Flea:
1 (define number (+ (rand 9) 1)) 2 3 (display "\n\nI'm thinking of a number between 1 and 10,\n") 4 (display "try to guess it!\n\n") 5 6 (define user-guess 7 (lambda () 8 (display "Take a guess - ") 9 (define guess (string-to-num (gets))) 10 (if (equal? guess number) 11 (display "Good guess!\n") 12 (begin 13 (if (greater-than? guess number) 14 (display "Lower!\n") 15 (display "Higher!\n")) 16 (user-guess))))) 17 18 (user-guess)
For ease of use the Flea is packaged as a RubyGem. Providing you already have Ruby and RubyGems installing Flea is as easy as entering the following command in a terminal:
1 gem install flea
Mac OS X and most Unix/Linux distributions come with an installation of Ruby and RubyGems. If you do not have Ruby and RubyGems installed please check the Ruby website for instructions.
After Flea is installed you can run a program by typing the following on the command line:
1 flea /path/to/program
You can also launch Flea’s interactive shell by simply calling
flea with no arguments.
The entire API for the language is documented on the project page at GitHub.
Experimenting and contributing:
Flea was designed as a learning tool, so with that in mind I strongly encourage you to pull it apart, play with it, and glue it back together in different ways. For an explanation of how Flea’s core code works check out my article Cheating your way to Lisp with Ruby.
Extending the language is as simple as writing Ruby code, so don’t be afraid to get your hands dirty!
I was originally inspired to create Flea by Peter Norvig’s Lispy. Many thanks to Peter for sparking a project that has enthralled me for many months!
I hope you have as much fun playing with Flea as I had writing it!