<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script>
function makeAddInstruction(amount, target) {
if (amount < 0) {
target.push("SUB "+(-amount));
} else if (amount > 0) {
target.push("ADD "+(amount));
} else if (force) {
target.push("NOP");
}
}
function buildLookupTable(amounts, bias) {
var instructions = [], i, dispSoFar=0;
instructions.push("JRO ACC");
offsets = [];
for (i=amounts.length-1; i>=0; i--) {
offsets[i+1] = amounts[i]-(i+1)-dispSoFar;
dispSoFar += offsets[i+1];
}
offsets[offsets.length-1] += bias
for (i=1; i<offsets.length; i++) {
makeAddInstruction(offsets[i], instructions, true);
}
return instructions;
}
function buildLookupTables(amounts, chunkSize) {
var instructions = [];
var chunks = [];
var chunkSize = chunkSize || 10;
for (let i = 0; i < amounts.length; i += chunkSize) {
const chunk = amounts.slice(i, i + chunkSize);
chunks.push(chunk);
}
instructions.push("#"+(chunks.length+1)+" NODES REQUIRED\n");
var bias = amounts.length;
for (let i = 0; i < chunks.length; i += 1) {
if(i > 0) {
instructions.push("MOV -"+chunkSize+" ACC");
instructions.push("ADD <PREV>");
} else {
instructions.push("MOV <INPUT> ACC");
}
instructions = instructions.concat(buildLookupTable(chunks[i], bias));
instructions.push("MOV ACC <NEXT>\n")
bias -= chunkSize;
}
instructions.push("MOV -"+(chunkSize+bias)+" ACC")
instructions.push("ADD <PREV>")
instructions.push("MOV ACC <OUTPUT>")
return instructions;
}
$(function() {
$('#build').on('click', function() {
var amounts = $('#outputs').val().split(',').map(function(x) {return +x;});
console.log(amounts);
var firstIndex = +($('#firstIndex').val()) || 10;
var instructions = buildLookupTables(amounts, firstIndex);
console.log(instructions);
$('#tis100code').text(instructions.join('\n'));
});
});
</script>
</head>
<body>
<p><label>Output values (comma separated) <input type=text id=outputs value="1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990"></label></p>
<p><label>Chunk Size <input type=text id=firstIndex value=8></label></p>
<p><input type=button value=Calculate id=build></p>
<output><pre id=tis100code></pre><output>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |