-
Notifications
You must be signed in to change notification settings - Fork 0
/
REPL.py
146 lines (101 loc) · 4.56 KB
/
REPL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''
Grupo Dacary:
Gregory Muñoz 16-11313
Daniela Ramirez 16-10940
Giancarlo Dente 15-10395
'''
import VM
# Funcion mensajeLexer
def mensajeLexer(data: str, arrayTokens: list, arrayErrores: list) -> str:
if (len(arrayErrores) > 0):
return f"ERROR: caracter inválido ({arrayErrores[0]}) en la entrada"
else:
return f'OK: lex("{data}") ==> {arrayTokens}'
# Funcion load
def load (data: str, arrayTuplas: list) -> str:
mensajeLoad = ""
try:
data = data[5:].strip()
file1 = open(data, "r")
except:
return f"ERROR: archivo no encontrado."
Lines = file1.readlines()
nombreArchivo = data
numline = 0
for line in Lines:
numline += 1
# Ignoramos las lineas en blanco o espacios en blanco o tabulaciones
if not line.isspace():
data = line.strip()
# Archivo contiene .lex en la linea numline
if data.startswith('.lex'):
data, arrayTokens, arrayErrores = VM.lexTest(data)
mensaje = mensajeLexer(data, arrayTokens, arrayErrores)
mensajeLoad = mensajeLoad + f"{mensaje}\n"
# Archivo contiene otros nombres de archivos dentro
elif data.startswith('.load'):
mensaje = load(data, arrayTuplas)
mensajeLoad = mensajeLoad + f"{mensaje}\n"
# Archivo contiene .failed en la linea numline
elif data.startswith('.failed'):
mensajeLoad = mensajeLoad + f"{failed(arrayTuplas)}\n"
# Archivo contiene .reset en la linea numline
elif data.startswith('.reset'):
reset(arrayTuplas)
# Archivo contiene .ast en la linea numline
elif data.startswith('.ast'):
mensaje = VM.testParser(data)
mensajeLoad = mensajeLoad + f"{mensaje}\n"
else:
mensaje = VM.process(data)
mensajeLoad = mensajeLoad + f"{mensaje}\n"
# # Si no se ingresa alguno de los comandos especificados se devuelve ERROR
# elif not data.startswith('.lex') or not data.startswith('.load') or not data.startswith('.failed') or not data.startswith('.reset') or not data.startswith('.ast'):
# arrayTuplas.append((nombreArchivo, numline, f"ERROR: interpretación no implementada"))
# mensajeLoad = mensajeLoad + f"ERROR: interpretación no implementada\n"
# Si la respuesta da ERROR se guarda el nombre del archivo, la linea y el mensaje en una lista de tuplas
if mensaje.startswith('ERROR:'):
arrayTuplas.append((nombreArchivo, numline, mensaje))
file1.close()
return mensajeLoad[:len(mensajeLoad)-1] # return mensajeLoad[:len(mensajeLoad)-1] para no incluir ultima linea en blanco
# Funcion failed
def failed (arrayTuplas: list) -> str:
msjFailed = f"[\n"
if len(arrayTuplas) > 0:
for i in range(0, len(arrayTuplas)-1):
msjFailed = msjFailed + f"\t{arrayTuplas[i]},\n"
msjFailed = msjFailed + f"\t{arrayTuplas[len(arrayTuplas)-1]}\n"
msjFailed = msjFailed + f"]"
return msjFailed
# Funcion reset
def reset(arrayTuplas: list) -> None:
for i in range(0, len(arrayTuplas)):
arrayTuplas.pop()
# Variables Globales
arrayTuplas = []
if __name__ == '__main__':
while True:
data = input("<Dacary> ").strip()
if data == '.':
break
elif data == "":
mensajeVM = ""
elif data.startswith('.lex'):
data, arrayTokens, arrayErrores = VM.lexTest(data)
mensajeVM = mensajeLexer(data, arrayTokens, arrayErrores)
elif data.startswith('.load'):
mensajeVM = load(data, arrayTuplas)
elif data.startswith('.failed'):
mensajeVM = failed(arrayTuplas)
elif data.startswith('.reset'):
reset(arrayTuplas)
continue
elif data.startswith('.ast'):
mensajeVM = VM.testParser(data)
elif data == ".ts":
mensajeVM = f"Tabla de Simbolos = {VM.ts_global}"
elif data == ".rv":
mensajeVM = f"Representacion de Variables = {VM.rv_global}"
else:
mensajeVM = VM.process(data)
print(mensajeVM)