1313 print (f"\n \n Usage: { argv [0 ]} [path/to/file.bf] [args]\n " )
1414 exit (1 )
1515
16-
16+ # Devuelve una estimacion del inicio del bucle
1717def loop (src , pos ):
1818 endLoop = 0
1919 while 1 :
@@ -28,8 +28,8 @@ def loop(src, pos):
2828 return pos
2929
3030# Busca errores, como bucles incompletos.
31- def checkErrors (source ):
32- lpStat = clStat = err = 0
31+ def checkErrors (source , argv ):
32+ lpStat = clStat = getInp = err = 0
3333 for i in source :
3434 if i == "[" :
3535 lpStat += 1
@@ -39,34 +39,27 @@ def checkErrors(source):
3939 clStat += 1
4040 elif i == "<" :
4141 clStat -= 1
42+ elif i == "," :
43+ getInp += 1
44+
4245 if lpStat != 0 :
4346 err += 1
44- print ( " \n \t 001: UNFINISHED LOOP \n " )
47+
4548 if clStat < 0 :
4649 err += 2
47- print ("\n \t 002: NEGATIVE CELL POSITION \n " )
50+
51+ if len (argv ) < 3 and getInp != 0 :
52+ err += 4
53+ try :
54+ if len (argv [2 ]) < getInp :
55+ err += 4
56+ except :
57+ pass
4858
4959 if err != 0 :
50- print (f"\n Code returned error { err } ." )
60+ print (f"\n Code returned { err } ." )
5161 exit (err )
5262
53- # Version en C de checkErrors():
54- #
55- # void checkErrors(char source[]){
56- # int lpStatus, err;
57- # for(int i; i=<strlen(source); i++){
58- # if(source[i] == '['){
59- # lpStatus++;
60- # }else if(source[i] == ']'){
61- # lpStatus--;
62- # }
63- # }
64- # if(lpStatus != 0){
65- # err+=1;
66- # fputs("\n\t 001: UNFINISHED LOOP \n\n", stderr);
67- # }
68- # }
69-
7063
7164# Calcula el numero necesario de celdas
7265# para que el programa funcione correctamente
@@ -83,24 +76,6 @@ def getCells(source):
8376 return cells + 2
8477
8578
86- # Version en C de 'getCells()':
87- #
88- # int getCells(char source[]){
89- # int num, cells;
90- # for(int i; i=<strlen(source); i++){
91- # if(source[i] == '>'){
92- # num++;
93- # }else if(source[i] == '<'){
94- # num--;
95- # }
96- # if(num > cells){
97- # cells = num;
98- # }
99- # }
100- # return cells + 5;
101- # }
102-
103-
10479# Obtiene el input y se lo mete a 'stdin'
10580if len (argv ) == 3 :
10681 stdin = argv [2 ]
@@ -112,7 +87,7 @@ def getCells(source):
11287 print (f"\n Codigo fuente:\n { source } \n \n " )
11388
11489# Comprueba que no hayan errores es el codigo.
115- checkErrors (source )
90+ checkErrors (source , argv )
11691
11792# Variables del interpretador.
11893prgPos = cellPos = pArg = 0
@@ -130,6 +105,8 @@ def getCells(source):
130105 # para mayor facilidad de acceso.
131106 obj = source [prgPos ]
132107
108+ # TODO: Un def para seguir de forma estricta las reglas de brainfuck
109+
133110 # Comportamiento segun el valor de la instruccion.
134111 if obj == ">" :
135112 cellPos += 1
@@ -156,3 +133,6 @@ def getCells(source):
156133
157134 # Pasa a la siguiente instruccion.
158135 prgPos += 1
136+
137+ print ("\n \n Code returned 0" )
138+ exit (0 )
0 commit comments