Created
January 5, 2025 14:27
bv1.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define MAX_MEMORY_SIZE 10 | |
// NOTE: | |
// I had to print the output in stderr to not polute the /dev/stdin if i want to pipe | |
// to the actual intepreator : | |
// | |
// $ gcc ./bv.c -o ./bv | |
// $ ./bv ">>>+++<++<" | bf /dev/stdin | |
// memory blocks | |
// ------------- | |
// [0][0][2][3][0][0][0][0][0][0] | |
// ^ | |
// >>>+++<++< | |
#define MAX_STACK_SIZE 30 // Define a maximum stack size | |
void display_memory(int memory[], int mem_pointer, int size) { | |
fprintf(stderr, "\n-------------\nmemory blocks\n-------------\n"); | |
for (int i = 0; i < size; ++i) { | |
fprintf(stderr, "[%d]", memory[i]); | |
} | |
fprintf(stderr, "\n"); | |
// Display the pointer position | |
for (int i = 0; i < mem_pointer; ++i) { | |
fprintf(stderr, " "); | |
} | |
fprintf(stderr, "^\nmemory pointer\n--------------\n\n"); | |
} | |
typedef struct Stack { | |
int items[MAX_STACK_SIZE]; | |
int top; | |
} Stack; | |
void init_stack(Stack* s) { | |
s->top = -1; | |
} | |
int is_empty(Stack* s) { | |
return s->top == -1; | |
} | |
void push(Stack* s, int value) { | |
if (s->top >= MAX_STACK_SIZE - 1) { | |
fprintf(stderr, "Woups: Stack overflow\n"); | |
exit(1); | |
} | |
s->items[++s->top] = value; | |
} | |
int pop(Stack* s) { | |
if (!is_empty(s)) { | |
return s->items[s->top--]; | |
} | |
fprintf(stderr, "Woups: Stack underflow\n"); | |
exit(1); | |
} | |
int memory_shift_printer(const char *input) { | |
// printf("%s\n", input); // To output the Brainfuck code to stdout | |
int memory[MAX_MEMORY_SIZE] = {0}; | |
int mem_pointer = 0; | |
Stack stack; | |
init_stack(&stack); | |
// Iterate over each character in the input | |
for (size_t i = 0; i < strlen(input); ++i) { | |
if (input[i] == '>' && mem_pointer < MAX_MEMORY_SIZE - 1) { | |
mem_pointer++; | |
} else if (input[i] == '<' && mem_pointer > 0) { | |
mem_pointer--; | |
} else if (input[i] == '+') { | |
memory[mem_pointer]++; | |
} else if (input[i] == '-' && memory[mem_pointer] > 0) { | |
memory[mem_pointer]--; | |
} else if (input[i] == '[') { | |
if (memory[mem_pointer] != 0) { | |
push(&stack, i); | |
continue; | |
} | |
int loop = 1; | |
while (loop > 0) { | |
i++; | |
if (i == strlen(input)) { | |
fprintf(stderr, "Woups: Mismatched brackets\n"); | |
return 1; | |
} | |
if (input[i] == '[') loop++; | |
if (input[i] == ']') loop--; | |
} | |
} else if (input[i] == ']') { | |
if (memory[mem_pointer] == 0) { | |
pop(&stack); | |
} | |
if (is_empty(&stack)) { | |
fprintf(stderr, "Woups: Mismatched brackets\n"); | |
return 1; | |
} | |
// to be sure move to the command after the matching [ | |
i = stack.items[stack.top] - 1; | |
} | |
// for verbose purpose... uncomment this | |
// display_memory(memory, mem_pointer, MAX_MEMORY_SIZE); | |
} | |
// for verbose purpose... uncomment this | |
display_memory(memory, mem_pointer, MAX_MEMORY_SIZE); | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s <brainfuck_code>\n", argv[0]); | |
return 1; | |
} | |
char *input = argv[1]; | |
memory_shift_printer(input); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment