Skip to content

Instantly share code, notes, and snippets.

@danielgindi
Last active March 6, 2017 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgindi/804b0b01ebfbd0531652c51199008342 to your computer and use it in GitHub Desktop.
Save danielgindi/804b0b01ebfbd0531652c51199008342 to your computer and use it in GitHub Desktop.
Convert syntax between languages
/* Note:
This is not meant for translating real code between languages.
Only for basic syntax translation, to get you started with a skeleton,
before doing line-by-line manually
*/
var Converter = {
curlifyWhitespace: function (code) {
var lines = code.split('\n');
var o = '';
var nextLine;
var ws1, ws2, wsp;
var wsStack = [];
for (var i = 0, llen = lines.length; i < llen ; i++) {
// Find whitespace length of current line
ws1 = lines[i].match(/^\s*/)[0];
// Find next line
for (nextLine = i + 1; nextLine < llen; nextLine++) {
if (lines[nextLine].trim().length === 0) continue;
break;
}
// Find whitespace length of next line
ws2 = (nextLine === llen ? '' : lines[nextLine]).match(/^\s*/)[0];
if (lines[i].trim().length === 0) {
// Empty line
o += lines[i] + '\n';
} else {
if (ws1 === ws2) {
// Same level
o += lines[i] + '\n';
} else {
// Different level
if (ws2.length > ws1.length) {
// Next line is a higher level, open a block
o += lines[i] + '{\n';
wsStack.push(ws1);
} else if (ws2.length < ws1.length) {
// Next line is a lower level, close all blocks until that level
o += lines[i] + '\n';
while (wsStack.length && wsStack[wsStack.length - 1] >= ws2) {
o += wsStack.pop() + '}\n'
}
} else {
// Should not arrive here!
o += lines[i];
console.warn('Different characters used in block whitespace!')
}
}
}
}
return o;
},
convertPythonCommentsToJs: function (code) {
return code
.replace(/(^|\r|\n)(\s*)#([^\r\n]*)/g, '$1$2//$3')
.replace(/^(\s*)(["']{3})(|[\s\S]*?(?:[^\\](?:(\\)\4)*))\2/gm, function () { return arguments[1] + '/*' + arguments[3].replace(/\*\//g, '* /') + '*/' });
},
python2js: function (code) {
// Convert comments
code = this.convertPythonCommentsToJs(code);
// Add curly braces
code = this.curlifyWhitespace(code);
// Finalize function definitions
code = code
.replace(/(\s*)def\s+([a-zA-Z0-9_]+)\s*\(\s*self\s*(?:,\s*([^)]+))?\)\s*:\s*{/g, '$1function $2($3) {')
.replace(/(\s*)def\s+([a-zA-Z0-9_]+)\s*\((?:\s*([^)]+))?\)\s*:\s*{/g, '$1function $2($3) {');
// Convert some fors
code = code.replace(/^([ \t]*)for ([a-zA-Z]+) in range\((.+?)\)\s*:/gm, '$1for (var $2 = 0, $2len = $3; $2 < $2len; $2++) ');
// Convert ifs
code = code
.replace(/^([ \t]*)if (.+?)\s*:/gm, '$1if ($2) ')
.replace(/^([ \t]*)else\s*:/gm, '$1else ')
.replace(/^([ \t]*)elif\s*(.+?)\s*:/gm, '$1else if ($2) ');
// Convert try-except-finally
code = code
.replace(/^([ \t]*)try\s*:/gm, '$1try ')
.replace(/^([ \t]*)except (.+?)\s*:/gm, '$1catch ($2) ')
.replace(/^([ \t]*)finally\s*:/gm, '$1finally ');
return code;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment