We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › BrainFu
Page Index Toggle Pages: 1
BrainFu (Read 810 times)
BrainFu
Apr 13th, 2009, 11:18pm
 
An "esoteric programming language". Search for that in your favorite engine. I don't know if the admins would like it if I posted a direct link.

BrainFu (think mental gymnastics) is not it's true name but that is what I will call it on this forum.


It is a minimalistic programming language/script/headache inducer.

I wanted to do this to learn flow control better for another project.

Uses the characters "<>+-[].," as instructions. Ignores anything else. Instead of using variables, etc. it only performs operations on memory.

I have not implemented user input: the ',' character.

Edit: does nested loops. Edit again: just because it looks like it works, doesn't mean it does...

Code:

int[] ram = new int[30000];


///*
//Division example
ram[0] = '8';//preset values
ram[1] = '2';//results in 8/2

String ops =
">>++++++[-<--------<-------->>] Store 2 numbers from keyboard in (0) and (1); and subtract 48 from each"+
"<<[ This is the main loop which continues until the dividend in (0) is zero"+
">[->+>+<<] Destructively copy the divisor from (1) to (2) and (3); setting (1) to zero"+
">[-<<- Subtract the divisor in (2) from the dividend in (0); the difference is stored in (0) and (2) is cleared"+
"[>]>>>[<[>>>-<<<[-]]>>]<<] If the dividend in (0) is zero; exit the loop"+
">>>+ Add one to the quotient in (5)"+
"<<[-<<+>>] Destructively copy the divisor in (3) to (1)"+
"<<<] Move the stack pointer to (0) and go back to the start of the main loop"+
">[-]>>>>[-<<<<<+>>>>>] Destructively copy the quotient in (5) to (0) (not necessary; but cleaner)"+
"<<<<++++++[-<++++++++>]<.";
//*/


//Hello world from Wikipedia
//String ops = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";


int instrpntr = 0;
int rampntr = 0;
int bracketcount = 0;
int letr = '0';


for (/*loop var declared above*/; instrpntr<ops.length(); instrpntr++) {

letr = ops.charAt(instrpntr);

switch (letr) {
case '>':
rampntr++;
break;

case '<':
rampntr--;
break;

case '+':
ram[rampntr]++;
break;

case '-':
ram[rampntr]--;
break;

case '.':
println(char(ram[rampntr]));
break;

case ',':
//get input
break;

case '[':
if (ram[rampntr] == 0) {
int bdepth = 0;

for (int b=instrpntr+1; b<ops.length(); b++) {

if (ops.charAt(b) == '[') {
bdepth++;

} else if (ops.charAt(b) == ']') {
if (bdepth > 0) {
bdepth--;
} else {
instrpntr = b;
break;
}

}
}
}
if (instrpntr == -1) {
println("No matching bracket!");
}
break;

case ']':
if (ram[rampntr] != 0) {
int bdepth = 0;

for (int b=instrpntr-1; b>=0; b--) {

if (ops.charAt(b) == ']') {
bdepth++;

} else if (ops.charAt(b) == '[') {
if (bdepth > 0) {
bdepth--;
} else {
instrpntr = b;
break;
}

}
}
}
break;

default:
break;

}

}

//println(ram);
Re: BrainFu
Reply #1 - Apr 14th, 2009, 12:05pm
 
I believe the classic name for this would be "turing machine"... :)
Re: BrainFu
Reply #2 - Apr 14th, 2009, 1:57pm
 
That it is. :]
Page Index Toggle Pages: 1