Skip to content

Commit

Permalink
added support for functions that don't return anything
Browse files Browse the repository at this point in the history
  • Loading branch information
lakras committed Mar 31, 2019
1 parent 8f31e45 commit 1f2e099
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
19 changes: 17 additions & 2 deletions matlab_to_julia_translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ translate = function(input)
functions[functionName] = functionLocation;
}
}
// function anything(anything)
var regex = /function\s+(.+?)\s*\([^\n]*\)/g;
var match;
while(match = regex.exec(input))
{
var functionName = match[1];
var functionLocation = indexOfGroup(match, 1);
if(!(functionName in matrixes)
&& !(functionName in reservedWordsDict))
{
functions[functionName] = functionLocation;
}
}

// SAVES NAMES OF PROBABLE MATRICES
// matrixName = load anything
Expand Down Expand Up @@ -333,7 +346,8 @@ translate = function(input)
// b = x * y; | b = x * y;
// end <- optional | return [a b]
// | end
if(/(function)\s*(\[?.*?\]?)\s*?=\s*(.*?)\s*?\((.*?)\)\n*?(\s*)((\n*?.*?)*?)/.test(contents))
if(/(function)\s*(\[?.*?\]?)\s*?=\s*(.*?)\s*?\((.*?)\)\n*?(\s*)((\n*?.*?)*?)/.test(contents)
|| /(function)()\s*(.*?)\s*?\((.*?)\)\n*?(\s*)((\n*?.*?)*?)/.test(contents))
{
// locates all end keywords
var endLocations = [];
Expand Down Expand Up @@ -417,7 +431,8 @@ translate = function(input)
var returnLocation = endLocations[endIndex - 1] - 1;
whitespace = whitespace.replace(/^\n(\s*)/g, "$1");
whitespace = whitespace.replace(/(\s*)\n$/g, "$1");
if(returnStatement.length > 0)
if(returnStatement.length > 0
&& !/\s*\[\s*\]\s*/.test(returnStatement))
{
returnStatement = whitespace + "return " + returnStatement + "";

Expand Down
38 changes: 38 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ julia = "f = (x) -> x^2";
assert(julia === translator.translate(matlab));

// Functions
// function out = f(x)
// out = x^2
// end
matlab = "function out = f(x)\n"
+ " out = x^2\n"
+ "end";
Expand All @@ -421,6 +424,10 @@ julia = "function f(x)\n"
+ "end";
assert(julia === translator.translate(matlab));

// function[c f] = temperature(x, y)
// f = x + y
// c = x * y
// end
matlab = "function[c f] = temperature(x, y)\n"
+ " f = x + y\n"
+ " c = x * y\n"
Expand All @@ -433,6 +440,8 @@ julia = "function temperature(x, y)\n"
assert(julia === translator.translate(matlab));

// Tuples
// t = {1 2.0 3 4}
// t{1}
matlab = "t = {1 2.0 3 4}"
+ "t{1}";
julia = "t = (1, 2.0, 3, 4)"
Expand All @@ -456,3 +465,32 @@ julia = "a = 2.0"
+ "f(x) = a + x"
+ "f(1.0)";
// assert(julia === translator.translate(matlab));



// Functions that don't return anything
// from top answer in
// https://stackoverflow.com/questions/12992619/can-a-function-be-created-in-matlab-that-returns-nothing

// function [] = my_awesome_function(image,filename,other_inputs)
// % Do awesome things
// end
matlab = "function [] = my_awesome_function(image,filename,other_inputs)\n"
+ " % Do awesome things\n"
+ "end";
julia = "function my_awesome_function(image,filename,other_inputs)\n"
+ " # Do awesome things\n"
+ "end";
assert(julia === translator.translate(matlab));

// function my_awesome_function(image,filename,other_inputs)
// % Do awesome things
// end
matlab = "function my_awesome_function(image,filename,other_inputs)\n"
+ " % Do awesome things\n"
+ "end";
julia = "function my_awesome_function(image,filename,other_inputs)\n"
+ " # Do awesome things\n"
+ "end";
assert(julia === translator.translate(matlab));

0 comments on commit 1f2e099

Please sign in to comment.