Code generation in Lisp (was Re: Self and Smalltalk)

Jecel Assumpcao Jr jecel@lsi.usp.br
Sat, 15 May 1999 02:14:19 -0300


Laurent Martelli wrote:
> 
> In managed to do this in emacs-lisp :
> 
>         (defun defun-plus (funcname)
>           (eval (list 'defun (symbol-concat (symbol-name funcname))
>                       '(a b) '(+ a b))))
> 
>         (defun-plus 'foo)
>         (foo 1 2)
>         => 3
> 
> Is this what you call "generating code" ?

Yes. Note that it is trivial to do the same thing in Self or
Smalltalk.

What I would call "code manipulation" would be a function that
would take some lambda expression that calculates some function
as an argument and would return another lambda expression that
would calculate the derivative of the original expression relative
to some given variable. A less exotic example would be partial
evaluation: given a piece of code and the condition that some
variables (arguments) will have a fixed value, generate a simplified
version of the code specialized for this case.

What makes code manipulation much harder in Self/Smalltalk is
that Lisp lists fully reflect the code structure (they are like
the intermediate code compilers use) while bytecodes don't. None
of this matters for 99.9% of the programmers...

-- Jecel