Ethgoin commited on
Commit
ace0b6b
verified
1 Parent(s): 57aca7a

Update semantico.py

Browse files
Files changed (1) hide show
  1. semantico.py +43 -12
semantico.py CHANGED
@@ -13,34 +13,65 @@ class AnalizadorSemantico:
13
  }
14
 
15
  def analizar_instruccion(self, nodo):
16
- if nodo["type"] == "assign":
 
17
  tipo_valor = self.analizar_expresion(nodo["value"])
18
  self.tabla_simbolos[nodo["var"]] = tipo_valor
19
- elif nodo["type"] == "if":
20
- tipo_condicion = self.analizar_expresion(nodo["condition"])
21
- if tipo_condicion != "int":
22
  self.errores.append(
23
- f"Condici贸n del IF debe ser de tipo int, pero se encontr贸 '{tipo_condicion}'"
24
  )
25
  for instr in nodo["body"]:
26
  self.analizar_instruccion(instr)
 
 
 
 
27
 
28
  def analizar_expresion(self, expr):
29
- if expr["type"] == "num":
 
30
  return "float" if "." in expr["value"] else "int"
31
- elif expr["type"] == "var":
32
  nombre = expr["value"]
33
  if nombre not in self.tabla_simbolos:
34
- self.errores.append(f"Variable '{nombre}' usada sin ser declarada.")
35
  return "error"
36
  return self.tabla_simbolos[nombre]
37
- elif expr["type"] == "binop":
38
  tipo_izq = self.analizar_expresion(expr["left"])
39
  tipo_der = self.analizar_expresion(expr["right"])
40
  if tipo_izq != tipo_der:
41
  self.errores.append(f"Tipos incompatibles: {tipo_izq} y {tipo_der}")
42
  return "error"
43
- return tipo_izq
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  else:
45
- self.errores.append(f"Expresi贸n no reconocida: {expr}")
46
- return "error"
 
13
  }
14
 
15
  def analizar_instruccion(self, nodo):
16
+ tipo = nodo["type"]
17
+ if tipo == "assign":
18
  tipo_valor = self.analizar_expresion(nodo["value"])
19
  self.tabla_simbolos[nodo["var"]] = tipo_valor
20
+ elif tipo == "if" or tipo == "while":
21
+ tipo_cond = self.analizar_expresion(nodo["condition"])
22
+ if tipo_cond != "boolean":
23
  self.errores.append(
24
+ f"La condici贸n de '{tipo}' debe ser tipo boolean, no '{tipo_cond}'"
25
  )
26
  for instr in nodo["body"]:
27
  self.analizar_instruccion(instr)
28
+ elif tipo == "function":
29
+ self.validar_funcion(nodo["name"], nodo["arg"])
30
+ else:
31
+ self.errores.append(f"Instrucci贸n no reconocida: {nodo}")
32
 
33
  def analizar_expresion(self, expr):
34
+ tipo = expr["type"]
35
+ if tipo == "num":
36
  return "float" if "." in expr["value"] else "int"
37
+ elif tipo == "var":
38
  nombre = expr["value"]
39
  if nombre not in self.tabla_simbolos:
40
+ self.errores.append(f"Variable '{nombre}' usada sin declarar.")
41
  return "error"
42
  return self.tabla_simbolos[nombre]
43
+ elif tipo == "binop":
44
  tipo_izq = self.analizar_expresion(expr["left"])
45
  tipo_der = self.analizar_expresion(expr["right"])
46
  if tipo_izq != tipo_der:
47
  self.errores.append(f"Tipos incompatibles: {tipo_izq} y {tipo_der}")
48
  return "error"
49
+ return tipo_izq if expr["op"] not in ("EQUAL", "NOT_EQUAL", "GREATER", "LESS") else "boolean"
50
+ elif tipo == "bool":
51
+ return "boolean"
52
+ elif tipo == "string":
53
+ return "string"
54
+ else:
55
+ self.errores.append(f"Expresi贸n no v谩lida: {expr}")
56
+ return "error"
57
+
58
+ def validar_funcion(self, nombre, arg):
59
+ funciones_sin_argumento = {
60
+ "START", "REBOOT", "SHUTDOWN"
61
+ }
62
+ funciones_con_argumento = {
63
+ "MOVE_FORWARD", "MOVE_BACKWARD", "PRINT", "SET",
64
+ "TOGGLE_LIGHT", "CALIBRATE", "ROTATE", "WAIT",
65
+ "TURN_LEFT", "TURN_RIGHT", "ACTIVATE_SENSOR", "DEACTIVATE_SENSOR"
66
+ }
67
+
68
+ if nombre in funciones_sin_argumento:
69
+ if arg is not None:
70
+ self.errores.append(f"La funci贸n '{nombre}' no debe tener argumentos.")
71
+ elif nombre in funciones_con_argumento:
72
+ if arg is None:
73
+ self.errores.append(f"La funci贸n '{nombre}' requiere un argumento.")
74
+ else:
75
+ self.analizar_expresion(arg)
76
  else:
77
+ self.errores.append(f"Funci贸n '{nombre}' no reconocida.")