Idea for a AST frontend for parsing between Julia and Python

Hi, I am interested in parsing projects between Julia and Python.
There is a good use case for converting Python code in a symbolic computational library like sympy into equivalent Julia forms.
Example would be converting this inverse_fourier_transform(sqrt(pi)*exp(-(pi*k)**2), k, x) into a AST , then a low level IR that adds optimizations etc finally resulting in efficient code recognizable using Symbolics.jl or so on.
The Ast representation of Julia is pretty good, to make this work in Sympy, a wrapper can be made since there isn’t any Python equivalent library.
An example of a AST library is this ast in lfortran which does the same for Fortran variant.

I’m not sure if this a right place to post. Is this a good feature to add related to Julia or are there already good tools for this?

Examples of various language AST’s for sympy ( sympy/sympy/parsing at master · sympy/sympy (github.com))

See lambdify in SymPy.jl for converting sympy expressions into Julia. And using SymPy.jl you can also take a Julia expression and evaluate it into a sympy expression just by making one or more of the inputs symbolic variables.

Is there a different problem you are trying to solve?

diojit uses Julia as a JIT backend for Python via DIO.jl, so it’s doing something like Python → Julia → AST → IR, but I don’t think it’ll be particularly easy to add Symbolics.jl into the mix.

@stevengj @stillyslalom those look quite interesting, thanks for the suggestions.

For an example on how code parsing is done in sympy.parsing:

from sympy.parsing.sym_expr import SymPyExpression
>>> src2 = ''' ... integer :: a, b, c, d ... real :: p, q, r, s ... '''
>>> p = SymPyExpression()
>>> p.convert_to_expr(src2, 'f')
>>> p.convert_to_c()
['int a = 0', 'int b = 0', 'int c = 0', 'int d = 0', 'double p = 0.0', 'double q = 0.0', 'double r = 0.0', 'double s = 0.0']

So it will process source language into code in a SympyExpression and convert it to any target language as supported. For that there are AST wrappers of those languages creating representations under the hood, a Python library wrapping Julia AST would be fitting here.

diojit would be great feature in code generation, though there will be an extra step of execution with Julia runtime.