6
最小バイトコードインタープリター/ VM
リーダーボード-JITコンパイル済み(低いほど良い) es1024-81.2ポイント(動作中のコンパイラを含む!) キース・ランドール-116ポイント Ell-121ポイント リーダーボード-解釈済み(低いほど良い) マーティン・ブットナー-706654ポイント(約2時間)。 クリプティック-30379ポイント(97秒) あなたがそれを受け入れることを選択した場合、あなたの使命は、可能な限り最小のバイトコードインタープリター/ VMを書くことです。VM /インタープリターは、以下で指定されている言語で、小さなCISCアーキテクチャを使用します(操作のサイズは異なる場合があります)。完了したら、3つのCPUレジスタの値を印刷して、正しい出力が印刷されたことを証明する必要があります(3,126,900,366)。 コンパイラ 独自のテストを作成する場合は、コンパイラを以下に掲載します。回答とともにテストを投稿してください。 コードスニペットを表示 window.compile=function(){var e=$("#text").val().toLowerCase().match(/[^\r\n]+/g);var t=[];for(var n=0;n<e.length;n++)compileLine(e[n],t);var r="";for(var n=0;n<t.length;n++)if(typeof t[n]=="string")r+="\n";else r+="0x"+t[n].toString(16)+" ";$("#compiledArray").val(r)};window.compileLine=function(e,t){var n=e.split(" ");if(n[0]=="load"){t.push(0);t.push(getInt(n[1]));t.pushArr(getInt(n[2]))}if(n[0]=="rload"){t.push(1);t.push(getInt(n[1]));t.push(getInt(n[1]))}if(n[0]=="push"){t.push(2);t.push(getInt(n[1]))}if(n[0]=="pop"){t.push(3);t.push(getInt(n[1]))}if(n[0]=="add"){t.push(4);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="sub"){t.push(5);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="mul"){t.push(6);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="div"){t.push(7);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="jmp"){t.push(8);t.pushArr(getInt(n[1]))}if(n[0]=="cmp"){t.push(9);t.push(getInt(n[1]));t.push(getInt(n[2]))}if(n[0]=="branchlt"){t.push(10);t.pushArr(getInt(n[1]))}if(n[0]=="brancheq"){t.push(11);t.pushArr(getInt(n[1]))}if(n[0]=="branchgt"){t.push(12);t.pushArr(getInt(n[1]))}if(n[0]=="branchne"){t.push(13);t.pushArr(getInt(n[1]))}t.push("NEW LINE")};window.getInt=function(e){if(e.trim().startsWith("<--"))return"COMMENT";if(e=="r0")return 0;if(e=="r1")return 1;if(e=="r2")return 2;if(e.startsWith("0x"))return parseInt(e,16);if(isNaN(parseInt(e)))alert(e);return getIntBytes(parseInt(e))};if(typeof String.prototype.startsWith!="function"){String.prototype.startsWith=function(e){return this.slice(0,e.length)==e}}Array.prototype.pushArr=function(e){this.push.apply(this,e)};window.getIntBytes=function(e){var t=[];var n=4;do{t[--n]=e&255;e=e>>8}while(n);return t} <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <textarea id="text" cols="40" rows="10"></textarea> <br/> <button onclick="compile()">Compile</button> <br/> <textarea id="compiledArray" cols="40" rows="10" readonly></textarea> …