private void RemoverDeclaracaoLocal(ComandosNo no) { foreach (var item in no.Comandos) { if (item is DeclaracaoNo) { string id = (item as DeclaracaoNo).Identificador.Valor; if (Variaveis.ContainsKey(id)) { Variaveis.Remove(id); VariaveisTipo.Remove(id); } } } }
public object GetAtribuicao(Token identificador, ArvoreNo expressao) { object result; if (Variaveis.ContainsKey(identificador.Valor)) { result = expressao.GetValor(this); Variaveis[identificador.Valor] = result; if (result.GetType() == typeof(int)) { if (VariaveisTipo[identificador.Valor] != "int") { throw new SemanticoException("Tipo errado foi atribuído a variável"); } return(Convert.ToInt32(result)); } else if (result.GetType() == typeof(float)) { if (VariaveisTipo[identificador.Valor] != "float") { throw new SemanticoException("Tipo errado foi atribuído a variável"); } return(float.Parse(result.ToString(), CultureInfo.InvariantCulture.NumberFormat)); } else { if (VariaveisTipo[identificador.Valor] != "char") { throw new SemanticoException("Tipo errado foi atribuído a variável"); } return(Convert.ToChar(result)); } } else { throw new SemanticoException($"Variável {identificador.Valor} não existe."); } }
public object GetFator(Token fator) { if (fator.Tipo == TipoToken.Identificador) { if (Variaveis.ContainsKey(fator.Valor)) { if (VariaveisTipo[fator.Valor] == "int") { return(Convert.ToInt32(Variaveis[fator.Valor])); } else if (VariaveisTipo[fator.Valor] == "float") { return(Convert.ToSingle(Variaveis[fator.Valor])); } else { return(Convert.ToChar(Variaveis[fator.Valor])); } } else { throw new SemanticoException($"Variável {fator.Valor} não existe."); } } else if (fator.Tipo == TipoToken.Literal) { return(Convert.ToChar(fator.Valor)); } else if (fator.Tipo == TipoToken.NumeroDecimal) { float result = float.Parse(fator.Valor, CultureInfo.InvariantCulture.NumberFormat); return(result); } else { return(Convert.ToInt32(fator.Valor)); } }