Translating code from Python to Smalltalk
Asked Answered
S

3

6

Let me say first that I'm NOT searching for automagical solutions here. I want to translate code from Python to Smalltalk because I've noticed some very simple sentences can be automatically translated, examples:

Assigning a variable to a value

Python

i = 1

Smalltalk

i := 1.

Creating a new instance of a class

Python

instance = module.ClassName()

Smalltalk

instance := ClassName new.

A for loop

Python

for a in [0,1,2]:
  print (str(a)+str(a))

Smalltalk

#(0 1 2) do: [: a | Transcript show: a + a; cr ]

and so on (while loops, conditionals, etc). My idea is to have a tool which translates all this extremely "simple" cases, and then I may complete or teach a rule system by hand.

Do you know any programming translation tool or library which can help me?

In case you haven't heard of any tool, what technique/pattern you will use to implement such translation? Can you provide a link to an example?

Thanks

Schappe answered 23/4, 2012 at 0:40 Comment(1)
you need to write a transcompiler. wikipedia list at least two that translate from python->(C, javascript) that you can use as a base en.wikipedia.org/wiki/Source-to-source_compilerTransformer
M
1

I am not aware of any such tool, and in general case it might be complicated and/or inefficient. So your route would depend on your more precise need: porting an existing python library, just using it from smalltalk, or making nice clean smalltalk code that does the same thing as python one.

Routes I would consider:

  • leaving python library as is, and calling it from smalltalk through c interface
  • implementing python parser in pettit parser an then:
    • implement smalltalk generator maybe assisted by human through user interface
    • python interpreter in smalltalk

Note that generator variant might face some difficult issues in general cases, for instance smalltalk has fixed number of instance variables, while in python you can attach then as you go. You could work around that, but resulting smalltalk code might not be pretty.

As for implementing python inside smalltalk take a look at the helvetia presentation from Lukas Renggli, it is on the subject of including other languages inside smalltalk IDE.

Minify answered 23/4, 2012 at 6:34 Comment(2)
You could also (if you don't mind its GPL licence) use OpenQwaq's Python-Smalltalk bridge: squeakingalong.wordpress.com/2011/05/04/…Ineluctable
I was not aware of it, great tip!Fescue
E
3

You need to parse the Python code, walk the abstract syntax tree that is generated by the parser and output your Smalltalk. There's a nice article about Python ASTs by Eli Bendersky and a slightly older one here. Python makes this relatively straight forward as the Python standard library exposes a lot of the internal tooling of the interpreter and the documentation is reasonably comprehensive.

Erastes answered 23/4, 2012 at 0:50 Comment(0)
M
1

I am not aware of any such tool, and in general case it might be complicated and/or inefficient. So your route would depend on your more precise need: porting an existing python library, just using it from smalltalk, or making nice clean smalltalk code that does the same thing as python one.

Routes I would consider:

  • leaving python library as is, and calling it from smalltalk through c interface
  • implementing python parser in pettit parser an then:
    • implement smalltalk generator maybe assisted by human through user interface
    • python interpreter in smalltalk

Note that generator variant might face some difficult issues in general cases, for instance smalltalk has fixed number of instance variables, while in python you can attach then as you go. You could work around that, but resulting smalltalk code might not be pretty.

As for implementing python inside smalltalk take a look at the helvetia presentation from Lukas Renggli, it is on the subject of including other languages inside smalltalk IDE.

Minify answered 23/4, 2012 at 6:34 Comment(2)
You could also (if you don't mind its GPL licence) use OpenQwaq's Python-Smalltalk bridge: squeakingalong.wordpress.com/2011/05/04/…Ineluctable
I was not aware of it, great tip!Fescue
V
0

Take a look at ply, which is a Lex-Yacc Python implementation. I've used it mostly for translating some other language into Python byte code by building a Python AST with it, but the opposite should be also possible.

Vanhorn answered 23/4, 2012 at 0:53 Comment(2)
This is a good point, and ply is an impressive piece of kit, but it does mean writing a parser from scratch. Using the ast from the standard library means the OP can leverage the work that has gone into the CPython parser.Erastes
Good point, but ply has an example implementation with most of the Python grammar.Vanhorn

© 2022 - 2024 — McMap. All rights reserved.