[clpython-devel] Fwd: mixed Lisp/Python source code

Willem Broekema metawilm at gmail.com
Sun Aug 2 21:32:21 UTC 2009


Hi everyone,

I just posted the following on comp.lang.lisp; it's about a new
feature (in development) to support mixed Lisp/Python syntax.
Hope you find it interesting.

http://groups.google.nl/group/comp.lang.lisp/browse_frm/thread/1861e201b6eabd33/c13b7ec4af5301ad
(copied below; I've shortened the irrelevant long path names in the output)

- Willem

- - -
On Aug 2, 9:10 pm, Oxide Scrubber <jharri... at hatlop.de> wrote:
> vippstar wrote:
> > With CL macros you can have any embedded language expanding to lisp
> > code, but the embedded language can't have a language feature that CL
> > does not, that's the only limitation.

> Balderdash. Any language feature that can be implemented by a Turing
> machine can be included, using a suitable macro to implement it.

And don't forget reader macros: they gives you _complete_ control over
syntax. Python and Ruby source code can be supported directly, with
the right readtable. CLPython defines a readtable that dispatches
every input character to the Python parser, and Python source files
are then compiled using basically:

(let ((*readtable* *python-parsing-readtable*))
  (compile-file "source.py"))

Even more interesting is a mixed-syntax Python/Lisp readtable. It's in
development, here is a preview:

clpython(213): (.parser::enter-mixed-lisp-python-syntax)
; The mixed Lisp/Python syntax mode is now enabled; Lispy *readtable*
is now set.
clpython(214): range(100)[98:2:-2]
#(98 96 94 92 90 88 86 84 82 80 ...)
clpython(215): import os
#<module `os'
   :src #P"/System/Library/Frameworks/.../python2.5/os.py"
   :binary #P"/Users/willem/.fasl/.../2.5/lib/python2.5/os.fasl"
   :src-time 3400632679 :bin-time 3457618461 @ #x121964e2>
clpython(216): os.urandom(1)
"M"
clpython(217): (funcall ~os.urandom 1)
"¹"
clpython(218): (format nil "OS is a ~A, and its source file is ~A"
                   (type-of ~os) (module-src-pathname ~os))
"OS is a module, and its source file is /System/..../python2.5/os.py"

Compiling and running a mixed-syntax file, using the normal compile-
file and load functions:

- - - foo.lispy - - -
(in-package :cl-user)
(clpython.parser::enter-mixed-lisp-python-syntax)

def pyfact(x):
  if x <= 1:
    return 1
  else:
    return x * lispfact(x-1)

(defun lispfact (x)
  (if (<= x 1)
      1
    (* x (clpython:py-call ~pyfact (1- x)))))

(trace lispfact)
print pyfact(10)
- - -

clpython(203): :cf
;;; Compiling file foo.lispy
; The mixed Lisp/Python syntax mode is now enabled; Lispy *readtable*
is now set.
;;; Writing fasl file foo.fasl
;;; Fasl write complete
clpython(204): :ld
; Fast loading foo.fasl
 0[2]: (lispfact 9)
   1[2]: (lispfact 7)
     2[2]: (lispfact 5)
       3[2]: (lispfact 3)
         4[2]: (lispfact 1)
         4[2]: returned 1
       3[2]: returned 6
     2[2]: returned 120
   1[2]: returned 5040
 0[2]: returned 362880
3628800

> With the right set of macros you could probably give your embedded language
> first-class continuations, for instance.

Good guess... CLPython has a Python CPS convertor, to support
generator expressions.

> The macros will essentially compile it to CL the way GCC compiles C to assembly.

Yeah, Common Lisp makes an excellent implementation language for
Python, and probably Ruby too. It's almost cheating.

- Willem




More information about the Clpython-devel mailing list