Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matlab in Transpyle? #7

Open
RobBW opened this issue Apr 25, 2019 · 7 comments
Open

Matlab in Transpyle? #7

RobBW opened this issue Apr 25, 2019 · 7 comments

Comments

@RobBW
Copy link

RobBW commented Apr 25, 2019

Would it be possible to include Matlab in the Transpyle Repertoire?
In my recent reading I have found several promising approaches which may do most of the work for you.
I attach my reading notes in case they are useful for you.

  1. Mc2For : described at http://www.sable.mcgill.ca/mclab/projects/mc2for/ uses a typed intermediate AST approach which might enable simple inclusion in the Transpyle architecture. Available in Java.
  2. "Matlab to Modelica" described at: http://www.ep.liu.se/ecp/096/137/ecp14096137.pdf also creates a typed intermediate AST
  3. Matisse/Lara described at: https://www.isi.edu/people/pedro/publications/matisse_matlab_compiler and https://web.fe.up.pt/~specs/projects/lara/doku.php?id=lara:usage#matisse
    An online demo is at:http://specs.fe.up.pt/tools/matisse/
  4. Rose compiler.There is evidence of some work on a matlab frontend for Rose at: https://github.com/rose-compiler/matlab-frontend
  5. Unparser for Python might also be helpful. See: https://mbdevpl.github.io/posts/typed-astunparse/
  6. MIX10: compiling MATLAB to X10 for high performance
    Kumar, Vineet & Hendren, Laurie. (2014). MIX10: compiling MATLAB to X10 for high performance. Proceedings of the Conference on Object-Oriented Programming Systems, Languages, and Applications, OOPSLA. 617-636. 10.1145/2660193.2660218.
  7. https://github.com/jonathf/matlab2cpp & https://ieeexplore.ieee.org/document/7542966
    8.https://stackoverflow.com/questions/9845292/a-tool-to-convert-matlab-code-to-python
    Has a very useful listing of relevant software.
  8. https://github.com/ebranlard/matlab2fortran
@mbdevpl
Copy link
Owner

mbdevpl commented Apr 26, 2019

Hi! Thanks for the interest in Transpyle!

And thanks for a long list of resources - from a quick read I think they would be definitely very useful for adding Matlab support into the framework.

Short comment about 5. - it's actually something that also I developed, and then I extended it in various ways to be used in Transpyle project for unparsing Python, Fortran and C++. I completely agree that this approach simplified unparser implementation greatly, and I suspect that adding only unparsing Python AST to Matlab code might be be actually quite relaxing coding time, since the unparsing machinery is quite robust and you have multiple existing extensions as examples to guide you.

And about 4. - ROSE AST is a very relevant concept for my work too, and one of the things I'd be very interested in looking at is actually what challenges ROSE team has faced when trying to add Matlab support. Did they abort due to some technical difficulty? Or no interest in the community? Or is the effort going on?

The same with LLVM IR since we're talking about widely-adopted formats - are you aware of any Matlab -> LLVM IR translators?

On the other hand, I have to admit that even if adding Matlab to Transpyle would be not only theoretically possible, but also seems feasible, currently there is little chance of such functionality to enter my development roadmap. I'm focusing on performance-oriented functionalities, and mostly work to improve transformations performed on typed Python AST (such as inlining, loop transformations, etc.), as well as unparsing Python->Fortran and Python->C++. In the future, I'm possibly gonna work on OpenCL (which is C-based, so earlier C++ efforts will come in handy), as well as possibly Cython which is a very very close cousin of Python and as such might be easiest of all to add - however the performance of Cython is in general worse than pure C++ or Fortran, so Cython might not happen anytime soon.

When speaking about Matlab in terms of performance, I can totally see the point of translating it into Fortran. And I can see that transpilation like: Matlab -> Python AST -> Fortran would probably be made possible with one small addition of some AST translator that takes output of one of the existing Matlab parsers and translates it into Python AST. I rely on this idea already for parsing Fortran and parsing C++ headers, and in Transpyle there is even "XmlAstGeneralizer" class (in https://github.com/mbdevpl/transpyle/blob/master/transpyle/general/ast_generalizer.py) that helps a lot to implement translation from foreign XML-based ASTs into Python AST.

Adding Matlab support should not conflict in any way with what I'm doing - it's just that I already have a lot of things on my to-do list, so I can't say that I can devote too much attention for it. So if you'd be willing to lead the work on it, I would welcome and help as much as I can. And as author of most of the code of the framework, I would probably be able to immediately suggest a solution to almost any implementation problem.

And finally, I'm very interested - what motivated you to consider adding Matlab support here?

@RobBW
Copy link
Author

RobBW commented Jun 1, 2019 via email

@mbdevpl
Copy link
Owner

mbdevpl commented Jun 14, 2019

Oh, thanks for trying it out! Indeed the:

  • typo in readme, and
  • missing "latest" tag on the Docker Hub

are my mistakes. I fixed those (and "latest" tag now corresponds to master branch), so hopefully at least future users won't run into the same trouble. Sorry for the trouble and thanks again for reporting the issues!

If you run into anything else unexpected, please let me know.

@RobBW
Copy link
Author

RobBW commented Jul 2, 2019 via email

@RobBW
Copy link
Author

RobBW commented Aug 10, 2019 via email

@RobBW
Copy link
Author

RobBW commented Jul 20, 2020

Are you aware of mlabpy at: https://github.com/led02/mlabpy ?
It claims to produce a python AST from Matlab.
It is a development based on smop.
I have not tested it.

@RobBW
Copy link
Author

RobBW commented Nov 29, 2021

There is a new and open source tool for parsing and lexing Matlab at: https://misshit.org
Github repository at https://github.com/florianschanda/miss_hit
It is described as follows:

"There have been other attempts at this, but MISS_HIT tries to do things differently (and I hope better).
Specifically:
• It is completely stand-alone. It does not require a working MATLAB or Octave setup.
• It is not just a bag of reg-ex, or a wrapper arounds some undocumented MATLAB internals. It is based on a feature complete lexer and parser, which supports every single weird feature of the MATLAB language.
• It contains features specifically for huge industrial code-bases (i.e. millions of lines): multi-threaded analysis and a sophisticated configuration and justification mechanism.
• Designed for both interactive use during development, and integration into modern CI environments."_

It will undoubtedly be a good resource for extending the Matlab capabilities of transpyle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants