Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Pseudo-Brainfuck interpreter  (Read 714 times)

MoonyTheHuman

  • Bay Watcher
  • I think the DEC VAX hates me.
    • View Profile
    • hellomouse
Pseudo-Brainfuck interpreter
« on: April 23, 2016, 10:23:16 am »

*This code needs to be compiled*
*This code is under a CC license*
Code: [Select]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// initialize the tape with 30,000 zeroes
unsigned char tape[30000] = {0};

// set the pointer to point at the left-most cell of the tape
unsigned char* ptr = tape;


void interpret(char* input) {
    char current_char;
    size_t i;
    size_t loop;

    for (i = 0; input[i] != 0; i++) {
        current_char = input[i];
        if (current_char == '>') {
            ++ptr;
        } else if (current_char == '<') {
            --ptr;
        } else if (current_char == '+') {
            ++*ptr;
        } else if (current_char == '-') {
            --*ptr;
        } else if (current_char == '.' ) {
            putchar(*ptr);
        } else if (current_char == ',') {
            *ptr = getchar();
        } else if (current_char == '[') {
            continue;
        } else if (current_char == ']' && *ptr) {
            loop = 1;
            while (loop > 0) {
                current_char = input[--i];
                if (current_char == '[') {
                    loop--;
                } else if (current_char == ']') {
                    loop++;
                }
            }
        }
    }
}


int main() {
    char* input[10000];
    scanf("%s",input);
    interpret(input);  // outputs input
    return 0;
}
Compile and run (=

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Pseudo-Brainfuck interpreter
« Reply #1 on: April 23, 2016, 02:01:41 pm »

There seems to be no provision to keep people from running off the ends of the tape?
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

ChairmanPoo

  • Bay Watcher
  • Send in the clowns
    • View Profile
Re: Pseudo-Brainfuck interpreter
« Reply #2 on: April 23, 2016, 02:25:48 pm »

What does it do
Logged
Everyone sucks at everything. Until they don't. Not sucking is a product of time invested.

MoonyTheHuman

  • Bay Watcher
  • I think the DEC VAX hates me.
    • View Profile
    • hellomouse
Re: Pseudo-Brainfuck interpreter
« Reply #3 on: April 23, 2016, 09:23:59 pm »

There seems to be no provision to keep people from running off the ends of the tape?
this design is quite basic, you can add it yourself, if you wish

Dozebôm Lolumzalìs

  • Bay Watcher
  • what even is truth
    • View Profile
    • test
Re: Pseudo-Brainfuck interpreter
« Reply #4 on: April 24, 2016, 07:39:01 pm »

What does it do

It's a coding language. Extremely minimalistic, and basically machine code. Brainfuck, so called because it's incredibly hard to read, about as hard as binary.
Logged
Quote from: King James Programming
...Simplification leaves us with the black extra-cosmic gulfs it throws open before our frenzied eyes...
Quote from: Salvané Descocrates
The only difference between me and a fool is that I know that I know only that I think, therefore I am.
Sigtext!

MoonyTheHuman

  • Bay Watcher
  • I think the DEC VAX hates me.
    • View Profile
    • hellomouse
Re: Pseudo-Brainfuck interpreter
« Reply #5 on: April 26, 2016, 07:41:30 pm »

What does it do

It's a coding language. Extremely minimalistic, and basically machine code. Brainfuck, so called because it's incredibly hard to read, about as hard as binary.
what he said Chairman