Patrick (patrickwonders) wrote,

Too smart for me...

After seeing boba's recent complaint about noweb documentation not matching the code involved, I remembered how much I love literate programming. I'm only partially thrilled with noweb though.

So, what's my thought? Throw together a tangle and weave in Lisp. And, in fact, if I limit the output language to Lisp, I might even be able to take advantage of (READ ...) and use s-expressions for my chunks. I just need to write my web file like this:


Here is an example of doing some Lisp-ified literate programming.
(:chunk "example.lisp"
   (defun wire-money-to-me (src-account-number amount)
        (:chunk-ref "make sure amount is acceptable")
        (:chunk-ref "make sure src-account-number is valid")
        (:chunk-ref "transfer money to me")))

Okay, admittedly, it's ugly to have the documentation in a list containing a string. But, the allure of using the s-expressions for handling the chunks was too great. So, I did it.

And, in no time at all, I had a functioning tangle program. But, it had some idiosyncrasies. For example, the reader upcased symbols and keywords so that in the tangled output, things looked significantly different. And, the reader stripped out any Lisp comments. Eh, small prices to pay. You shouldn't be reading the tangled source anyhow. :) Forge ahead.

So, then I wanted to use getopt (even though it's barely documented and fairly old) to read things from the command-line. This is when I discovered that which I should have realized from the beginning. The Lisp reader was busily interning all of the symbols that I read in. And, of course, when I went to read in a symbol that specifies the getopt package, the reader freaks out because it doesn't know that I wanted the getopt package.

I played around with having command-line arguments to tell which packages the tangle program has to load before tangling the source code. But, it got way out of hand.

So, I downloaded cl-ppcre, reworked it to use strings and the noweb format of <<chunk name here>>. It's 70 lines of Lisp.

It feels now though like maybe it should just be in Perl. The fact that cl-ppcre's (REGEX-REPLACE ...) can take a function as its second argument seems really natural. But, maybe this isn't so bad either:

sub chunk_expand {
    my ( $name ) = @_;

    my $body = get_chunk_text( $name );

        # broken into multiple lines so it won't match itself.
    $body =~ s/<<
               >>/chunk_expand( $1 )/egx;

    return $body;

Well, it has been a fun exercise. And, I'm sure I will have many uses for cl-ppcre in the future.

Tags: lisp
  • Post a new comment


    default userpic

    Your reply will be screened