HTML + Javascript + jQuery + jQuery UI
La entrada debe darse en el área de texto y debe formatearse como cada comando en una línea. Los comandos no distinguen entre mayúsculas y minúsculas. Todos los comandos se implementaron completamente. Espero que lo disfrutes.
Puedes probarlo en 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>
Tiene un nuevo comando:
Es el 42
comando que calcula la respuesta de la vida, el universo y todo. La única peculiaridad es que lleva 7.5 millones de años terminar.
Otros spoilers:
Esta entrada tiene varias características:
- Realmente odiarás el
FAIL
comando.
-
BOOM!
te arruinará la "unidad de ejecución". Al menos las ventanas DONTUSEME(n)
pueden sobrevivir a esto.
-
KABOOM!
Es realmente peligroso para el mundo de varias maneras malas.
-
DONTUSEME(n)
siempre abre infinitas ventanas vacías que no se pueden cerrar, una cada 0.8 segundos. Pero hay un interruptor oculto de matar.
-
DONTUSEME(n)
sobrevive NO.
, FAIL
e incluso BOOM!
. Sin embargo, no pude hacerlo sobrevivir KABOOM!
. La razón es que las ventanas emergentes no funcionarán, ya que esto no es el resultado de un clic (y las ventanas emergentes creadas por otras formas están prohibidas en todos los principales navegadores), y tampoco pueden usar iframes debido a una violación de la política del mismo origen .
-
INCLUDE-xxx
puede crear cualquier instrucción, incluida DONTUSEME(n)
u otra INCLUDE-yyy
instrucción.
- Si
INCLUDE-xxx
genera una INCLUDE-yyy
instrucción, el yyy
nombre se genera al azar. Si toma el nombre del comando generado con firebug o algo similar, puede usarlo.
- Maneja la sintaxis con formato incorrecto y la entrada incompleta o vacía.