|
| 1 | +var stacks = {}; |
| 2 | +function pushstack(st, v) { |
| 3 | + v &= 4294967295; |
| 4 | + if (st == "@") { |
| 5 | + if (v == 0) { |
| 6 | + stacks[st].push(48); |
| 7 | + } else { |
| 8 | + if (v & 2147483648) { |
| 9 | + for (let i of (v & 2147483647) - 2147483648 + "") { |
| 10 | + stacks[st].push(i.charCodeAt(0)); |
| 11 | + } |
| 12 | + } else { |
| 13 | + for (let i of v + "") { |
| 14 | + stacks[st].push(i.charCodeAt(0)); |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + } else { |
| 19 | + stacks[st].push(v); |
| 20 | + } |
| 21 | +} |
| 22 | +function topstack(st) { |
| 23 | + if (stacks[st].length) { |
| 24 | + return stacks[st][stacks[st].length - 1]; |
| 25 | + } |
| 26 | + return 0; |
| 27 | +} |
| 28 | +function popstack(st) { |
| 29 | + if (stacks[st].length) { |
| 30 | + return stacks[st].pop(); |
| 31 | + } |
| 32 | + return 0; |
| 33 | +} |
| 34 | +function kipple_helper(program, input, recur) { |
| 35 | + program += " "; |
| 36 | + if (!recur) { |
| 37 | + for (let i of "qwertyuiopasdfghjklzxcvbnm@") { |
| 38 | + stacks[i] = []; |
| 39 | + } |
| 40 | + for (let i of input) { |
| 41 | + stacks["i"].push(i.charCodeAt(0)); |
| 42 | + } |
| 43 | + } |
| 44 | + while (program) { |
| 45 | + if (program[0] == "(") { |
| 46 | + program = program.slice(1); |
| 47 | + var p = 1; |
| 48 | + var i = 0; |
| 49 | + var fb = ""; |
| 50 | + for (; i < program.length; i++) { |
| 51 | + if (program[i] == "(") { |
| 52 | + p++; |
| 53 | + } else if (program[i] == ")") { |
| 54 | + p--; |
| 55 | + } |
| 56 | + if (!p) { |
| 57 | + break; |
| 58 | + } |
| 59 | + fb += program[i]; |
| 60 | + } |
| 61 | + program = program.slice(i + 1); |
| 62 | + while (stacks[fb[0]].length) { |
| 63 | + kipple_helper(fb, "", 1); |
| 64 | + } |
| 65 | + } else if (" \n\t\r\v".includes(program[0])) { |
| 66 | + program = program.slice(1); |
| 67 | + } else { |
| 68 | + var cmds = ""; |
| 69 | + while (!"() \n\t\r\v".includes(program[0])) { |
| 70 | + cmds += program[0]; |
| 71 | + program = program.slice(1); |
| 72 | + } |
| 73 | + while (cmds) { |
| 74 | + var cmd = ""; |
| 75 | + while (cmds.length && !"><+-?".includes(cmds[0])) { |
| 76 | + cmd += cmds[0]; |
| 77 | + cmds = cmds.slice(1); |
| 78 | + } |
| 79 | + cmd += cmds[0]; |
| 80 | + cmds = cmds.slice(1); |
| 81 | + var tcmds = cmds; |
| 82 | + while (tcmds.length && !"><+-?".includes(tcmds[0])) { |
| 83 | + cmd += tcmds[0]; |
| 84 | + tcmds = tcmds.slice(1); |
| 85 | + } |
| 86 | + if (cmd.includes(">")) { |
| 87 | + var lf = cmd.slice(0, cmd.indexOf(">")), |
| 88 | + rg = cmd.slice(cmd.indexOf(">") + 1); |
| 89 | + if ("0123456789".includes(lf[0])) { |
| 90 | + pushstack(rg, parseInt(lf)); |
| 91 | + } else { |
| 92 | + pushstack(rg, popstack(lf)); |
| 93 | + } |
| 94 | + } else if (cmd.includes("<")) { |
| 95 | + var lf = cmd.slice(0, cmd.indexOf("<")), |
| 96 | + rg = cmd.slice(cmd.indexOf("<") + 1); |
| 97 | + if ("0123456789".includes(rg[0])) { |
| 98 | + pushstack(lf, parseInt(rg)); |
| 99 | + } else { |
| 100 | + pushstack(lf, popstack(rg)); |
| 101 | + } |
| 102 | + } else if (cmd.includes("+")) { |
| 103 | + var lf = cmd.slice(0, cmd.indexOf("+")), |
| 104 | + rg = cmd.slice(cmd.indexOf("+") + 1); |
| 105 | + if ("0123456789".includes(rg[0])) { |
| 106 | + pushstack(lf, topstack(lf) + parseInt(rg)); |
| 107 | + } else { |
| 108 | + pushstack(lf, topstack(lf) + popstack(rg)); |
| 109 | + } |
| 110 | + } else if (cmd.includes("-")) { |
| 111 | + var lf = cmd.slice(0, cmd.indexOf("-")), |
| 112 | + rg = cmd.slice(cmd.indexOf("-") + 1); |
| 113 | + if ("0123456789".includes(rg[0])) { |
| 114 | + pushstack(lf, topstack(lf) - parseInt(rg)); |
| 115 | + } else { |
| 116 | + pushstack(lf, topstack(lf) - popstack(rg)); |
| 117 | + } |
| 118 | + } else if (cmd.includes("?")) { |
| 119 | + if (!topstack(cmd.slice(0, cmd.length - 1))) |
| 120 | + stacks[cmd.slice(0, cmd.length - 1)] = []; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + if (recur) { |
| 126 | + return; |
| 127 | + } |
| 128 | + var output = ""; |
| 129 | + while (stacks["o"].length) { |
| 130 | + output += String.fromCharCode(stacks["o"].pop()); |
| 131 | + } |
| 132 | + return output; |
| 133 | +} |
| 134 | +function kipple(program, input) { |
| 135 | + return kipple_helper(program, input, 0); |
| 136 | +} |
0 commit comments