Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>A brainfuck interpreter in Javascript</title>
- <script type='text/javascript'>
- //first we create the brainfuck environment
- //empty array of 30000 elements
- var ar=new Array(30000);
- for(var n=0;n<30000;n++)
- {
- ar[n]=0;
- }
- //current location in array
- var pointer=0;
- //the text of the program we're running (for parsing)
- var prog="";
- //the current character in program we're running
- var run_index=0;
- //these are internal functions for some dynamic content using divs and such
- //input returns the entered text
- function input()
- {
- text=document.getElementById("input_area").value.substr(0,1);
- document.getElementById("input_area").value=text.substr(1);
- //return the ascii value of the text
- return text.charCodeAt();
- }
- function output(str)
- {
- document.getElementById("output_area").value+=str;
- }
- //okay, so these are functions for each of brainfuck's commands
- function inc_point() //>
- {
- if(pointer<29999)
- {
- pointer=pointer+1;
- }
- else
- {
- //I suppose I should give an error message
- output("Err: Pointer past end of array");
- run_index=prog.length; //stop program execution, this was a fatal error
- return;
- }
- }
- function dec_point() //<
- {
- if(pointer>0)
- {
- pointer=pointer-1;
- }
- else
- {
- //same deal as before
- output("Err: Pointer before start of array");
- run_index=prog.length; //stop program execution, this was a fatal error
- return;
- }
- }
- function inc_byte() //+
- {
- ar[pointer]=ar[pointer]+1;
- }
- function dec_byte() //-
- {
- ar[pointer]=ar[pointer]-1;
- }
- function out_byte() //.
- {
- output(String.fromCharCode(ar[pointer]));
- }
- function in_byte() //,
- {
- //input is made to accept one byte only
- ar[pointer]=input();
- }
- function jmp_f() //[
- {
- if(ar[pointer]==0)
- {
- while(prog.charAt(run_index)!="]")
- {
- if(run_index<prog.length)
- {
- run_index++;
- }
- else
- {
- output("Err: Program ends within a conditional");
- run_index=prog.length; //stop program execution, this was a fatal error
- return;
- }
- }
- //we didn't return, so we found the end of the conditional, handle that
- run_index++;
- }
- else
- {
- //else continue normal execution
- run_index++;
- }
- }
- function jmp_b() //]
- {
- if(ar[pointer]!=0)
- {
- while(prog.charAt(run_index)!="[")
- {
- if(run_index>0)
- {
- run_index--;
- }
- else
- {
- output("Err: No matching [ for a given ]");
- run_index=prog.length; //stop program execution, this was a fatal error
- return;
- }
- }
- //we didn't return, so we found the start of the conditional, handle that
- run_index++;
- }
- else
- {
- //else continue normal execution
- run_index++;
- }
- }
- //okay, now the parser
- function bf_eval(text)
- {
- //resets all defaults to ignore all previous programs' environments
- run_index=0;
- pointer=0;
- for(var n=0;n<30000;n++)
- {
- ar[n]=0;
- }
- prog=text;
- document.getElementById("output_area").value="";
- document.getElementById("input_area").value="";
- document.getElementById("code_area").value="";
- while(run_index<prog.length)
- {
- // alert(prog.charAt(run_index)); //debug
- switch(prog.charAt(run_index))
- {
- case ">":
- inc_point();
- run_index++;
- break;
- case "<":
- dec_point();
- run_index++;
- break;
- case "+":
- inc_byte();
- run_index++;
- break;
- case "-":
- dec_byte();
- run_index++;
- break;
- case ".":
- out_byte();
- run_index++;
- break;
- case ",":
- in_byte();
- run_index++;
- break;
- case "[":
- jmp_f();
- break;
- case "]":
- jmp_b();
- break;
- }
- }
- }
- </script>
- </head>
- <body>
- <form>
- <textarea rows="10" cols="80" id="code_area" ondblclick="bf_eval(this.value);"></textarea><br>
- <input id="input_area"></input><br>
- <textarea rows="10" cols="80" id="output_area"></textarea>
- </form>
- </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement