Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

孙世龙 sunshilong
Hi, list

Is it possible to perfectly convert the Python script to Lua script?
The script that contains peripheral operations is not to be considered.

I find an open source project(i.e
https://github.com/dmitrii-eremin/python-lua) which may achieve this
goal, but it seems not been maintained anymore since 2017.


Best regards
sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

Robert Burke
Hello,

This seems like something that should be done by hand by someone who
knows both languages.

On Fri, Jan 29, 2021 at 12:29 PM 孙世龙 sunshilong <[hidden email]> wrote:

>
> Hi, list
>
> Is it possible to perfectly convert the Python script to Lua script?
> The script that contains peripheral operations is not to be considered.
>
> I find an open source project(i.e
> https://github.com/dmitrii-eremin/python-lua) which may achieve this
> goal, but it seems not been maintained anymore since 2017.
>
>
> Best regards
> sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

孙世龙 sunshilong
Thank you for your reply.
>This seems like something that should be done by hand by someone who
>knows both languages.
It's really a bad news. I really hope to automatically achieve this goal.

On Fri, Jan 29, 2021 at 2:44 PM Robert Burke <[hidden email]> wrote:

>
> Hello,
>
> This seems like something that should be done by hand by someone who
> knows both languages.
>
> On Fri, Jan 29, 2021 at 12:29 PM 孙世龙 sunshilong <[hidden email]> wrote:
> >
> > Hi, list
> >
> > Is it possible to perfectly convert the Python script to Lua script?
> > The script that contains peripheral operations is not to be considered.
> >
> > I find an open source project(i.e
> > https://github.com/dmitrii-eremin/python-lua) which may achieve this
> > goal, but it seems not been maintained anymore since 2017.
> >
> >
> > Best regards
> > sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

Paul K-2
>>This seems like something that should be done by hand by someone who
>>knows both languages.
>It's really a bad news. I really hope to automatically achieve this goal.

It's unlikely there is a way to do this automatically. If you really
want a script that does most of the work, you can look into converting
your Python script into an AST structure
(https://docs.python.org/3/library/ast.html) and then convert it back
into Lua using metalua
(https://github.com/fab13n/metalua/blob/master/doc/ast.txt).

There is a python modules that solves the opposite problem (builds a
Python AST from Lua code): https://pypi.org/project/luaparser/, so
maybe you can review it to see how it was done.

> I find an open source project(i.e
> https://github.com/dmitrii-eremin/python-lua) which may achieve this
> goal, but it seems not been maintained anymore since 2017.

Why does it matter that it's not maintained anymore? Judging by the
description it does just what you need.

Paul.

On Thu, Jan 28, 2021 at 10:52 PM 孙世龙 sunshilong <[hidden email]> wrote:

>
> Thank you for your reply.
> >This seems like something that should be done by hand by someone who
> >knows both languages.
> It's really a bad news. I really hope to automatically achieve this goal.
>
> On Fri, Jan 29, 2021 at 2:44 PM Robert Burke <[hidden email]> wrote:
> >
> > Hello,
> >
> > This seems like something that should be done by hand by someone who
> > knows both languages.
> >
> > On Fri, Jan 29, 2021 at 12:29 PM 孙世龙 sunshilong <[hidden email]> wrote:
> > >
> > > Hi, list
> > >
> > > Is it possible to perfectly convert the Python script to Lua script?
> > > The script that contains peripheral operations is not to be considered.
> > >
> > > I find an open source project(i.e
> > > https://github.com/dmitrii-eremin/python-lua) which may achieve this
> > > goal, but it seems not been maintained anymore since 2017.
> > >
> > >
> > > Best regards
> > > sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

Viacheslav Usov
In reply to this post by 孙世龙 sunshilong
On Fri, Jan 29, 2021 at 4:29 AM 孙世龙 sunshilong <[hidden email]> wrote:

> Is it possible to perfectly convert the Python script to Lua script?

Unless you define "perfectly convert", we do not know what you mean.

If that means "obtain code in Lua that does what the code in Python
does", yes, in theory it is possible, but it may need some supporting
C-libraries.

In practice, probably not trivial, and a major consideration would be
the need (or not) to use Python's standard or external libraries.

If the Python code runs embedded in some custom environment (i.e., not
the stock command-line interpreter), then one would also have to
consider the need (or not) to wrap the Lua VM in a Python-compatible
API/ABI.

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

Pierre Chapuis
Here is a weird idea that might work:

- Compile Python to C with something like Nuitka
- Compile the C to WASM
- Find a WASM runtime in Lua (there are several floating around)

This is a very convoluted way to do it and will probably cause issues for slightly complicated programs though...
Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

Peter Hickman-3
Not looked too deeply into this but there is a Python AST tool. You could presumably write a tool to rewrite the AST into Lua. However there are some features in Python, such as the try: ... except: ... else: ... finally: blocks that might be difficult to translate into Lua. Also the behaviour of the basic data collections in Python is different to Lua (or even Ruby in my experience). You would find yourself having to implement all sorts of Python features in Lua, making it a lot less Lua and more Python in another syntax. For example referencing a non-existent key in a dictionary in Python will throw an exception but Lua will return a nil. So if this is used in the Python source then you will have to implement that in Lua tables too (that is you will need to implement an exception handling mechanism that works the same was as the one in Python)

Of course this depends on which features of Python are used in the source. You only need to implement what you actually use. You also might have the option to rewrite problematic parts on the Python source into something easier to translate

Python is, sort of, OO and it is likely to be used in most codebases of significant size so you will need to implement that too. Remember it will not be enough to simply implement OO in Lua (which can easily be done) but to implement Python OO in Lua

Even if you got that all to work the resulting code would be an abomination and very difficult to support or extend

Unless it is a really trivial Python source it is unlikely to be a "hands off" automatic translation

Like someone said, you will be wanting some tests. Lots of tests

Reply | Threaded
Open this post in threaded view
|

Re: Is it possible to perfectly convert the Python script (the one without peripheral operations, e.g: IO) to Lua script?

Brian Kuhl
In reply to this post by 孙世龙 sunshilong
When porting between any two platforms or languages, the logic of basic programming is typically not the issue.  Computer languages are strictly specified, so translation is exact.   The problem is always features that are unique in one language and not the other.

Python supports many more packages in it's base instantiation than Lua. For example consider sockets, a part of the basic Python for quite some time, but in Lua there is LuaSocket that might be considered a deFacto Lua target, but then I see a discussion on this list of alternates at least once every 6 months :)

Similarly, Python has some language features, that while not typically used in simple scripts, don't have a direct analog in Lua.

So without an examination of the style of Python programming and its package dependencies the viability of moving it to Lua efficiently even with the assistance of some sort of translator like the one you cite, is not  a simple question. You have not described the Python you want to translate and why you hope it would be better suited as a Lua?

Generally any automated translation loses its maintainability.
Even poorly written programs written by humans are more easily understood by other humans than programs written by machines. 
Computer languages are humans communicating what they want a computer to do to each other, computers only understand binary :)         

Brian


On Thu, 28 Jan 2021 at 22:29, 孙世龙 sunshilong <[hidden email]> wrote:
Hi, list

Is it possible to perfectly convert the Python script to Lua script?
The script that contains peripheral operations is not to be considered.

I find an open source project(i.e
https://github.com/dmitrii-eremin/python-lua) which may achieve this
goal, but it seems not been maintained anymore since 2017.


Best regards
sunshilong