Greg Hewgill (ghewgill) wrote,
Greg Hewgill

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

  • debug line information in psil compiler

    I've only had a bit of time to work on the Psil compiler, but it's coming along well. The compiler now generates Python AST code for many kinds of…

  • psil presentation and compiler details

    As promised last week, I've been working on making Psil work in a Python 3 environment. But first, a small but entirely relevant digression which…

  • psil manifesto

    I haven't talked about Psil (my implementation of Lisp on Python) for a while. It's been on the back burner while I've explored other things (such…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded