HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Brainfuck with C# operator overloading

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
brainfuckoverloadingwithoperator

Problem

I overloaded some C# operators to get a pseudo version of the Brainfuck symbols in the effort of understanding the flow of this language.

I got this version:

``
void Main()
{
/*
* Write
Hello World!`
* using operators overloading for brainfuck symbols
*/
var _ = new brainfuck();

_ = + + + + + + + + + + _ ; // set cell #0 to 10

while (_)
{
_ = + + + + + + + (_ > 1) ; // add 7 to #1
_ = + + + + + + + + + + (_ > 1) ; // add 10 to #2
_ = + + + (_ > 1) ; // add 3 to #3
_ = + (_ > 1) ; // add 1 to #4

_ = - (_ 1) ; // write 'H'
_ = ~ + (_ > 1) ; // write 'e'
_ = ~ + + + + + + + _ ; //write 'l'
_ = ~ _ ; //write 'l'
_ = ~ + + + _ ; // write 'o'
_ = ~ + + (_ > 1) ; // write ' '
_ = ~ + + + + + + + + + + + + + + + (_ 1) ; // write 'o'
_ = ~ + + + _ ; // write 'r'
_ = ~ - - - - - - _ ; // write 'l'
_ = ~ - - - - - - - - _ ; // write 'd'
_ = ~ + (_ > 1) ; // write '!'
_ = ~ (_ > 1) ; // write '\n'
}
public class brainfuck
{
private List cells;
private int currentCell;
public brainfuck()
{
cells = new List{0};
}

public static brainfuck operator + (brainfuck bf)
{
bf.plus();
return bf;
}
public static brainfuck operator - (brainfuck bf)
{
bf.minus();
return bf;
}
public static brainfuck operator

Solution

You could get slightly closer with the [] command pair.

It's actually quite trivial:

Add an indexer to your brainfuck class (public void this[Action bf]) and put your while loop in there.

The indexer would look like:

public brainfuck this[Action action]
{
    get
    {
        while (this)
        {
            action.Invoke();
        }

        return this;
    }
}


You would call it as:

_ = _[() => {
    _ =     + + + + +    + +       (_ > 1)              ; // add 7 to #1
    _ =     + + + + +    + + + + + (_ > 1)              ; // add 10 to #2
    _ =     + + +                  (_ > 1)              ; // add 3 to #3
    _ =     +                      (_ > 1)              ; // add 1 to #4

    _ =     -                      (_ < 1 < 1 < 1 < 1)  ; // decrement 0
}];


Is it a pain in the rear to use? Yes. Is it in the Brainfuck spirit? Yes.

It's still not true brainfuck syntax, but you won't get there with C#.

After that, you have a real mess here that needs cleaned.

First: capitalization. In C# we PascalCase public members and types (and all methods), and camelCase private and local members and types.

public class Brainfuck
private bool HasData()


Since "Brainfuck" is one word, the "F" in "fuck" is not capitalized.

The optional argument defaults in operator > and operator < are not at all going to work, remove them.

public static brainfuck operator > (brainfuck bf, int pos) 
{
    bf.right();
    return bf;
}
public static brainfuck operator < (brainfuck bf, int pos) 
{
    bf.left();
    return bf;
}


Always use braces, even when you don't need them.

private void Right()
{
    if (currentCell + 1 == cells.Count)
    {
        cells.Add(0);
    }

    currentCell++;
}


Use int instead of Int32 everywhere.

int value = int.TryParse(input, out value) ? value : 0;


Once we apply all these changes, fix some spacing, we end up with a not-so-bad looking class:

public class Brainfuck
{
    private List cells;
    private int currentCell;

    public Brainfuck()
    {
        cells = new List{0};
    }

    public Brainfuck this[Action action]
    {
        get
        {
            while (this)
            {
                action.Invoke();
            }

            return this;
        }
    }

    public static Brainfuck operator + (Brainfuck bf) 
    {
        bf.Plus();
        return bf;
    }

    public static Brainfuck operator - (Brainfuck bf) 
    {
        bf.Minus();
        return bf;
    }

    public static Brainfuck operator > (Brainfuck bf, int pos) 
    {
        bf.Right();
        return bf;
    }

    public static Brainfuck operator  0)
        {
            currentCell--;
        }
    }

    private int Current()
    {
        return cells[currentCell];
    }

    private void Write()
    {
        Console.Write((char)Current());
    }

    private void Read()
    {
        var input = Console.ReadLine();
        int value = int.TryParse(input, out value) ? value : 0;
        Current(value);
    }

    private bool HasData()
    {
        return Current() > 0;
    }

    private void Current(int value)
    {
        cells[currentCell] = value;
    }

    public override string ToString()
    {
        return String.Join(",",cells);
    }
}


And our code for the "Hello World" looks like:

void Main()
{
    /*
    *   Write `Hello World!`
    *           using operators overloading for brainfuck symbols
    */
    var _ = new Brainfuck();

    _ = + + + + +    + + + + +     _        ; // set cell #0 to 10

    _ = _[() => {
        _ =     + + + + +    + +       (_ > 1)              ; // add 7 to #1
        _ =     + + + + +    + + + + + (_ > 1)              ; // add 10 to #2
        _ =     + + +                  (_ > 1)              ; // add 3 to #3
        _ =     +                      (_ > 1)              ; // add 1 to #4

        _ =     -                      (_  1)              ; // write 'H'
    _ =     ~   +                      (_ > 1)              ; // write 'e'
    _ =     ~   + + + + +    + +        _                   ; //write 'l'
    _ =     ~                           _                   ; //write 'l'
    _ =     ~   + + +                   _                   ; // write 'o'
    _ =     ~   + +                     (_ > 1)             ; // write ' '
    _ =     ~   + + + + +    + + + + +   + + + + +      (_  1)             ; // write 'o'
    _ =     ~   + + +                   _               ; // write 'r'
    _ =     ~   - - - - -    -          _               ; // write 'l'
    _ =     ~   - - - - -    - - -      _               ; // write 'd'
    _ =     ~   +                       (_ > 1)         ; // write '!'
    _ =     ~                           (_ > 1)         ; // write '\n'
}


Overall, not too bad. I wouldn't have done it, but that's because I wrote a Brainfuck to C# transpiler. Good work. :)

Code Snippets

public brainfuck this[Action action]
{
    get
    {
        while (this)
        {
            action.Invoke();
        }

        return this;
    }
}
_ = _[() => {
    _ =     + + + + +    + +       (_ > 1)              ; // add 7 to #1
    _ =     + + + + +    + + + + + (_ > 1)              ; // add 10 to #2
    _ =     + + +                  (_ > 1)              ; // add 3 to #3
    _ =     +                      (_ > 1)              ; // add 1 to #4

    _ =     -                      (_ < 1 < 1 < 1 < 1)  ; // decrement 0
}];
public class Brainfuck
private bool HasData()
public static brainfuck operator > (brainfuck bf, int pos) 
{
    bf.right();
    return bf;
}
public static brainfuck operator < (brainfuck bf, int pos) 
{
    bf.left();
    return bf;
}
private void Right()
{
    if (currentCell + 1 == cells.Count)
    {
        cells.Add(0);
    }

    currentCell++;
}

Context

StackExchange Code Review Q#139971, answer score: 6

Revisions (0)

No revisions yet.