/// <summary> /// Searches for a type in the current scope or global /// </summary> /// <param name="typeName">type name</param> /// <param name="localType">out type</param> /// <returns>True if succeded, False otherwise</returns> public bool GetDefinedTypeShallow(string typeName, out SemanticInfo localType) { ///verificamos si está en el scope local if (currentScope.GetDefinedLocalType(typeName, out localType)) { return(true); } ///verificamos si es uno de los BuiltIn return(globalScope.GetDefinedLocalType(typeName, out localType)); }
/// <summary> /// Search for an existing type /// </summary> /// <param name="typeName">type name</param> /// <param name="localType">stores the type at this out parameter</param> /// <returns>True if the type exists, False otherwise</returns> public bool GetDefinedLocalType(string typeName, out SemanticInfo localType) { ///en caso de que no existe if (!declaredTypes.ContainsKey(typeName)) { localType = SemanticInfo.SemanticError; return(false); } ///lo buscamos en el diccionario localType = declaredTypes[typeName]; return(true); }
/// <summary> /// Search for an existing var or function /// </summary> /// <param name="varOrFunctionName">var or function name</param> /// <param name="localVarOrFunction">stores the var or function at this out parameter</param> /// <returns>True if var or function exists, False otherwise</returns> public bool GetDefinedLocalVarOrFunction(string varOrFunctionName, out SemanticInfo localVarOrFunction) { ///en caso de que no existe if (!declaredVarsAndFunctions.ContainsKey(varOrFunctionName)) { localVarOrFunction = SemanticInfo.SemanticError; return(false); } ///lo buscamos en el diccionario localVarOrFunction = declaredVarsAndFunctions[varOrFunctionName]; return(true); }
/// <summary> /// Declares a new variable or function in the current scope /// </summary> /// <param name="varOrFunction">new var or function</param> /// <returns></returns> public bool DefineLocalVarOrFunction(SemanticInfo varOrFunction) { SemanticInfo localVarOrFunction; ///si ya ha sido definida una variable o función con ese nombre if (GetDefinedLocalVarOrFunction(varOrFunction.Name, out localVarOrFunction)) { return(false); } ///lo agregamos a las variables y funciones declaradas declaredVarsAndFunctions.Add(varOrFunction.Name, varOrFunction); return(true); }
/// <summary> /// Declares a new type in the current scope /// </summary> /// <param name="type">new type</param> /// <returns>True if succeded, False otherwise</returns> public bool DefineLocalType(SemanticInfo type) { SemanticInfo localType; ///si ya ha sido definido un tipo con ese nombre if (GetDefinedLocalType(type.Name, out localType)) { return(false); } ///lo agregamos a los tipos declarados declaredTypes.Add(type.Name, type); return(true); }
/// <summary> /// Determines if two elements of type SemanticInfo are compatibles /// </summary> /// <param name="element">First SemanticInfo element</param> /// <param name="other">Second SemanticInfo element</param> /// <returns>True if the elements are compatible, False otherwise</returns> public static bool IsCompatibleWith(this SemanticInfo element, SemanticInfo other) { ///si ambos son arrays if (element.BuiltInType == BuiltInType.Array && other.BuiltInType == BuiltInType.Array) { return(Object.Equals(element.Type, other.Type)); } ///si ambos son record if (element.BuiltInType == BuiltInType.Record && other.BuiltInType == BuiltInType.Record) { return(Object.Equals(element.Type, other.Type)); } ///en caso contrario retornamos la compatibilidad de los tipos BuiltIn return(element.BuiltInType.IsCompatibleWith(other.BuiltInType)); }
/// <summary> /// Searches for a type in the entire program /// </summary> /// <param name="typeName">type name</param> /// <param name="type">out type</param> /// <returns>True if succeded, False otherwise</returns> public bool GetDefinedTypeDeep(string typeName, out SemanticInfo type) { Scope localScope = currentScope; while (localScope != null) { ///se encontró en el scope local if (localScope.GetDefinedLocalType(typeName, out type)) { return(true); } ///buscamos en el padre localScope = localScope.Outer; } ///no se encontró type = SemanticInfo.SemanticError; return(false); }
/// <summary> /// Searches for a variable in the current scope or global /// </summary> /// <param name="variableName">variable name</param> /// <param name="localVariable">out variable</param> /// <returns>True if succeded, False otherwise</returns> public bool GetDefinedVariableShallow(string variableName, out SemanticInfo localVariable) { ///verificamos si está en el scope local if (currentScope.GetDefinedLocalVarOrFunction(variableName, out localVariable)) { ///si lo encontrado fue una variable if (localVariable.ElementKind == SymbolKind.Variable) { return(true); } else { localVariable = SemanticInfo.SemanticError; return(false); } } ///verificamos si es una de las variables globales(Tiger no tiene ninguna) return(globalScope.GetDefinedLocalVarOrFunction(variableName, out localVariable)); }
/// <summary> /// Searches for a function or procedure in the current scope or predefined /// </summary> /// <param name="callableName">function or procedure name</param> /// <param name="localCallable">out function or procedure</param> /// <returns>True if succeded, False otherwise</returns> public bool GetDefinedCallableShallow(string callableName, out SemanticInfo localCallable) { ///verificamos si está en el scope local if (currentScope.GetDefinedLocalVarOrFunction(callableName, out localCallable)) { ///si nos encontramos con una función o un procedimiento if (localCallable.ElementKind != SymbolKind.Variable) { return(true); } else { localCallable = SemanticInfo.SemanticError; return(false); } } ///verificamos si es una de las funciones o procedimientos predeterminados return(globalScope.GetDefinedLocalVarOrFunction(callableName, out localCallable)); }
/// <summary> /// Searches for a variable in the entire program /// </summary> /// <param name="variableName">variable name</param> /// <param name="variable">out variable</param> /// <returns>True if succeded, False otherwise</returns> public bool GetDefinedVariableDeep(string variableName, out SemanticInfo variable) { Scope localScope = currentScope; while (localScope != null) { ///se encontró en el scope local if (localScope.GetDefinedLocalVarOrFunction(variableName, out variable)) { ///si es una variable if (variable.ElementKind == SymbolKind.Variable) { return(true); } } ///buscamos en el padre localScope = localScope.Outer; } ///no se encontró variable = SemanticInfo.SemanticError; return(false); }
/// <summary> /// Searches for a function or procedure in the entire program /// </summary> /// <param name="callableName">function or procedure name</param> /// <param name="callable">out function or procedure</param> /// <returns>True if succeded, False otherwise</returns> public bool GetDefinedCallableDeep(string callableName, out SemanticInfo callable) { Scope localScope = currentScope; while (localScope != null) { ///se encontró en el scope local if (localScope.GetDefinedLocalVarOrFunction(callableName, out callable)) { ///si es función o procedimiento if (callable.ElementKind != SymbolKind.Variable) { return(true); } } ///buscamos en el scope padre localScope = localScope.Outer; } ///no se encontró callable = SemanticInfo.SemanticError; return(false); }
/// <summary> /// Inserts a new symbol in the current scope /// </summary> /// <param name="element">new element</param> /// <returns>True if succeded, False otherwise</returns> public bool InsertSymbol(SemanticInfo element) { switch (element.ElementKind) { case SymbolKind.Type: { ///verificamos que no oculte ninguno de los BuiltIn SemanticInfo builtIn; if (globalScope.GetDefinedLocalType(element.Name, out builtIn)) { return(false); } ///lo tratamos de insertar en el scope local return(currentScope.DefineLocalType(element)); } case SymbolKind.Variable: case SymbolKind.Function: case SymbolKind.Procedure: { ///verificamos que no oculte ninguno de los predefinidos SemanticInfo predefined; if (globalScope.GetDefinedLocalVarOrFunction(element.Name, out predefined)) { return(false); } ///lo tratamos de insertar en el scope local return(currentScope.DefineLocalVarOrFunction(element)); } default: return(false); } }