public override void ValidSemantic() { if (tipo != null) { foreach (var stack in ContenidoStack.InstanceStack.Stack) { if (stack.VariableExist(value)) { throw new SintanticoException("variable " + value + " existe+ fila:" + _TOKEN.Fila + " columna " + _TOKEN.Columna); } } ; ContenidoStack.InstanceStack.Stack.Peek().DeclareVariable(value, obtenerTipo(tipo)); } else { if (ContenidoStack.InstanceStack.Stack.Peek()._variables.Count == 0) { throw new SintanticoException("variable " + value + " no existe" + _TOKEN.Fila + " columna " + _TOKEN.Columna); } bool existe = false; foreach (var stack in ContenidoStack.InstanceStack.Stack) { if (stack.VariableExist(value)) { existe = true; tipo = stack.GetVariable(value).ToString(); } } ; if (existe == false) { throw new SintanticoException("variable " + value + " no existe" + _TOKEN.Fila + " columna " + _TOKEN.Columna); } } TiposBases tipoAasignar = obtenerTipo(tipo); if (obtenerTipo(tipo) is ConstTipo) { throw new SintanticoException("variable " + value + " es constante no se puede asignar" + _TOKEN.Fila + " columna " + _TOKEN.Columna); } if (obtenerTipo(tipo) is ArrayTipo) { Type fieldsType = typeof(ArrayTipo); FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public | BindingFlags.Instance); var unidimensional = (bool)fields[1].GetValue(tipo); var bidimensional = (bool)fields[2].GetValue(tipo); var tipodeArray = (TiposBases)fields[0].GetValue(tipo); foreach (var accesores in Asesores) { if (accesores is LogicaStructNode) { throw new SintanticoException(value + "es un arreglo no una structura"); } if (accesores is PuntoNode) { throw new SintanticoException(value + "es un arreglo no una enum"); } accesores.ValidSemantic(); } if (bidimensional == true && Asesores.Count == 2) { tipoAasignar = tipodeArray; } if (bidimensional == true && Asesores.Count == 1) { tipoAasignar = new ArrayTipo(); } if (bidimensional == false && Asesores.Count > 1) { throw new SintanticoException("el elemento " + value + " no es un arreglo de mas de una dimension fila " + _TOKEN.Fila + " columna " + _TOKEN.Columna); } if (bidimensional == false && Asesores.Count == 1 && unidimensional == true) { tipoAasignar = tipodeArray; } if (bidimensional == false && Asesores.Count == 0 && unidimensional == true) { tipoAasignar = new ArrayTipo(); } if (bidimensional == false && Asesores.Count == 0 && unidimensional == false) { throw new SintanticoException("el elemento " + value + " no es un arreglo"); } if (bidimensional == true && Asesores.Count == 0) { tipoAasignar = new ArrayTipo(); } } if (obtenerTipo(tipo) is StructTipo) { var structAhora = obtenerTipo(tipo); Type fieldsType = typeof(StructTipo); FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public | BindingFlags.Instance); var enumeracion = (Dictionary <string, TiposBases>)fields[0].GetValue(structAhora); if (Asesores == null) { tipoAasignar = new StructTipo(); } foreach (var accesores in Asesores) { if (accesores is ArrayAsesorNode) { throw new SintanticoException(value + "es un struct no una arreglo"); } } if (Asesores.Count > 1) { throw new SintanticoException(value + " solo se puede acceder a un elemento de el struct"); } Type fieldsTypeAcessor = typeof(LogicaStructNode); FieldInfo[] fieldsAcessor = fieldsTypeAcessor.GetFields(BindingFlags.Public | BindingFlags.Instance); var elementoDestruct = fieldsAcessor[0].GetValue(Asesores[0]); var elementoId = elementoDestruct.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); var id = (string)elementoId[0].GetValue(elementoDestruct); var acesores = (List <AccesoresNode>)elementoId[2].GetValue(elementoDestruct); if (enumeracion.ContainsKey(id)) { var tipoDeElemento = enumeracion[id]; if (tipoDeElemento is ArrayTipo) { Type fieldsType1 = typeof(ArrayTipo); FieldInfo[] fields1 = fieldsType1.GetFields(BindingFlags.Public | BindingFlags.Instance); var unidimensional = (bool)fields1[1].GetValue(tipoDeElemento); var bidimensional = (bool)fields1[2].GetValue(tipoDeElemento); var tipodeArray = (TiposBases)fields1[0].GetValue(tipoDeElemento); if (bidimensional == true && acesores.Count == 2) { tipoAasignar = tipodeArray; } if (bidimensional == true && acesores.Count == 1) { tipoAasignar = new ArrayTipo(); } if (bidimensional == false && acesores.Count > 1) { throw new SintanticoException("el elemento no es un arreglo de mas de una dimension"); } if (bidimensional == false && acesores.Count == 1 && unidimensional == true) { tipoAasignar = tipodeArray; } if (bidimensional == false && acesores.Count == 0 && unidimensional == true) { tipoAasignar = new ArrayTipo(); } if (bidimensional == false && acesores.Count == 0 && unidimensional == false) { throw new SintanticoException("no es un arreglo "); } if (bidimensional == true && acesores.Count == 0) { tipoAasignar = new ArrayTipo(); } } else { tipoAasignar = tipoDeElemento; } } } if (inicializacion != null) { var inicial = inicializacion.ValidateSemantic(); if (tipoAasignar.GetType() != inicial.GetType()) { throw new SintanticoException(" no se puede asignar " + tipo + " con " + inicial); } } }
public override void ValidSemantic() { var existeIdentificador = false; Dictionary <string, TiposBases> elementosHeredados = new Dictionary <string, TiposBases>(); foreach (var stack in ContenidoStack.InstanceStack.Stack) { if (stack.VariableExist(variableNombre)) { throw new SintanticoException("variable " + variableNombre + " existe " + _TOKEN.Fila + " columnas " + _TOKEN.Columna); } } ; foreach (var stack in ContenidoStack.InstanceStack.Stack) { foreach (var tipo in stack._variables.ToList()) { if (tipo.Value is StructTipo) { Type fieldsType = typeof(StructTipo); FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public | BindingFlags.Instance); var identificadorNode = fields[0].GetValue(tipo.Value); if ((string)identificadorNode == identificador) { if (bloqueStruct != null) { throw new SintanticoException("variable " + identificador + " existe no se puede definir el struct " + _TOKEN.Fila + " columnas " + _TOKEN.Columna); } elementosHeredados = (Dictionary <string, TiposBases>)fields[1].GetValue(tipo.Value); existeIdentificador = true; } } } } ; if (bloqueStruct == null) { if (ContenidoStack.InstanceStack.Stack.ToList()[0]._variables.Count != 0) { if (existeIdentificador == false) { throw new SintanticoException("variable " + this.identificador + " no existe el struct " + _TOKEN.Fila + " columnas " + _TOKEN.Columna); } } } Dictionary <string, TiposBases> elementos = new Dictionary <string, TiposBases>(); if (bloqueStruct != null) { foreach (var lista in bloqueStruct) { if (lista is IdentificadorArrayNode) { Type fieldsType = typeof(IdentificadorArrayNode); FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public | BindingFlags.Instance); var identificadorNode = fields[0].GetValue(lista); var unidimesional = fields[1].GetValue(lista); var bidimesional = fields[2].GetValue(lista); var tipo = identificadorNode.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)[0].GetValue(identificadorNode); var identificador = identificadorNode.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)[2].GetValue(identificadorNode); var uniarray = (unidimesional != null)?true:false; var biarray = (bidimesional != null) ? true : false; var array = new ArrayTipo(); array.tipo = obtenerTipo((string)tipo); array.unidimensional = uniarray; array.bidimensional = biarray; elementos.Add((string)identificador, array); } if (lista is GeneralDeclarationNode) { Type fieldsType = typeof(GeneralDeclarationNode); FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public | BindingFlags.Instance); var Tipo = fields[0].GetValue(lista); var identificador = fields[2].GetValue(lista); elementos.Add((string)identificador, obtenerTipo((string)Tipo)); } } } var structTipo = new StructTipo(); if (elementosHeredados.Count == 0) { structTipo.elementos = elementos; } else { structTipo.elementos = elementosHeredados; } structTipo.identificadorStruct = this.identificador; ContenidoStack.InstanceStack.Stack.Peek()._variables.Add(variableNombre, structTipo); }