bfckr.c
/**
 * @brief       Parses and executes a brainfuck expression using a simple state machine
 * @type        static
 * @param[in]   Pointer to a bf_code_t type
 * @return      void
 **/
@brief       Parses and executes a brainfuck expression using a simple state machine
@type        static
@param[in]   Pointer to a bf_code_t type
@return      void
@brief Parses and executes a brainfuck expression using a simple state machine @type static @param [ in ] Pointer to a bf_code_t type @return void
static void bfuck_execute(bf_code_t *bf)
{
    int loop = 0;
    for(size_t i = bf->ip; bf->code[i] != 0; bf->ip=i++) { 
        if(bf->debug) {
            bfuck_debugger(bf);
        }
        switch(bf->code[i]) {
        case '>':
            if(bf->mp >= MEMORY_SIZE) { 
                bf->mp = 0;
            } else {
                ++(bf->mp); 
            }
            break;
        case '<':
            if(bf->mp < 0) { 
                bf->mp = MEMORY_SIZE-1;
            } else {
                --(bf->mp); 
            }
            break;
        case '+':
            ++(bf->memory[bf->mp]); 
            break;
        case '-':
            --(bf->memory[bf->mp]); 
            break;
        case '.':
            putchar(bf->memory[bf->mp]); 
            (n_output_buffer < MEMORY_SIZE) ? output_buffer[++n_output_buffer] = bf->memory[bf->mp] : 0; 
            break;
        case ',':
            bf->memory[bf->mp] = getchar(); 
            break;
        case '[':
            if(bf->memory[bf->mp] == 0) { 
                loop = 1;
                while(loop > 0) { 
                    bf->ip = i++; 
                    if(bf->code[i] == '[') {
                        loop++;
                    }
                    if(bf->code[i] == ']') {
                        loop--;
                    }
                }
            } else {
                continue; 
            }
            break;
        case ']':
            if(bf->memory[bf->mp] != 0) { 
                loop = 1;
                while(loop > 0) { 
                    bf->ip = i--; 
                    if(bf->code[i] == '[') {
                        loop--;
                    }
                    if(bf->code[i] == ']') {
                        loop++;
                    }
                }
            } else {
                continue; 
            }
            break;
        case '#':
            bf->debug = true;
            break;
        default:
            break;
        }
    }
}
static void bfuck_execute ( bf_code_t * bf ) { int loop = 0 ; for ( size_t i = bf -> ip ; bf -> code [ i ] != 0 ; bf -> ip = i ++ ) { if ( bf -> debug ) { bfuck_debugger ( bf ) ; } switch ( bf -> code [ i ] ) { case ' ' : if ( bf -> mp >= MEMORY_SIZE ) { bf -> mp = 0 ; } else { ++ ( bf -> mp ) ; } break ; case ' ' : if ( bf -> mp < 0 ) { bf -> mp = MEMORY_SIZE - 1 ; } else { -- ( bf -> mp ) ; } break ; case ' ' : ++ ( bf -> memory [ bf -> mp ] ) ; break ; case ' ' : -- ( bf -> memory [ bf -> mp ] ) ; break ; case ' ' : putchar ( bf -> memory [ bf -> mp ] ) ; ( n_output_buffer < MEMORY_SIZE ) ? output_buffer [ ++ n_output_buffer ] = bf -> memory [ bf -> mp ] : 0 ; break ; case ' ' : bf -> memory [ bf -> mp ] = getchar ( ) ; break ; case ' ' : if ( bf -> memory [ bf -> mp ] == 0 ) { loop = 1 ; while ( loop > 0 ) { bf -> ip = i ++ ; if ( bf -> code [ i ] == ' ' ) { loop ++ ; } if ( bf -> code [ i ] == ' ' ) { loop -- ; } } } else { continue ; } break ; case ' ' : if ( bf -> memory [ bf -> mp ] != 0 ) { loop = 1 ; while ( loop > 0 ) { bf -> ip = i -- ; if ( bf -> code [ i ] == ' ' ) { loop -- ; } if ( bf -> code [ i ] == ' ' ) { loop ++ ; } } } else { continue ; } break ; case ' ' : bf -> debug = true ; break ; default : break ; } } }
@brief       Parses and executes a brainfuck expression using a simple state machine
 @type        static
 @param[in]   Pointer to a bf_code_t type
 @return      void
@brief Parses and executes a brainfuck expression using a simple state machine @type static @param [ in ] Pointer to a bf_code_t type @return void
// where i is the instruction pointer // prevent overrun // increment memory pointer // prevent underun // decrement memory pointer // increment byte at memory pointer // decrement byte at memory pointer // output the byte at memory pointer // write to output buffer for debugging // accept one byte of input and store it at memory pointer // if the byte at the memory pointer is zero // jump forward to the command after the next ] // count nested loops and make sure to get the matching ] // save current instruction pointer and increment it // increment instruction pointer // if the byte at the memory pointer is nonzero // jump back to the command after the matching [ // count nested loops and make sure to get the matching [ // save current instruction pointer and increment it // increment instruction pointer // Breakpoint reached, start debugger ... // Do nothing