|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
3 | 3 | #include <string.h> |
| 4 | +#define LEN 80000 |
4 | 5 |
|
| 6 | +// Calcula estimacion del inicio del bucle |
| 7 | +int loop(char src[], int pos); |
| 8 | + |
| 9 | +// Busca errores en el codigo |
| 10 | +// void checkErrors(char src[], char args[]); |
| 11 | + |
| 12 | +// Interpretador |
5 | 13 | int main(int argc, char *argv[]){ |
6 | 14 |
|
7 | | - // Check if all the needed arguments are provided |
8 | | - if (argc != 2){ |
9 | | - fputs("usage: ./brainfuck [path/to/file.bf]\n", stderr); |
| 15 | + // Comprueba que los argumentos sean los correctos |
| 16 | + if (argc < 2 || argc > 3){ |
| 17 | + fputs("usage: ./brainfuck [path/to/file.bf] [args]\n", stderr); |
10 | 18 | return 1; |
11 | 19 | } |
12 | 20 |
|
13 | | - // Get the source code from the file provided and print it |
14 | | - char source[LENGTH]=""; |
| 21 | + // Obtener el codigo fuente del archivo e imprimirlo |
| 22 | + char source[LEN]=""; |
15 | 23 | FILE *file = fopen(argv[1], "r"); |
16 | | - printf("Source code: %s\n", fgets(source, LENGTH, file)); |
| 24 | + if (file == NULL){ |
| 25 | + printf("\n\nFile \"%s\" was not found.\n", argv[1]); |
| 26 | + exit(1); |
| 27 | + } |
| 28 | + printf("\nSource code:\n %s", fgets(source, LEN, file)); |
| 29 | + fclose(file); |
| 30 | + |
| 31 | + // Obtiene los argumentos |
| 32 | + if (argc == 3){ |
| 33 | + char args = *argv[1]; |
| 34 | + printf("\n\nArguments: %s", args); |
| 35 | + } |
| 36 | + |
| 37 | + puts("\n\n"); |
17 | 38 |
|
| 39 | + // Busca errores simples (no es 100% necesario para el funcionamiento del programa) |
| 40 | + // FIXME: |
| 41 | + // checkErrors(source, args); |
18 | 42 |
|
| 43 | + puts("\n\nCode returned 0"); |
19 | 44 | return 0; |
20 | 45 | } |
| 46 | + |
| 47 | +int loop(char src[], int pos){ |
| 48 | + int endLoop; |
| 49 | + char item; |
| 50 | + for(;;){ |
| 51 | + item = src[--pos]; |
| 52 | + if (item == ']'){ |
| 53 | + endLoop++; |
| 54 | + } else if (item == '[' && endLoop != 0){ |
| 55 | + endLoop--; |
| 56 | + } else if (item == '[' && endLoop == 0){ |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + return pos; |
| 61 | +} |
| 62 | + |
| 63 | +// FIXME: Saltan errores que no deberan |
| 64 | +/*void checkErrors(char src[], char args[]){ |
| 65 | + int lpStat, clStat, getInp, err; |
| 66 | + for (int i; i != strlen(src); i++){ |
| 67 | + switch(src[i]){ |
| 68 | + case('['): |
| 69 | + lpStat++; |
| 70 | +
|
| 71 | + case(']'): |
| 72 | + lpStat--; |
| 73 | +
|
| 74 | + case('>'): |
| 75 | + clStat++; |
| 76 | +
|
| 77 | + case('<'): |
| 78 | + clStat--; |
| 79 | +
|
| 80 | + case(','): |
| 81 | + getInp++; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (lpStat != 0){ |
| 86 | + err += 1; |
| 87 | + } |
| 88 | +
|
| 89 | + if (clStat < 0){ |
| 90 | + err += 2; |
| 91 | + } |
| 92 | +
|
| 93 | + // Si no hay argumentos pero el programa pregunta por ellos o si no hay suficientes argumentos |
| 94 | + if (strlen(*argv) == 3){ |
| 95 | + if (strlen(*argv) < getInp){ |
| 96 | + err += 4; |
| 97 | + } |
| 98 | + } else if (getInp != 0){ |
| 99 | + err += 4; |
| 100 | + } |
| 101 | +
|
| 102 | + if (err > 0){ |
| 103 | + printf("\n\nCode returned %i\n", err); |
| 104 | + exit(err); |
| 105 | + } |
| 106 | +}*/ |
0 commit comments