Created
July 24, 2016 02:16
-
-
Save kiasaki/bf7a18803ab058365dc4fb28917b9276 to your computer and use it in GitHub Desktop.
regexes for fun, and pr... really just for fun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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