Translate code from Python to Smalltalk

Let me first say that I am NOT looking for automatic solutions here. I want to translate the code from Python to Smalltalk, because I noticed that some very simple sentences can be automatically translated, examples:

Assigning a variable to a value

Python

i = 1 

Smalltalk

 i := 1. 

Create a new instance of the class

Python

 instance = module.ClassName() 

Smalltalk

 instance := ClassName new. 

A for cycle

Python

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

Smalltalk

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

etc. (while loops, conventions, etc.). My idea is to have a tool that translates all these extremely β€œsimple” cases, and then I can finish or train the rule system manually.

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

If you have not heard of any tool, what method / template will you use to implement such a translation? Can you provide a link to an example?

thanks

+6
source share
3 answers

I do not know such a tool, and in the general case it can be complicated and / or ineffective. That way, your route will depend on your more specific need: porting an existing python library, just using it from smalltalk, or doing nice clean little code that does the same thing as python.

Routes I would like to consider:

  • leaving the python library as is and calling it from smalltalk via the c-interface
  • The implementation of the python parser in the partner parser is then:
    • implement a smalltalk generator, possibly using a person through the user interface
    • python interpreter in smalltalk

Note that the generator option may run into some complex problems in general cases, for example, smalltalk has a fixed number of instance variables, whereas in python you can attach it when you go. You could get around this, but the result of the smalltalk code might not be very nice.

Regarding the python implementation inside smalltalk, look at the Lukas Renggli helvetia view, it is intended to include other languages ​​inside the smalltalk IDE.

+1
source

You need to parse the Python code, skip the abstract syntax tree that is generated by the parser, and output your Smalltalk. There's a nice article on Python AST from Eli Bendersky and a little older here . Python makes this relatively straightforward since the Python standard library provides many internal interpreter tools, and the documentation is fairly comprehensive.

+3
source

Check out ply , which is a Python implementation from Lex-Yacc. I used it mainly to translate some other language into Python bytecode, creating Python AST with it, but the opposite is also possible.

0
source

Source: https://habr.com/ru/post/913884/


All Articles