PDA

View Full Version : a whole Brainf*** Compiler



McGiver
January 15th, 2006, 05:37 PM
I guess I really fathom kirupa's rules, but I wanted to do this for a very long time:

A compiler/interpreter in Flash! :jailbreak

the Language is Brainf uck (BF, and I hope the mod's don't sue me for that name: I didn't invent it)


Believe it or not this language is indeed Turing complete!
Combines the speed of BASIC with the ease of INTERCAL and
the readability of an IOCCC entry!
(but forget about the speed thing - seriously: a lot worse with my compiler)

since the Forum's swearing blocker blocked any attempt to post a link to anything containing BF, I suggest you to google for
Brainf uck wikipedia

here a list of the commands

Character Meaning
>
increment the pointer (to point to the next cell to the right).
<
decrement the pointer (to point to the next cell to the left).
+
increment (increase by one) the byte at the pointer.
-
decrement (decrease by one) the byte at the pointer.
.
output the value of the byte at the pointer.
,
accept one byte of input, storing its value in the byte at the pointer.
[
jump forward to the command after the corresponding ] if the byte at the pointer is zero.
]
jump back to the command after the corresponding [ if the byte at the pointer is nonzero.

I hope you will be abled to keep the commands ;)


so here it comes:

for (i=(a = ["code (press Shift+Enter to compile)","input", "output","dynamic", "input","input", "status","dynamic"]).length-1;i>0;i-=2) {
_root.createTextField(a[i-1]+"title", 10+i+1, 10, 240*(Math.log((i-1)/2+1))+10, 500, 20).text = a[i-1];
_root.createTextField(a[i-1], 10+i, 10, 240*(Math.log((i-1)/2+1))+30, 500, 85/((i-1)/2+0.6)).type =a[i];
_root[a[i-1]].border = true;
}

_root.onEnterFrame = function() {
((run == ((_root[a[0]].multiline=1)-(_root[a[2]].multiline=1)) || run == undefined) && (Key.isDown(Key.SHIFT) && Key.isDown(Key.ENTER))) ? pocket=[(n=20)-20, (maxIterations=10000)-10000, awaitingInput=0, (run=1)-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] : 1;
if (run && awaitingInput == 1) {
_root[a[6]].text = "awaiting input";
((Key.isDown(Key.ENTER) && _root[a[4]].text.length>0)) ? inputstack=_root[a[4]].text : 1;
((Key.isDown(Key.ENTER) && _root[a[4]].text.length>0)) ? _root[a[4]].text="" : 1;
(inputstack.length>0) ? pocket[n]=inputstack.charCodeAt(0) : 1;
(inputstack.length>0) ? awaitingInput=2 : 1;
(inputstack.length>0) ? inputstack=inputstack.substring(1) : 1;
} else {
for (((awaitingInput == 0) && (run == 1) && (Key.isDown(Key.SHIFT) && Key.isDown(Key.ENTER))) ? _root[a[(i=0)+2]].text="" : 1; (run == 1) && (maxIterations>0) && (i<_root[a[0]].text.length) && ((awaitingInput == 2) || (awaitingInput == 0 && (Key.isDown(Key.SHIFT) && Key.isDown(Key.ENTER)))); i++) {
(_root[a[0]].text.charAt(i) == ">") ? n++ : loops=0;
(_root[a[0]].text.charAt(i) == "<") ? n-- : loops=0;
(_root[a[0]].text.charAt(i) == "+") ? pocket[n]++ : 0;
(_root[a[0]].text.charAt(i) == "-") ? pocket[n]-- : 0;
(_root[a[0]].text.charAt(i) == ",") ? awaitingInput=1 : _root[a[3]].text="compiling";
(_root[a[0]].text.charAt(i) == ".") ? _root[a[2]].text += String.fromCharCode(pocket[n]) : _root[a[6]].text="compiling";
for (maxIterations--; (maxIterations>0) && (pocket[n] != 0) && (_root[a[0]].text.charAt(i) == "]" || loops>0) && (i>=0); i--) {
(_root[a[0]].text.charAt(i) == "]") ? loops++ : 1;
(_root[a[0]].text.charAt(i) == "[") ? loops-- : 1;
}
(maxIterations>0) ? _root[a[6]].text="finished" : _root[a[6]].text="safety quit, more than "+10000+" iterations";
}
(i == _root[a[0]].text.length || !(maxIterations>0)) ? run=0 : 1;
}
};



I hope this is valid :)

try to use this link:

-> http://www.digitalhalo.de/kirupa/bfc_18.html<-


see also
->sharks (http://www.kirupa.com/forum/showthread.php?t=205743)
->flower chain (http://www.kirupa.com/forum/showthread.php?t=205347)

the attached is a little slow, and seems to have problems with shift+enter :(

McGiver
January 15th, 2006, 05:54 PM
not to forget:
a Hello World Program

>+++++++++[<++++++++>-]<.---.+++++++..+++.>++++++++[<------>-]<+.>+++++++++[<++
++++>-]<+.--------.+++.------.--------.>+++++++[<----->-]<.[-]++++++++++.

sed|thh
January 15th, 2006, 06:04 PM
(-: lol this is so cool

bombsledder
January 15th, 2006, 07:04 PM
i dont get it :puzzled:

ElectricGrandpa
January 15th, 2006, 07:06 PM
Other than the dumb name(although I understand you have to give credit where credit is due), it's pretty sweet.

bombsledder
January 15th, 2006, 07:10 PM
i get it now but it takes to long to write stuff lol XD tight though

Sammo
January 15th, 2006, 07:28 PM
http://tinyurl.com/4f6mt

a tinyURL for the BF wiki article

kookaburra
January 16th, 2006, 01:09 AM
smart work.. :alien2:

hybrid101
January 16th, 2006, 06:40 AM
how do you use this? i don't get it...it seems pretty cool though!

McGiver
January 16th, 2006, 07:54 AM
well you enter some brai**** code (i.e. copy the hello world thing) in the code textfield, and compile it with SHIFT+ENTER


,++. for example would read an input, increase its value by 2 and print it. (if you enter "a", it would print out "c")