Skip to content

Instantly share code, notes, and snippets.

@kiasaki
Created July 24, 2016 02:16
Show Gist options
  • Save kiasaki/bf7a18803ab058365dc4fb28917b9276 to your computer and use it in GitHub Desktop.
Save kiasaki/bf7a18803ab058365dc4fb28917b9276 to your computer and use it in GitHub Desktop.
regexes for fun, and pr... really just for fun
function compile(code) {
code = code.replace(/def\s+([a-zA-Z0-9_$]+)((?:\s[a-zA-Z0-9_$]+)+)\s+=((?:(?!\n\n)[\s\S])+)/g, function (_, functionName, args, functionBody) {
var functionArgs = args.trim().replace(/\s+/g, ', ');
return ['function ', functionName, '(', functionArgs, ') {\n return ', functionBody.trimLeft(), ';}'].join('');
});
code = code.replace(/let((?:(?!in)[\s\S])+)in/g, function(_, args) {
return 'var ' + args.trim() + ';;';
});
code = code.replace(/\n[\n]+/g, ';\n');
code = code.replace(/\n$/g, ';\n');
code = code.replace(/\s+/g, ' ');
code = code.replace(/\) ;/g, ');');
code = code.replace(/;;/g, ';');
code = code.replace(/; /g, ';');
code = code.replace(/;([^\n])/g, ';\n$1');
code = code.replace(/;\s+}/g, ';}');
return code;
}
/*
for compiling
let
a = 5,
b = 6
in
console.log(a + b) ;;
let x = 5 in
def derp y =
x + y
console.log(derp(8))
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment