Octave, GNU Octave, Matlab, Scientific Computing, Language, Interpreter, Compiler, C++, LAPACK, Fortran, Fun , GNU

Monday, June 25, 2007

Octave-2-Matlab and back

Octave-Lint (3)
A few more additional features that could be copied from existing tools in a similar areas. Immediately we can think of all the warnings that GCC would spit out, when you compile C/C++ code using 'gcc -Wall code.cc'. Specifically, flagging unused variables, data type conversions in known stdlibrary of Octave functions and such. Its time, I could show the code, instead of waxing eloquent about it. Just that I dont have anything to show.

Octave -> Matlab -> Octave
Next in a similar spirit of the AST walker static-checker we could also have a program transformation tool for converting Octave -> Matlab code. I could 'ask' for type inference whenever it is 'confused' by 'ambiguous' nature of the code. This is exciting, in terms of the possibilities it opens up for us. It is going to be a long project, but defnitely rewarding one.
The Octave parser is widely acknowledged on our mailing lists as a superset of Matlab
language. Using the Octave parser, you could in theory convert Matlab code the other-way
round too. Now this is double bonanza, a kind of buy-1 and you get-1 free. Am I excited?

The starting point of the program transformation tool would be from Paul Kienzle's work on
Oct2Mat conversion script. A few things are simple to understand, and require plain substituion. Paul's code does it using an AWK script, and clearly mentions not wanting to Octave's parse tree for doing the conversion. Paul attempts to do it using filters in AWK, and aims for an independent program for doing the conversion. Since our aims are to take advantage of whatever we have (GPL, Octave, Parser) then we can stand on the shoulders of giants instead of 'lets start at the very beginning, Do Re Mi'. Paul's code helps us easily identify the transformation rules to be applied.

The Octave->Matlab transformation rules, applied in Paul's code include
  1. gsub("#" , "%"); convert # to %
  2. gsub("[(][)]",""); convert () to ' ' as Matlab 4.0 and Octave-2.1.50 dont support empty arguments. This is not a problem anymore.
  3. gsub("gset[^;%]*;",""); a graphics command conversion.
  4. gsub("gset[^;]*%","%"); a graphics command conversion.
  5. gsub("gset[^;]*$",""); a graphics command conversion.
  6. gsub("endfunction",""); Octave has endif, endfor, endfunction etc, so those get converted to end, or in Matlab nothing at all. Infact Matlab complains if you have the end keyword at the end of a function file.
  7. gsub("endif","end"); Same as 6.
  8. gsub("endwhile","end"); Same as 6.
  9. gsub("endfor","end"); Same as 6.
  10. gsub("end_try_catch","end"); Same as 6.
  11. gsub(/&&/,"\\&"); Matlab didnot have the short circuit logical operators initially, so we had to use the logical &.
  12. gsub("SEEK_CUR",0); They also seemingly lacked options for STDIO processing.
  13. gsub("SEEK_END",1); Same as 12.
  14. gsub("SEEK_SET",-1); Same as 12.
  15. gsub("usage","error"); Our usage() and print_usage() are replaced by error() function.
  16. gsub("__error_text__","lasterr"); Some Octave specific code
  17. gsub("unwind_protect_cleanup",""); Again Octave was first here, w.r.t try/catch ...
  18. gsub("end_unwind_protect",""); ... doing it the LISP way.
  19. gsub("unwind_protect",""); Same as [17, 18].
  20. gsub(/\|\|/,"|"); Logical short-circuit OR operator was NOT there in Matlab first. We got here earlier.
  21. gsub("!","~"); They dont know whats Not !
  22. gsub("[\\]$","..."); Line continuations are OK for us. Not them!

These rules are easily loaded as actions for a AST walker, for operators in Octave. We just generate the do_convert_oct_to_mat() on each node. As an example, the node for the operator short-circuit && will have the conversion rule embedded in this routine as follows,

string short_ckt_and::do_convert_oct_to_mat() {
l_str=left_node.do_convert_oct_to_mat();
r_str=right_node.do_convert_oct_to_mat();
str=l_str + " & " + r_str;
return str;
}

So basically it would be a really syntactically correct way of doing things for bulilding transformation tools from the existing code base. As explained earlied since Octave being a superset of Matlab we could also turn the world the other-way round.

Ofcourse the validity of the converter can be verified by doing the rountripping, O-2-M-2-O and running the code to see if we have the regression tests validated. John Eaton (JWE), has a set of regression cases for parser, which is again something we can re-use.

It would be fun to see this happen, and I suspect this is much lower hanging fruit than writing an evaluating AST-walker for building the profiler or a debugger.

Cheers,
Muthu

4 comments:

Malaska said...
This comment has been removed by the author.
Malaska said...

Hello. I found your post very interesting. I would like to convert Octave code to Matlab code for version 7. Have you an updated version of your code? Where can I download it?

Malaska said...
This comment has been removed by the author.
justmylife said...

Hello, I am a student who has been taught octave, but for a design project my teammates knows only matlab, so could you specify on how to convert octave codes to matlab recognizable code?

Creative Commons License