I'm working on a language called grond (see it on GitHub); the compiler is so small it is included in this post.
My gut tells me that this code is awful, but it works. I am hesitant to change things to make the code better because generally, I have found that bad things happen when I "fix" something that isn't necessarily broken.
How can I make this "ugly" code better? Note - If you don't think this code is ugly, please tell me!
String.prototype.reverse = function() {
return this.split("").reverse().join("");
}
function replace(wc) {
var lastWC;
while (true) {
lastWC = wc;
wc = wc.reverse();
wc = wc.replace("§", "");
wc = wc.replace("Ś", "");
wc = wc.replace(/\(a(?!\w)/, "(trela"); //alert ,a
wc = wc.replace(/\(f(?!\w)/, "(rof"); // for, f
wc = wc.replace(/\(w(?!\w)/, "(elihw"); //while,w
wc = wc.replace(/\(p(?!\w)/, "(tpmorp"); //prompt,p
wc = wc.replace(/\(vr\./, "(esrever."); //reverse, rv
wc = wc.replace(/\(tm\./, "(chtam."); // match, mt
wc = wc.replace(/\(nj\./, "(nioj."); //join, .jn
wc = wc.replace(/\(pr\./, "(ecalper."); //replace, .rp
wc = wc.replace(/\(ls\./, "(.ecils"); //slice, sl
wc = wc.replace(/\(lt\./, "(esaCrewoLot."); //toLowerCase, tl
wc = wc.replace(/\(ut\./, "(esaCreppUot."); //toUpperCase, tu
wc = wc.replace(/\(ps\./, "(tilps."); //split, sp
wc = wc.reverse();
wc = wc.replace(/\.l(?!\w)/, ".length"); // .length, .l
if (lastWC === wc)
break;
}
return wc;
}
var source = prompt("Enter Source");
var sourceCopy = source;
var fullASCII = ' !#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.split('');
var y = (a) => {
for (let char in fullASCII) {
if (!source.contains(char)) {
source.replace(a, char);
}
}
}
var OK = false;
var OK2 = false;
for (let char in fullASCII) {
if (!source.contains(char)) {
OK2 = OK ? true : false;
OK = true;
}
}
if (!OK2) {
throw "Your program uses every ASCII charactor. Grond requires your program to leave 2 ASCII chars unused. This is stupid and will be fixed.";
}
y('\\\"');
y("\\\'");
var sourceSplit = sourceCopy.split("\'");
for (let i = 0; i < sourceSplit.length; i += 2) {
var o = sourceSplit[i].split('\"');
for (let e = 0; e < o.length; e += 2) {
o[e] = replace(o[e]);
}
sourceSplit[i] = o.join('\"');
}
sourceCopy = sourceSplit.join("\'");
var append = "if(s != \"\"){alert(s)}";
var prepend = "var s = prompt();";
var oSS = source.split('');
if (oSS[0] == "§") {
prepend = "var s = \"\"";
}
if (oSS[0] == "Ś" || oSS[1] == "Ś") {
prepend = "with(math){" + prepend;
append += "}";
}
sourceCopy = prepend + " String.prototype.la=function(h){var x=this.split(''); x[h] = x[h].toLowerCase(); return x.join('');};String.prototype.ca=function(h){var x=this.split(''); x[h] = x[h].toUpperCase(); return x.join('');};String.prototype.reverse=function(){return this.split('').reverse().join('');}; \n var h = ['hello, world', 'Hello, World', 'Hello, World!', 'hello, world!', 'hello world', 'hello world!', 'Hello World', 'Hello World!', 'Hello, world.', 'Hello, World.' ,'hello world.' , 'Hello World.']; var q ='" + source + "'; " + sourceCopy + append;
alert(sourceCopy);
eval(sourceCopy);
prompt();
... you should click run code snippet or read the code... \$\endgroup\$String.prototype.contains
method. \$\endgroup\$