182 letters - forked from: BrainF*ck interpreter challenge
forked from 188 letters - forked from: BrainF*ck interpreter challenge (diff: 52)
細かいところで稼ぐ
ActionScript3 source code
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nTie
*/
// forked from h013's 188 letters - forked from: BrainF*ck interpreter challenge
// forked from keim_at_Si's 192 letters - forked from: BrainF*ck interpreter challenge
// forked from keim_at_Si's BrainF*ck interpreter challenge
package {
import flash.events.*;
import flash.display.*;
import com.bit101.components.*;
public class BFInterpreter extends Sprite {
// my current brainf*ck interpreter (182 letters)----------------------------------------------------------------------------------------------------
//function $(b,i){with(i){for(s=[n=j=p=k=0],o=[];c=b[k<0?--p:p++];)k?k-=c>>6&&c-92:c<60?c&1?s[j]+=44-c:c&2?o.push(s[j]):s[j]=shift()||(p=b):c&1?p+=k=c&4?-1:!s[j]:s[j+=c-61]^=0;return o}}
// b : sourceChars
// i : inputChars
// c : command
// j : pointer //(x,y,zは最初から0が代入されている,がこれらをつかうと画面が動くため変更した)
// p : position
// o : output
// k : "[" counter
function $(b,i){
//withをつかうことで変数宣言が必要なくなる
with(i){
for(s=[j=p=k=0],o=[];c=b[k<0?--p:p++];)
k ? //カッコ内を移動しているときの処理
k-=c>>6&&c-92
:
c<60?
c&1? //2進数表現の下一桁で分岐
// 43 = 101011(2)
// 45 = 101101(2)
// "+"(43),"-"(45) の処理
s[j]+=44-c
:
c&2? //2進数表現の下から2桁目で分岐
// 46 = 101110(2)
// "."(46) の処理
o.push(s[j])
:
// 44 = 101100(2)
// ","(44) の処理 i.shift()==null なら終了
s[j] = shift() || (p=b)
:
c&1? //2進数表現の下一桁で分岐
// 91 = 1011011(2)
// 93 = 1011101(2)
// "["(91),"]"(93) の処理
// "["のとき s[j] != null なら k = 1
// "]"のとき k = -1
p += k= c&4 ? -1 : !s[j]
:
// 60 = 111100(2)
// 62 = 111110(2)
// "<"(60),">"(62) の処理
s[ j+=c-61 ]^=0
;return o
}
}
//*/
// User Interface ----------------------------------------------------------------------------------------------------
function BFInterpreter() { addEventListener(Event.ADDED_TO_STAGE, _setup); }
private function _setup(event:Event) : void {
event.target.removeEventListener(event.type, arguments.callee);
_source = _newTextArea("source :", 20, 200);
_input = _newTextArea("input :", 240, 18);
_output = _newTextArea("output :", 280, 160);
new PushButton(this, 132, 221, "execute", _execute).setSize(200, 18);
// Hello, world! (72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33)
_source.text = "+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.------------.<++++++++.--------.+++.------.--------.>+.";
}
private function _newTextArea(label:String, ypos:Number, height:Number) : TextArea {
var textArea:TextArea = new TextArea(this, 32, ypos);
textArea.setSize(400, height);
new Label(this, 32, ypos-20, label);
return textArea;
}
private function _execute(event:Event) : void {
var sourceCode:String, sourceChars:Array=[], inputChars:Array=[], outputChars:Array, i:int;
// translate input text to char[]
sourceCode = _source.text.replace(/[^<>+\-.,[\]]/gm, "");
for (i=0; i<sourceCode.length; i++) sourceChars.push(sourceCode.charCodeAt(i));
for (i=0; i<_input.text.length; i++) inputChars.push(_input.text.charCodeAt(i));
// null for the end of char[]
sourceChars.push(0);
inputChars.push(0);
// execute interpreter
outputChars = $(sourceChars, inputChars);
// translate output char[] to text
for (i=0; i<outputChars.length; i++) _output.text += String.fromCharCode(outputChars[i]);
_output.text += "\n";
}
private var _source:TextArea, _input:TextArea, _output:TextArea;
}
}