Reorganization and improvements on matlab2r

This commit is contained in:
Waldir Leoncio 2020-11-19 10:48:33 +01:00
parent 22cea5b223
commit 0257e5fbfb
2 changed files with 54 additions and 19 deletions

View file

@ -1,13 +1,16 @@
#' @title Convert Matlab function to R
#' @description Performs basic syntax conversion from Matlab to R
#' @param filename name of the file
#' @param saveOutput if `TRUE`, `filename` is overwritten. Defaults to `FALSE`
#' @param output can be "asis", "clean" (default) or "save"
#' @param improve_formatting if `TRUE` (default), makes minor changes
#' to conform to best-practice formatting conventions
#' @return text converted to R, printed to screen or replacing input file
#' @author Waldir Leoncio
#' @importFrom utils write.table
#' @export
matlab2r <- function(filename, saveOutput = FALSE) {
matlab2r <- function(
filename, output = "clean", improve_formatting=TRUE
) {
# ======================================================== #
# Verification #
# ======================================================== #
@ -22,10 +25,18 @@ matlab2r <- function(filename, saveOutput = FALSE) {
# Replacing text #
# ======================================================== #
# Uncommenting ------------------------------------------- #
txt <- gsub("^#\\s?(.+)", "\\1", txt)
# Function header ---------------------------------------- #
out <- gsub(
pattern = "\\t*function (.+)\\s*=\\s*(.+)\\((.+)\\)",
replacement = "\treturn(\\1)",
x = txt[1]
)
txt <- gsub(
pattern = "function (.+)\\s+=\\s*(.+)\\((.+)\\)",
replacement = "\\2 <- function(\\3) { return(\\1)",
pattern = "\\t*function (.+)\\s*=\\s*(.+)\\((.+)\\)",
replacement = "\\2 <- function(\\3) {",
x = txt
)
txt <- gsub(
@ -33,24 +44,45 @@ matlab2r <- function(filename, saveOutput = FALSE) {
replacement = "\\1 <- function(\\2) {",
x = txt
)
# txt <- gsub("\\%\\s*(\\w+)", "# \\1", txt)
# Function body ------------------------------------------ #
txt <- gsub("(.+)\\.\\.\\.", "\\1", txt)
txt <- gsub(";", "", txt)
# Loops and if-statements
txt <- gsub("for (.+)=(.+)", "for (\\1 in \\2) {", txt)
txt <- gsub("end", "}", txt)
txt <- gsub("(.),(\\S)", "\\1, \\2", txt)
# TODO: replace forms like (:,:) with [, ] if they come before <-
# TODO: add argument to skip some of these rules
txt <- gsub("end$", "}", txt)
txt <- gsub("if (.+)", "if (\\1) {", txt) # FIXME: paste comments after {
txt <- gsub("else$", "} else {", txt)
txt <- gsub("elseif", "} else if", txt)
txt <- gsub("\\(~", "(!", txt)
txt <- gsub("while (.+)", "while \\1 {", txt)
## Math operators
txt <- gsub("(\\S)\\+(\\S)", "\\1 + \\2", txt)
txt <- gsub("(\\S)\\-(\\S)", "\\1 - \\2", txt)
txt <- gsub("(\\S)\\*(\\S)", "\\1 * \\2", txt)
# Returning converted code
if (!saveOutput) {
# MATLAB-equivalent functions in R
txt <- gsub("gamma_ln", "log_gamma", txt)
# Subsets ------------------------------------------------ #
txt <- gsub("([^\\(]+)\\((.+)\\)\\s?=(.+)", "\\1[\\2] <- \\3", txt)
# Formatting --------------------------------------------- #
if (improve_formatting) {
txt <- gsub("(.),(\\S)", "\\1, \\2", txt)
# Math operators
txt <- gsub("(\\S)\\+(\\S)", "\\1 + \\2", txt)
txt <- gsub("(\\S)\\-(\\S)", "\\1 - \\2", txt)
txt <- gsub("(\\S)\\*(\\S)", "\\1 * \\2", txt)
# Logic operators
txt <- gsub("\\(~", "(!", txt)
# Assignment
txt <- gsub("(.+)\\s?=\\s?(.+)", "\\1 <- \\2", txt)
}
# Adding output and end-of-file brace -------------------- #
txt <- append(txt, paste(out, "\n}"))
# Returning converted code ------------------------------- #
if (output == "asis") {
return(txt)
} else if (output == "clean") {
return(cat(txt, sep="\n"))
} else {
return(

View file

@ -4,12 +4,15 @@
\alias{matlab2r}
\title{Convert Matlab function to R}
\usage{
matlab2r(filename, saveOutput = FALSE)
matlab2r(filename, output = "clean", improve_formatting = TRUE)
}
\arguments{
\item{filename}{name of the file}
\item{saveOutput}{if `TRUE`, `filename` is overwritten. Defaults to `FALSE`}
\item{output}{can be "asis", "clean" (default) or "save"}
\item{improve_formatting}{if `TRUE` (default), makes minor changes
to conform to best-practice formatting conventions}
}
\value{
text converted to R, printed to screen or replacing input file