commit bb6c9f7ad675eeb1212d53aa76e5d37a2f0063a8 Author: Guilleag01 Date: Thu Feb 24 17:15:45 2022 +0100 Si diff --git a/brainfuck.c b/brainfuck.c new file mode 100644 index 0000000..8e25c09 --- /dev/null +++ b/brainfuck.c @@ -0,0 +1,83 @@ +#include +#include +#include + +#define SIZE 3000000 + +void run(char *input); +void printList(); + +char *cinta; +int main(){ + cinta = (char*)malloc(SIZE); + char fileName[50]; + printf("Introduce el nombre del archivo: "); + scanf("%s", fileName); + char *input = (char*)malloc(sizeof(char)); + FILE *file; + file = fopen(fileName, "r"); + int i=0; + while(!feof(file)){ + input[i++] = fgetc(file); + input = realloc(input, strlen(input) + 1); + } + fclose(file); + run(input); + printf("\n"); + return 0; +} + +void run(char *input){ + int loopDepth = 0; + for (int i=0; i': + cinta++; + break; + case '<': + cinta--; + break; + case '+': + ++*cinta; + break; + case '-': + --*cinta; + break; + case '.': + putchar(*cinta); + break; + case ',': + getchar(); + *cinta = getchar(); + case '[': + if (*cinta == 0){ + int depth = loopDepth; + depth++; + while(depth > loopDepth){ + i++; + c = input[i]; + if (c == '[') + depth++; + else if(c == ']') + depth--; + } + } + break; + case ']': + if (*cinta){ + int depth = loopDepth; + depth++; + while(depth > loopDepth){ + i--; + c = input[i]; + if (c == ']') + depth++; + else if(c == '[') + depth--; + } + } + break; + } + } +} diff --git a/brainfuck.out b/brainfuck.out new file mode 100755 index 0000000..8bf9918 Binary files /dev/null and b/brainfuck.out differ