Skip to content

Commit 97c7e7a

Browse files
committed
Added more error detection & documentation
1 parent 719c168 commit 97c7e7a

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ Every character other than those is (hopefully) ignored.
3232

3333
Why i did it? Because I was bored. Why does this language exists in the first place? I guess someone felt the same way ~15 years before me.
3434

35+
# ERRORS
36+
If it doesn't work, depending on the exit it can be the following:
37+
0 - No problems on the code, have you checked that it's well written?
38+
1 - There is an unfinished loop.
39+
2 - You are trying to access a negative cell position.
40+
3 - 1 & 2.
41+
4 - Not enough input or not input at all.
42+
5 - 1 & 4
43+
6 - 2 & 4
44+
7 - 1 & 2 & 4
45+
3546
# TODO
3647
[x] Make it to interpret brainfuck(only in the python version atm).
3748
[ ] Make it to COMPILE brainfuck.

main.py

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
print(f"\n\nUsage: {argv[0]} [path/to/file.bf] [args]\n")
1414
exit(1)
1515

16-
16+
# Devuelve una estimacion del inicio del bucle
1717
def 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"\nCode returned error {err}.")
60+
print(f"\nCode 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'
10580
if len(argv) == 3:
10681
stdin = argv[2]
@@ -112,7 +87,7 @@ def getCells(source):
11287
print(f"\nCodigo 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.
11893
prgPos = 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\nCode returned 0")
138+
exit(0)

test/ioTest.bf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
,.
1+
,.,.

0 commit comments

Comments
 (0)