7

Because I want to use Lisp's syntax and Python's libraries.

Maybe some tools like Parenscript but generates Python code instead of Javascript.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
SaltyEgg
  • 1,498
  • 1
  • 15
  • 26
  • Which lips? Common Lisp, Scheme, elisp, autocad lisp? – László Papp Aug 31 '13 at 05:43
  • You could do it, but it'd be an awful mess. The code you'd end up writing wouldn't be good Lisp *or* good Python. You'd probably get a stack overflow if you tried to translate Lisp directly, and you'd need to invent Lisp-like equivalents of all the Python control flow structures. – user2357112 Aug 31 '13 at 05:49
  • @LaszloPapp Best to be Common Lisp. And I can accept other dialects, too. – SaltyEgg Aug 31 '13 at 05:50
  • 1
    Most importantly, it would place an artificial barrier between you and any programming community you want to interact with. Anyone who would have to read your code would rue the day you were born, and you'd have a harder time reading anyone else's code since you're always doing your own strange thing. – user2357112 Aug 31 '13 at 05:54
  • @user2357112 I just want to use python's libraries like numpy and so, I'm not really want a strange language. – SaltyEgg Aug 31 '13 at 06:01
  • I imagine [quine-relay](https://github.com/mame/quine-relay?source=c) might work, although I've never actually used it myself – Greg Aug 31 '13 at 06:05
  • That sounds like what you really want is an interoperability layer. I get the feeling that you want to be able to use Lisp and Python libraries in the same system. – user2357112 Aug 31 '13 at 06:08
  • @Greg It's just ... too weird ... – SaltyEgg Aug 31 '13 at 06:25
  • @SaltyEgg which is pretty much why ive never used it :) I'd like to mess around with it in the future tho to see what kind of code is output – Greg Aug 31 '13 at 07:10
  • I suspect writing a "Python to Common Lisp" compiler would be a better way to be able to use Python libraries in a CL environment, than the other way around. – Vatine Aug 31 '13 at 08:03
  • @Vatine: you mean something like http://common-lisp.net/project/clpython/ ? – 6502 Aug 31 '13 at 15:11
  • @6502 clpython can't import libraries such as numpy till now. – SaltyEgg Aug 31 '13 at 16:29

2 Answers2

8

I've been experimenting a bit with a Lisp compiler targeting Python bytecode.

You can see a small video here.

It's just a proof-of-concept toy but it's IMO a viable path and the end result would be able to call and be called from python freely (and it would be compatible with any python extension library). All this keeping however the power of macros (metaprogramming is probably the area in which Python is farthest from lisp).

Targeting Python source code instead is quite more annoying to do because there are explicit syntax limitations that make compiling Lisp difficult (e.g. assignment is not an expression, no statement is permitted in lambda, captured variables are read-only in Python 2.x).

The VM runtime however doesn't have these limitations and Python bytecode is reasonably nice.

My toy currently can target Python 2.x, Python 3.x and works even with PyPy (so you get a JIT compiler too).

Of course aiming at becoming a full compliant Common Lisp implementation would be IMO nonsense from a technical point of view, but a lisp dialect based on Python runtime types and compatible with Python object system could instead be a reasonable tool with practical applications.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 1
    Also have a look at my [Psil](https://github.com/ghewgill/psil) project that I worked on a few years ago, it compiles to Python AST and then uses the Python compiler to generate bytecode. It's a different approach to the same problem. (As a bonus there's even an AST-to-source decompiler buried in there!) – Greg Hewgill Sep 01 '13 at 08:17
  • @GregHewgill: but how can you get read/write closed over vars on python 2.x going through the AST? With bytecode level is ok (the runtime has no problem with it) but I thought that it would have been impossible using the AST level because of lacking of `nonlocal`. – 6502 Sep 01 '13 at 08:37
  • It looks like my project doesn't support that particular operation. :) That could have been about the point where I stopped working on it, I don't really remember. – Greg Hewgill Sep 01 '13 at 08:45
3

I believe that Hy is what you are looking for. From the tutorial:

Hy converts to python’s own abstract syntax tree, so you’ll soon start to find that all the familiar power of python is at your fingertips.

However note that Hy isn't Common Lisp, so you can't cut and paste.

Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
James K
  • 3,692
  • 1
  • 28
  • 36