0

Can anyone help me to interpret command line arguments in Brainfuck program (if possible) as other languages like C read them? For example

gcc cmd.c arg1 arg2

I have installed the following interpreter on my Ubuntu machine-

bf - a Brainfuck interpreter version 20041219 (C) 2003, 2004, Stephan Beyer, GPL, s-beyer@gmx.net

Is the following possible traditionally or with some hack?

bf cmd.bf arg1 arg2

Zoe
  • 27,060
  • 21
  • 118
  • 148
Monti Chandra
  • 432
  • 5
  • 21

2 Answers2

0

You could do it like this:

echo arg1 arg2 | bf cmd.bf

Then, your cmd.bf will have to handle/interpret the input and process it.

erdal.karaca
  • 693
  • 1
  • 4
  • 20
  • hey erdal, Thank you for your response. Can you please give a sample to add two integer arguments by cmd.bf? For example- echo 1 1 | bf cmd.bf that will print "2" as output. – Monti Chandra Jul 15 '16 at 07:45
  • http://stackoverflow.com/questions/10670510/how-to-calculate-the-sum-of-2-numbers-with-brainfuck – erdal.karaca Jul 15 '16 at 20:44
0

I advise anyone getting started with BrainFuck should write the C implementation of the code you have in mind. To learn more https://en.wikipedia.org/wiki/Brainfuck

The C implementation of your question will look like this

#include <stdlib.h>
#include <stdio.h>

void main(void)
{
    char array[20];

    array[0] = 8;
    array[1] = 51;
    array[2] = 50;
    while (array[0] != 0)
    {
        array[1] -= 6;
        array[2] -= 6;
        array[0]--;
    }
    array[1]++;
    while (array[1] != 0)
    {
        while (array[2] != 0)
        {
            array[3]++;
            array[4]++;
            array[2]--;
        }
        while (array[3] != 0)
        {
            array[2]++;
            array[3]--;
        }
        array[1]--;
    }
    
    array[0] = 8;
    while (array[0] != 0)
    {
        array[4] += 6;
        array[0]--;
    }
    
    putchar(array[4]);

}

then you can move over to the BrainFuck code which is this

++++ ++++               Cell c0 = 8
>,                      Reads input and stores in c1
>,                    Reads input and stores in c2
<<                     Moves to c0
[
    >--- ---          Minus 6 from c1
    >--- ---          Minus 6 from c2
    <<-                  Minus 1 from c0
]

>                      Moves to c1
[   
    >                   Moves to c2
    [
        >+              Increment c3 by 1
        >+              Increment c4 by 1
        <<-             decrement c2 by 1
    ]
    >                   move to c3
    [
        <+              increment c2
        >-              decrement c3
    ]
    <<-                  decrement c1 by 1
]

<++++ ++++              cell c0 = 8
[
    >>>>+++ +++        increment c5 by 6
    <<<<-              decrement c0 by 1
]
>>>>.                  print c4

my suggestion any line you write in BrainFuck always comment what it's doing