Greg Hewgill (ghewgill) wrote,
Greg Hewgill
ghewgill

an embeddable s-expression language in python

A few weeks ago, I was working on some code that needed an HTML templating system (for substituting variables and content into an HTML page template). I know there are many existing choices out there, but I've never actually looked at them and anyway, I like to build stuff myself. My first rendition was a really awful simple substitution that had no options for looping or conditionals. It did the job I needed at the time so I left it.

Last week I needed to add looping to the template system, so I patched it to sort of understand arrays of data, which aren't quite loops but it worked for the moment. I can foresee needing to add conditionals which would make the code (not to mention the template language) even more crufty.

I decided that if I'm going to solve this problem, I should solve it the right way. I remembered reading somewhere that "S-expressions make better XML than XML", so I thought I'd see whether I can apply that in practice. S-expressions were introduced about 50 years ago for the Lisp programming language and haven't changed since. If I'm going to use S-expressions to represent HTML, then I might as well implement the Lisp (or a Lisp-like) programming language to work with them.

To make what could be a long story shorter, I've now got an implementation of a subset of Lisp written in Python. Somehow, I managed to get this implemented before I found PyScheme, pylisp, Psyche, lython, and A simple scheme interpreter. Clearly I wasn't the first to think of this.

My plan is to integrate this Lisp-ish subset closely with Python so I can call back and forth between them. I've taken some liberties, for example I haven't implemented Lisp lists as linked lists, but instead as Python lists. I might regret this choice but I think it will make common things easier. Fortunately, I've been using the excellent doctest module to build unit tests which will make it a whole lot easier to change things when I need to.

Oh yeah, I called this project "Psil", which might stand for Python S-expression Intermediate Language, or it might be Lisp spelled backwards.

I'm currently reading up on Scheme because it's a much smaller language than the full Common Lisp and is better suited for embedding into other systems. I think I'll probably switch to a Scheme-based dialect.
Tags: lisp, psil
Subscribe

  • open source at work

    I was doing some quick egosurfing and found that some code I wrote six years ago has been incorporated into the TWiki project. Cool!

  • positive feedback

    It's really cool to get email like this. This is the first feedback that I've got regarding my NOAA Weather Radio feed for the Austin, TX area.…

  • feliĉan naskiĝtagon al mi

    Mi naskiĝtagon ne tro gravas al mi. Ĝi estas nur alia tago, mi ne malfeliĉas se iu ajn malmemoras ĝin. Tamen, mi recevis bonvolojn de mia gepatroj,…

  • Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 2 comments