HTML + Javascript + jQuery + jQuery UI
L'input deve essere indicato nell'area di testo e deve essere formattato come ciascun comando in una riga. I comandi non fanno distinzione tra maiuscole e minuscole. Tutti i comandi sono stati completamente implementati. Spero che ti piaccia.
Puoi provarlo su http://jsfiddle.net/bCBfk/
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Useless interpreter</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style type="text/css">
textarea { height: auto; }
.badshit { color: red; }
.ui-dialog-titlebar-close { display: none; }
</style>
<script type="text/javascript">
String.prototype.startsWith = function(x) {
return this.substring(0, x.length) === x;
};
String.prototype.endsWith = function(x) {
return this.substr(this.length - x.length, x.length) === x;
};
var npe = "<pre>java.lang.NullPointerException\n"
+ " at org.esolangs.wiki.useless.memorymodel.ExistentObjectPool.findObject(ExistentObjectPool.java:684)\n"
+ " at org.esolangs.wiki.useless.interpreter.WhereInstruction.visit(WhereInstruction.java:29)\n"
+ " at org.esolangs.wiki.useless.interpreter.UselessProgram.run(UselessProgram.java:413)\n"
+ " at org.esolangs.wiki.useless.interpreter.Main.main(Main.java:53)</pre>";
var wut = navigator.userAgent + " - " + navigator.language + " - " + navigator.platform + " - Ii?".toLocaleUpperCase();
var wut2 = "";
for (var c = wut.length - 1; c >= 0; c--) {
wut2 += wut.charAt(c);
}
var popupMasterMind;
function killIt() {
clearInterval(popupMasterMind);
$(".dontuseme").remove();
popupMasterMind = null;
}
function spawn() {
var x = $("<div class='dontuseme' title=''><p></p></div>");
$("body").append(x);
x.dialog();
var bw = $("body").innerWidth();
var bh = $("body").innerHeight();
if (bh < 500) bh = 500;
var xw = x.width();
var xh = x.height();
x.parent().css({left: Math.random() * (bw - xw) + "px", top: Math.random() * (bh - xh) + "px"});
}
function dontuseme() {
if (popupMasterMind) return;
spawn();
popupMasterMind = setInterval(spawn, 700);
}
var hasOutput = false;
function clearOutput() {
$("#output").empty();
hasOutput = false;
$("#cc").hide();
}
function out(a) {
$("#output").append($(a));
hasOutput = true;
}
function finish() {
$("#running").hide();
$("#bt").show();
if (hasOutput) $("#cc").show();
}
var annoyingUser = false;
function swap() {
annoyingUser = true;
$("#everything").toggle();
setTimeout(swap, 800);
}
function randomString() {
var r = "";
var f = Math.floor(Math.random() * 12) + 8;
for (var i = 0; i < f; i++) {
r += "ABCDEFGHIJKLMNOPQRSTUVWXYZ.!?0123456789".charAt(Math.floor(Math.random() * 39));
}
return r;
}
var instructions;
function includeInstruction(name) {
name = name.toUpperCase();
if (instructions[name]) return; // Do not add it twice or overwrite existing instructions.
var array = [];
for (var e in instructions) {
array.push(e);
}
var rand = Math.floor(Math.random() * array.length);
//alert(name + ": " + array[rand]);
instructions[name] = instructions[array[rand]];
}
// DONTUSEME(n) are special cases handled elsewhere.
instructions = {
"FAIL": function() { out("<p class='badshit'>Warning: The <blink> tag is obsolete.</p>"); if (!annoyingUser) swap(); return "next"; },
"NOT": function() { out("<p class='badshit'>Warning: The NOT instruction is discouraged because it breaks yor Useless program.</p>"); return "quit"; },
"NEVER": function() { out("<pre>Wild MISSINGNO. appeared!</pre>"); return "next"; },
"IDK": function() { out("<pre>" + {}.idk + "</pre>"); return "next"; },
"BOOM!": function() { $("#everything").empty(); return "quit"; },
"KABOOM!": function() { window.location = "http://answers.yahoo.com/question/index?qid=20110816062515AANqygl"; return "quit"; },
"NO.": function() { finish(); return "quit"; },
"QWAOZAPWQFUOA": function() { out("<p class='badshit'>Sorry, I could not understand <a href='https://www.google.com.br/#q=women+psychology+and+behaviour'>this</a>.</p>"); return "next"; },
"WUT?": function() { out("<p>" + wut2 + "</p>"); return "next"; },
"WHERE?": function() { out(npe); return "next"; },
"HOW?": function() { out("<p class='badshit'>Regular expression parser failed for HTML. Cause: \"ZALGO\"</p>"); return "next"; },
"ILLEGAL": function() { out("<pre>codegolfer is not in the sudoers file. This incident will be reported</pre>"); return "next"; },
"GODEXISTS": function() { out("<p>'GOD' spelled backwards is 'DOG'. A DOG is an animal that does not exists, and by backwarding this, we conclude that GOD exists and is not an animal.</p>"); return "next"; },
"WINDOWS": function() { out("<p><img width='640' height='400' src='http://upload.wikimedia.org/wikipedia/commons/3/3b/Windows_9X_BSOD.png' alt='Sorry, this optional instruction was not implemented. Please, install the service pack.'></p>"); return "next"; },
"NOOP": function() { return "next"; },
"TURINGVSALONZO": function() {
var r = Math.random() * 10;
if (r < 2) return "next";
if (r < 7) return "t" + (Math.random() * 14 + 1) * 1000;
if (r < 9) return "t" + (Math.random() * 50 + 10) * 60 * 1000;
return "quit";
},
"42": function() {
out("<p>Calculating the answer of the life, the universe and everything.</p>");
out("<p>Estimated time is 7.5 million years.</p>");
out("<p>Don't you want to briefly take a coffe while you wait? It will not take long, I promise.</p>");
return "quit";
},
// This is special, as it needs a (surprising) useless parameter, it can't be acessed directly without prior processing, this is why it is lowercase.
"dontuseme": function() { dontuseme(); return "next"; },
// This is special. If the INCLUDE-xxx generates a INCLUDE-yyy instruction, the yyy instruction will have an unknown random generated name.
// Since yyy is random and unknown, it probably won't appear in the input source code, but implement it regardless.
"include-random": function() { includeInstruction(randomString()); return "next"; }
};
function bad(line) {
//alert(line);
out("<p class='badshit'>Syntax error: </p>");
}
function beyondEnd() {
out("<p class='badshit'>Unrecoverable error: Tried to execute code beyond the end or program.</p>");
}
function interpretInstruction(lines, idx) {
if (idx >= lines.length) { beyondEnd(); return; }
// The toUpperCase serves two purposes: Making the language case-insensitive and hiding private implementations as lowercase instructions.
ins = lines[idx].trim().toUpperCase();
var result;
// Special handling for parsing DONTUSEME(n)
if (ins.startsWith("DONTUSEME(") && ins.endsWith(")")) {
try {
parseInt(ins.substring("DONTUSEME(".length, ins.length - 1));
} catch (e) {
bad(ins);
return;
}
ins = "dontuseme";
// Special handling for INCLUDE-xxx
} else if (ins.startsWith("INCLUDE-") && ins.length > 8) {
var name = ins.substring(8);
includeInstruction(name);
ins = "NOOP"; // Already executed, follow-up as noop.
}
// Execute the instruction.
var f = instructions[ins];
if (!f) { bad(ins); return; }
var result = f();
// Move on.
if (result === "quit") return;
var toWait = result === "next" ? 0 : parseInt(result.substring(1));
var ii = idx + 1;
setTimeout(function() {
interpretInstruction(lines, ii);
}, toWait);
}
function startInterpreter() {
$("#bt").hide();
$("#cc").hide();
$("#running").show();
var src = $("#input").val();
var lines = src.split('\n');
interpretInstruction(lines, 0);
}
$(document).ready(function() {
$("#bt").click(startInterpreter);
$("#cc").click(clearOutput);
});
</script>
</head>
<body>
<div id="everything">
<p>Type here your program input:</p>
<textarea id="input" style="width: 400px; height: 150px;"></textarea>
<p>
<button id="bt">Run the program</button>
<span id="running" style="display: none;">Running the program...</span>
</p>
<p>Here is the program output:</p>
<p id="output" class="useless"></p>
<button id="cc" style="display: none;">Clear the output</button>
</div>
</body>
</html>
Ha un nuovo comando:
È il 42
comando che calcola la risposta della vita, dell'universo e di tutto. L'unica stranezza è che ci vogliono 7,5 milioni di anni per finire.
Altri spoiler:
Questa voce ha diverse funzionalità:
- Odierai davvero il
FAIL
comando.
-
BOOM!
ti rovinerà "unità di esecuzione". Almeno le finestre DONTUSEME(n)
sono in grado di sopravvivere a questo.
-
KABOOM!
è davvero pericoloso per il mondo in diversi modi.
-
DONTUSEME(n)
apre sempre infinite finestre vuote non chiudibili, una ogni 0,8 secondi. Ma c'è un interruttore di uccisione nascosto.
-
DONTUSEME(n)
sopravvive NO.
, FAIL
e persino BOOM!
. Non riuscivo proprio a sopravvivere al KABOOM!
pensiero. Il motivo è che le finestre popup non funzioneranno poiché questo non è il risultato di un clic (e i popup creati in altri modi sono a lungo vietati in tutti i principali browser) e non potrebbero utilizzare anche iframe a causa di una violazione delle norme sulla stessa origine .
-
INCLUDE-xxx
può creare qualsiasi istruzione, inclusa DONTUSEME(n)
o un'altra INCLUDE-yyy
istruzione.
- Se
INCLUDE-xxx
genera INCLUDE-yyy
un'istruzione, il yyy
nome viene generato in modo casuale. Se prendi il nome del comando generato con firebug o qualcosa di simile, puoi usarlo.
- Gestisce sintassi non corretta e input incompleto o vuoto.