Lisp has some of the simplest syntax of any of the languages I've worked with (perhaps with the exception of BF). I was looking for something simple which I could quickly implement, so I used it as a model and created an interpreter in Python. The whole project took just a couple of hours, but I was able to write an interpreter that would run the following program:
set[x 5]
set[x add[1 2 3 4 5]]
set[y add[1 2]]
println[get[x]]
println[get[y]]
As you might have guessed, this program displays the following in the terminal:
15I implemented a simple parser using Python's regular expression module
3
re
(The group
method can come in quite handy), and I defined each of the above functions in a dictionary which maps function calls in my custom language to functions in a Python script which I import. You might have noticed that each of the methods is designed to take a variable number of arguments (thought only they may not all be used), and all function calls are made recursively, with nested functions being evaluated first. The syntax looks slightly like s-expressions but it's a bit more accessible for someone who has worked with C-like languages (except there's no need to reach for the shift key for those tricky parenthesis). I haven't added a mechanism for lambda expressions yet, but defining new functions might look something like this: function[my_fun group[x y] group[multiply[x add[x y]]]]The above would define a function named
my_fun
which takes two arguments, and multiplies the first by the sum of the two arguments.I just made up this micro language, and haven't given this a great deal of thought. The idea was to create something quick and dirty to create a working interpreter, rather than the usual of intense planning without a working prototype. This language will probably never see any further development. It was a mental exercise, but I found it quite enjoyable. If you'd like to see the source code for the interpreter, please let me know (I get the feeling Matt might be somewhat interested).
No comments:
Post a Comment