In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

242 letters forked from: BrainF*ck interpreter challenge

@see http://wonderfl.net/c/lbq5
Get Adobe Flash player
by bkzen 02 Jun 2011
/**
 * Copyright bkzen ( http://wonderfl.net/user/bkzen )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cXpH
 */

// 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 brainf*ck interpreter (currently in 262 letters) ----------------------------------------------------------------------------------------------------
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c==62?x++:c==60?--x:c==43?(s[x]=(s[x]||0)+1):c==45?(s[x]=(s[x]||0)-1):c==46?o.push(s[x]):c==44?(s[x]=i.shift())?0:(p=b.length):c==93?(p=l.pop()-1):c==91?(s[x])?l.push(p):b:0)==b)for(k=1;k&&(c=b[++p]);)k-=(c==93)?1:(c==91)?-1:0;return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c==62?(s[++x]||=0):c==60?--x:c==43?s[x]++:c==45?--s[x]:c==46?o.push(s[x]):c==44?(s[x]=i.shift())?0:(p=b.length):c==93?(p=l.pop()-1):c==91?(s[x])?l.push(p):b:0)==b)for(k=1;k&&(c=b[++p]);)k-=(c==93)?1:(c==91)?-1:0;return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c==62?(s[++x]||=0):c==60?--x:c==43?s[x]++:c==45?--s[x]:c==46?o.push(s[x]):c==44?(s[x]=i.shift())?0:(p=b.length):c==93?(p=l.pop()-1):c==91?(s[x])?l.push(p):b:0)==b)for(k=1;k&&(c=b[++p]);)k-=int(c==93)-int(c==91);return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c==62?(s[++x]||=0):c==60?--x:c==43?s[x]++:c==45?--s[x]:c==46?o.push(s[x]):c==44?(s[x]=i.shift())?0:(p=-9):c==93?(p=l.pop()-1):c==91?(s[x])?l.push(p):b:0)==b)for(k=1;k&&(c=b[++p]);)k-=int(c==93)-int(c==91);return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c==62?(s[++x]||=0):c==60?--x:c==43?s[x]++:c==45?--s[x]:c==46?o.push(s[x]):c==44?(s[x]=i.shift())?0:(p=-9):c==93?(p=l.pop()-1):c==91?(s[x])?l.push(p):b:0)==b)for(k=1;k&&(c=b[++p]);)k-=(c>90&&c<94)?c-92:0;return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c<44?s[x]++:c<45?(s[x]=i.shift())?0:(p=-9):c<46?--s[x]:c<47?o.push(s[x]):c<61?--x:c<63?(s[++x]||=0):c<92?(s[x])?l.push(p):b:c<94?(p=l.pop()-1):0)==b)for(k=1;k&&(c=b[++p]);)k-=(c>90&&c<94)?c-92:0;return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c<44?s[x]++:c<45?(s[x]=i.shift())?0:(p=-9):c<46?--s[x]:c<47?o.push(s[x]):c<61?--x:c<63?(s[++x]||=0):c<92?(s[x])?l.push(p):b:(p=l.pop()-1))==b)for(k=1;k&&(c=b[++p]);)k-=(c>90&&c<94)?c-92:0;return o}
//function $(b,i){for(var s=[0],x=0,o=[],l=[],c,k,p=0;c=b[p];p++)if((c-43?c-44?c-45?c-46?c-60?c-62?c-91?(p=l.pop()-1):(s[x])?l.push(p):b:(s[++x]||=0):--x:o.push(s[x]):--s[x]:(s[x]=i.shift())?0:(p=-9):s[x]++)==b)for(k=1;k&&(c=b[++p]);)k-=c-93?c-91?0:-1:1;return o}

// my brainf*ck interpreter (currently in 242 letters) ----------------------------------------------------------------------------------------------------
function $(b,i){with(i){for(s=[p=k=0],o=[];c=b[p];p++){s[p]|=0,t=1,c&32&&c&16?k+=(c&3)-1:c&1?s[k]+=(c&3)-2:c&2?o.push(s[k]):s[k]=shift();if(c&64&&(c=(c&3)-2)>0?!s[k]:s[k])for(q=p;n=b[q+=c];)if(n&64&&!(t+=((n&3)-2)*c)&&(p=q+c))break}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;
    }
}