Ethgoin commited on
Commit
4cfc876
verified
1 Parent(s): 26b5c29

Update semantico.py

Browse files
Files changed (1) hide show
  1. semantico.py +18 -4
semantico.py CHANGED
@@ -14,10 +14,22 @@ class AnalizadorSemantico:
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(
@@ -46,7 +58,9 @@ class AnalizadorSemantico:
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":
 
14
 
15
  def analizar_instruccion(self, nodo):
16
  tipo = nodo["type"]
17
+ if tipo == "declaration":
18
+ var = nodo["var"]
19
+ dtype = nodo["datatype"]
20
+ if var in self.tabla_simbolos:
21
+ self.errores.append(f"Variable '{var}' ya fue declarada.")
22
+ else:
23
+ self.tabla_simbolos[var] = dtype
24
+ elif tipo == "assign":
25
+ if nodo["var"] not in self.tabla_simbolos:
26
+ self.errores.append(f"Variable '{nodo['var']}' usada sin declarar.")
27
+ return
28
  tipo_valor = self.analizar_expresion(nodo["value"])
29
+ tipo_var = self.tabla_simbolos[nodo["var"]]
30
+ if tipo_valor != tipo_var and tipo_valor != "error":
31
+ self.errores.append(f"Tipo incompatible en asignaci贸n a '{nodo['var']}': {tipo_var} = {tipo_valor}")
32
+ elif tipo in ("if", "while"):
33
  tipo_cond = self.analizar_expresion(nodo["condition"])
34
  if tipo_cond != "boolean":
35
  self.errores.append(
 
58
  if tipo_izq != tipo_der:
59
  self.errores.append(f"Tipos incompatibles: {tipo_izq} y {tipo_der}")
60
  return "error"
61
+ if expr["op"] in ("EQUAL", "NOT_EQUAL", "GREATER", "LESS"):
62
+ return "boolean"
63
+ return tipo_izq
64
  elif tipo == "bool":
65
  return "boolean"
66
  elif tipo == "string":