matconv: The use of syntax converters

Basic calls

The first thing that the translator does is go through the program and convert the basic syntax from Matlab to R. This includes the following:

  1. Changing symbols around (‘~’ to ‘!’, ‘end’ to ‘}’)
  2. Get rid of the semicolons at the end of the line
  3. Change the assignment symbol from ‘=’ to ‘<-’

This is done with simple ‘gsub’ calls and examples are shown below.

matCode rCode
thing = 5 * 3;
thing2 = (thing ~= 14);
thing <- 5 * 3
thing2 <- (thing != 14)

Flow Control

Next it goes through all the different flow control blocks and converts them to the R equivalent. The converted blocks are as follows.

  1. if
  2. if else
  3. for
  4. while

Switch statements will be included in a future version as R does not really have a true switch / case type block. The ‘switch’ function is helpful in a lot of situations but the usage is different from how other languages do switch blocks. Below are some examples of how its used for the other blocks.

matCode rCode
if argLen == 1
  doThing = 9999;
else
  doThing = 1;
end
if (argLen == 1){
  doThing <- 9999
} else {
  doThing <- 1
}

User functions

In matlab there is a distinction between script files and function files. If a function file is supplied (first word is “function”) than the function definitions will be changed with some of syntax.

The returned objects are gathered and put in a ‘return’ statement at the end of the function. If there is more than one return statement a list of those objects are returned. Default inputs or an unknown length of inputs into a Matlab function is handled using a protected word ‘varargin’. The closest R equivalent is ‘…’ which will be turned into a list as the variable ‘varargin’ in the R version. There is a whole family of input and output handlers in Matlab but this is the most useful in my eyes so I gave it higher priority. Examples of this type of conversion is shown below.

matCode rCode
function [ dat ] = xlsReadPretty(varargin))
  didThing = 1*3;
  dat = didThing / 3;
end
xlsReadPretty <- function(…){
  varargin <- list(…)
  didThing <- 1*3
  dat <- didThing / 3
  return(dat)
}

In another version, there could be options to make this conversion more integrated with the rest of the code or specifying another return class other than ‘list’. This is a precursour to the function converters by adding these user functions into the dictionary of Matlab base functions to R base functions.