brain-labs / brain

An esoteric programming language compiler on top of LLVM based on Brainfuck
https://brain-labs.github.io/brain/docs/html/index.html
GNU General Public License v3.0
131 stars 18 forks source link

Nested if statements #37

Closed sunjay closed 7 years ago

sunjay commented 7 years ago

? if the value at the data pointer is zero , jumps to the block with : or ; and executes the commands one by one up to its correlative ;, otherwise, it executes the code until it finds a : or ;.

Do you support code like this?

+?>+?>>:<<;:<;

Expanded:

+?
    >+?
        >>
        :
        <<
    ;
    :
    <
;

Are you able to figure out which : to jump to?

luizperes commented 7 years ago

Hi @sunjay, It is completely possible to write code such as the snippet above. The code below was taken from a working system, the BrainStation(https://github.com/brain-labs/brainstation).

setup
>>>>>>>?:++; we will only have two sprites at cell 7
build values for distant cells for later jump
>?<<<<<:++++++++>+*>+*>+++++++*<--*<---*<<<<<<;

?->>>>>>^>>>+<<<<^:<<; if btn DOWN add Y of human object
?->>>>>>>>^>>>-<<<<^; if btn UP subtract Y of human object

player
>>>>>>>>^?:++; make number two if cell 41 is empty
<<?:++++++;>> go to cell 38 and make it 6 if empty
>+% add 1 to cell 42 and take it mod 2 because of the sprite number
<<?:+;^ go back to cell 1

monster 1
>>>>>>>>>^?:++; go to cell 48 and make number two if cell 48 is empty
>+%++ add 1 to cell 49 and take it mod 2 and add 2 because of monster sprite
>?--:++>_%<++++++++++++; if cell 16 is equal to 0 then we reset it to 15

?: if cell 50 is equal to zero
   > go to cell 51
   ? check if it is different from zero
     <<<<<<<< go to cell 43
     ?<<<<<^?:+;; if cell 43 different from zero then game over
   : if cell 51 equal to zero
     <<<<<<<< go to cell 43
     ?:<<<<<^?:+;; if cell equal to zero then game over
   ; 
;

You should also be able to find other examples here: https://github.com/brain-labs/brain/tree/master/tests

Brain also has it's own visualizer for fast tests purposes here: https://brain-labs.github.io/brain-visualizer/

Thank you

luizperes commented 7 years ago
?+++++++?;; nested if

Nested ifs(without elses) are also possible