Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top