Is there any tool to translate Lisp code into Python? [closed]
Asked Answered
N

2

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.

Numen answered 31/8, 2013 at 5:41 Comment(12)
Which lips? Common Lisp, Scheme, elisp, autocad lisp?Superphysical
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.Proclamation
@LaszloPapp Best to be Common Lisp. And I can accept other dialects, too.Numen
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.Proclamation
@Proclamation I just want to use python's libraries like numpy and so, I'm not really want a strange language.Numen
I imagine quine-relay might work, although I've never actually used it myselfForeskin
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.Proclamation
@Foreskin It's just ... too weird ...Numen
@Numen 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 outputForeskin
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.Smoothspoken
@Vatine: you mean something like common-lisp.net/project/clpython ?Magic
@Magic clpython can't import libraries such as numpy till now.Numen
M
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.

Magic answered 31/8, 2013 at 10:22 Comment(3)
Also have a look at my 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!)Busby
@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.Magic
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.Busby
P
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.

Pentaprism answered 31/8, 2013 at 8:21 Comment(4)
This is cool~ But I found that clojure-py is more suitable for me, and I'm still searching for better ones.Numen
Can you convert Hy code to Python?Finite
Hy's site says its single quote works as in most Lisp dialects and there doesn't appear to be a source translation feature.Matteo
There's also Hissp now.Blackfellow

© 2022 - 2024 — McMap. All rights reserved.