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

BF interpreter written in C#

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

Problem

I have recently written a Brainfuck interpreter in C#. I tested it with the examples given on EsoLang website. It does not handle errors right now.

Questions:

  • Even that it can run only one program at once, should I maybe put the variables into the class (create new class) or just leave them inside the BF method?



  • Should I split all of the code from the cases and create separate methods for them, accessing them from the switch?



Of course, I am also looking for all other suggestions on how I can improve this code.

```
using System;

namespace BF
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Specify the source file!");
} else {
if (System.IO.File.Exists(args[0]) == true) {
BF(System.IO.File.ReadAllText(args[0]).ToCharArray());
} else {
Console.WriteLine("The path to the file is not valid!");
}
}

}

private static void BF(char[] instructions)
{
int instructionPointer = 0;

int[] memory = new int[30000];

int pointer = 1;

while (instructionPointer ': {
pointer += 1;
break;
}

case ' 0) {
if (instructions[ptr] == '[') {
s += 1;
} else if (instructions[ptr] == ']') {
s -= 1;
}
ptr += 1;
instructionPointer = ptr;
}
}
break;
}

case ']': {
if (memory[pointer] != 0) {

Solution

int pointer = 1;


Looks like you're throwing away memory[0] here; I'd expect the pointer to initialize at the very start of the "tape".

if (System.IO.File.Exists(args[0]) == true)


The == true part is redundant, and the fully-qualified File.Exists feels... crowded. I'd remove System.IO. and add using System.IO; at the top of the file.

Arguably if no argument is specified, you should be throwing some ArgumentException instead of just saying "specify the source file" and exiting with a code-0, which would be interpreted as a successful run. Such a guard clause would help reduce nesting in the Main procedure:

public static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            throw new ArgumentException("No source file was specified.");
        }
        if (!File.Exists(args[0]))
        {
            throw new FileNotFoundException("Specified source file was not found.");
        }

        BF(System.IO.File.ReadAllText(args[0]).ToCharArray());
    }


Notice the consistency with brace positioning; your code alternates between C#-style and Java-style braces, depending on whether we're looking at a member or its body - it really doesn't matter which one you prefer, but pick a style, and stick to it.

"BF" might perhaps possibly make a [bad] class name - it's a noun. But here BF is a method, and methods do something, they're verbs. Interpret or even Run would be a better choice.

BF is a simple language; you don't need to go out of your way and implement a lexer to tokenize input and a parser to make sense out of it - basically every single character is an instruction, so it makes sense to process it one character at a time.

But is it ideal to read an entire file's contents, iterate every single character to create a character array, and then pass that array to a method that will iterate it one item at a time? Seems rather inefficient, considering you could be streaming the file content into the interpreter, and finish interpreting the BF program as you finish reading the last character in the file, having iterated the contents exactly once.

I think I'd make a separate dedicated class for the interpreter, with the pointer and memory[] as instance fields; I'd make a Dictionary to map each BF token to a dedicated method, too.

Code Snippets

int pointer = 1;
if (System.IO.File.Exists(args[0]) == true)
public static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            throw new ArgumentException("No source file was specified.");
        }
        if (!File.Exists(args[0]))
        {
            throw new FileNotFoundException("Specified source file was not found.");
        }

        BF(System.IO.File.ReadAllText(args[0]).ToCharArray());
    }

Context

StackExchange Code Review Q#144835, answer score: 3

Revisions (0)

No revisions yet.