chevron_left chevron_right
Login Register invert_colors photo_library
Thread Rating:
  • 0 Vote(s) - 0 Average


filter_list How to make a Brainfuck: Part 3, implementing I/O and loops
Author
Message
How to make a Brainfuck: Part 3, implementing I/O and loops #1
How to make a Brainfuck: Implementing I/O and loops


In this tutorial, we'll be implementing the following operations:
  • [
  • ]
  • .
  • ,

You should have read the last part of this series, which can be found here: https://sinister.ly/Thread-Tutorial-How-...tions-in-C

How is this going to work?

We'll have a stack that we can push the current position to the code to, and that we can pop from.  The way that we will do this, is we will have an array, that we can find the

Lets begin:

As I said earlier, we'll go over dynamic memory allocation in a later thread.  For now, we'll have another array, also 256 items long, which we'll name data; along with that, we'll have an int named index to go with it.
Code:
struct stack
{
   int data[255];
   int index;
}
And, we'll initialize it as foo
Code:
struct stack foo; //I can't come up with name

Let's set index to 0:
Code:
foo.index = 0;
Now we need some functions to act on the structure. We'll need a push() function, and a pop() function.
Code:
int push(int value);
int pop();

Pop pushes a value to the next unfilled item on the stack, and pop will remove the last filled value, and return it. (Google can explain stacks better, try that)  Lets implement these. Try and do it yourself, then check against this:
Spoiler: code
Code:
int push(int value)
{
   if (index > 255)
   {
        return -1
    }
    foo.data[foo.index++] = value;
    return 0;
}
int pop() {
    if (index == 0)
   {
       return -1
   }
    foo.data[--foo.index] = 0;
    return 0
}

Cool, now we have a basic stack to work with.

Go back to your interpret function, and add add case statements for the rest of the instructions.
The '[' instruction should push x to foo, the ']' instruction should pop from foo into x (x = pop()), , should get a single character from the user and put it into *cell, and ,  should output the value of *cell.
I like to prompt to the user with a '>' for input, otherwise they won't know when to give input.

Write the code yourself, in the end it should look like this:
Spoiler: code
Code:
      switch(instruction)
      {
          //...
          case '[':
              push(x);
              break;
          case ']':
              x = pop();
              break;
          case '.':
              putchar(*cell);
              break;
          case ',':
              puts("\n\n > ");
              *cell = getchar();
              break;
      }

And... That's all!

Conclusion:

I hope you enjoyed this, don't forget to post below with comments or questions!   Biggrin

The next, and last thread in the C part of this series will be about fixing some code issues, and adding extensions to the language.  After that, I'll remake all of this in assembly, and then using logic gates xD.
(This post was last modified: 12-07-2017, 02:22 AM by Ender.)
+++++++++[>++++++++++>>++++++++++<<<-]>+>>+++<<<++++[>>++++++++++>>++++++++++>++++++++++<<<<<-]>>+++++>>+++>++++++[<<<<<,[>>>>.<<<<-]>>>>>.<<<<.>.>.>>]

[Image: duwskq.gif]

Reply






Users browsing this thread: 1 Guest(s)