Skip to main content
added 3 characters in body
Source Link
// should read from file - easy with node.js
// this is the `Hello World! ` program from wikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",

// declare a and b. If a needs to be longer, can use:
//     fora=[];for(a=[];a0;a.length<3000;alength<30000;a.push(0))b=0
a=[0,0,0,0,0],b=0

// evaluate
eval(
  // the brainfuck code
  p
  // replacing all the non brainfuck commands with nothing 
  .replace(/[^\][.,+><-]/g,'')
  // replacing all commands (captured in parenthesis) with callback
  .replace(/(.)/g,function(e){
     // return swapped commands
     return "0while(a[b]){0}0console.log(String.fromCharCode(a[b]))0a[b]=prompt()0++a[b]0--a[b]0++b0--b"
     // split into array on the 0 (used as seperator - shorter than "|" when
     // called in .split(0) function)
     .split(0)[
       // matching brainfuck commands
       " [].,+-><"
       // searched with escaped, captured command
       .search(new RegExp("\\"+e))
       // add a semicolon to all statements - extra semicolons do not interfere
       // with execution of javascript
     ]+";"
  })
)
// should read from file - easy with node.js
// this is the `Hello World! ` program from wikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",

// declare a and b. If a needs to be longer, can use:
//     for(a=[];a.length<3000;a.push(0))b=0
a=[0,0,0,0,0],b=0

// evaluate
eval(
  // the brainfuck code
  p
  // replacing all the non brainfuck commands with nothing 
  .replace(/[^\][.,+><-]/g,'')
  // replacing all commands (captured in parenthesis) with callback
  .replace(/(.)/g,function(e){
     // return swapped commands
     return "0while(a[b]){0}0console.log(String.fromCharCode(a[b]))0a[b]=prompt()0++a[b]0--a[b]0++b0--b"
     // split into array on the 0 (used as seperator - shorter than "|" when
     // called in .split(0) function)
     .split(0)[
       // matching brainfuck commands
       " [].,+-><"
       // searched with escaped, captured command
       .search(new RegExp("\\"+e))
       // add a semicolon to all statements - extra semicolons do not interfere
       // with execution of javascript
     ]+";"
  })
)
// should read from file - easy with node.js
// this is the `Hello World! ` program from wikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",

// declare a and b. If a needs to be longer, can use:
//     a=[];for(0;a.length<30000;a.push(0))b=0
a=[0,0,0,0,0],b=0

// evaluate
eval(
  // the brainfuck code
  p
  // replacing all the non brainfuck commands with nothing 
  .replace(/[^\][.,+><-]/g,'')
  // replacing all commands (captured in parenthesis) with callback
  .replace(/(.)/g,function(e){
     // return swapped commands
     return "0while(a[b]){0}0console.log(String.fromCharCode(a[b]))0a[b]=prompt()0++a[b]0--a[b]0++b0--b"
     // split into array on the 0 (used as seperator - shorter than "|" when
     // called in .split(0) function)
     .split(0)[
       // matching brainfuck commands
       " [].,+-><"
       // searched with escaped, captured command
       .search(new RegExp("\\"+e))
       // add a semicolon to all statements - extra semicolons do not interfere
       // with execution of javascript
     ]+";"
  })
)
deleted 218 characters in body
Source Link

JavaScript - Partial Solution (247241 235)

// not included in 247235 charscount, the hello world code from wikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",

// 247 chars of javascript interprets hello world,partial butsolution trips- updies on primes
var a=[0,0,0,0,0],b=0
x="}0whileeval(a[b]p.replace(/[^\][.,+><-]/g,'').replace(/(.)/g,function(e){0a[b]=promptreturn "0while(a[b])0a[b]++0console{0}0console.log(String.fromCharCode(a[b]))0a[b]-0a[b]=prompt()0++a[b]0-0b++0b-a[b]0++b0-0]0[0,0+0.0-0>0<"b".split(0)
p=p.replace(/[^\][[" [].,+><+-]/g,'')
for(i=0;i<8;i++)
p=p><".replacesearch(new RegExp("\\"+x[i+8],'g'"\\"+e),x[i]+';')
eval(p]+";"}))
// thisshould isread thefrom hellofile world- programeasy fromwith wikipedianode.js
// this should be readis fromthe a`Hello file,World! at` leastprogram infrom node.jswikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."

// 247 chars of javascript interprets hello world, but trips up on primes 

// initialisingdeclare a as array of 5 0's - notand sureb. whyIf ita needs 5to andbe notlonger, more/lesscan use:
// also not sure if it will trip up on other scripts - maybe with more recursion?for(a=[];a.length<3000;a.push(0))b=0
var a = [0a=[0,0,0,0,0], b =b=0

// 0evaluate
eval(
  // stringthe ofbrainfuck commandscode
 and valuesp
 to be// replacedreplacing seperatedall bythe 0,non becausebrainfuck itcommands saveswith anothing 
  .replace(/[^\][.,+><-]/ coupleg,'')
 of chars// inreplacing theall splitcommands -(captured soin .split(0parenthesis) ratherwith thancallback
  .splitreplace(";")
/(.)/g,function(e){
 the brainfuck commands are at// offsetreturn +8swapped incommands
 the resulting array
x = "}0whilereturn "0while(a[b]){0a[b]=prompt()0a[b]++0console0}0console.log(String.fromCharCode(a[b]))0a[b]0a[b]=prompt()0++a[b]0--0b++0ba[b]0++b0--0]0[0,0+0.0-0>0<".split(0)
b"
     // replacesplit allinto nonarray brainfuckon commandsthe with0 empty(used string.as Commandsseperator have- beenshorter arrangedthan so"|" thatwhen
// "-" is at the end, so// doescalled notin need.split(0) escapingfunction)
p = p   .replacesplit(0)[
       /[^\][/ matching brainfuck commands
       " [].,+><+-]/g,'')
><"
// loop through the 8 commands and replace// thesearched with escaped, brainfuckcaptured command 
 globally with the replacement   .search(new RegExp("\\"+e))
       // javascript, andadd a semicolon lineto terminator.all Thisstatements adds- extra semi-colonssemicolons todo thenot javascriptinterfere
 code, 
// but does not interfere // with execution
for(i=0;i<8;i++) of javascript
  p=p.replace(new RegExp("\\"+x[i+8],'g'),x[i]+';')

// evaluate the]+";"
 generated code})
eval(p)

JavaScript - Partial Solution (247)

// not included in 247 chars
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."

// 247 chars of javascript interprets hello world, but trips up on primes
var a=[0,0,0,0,0],b=0
x="}0while(a[b]){0a[b]=prompt()0a[b]++0console.log(String.fromCharCode(a[b]))0a[b]--0b++0b--0]0[0,0+0.0-0>0<".split(0)
p=p.replace(/[^\][.,+><-]/g,'')
for(i=0;i<8;i++)
p=p.replace(new RegExp("\\"+x[i+8],'g'),x[i]+';')
eval(p)
// this is the hello world program from wikipedia
// this should be read from a file, at least in node.js
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."

// 247 chars of javascript interprets hello world, but trips up on primes
// initialising a as array of 5 0's - not sure why it needs 5 and not more/less
// also not sure if it will trip up on other scripts - maybe with more recursion?
var a = [0,0,0,0,0], b = 0

// string of commands and values to be replaced seperated by 0, because it saves a
// couple of chars in the split - so .split(0) rather than .split(";")
// the brainfuck commands are at offset +8 in the resulting array
x = "}0while(a[b]){0a[b]=prompt()0a[b]++0console.log(String.fromCharCode(a[b]))0a[b]--0b++0b--0]0[0,0+0.0-0>0<".split(0)

// replace all non brainfuck commands with empty string. Commands have been arranged so that
// "-" is at the end, so does not need escaping
p = p.replace(/[^\][.,+><-]/g,'')

// loop through the 8 commands and replace the escaped brainfuck command globally with the replacement
// javascript, and a semicolon line terminator. This adds extra semi-colons to the javascript code, 
// but does not interfere with execution
for(i=0;i<8;i++)
  p=p.replace(new RegExp("\\"+x[i+8],'g'),x[i]+';')

// evaluate the generated code
eval(p)

JavaScript - Partial Solution (241 235)

// not included in 235 count, the hello world code from wikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",

// partial solution - dies on primes
a=[0,0,0,0,0],b=0
eval(p.replace(/[^\][.,+><-]/g,'').replace(/(.)/g,function(e){return "0while(a[b]){0}0console.log(String.fromCharCode(a[b]))0a[b]=prompt()0++a[b]0--a[b]0++b0--b".split(0)[" [].,+-><".search(new RegExp("\\"+e))]+";"}))
// should read from file - easy with node.js
// this is the `Hello World! ` program from wikipedia
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.", 

// declare a and b. If a needs to be longer, can use:
//     for(a=[];a.length<3000;a.push(0))b=0
a=[0,0,0,0,0],b=0

// evaluate
eval(
  // the brainfuck code
  p
  // replacing all the non brainfuck commands with nothing 
  .replace(/[^\][.,+><-]/g,'')
  // replacing all commands (captured in parenthesis) with callback
  .replace(/(.)/g,function(e){
     // return swapped commands
     return "0while(a[b]){0}0console.log(String.fromCharCode(a[b]))0a[b]=prompt()0++a[b]0--a[b]0++b0--b"
     // split into array on the 0 (used as seperator - shorter than "|" when
     // called in .split(0) function)
     .split(0)[
       // matching brainfuck commands
       " [].,+-><"
       // searched with escaped, captured command 
       .search(new RegExp("\\"+e))
       // add a semicolon to all statements - extra semicolons do not interfere
       // with execution of javascript
     ]+";"
  })
)
Source Link

JavaScript - Partial Solution (247)

Does not read from file - does not manage PRIMES.BF, but works for Hello World!

// not included in 247 chars
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."

// 247 chars of javascript interprets hello world, but trips up on primes
var a=[0,0,0,0,0],b=0
x="}0while(a[b]){0a[b]=prompt()0a[b]++0console.log(String.fromCharCode(a[b]))0a[b]--0b++0b--0]0[0,0+0.0-0>0<".split(0)
p=p.replace(/[^\][.,+><-]/g,'')
for(i=0;i<8;i++)
p=p.replace(new RegExp("\\"+x[i+8],'g'),x[i]+';')
eval(p)

Just copy and paste it into javascript console to see it in action. Works in node.js, or broswer.

I was hoping to get PRIMES.BF to work in node.js, but not been able to emulate STDIN in a synchronous way yet.


With comments

// this is the hello world program from wikipedia
// this should be read from a file, at least in node.js
var p="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."

// 247 chars of javascript interprets hello world, but trips up on primes
// initialising a as array of 5 0's - not sure why it needs 5 and not more/less
// also not sure if it will trip up on other scripts - maybe with more recursion?
var a = [0,0,0,0,0], b = 0

// string of commands and values to be replaced seperated by 0, because it saves a
// couple of chars in the split - so .split(0) rather than .split(";")
// the brainfuck commands are at offset +8 in the resulting array
x = "}0while(a[b]){0a[b]=prompt()0a[b]++0console.log(String.fromCharCode(a[b]))0a[b]--0b++0b--0]0[0,0+0.0-0>0<".split(0)

// replace all non brainfuck commands with empty string. Commands have been arranged so that
// "-" is at the end, so does not need escaping
p = p.replace(/[^\][.,+><-]/g,'')

// loop through the 8 commands and replace the escaped brainfuck command globally with the replacement
// javascript, and a semicolon line terminator. This adds extra semi-colons to the javascript code, 
// but does not interfere with execution
for(i=0;i<8;i++)
  p=p.replace(new RegExp("\\"+x[i+8],'g'),x[i]+';')

// evaluate the generated code
eval(p)