Talk:Bub
Spec Talk
The following is a direct quote from the Bub page in the Muriel archive:
Essentially, Bub is Brainf*ck using GOTOs instead of 'while' loops. A Bub program consists of a string of digits, and these are split into blocks of 'w' digits (w=4 in the Hello World program). In most instructions, all but the last digit can be ignored - the last digit corresponds to a Brainf*ck instruction, with 012345678 corresponding to <>+-,.[] and 'end of program' respectively. In the case of [ and ], the preceding digits signify the instruction number to jump to (counting from 0) if the jump takes place - in other words, when translating from Brainf*ck to Bub, the location of the corresponding bracket is calculated in advance.
For example, the program [-] could be translated as
0036 9993 0017 9998
The Muriel interpreter for Bub is the original Bub implementation. It verifies the following observations about the above "spec":
- Instructions ending in "6" and "7" are "jump if zero" and "jump if nonzero", respectively.
- A jump from a "6" instruction could jump directly to its corresponding "7" instruction or to the immediate instruction after the corresponding "7" instruction (since it is known that the "7" instruction will not branch). Likewise for "7" jumps.
- In fact, jumps of either style can point to any instruction either before or after the jump origination. They do not have to be paired with another jump.
- Thus, Bub is a more powerful language than brainfuck (at the expense of increased syntax). While brainfuck can be translated one-for-one into Bub, the reverse is not necessarily so.
--Nthern 16:17, 14 August 2007 (UTC)
A Bub-Specific Syntax
For my personal use, I kludged up a hybrid syntax for Bub using brainfuck instructions plus non-paired jumps. Here is my simple parser. Rules of my syntax are:
- Destination labels precede the destination command and are an alphanumeric sequence followed by a ":".
- The "jump-if-0" command is an alphanumeric sequence followed by an "@".
- The "jump-if-not-0" command is an alphanumeric sequence followed by an "!".
- The alphanumeric sequence preceding a "@" or "!" designates the destination label to jump to.
- A "$" character is a Bub "end-of-program" directive.
- "[" and "]" will pair up poperly, regardless of the extended syntax used between them.
- Although any characters that don't match the syntax will be ignored, all characters from a "#" to the end of the line will be stripped, so that is the safest way to insert comments.
Here is a direct translation of "Hello World!" off the brainfuck page using my syntax:
>+++++++++ a@ b: <++++++++>- b! a: <.>+++++++ c@ d: <++++>- d! c: <+.+++++++..+++. e@ f: - f! e: >++++++++ g@ h: <++++>- h! g: <.>+++++++++++ i@ j: <+++++>- j! i: <.>++++++++ k@ l: <+++>- l! k: <.+++.------.--------. m@ n: - n! m: >++++++++ o@ p: <++++>- p! o: <+. q@ r: - r! q: ++++++++++. $
Of course, half the jumps will never trigger, so the code can be reduced to:
>+++++++++ a: <++++++++>- a! <.>+++++++ b: <++++>- b! <+.+++++++..+++. c: - c! >++++++++ d: <++++>- d! <.>+++++++++++ e: <+++++>- e! <.>++++++++ f: <+++>- f! <.+++.------.--------. g: - g! >++++++++ h: <++++>- h! <+. i: - i! ++++++++++. $
--Nthern 19:19, 29 September 2008 (UTC)