Ethgoin commited on
Commit
f105117
verified
1 Parent(s): 0122fb5

Update semantico.py

Browse files
Files changed (1) hide show
  1. semantico.py +8 -9
semantico.py CHANGED
@@ -50,19 +50,20 @@ FUNCIONES_VALIDAS = {
50
  "WAIT": "Hace una pausa durante X tiempo"
51
  }
52
 
53
-
54
  class AnalizadorSemantico:
55
  def __init__(self, ast):
56
  self.ast = ast
57
  self.tabla_simbolos = {}
58
  self.errores = []
 
59
 
60
  def analizar(self):
61
  for nodo in self.ast:
62
  self.analizar_instruccion(nodo)
63
  return {
64
  "variables_declaradas": self.tabla_simbolos,
65
- "errores_semanticos": self.errores
 
66
  }
67
 
68
  def analizar_instruccion(self, nodo):
@@ -85,9 +86,7 @@ class AnalizadorSemantico:
85
  elif tipo in ("if", "while"):
86
  tipo_cond = self.analizar_expresion(nodo["condition"])
87
  if tipo_cond != "boolean":
88
- self.errores.append(
89
- f"La condici贸n de '{tipo}' debe ser tipo boolean, no '{tipo_cond}'"
90
- )
91
  for instr in nodo["body"]:
92
  self.analizar_instruccion(instr)
93
  elif tipo == "function":
@@ -129,7 +128,7 @@ class AnalizadorSemantico:
129
 
130
  descripcion = FUNCIONES_VALIDAS[nombre]
131
  self.anotaciones.append(f"{nombre}: {descripcion}")
132
-
133
  funciones_sin_argumento = {
134
  "ACTIVATE_ALARM", "ACTIVATE_SENSOR", "BREAK", "CHARGE_BATTERY", "CHECK_BATTERY",
135
  "CLOSE_DOOR", "CONTINUE", "DEACTIVATE_ALARM", "DEACTIVATE_SENSOR", "DECREASE_SPEED",
@@ -138,13 +137,13 @@ class AnalizadorSemantico:
138
  "TURN_LEFT", "TURN_RIGHT", "TURN_UP", "UNLOCK", "LOG", "INIT", "LOCK", "LOW_BATTERY",
139
  "OPEN_DOOR", "PAUSE"
140
  }
141
-
142
  funciones_con_argumento = {
143
  "CALIBRATE", "COPY_FILE", "DELETE_FILE", "MOVE_BACKWARD", "MOVE_FORWARD", "MOVE_TO",
144
  "PRINT", "RENAME_FILE", "ROTATE", "SAVE_FILE", "SCAN", "SET", "SET_SPEED", "UPLOAD",
145
  "UPLOAD_FILE", "WAIT"
146
  }
147
-
148
  if nombre in funciones_sin_argumento:
149
  if arg is not None:
150
  self.errores.append(f"La funci贸n '{nombre}' no debe tener argumentos.")
@@ -154,4 +153,4 @@ class AnalizadorSemantico:
154
  else:
155
  self.analizar_expresion(arg)
156
  else:
157
- self.errores.append(f"Funci贸n '{nombre}' no reconocida.")
 
50
  "WAIT": "Hace una pausa durante X tiempo"
51
  }
52
 
 
53
  class AnalizadorSemantico:
54
  def __init__(self, ast):
55
  self.ast = ast
56
  self.tabla_simbolos = {}
57
  self.errores = []
58
+ self.anotaciones = []
59
 
60
  def analizar(self):
61
  for nodo in self.ast:
62
  self.analizar_instruccion(nodo)
63
  return {
64
  "variables_declaradas": self.tabla_simbolos,
65
+ "errores_semanticos": self.errores,
66
+ "anotaciones": self.anotaciones
67
  }
68
 
69
  def analizar_instruccion(self, nodo):
 
86
  elif tipo in ("if", "while"):
87
  tipo_cond = self.analizar_expresion(nodo["condition"])
88
  if tipo_cond != "boolean":
89
+ self.errores.append(f"La condici贸n de '{tipo}' debe ser tipo boolean, no '{tipo_cond}'")
 
 
90
  for instr in nodo["body"]:
91
  self.analizar_instruccion(instr)
92
  elif tipo == "function":
 
128
 
129
  descripcion = FUNCIONES_VALIDAS[nombre]
130
  self.anotaciones.append(f"{nombre}: {descripcion}")
131
+
132
  funciones_sin_argumento = {
133
  "ACTIVATE_ALARM", "ACTIVATE_SENSOR", "BREAK", "CHARGE_BATTERY", "CHECK_BATTERY",
134
  "CLOSE_DOOR", "CONTINUE", "DEACTIVATE_ALARM", "DEACTIVATE_SENSOR", "DECREASE_SPEED",
 
137
  "TURN_LEFT", "TURN_RIGHT", "TURN_UP", "UNLOCK", "LOG", "INIT", "LOCK", "LOW_BATTERY",
138
  "OPEN_DOOR", "PAUSE"
139
  }
140
+
141
  funciones_con_argumento = {
142
  "CALIBRATE", "COPY_FILE", "DELETE_FILE", "MOVE_BACKWARD", "MOVE_FORWARD", "MOVE_TO",
143
  "PRINT", "RENAME_FILE", "ROTATE", "SAVE_FILE", "SCAN", "SET", "SET_SPEED", "UPLOAD",
144
  "UPLOAD_FILE", "WAIT"
145
  }
146
+
147
  if nombre in funciones_sin_argumento:
148
  if arg is not None:
149
  self.errores.append(f"La funci贸n '{nombre}' no debe tener argumentos.")
 
153
  else:
154
  self.analizar_expresion(arg)
155
  else:
156
+ self.errores.append(f"Funci贸n '{nombre}' no reconocida.")