Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating code from Python to Smalltalk

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

like image 583
user183928 Avatar asked Apr 23 '12 00:04

user183928


2 Answers

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.

like image 121
snim2 Avatar answered Nov 09 '22 03:11

snim2


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.

like image 1
Davorin Ruševljan Avatar answered Nov 09 '22 04:11

Davorin Ruševljan