/// <summary> /// Attempts to add a variable to the current code block hash, checking /// that it is a valid movement to make. /// </summary> /// <param name="name">The name of the variable to add.</param> /// <param name="tipo">The type of the variable to add.</param> /// <param name="isArr">Boolean determining if the variable is an array /// </param> /// <param name="sizes">In case of being an array, its size</param> private void AddVariable(string name, Type tipo, bool isArr, List <int> sizes) { if (!_currentScope.ExistsInScope(name)) { if (isArr) { Symbol symbol = new VariableArray(sizes) { Name = name, Type = tipo }; _currentScope.Add(symbol); } else { Symbol symbol = new Variable { Name = name, Type = tipo, Value = ConstantBuilder.DefaultValue(tipo) }; _currentScope.Add(symbol); } } else { SemErr($"El nombre '{name}' ya ha sido declarado en este scope."); } }
/// <summary> /// Pushes the default values into every symbol in its hash. /// </summary> public void PushDefaultValues() { foreach (var obj in _hash.Values) { if (!(obj is DirectValueSymbol)) { continue; } var variable = (DirectValueSymbol)obj; variable.SaveAndClear(ConstantBuilder.DefaultValue(variable.Type)); } }
/// <summary> /// Inicializes a new instance of the <see cref="VariableArray"/> class. /// </summary> /// <param name="lengths">The lengths of the array.</param> public VariableArray(List <int> lengths) { Lengths = lengths; var totalLength = lengths.Aggregate((accum, next) => accum * next); Variables = new Variable[totalLength]; for (var i = 0; i < totalLength; i++) { Variables[i] = new Variable { Type = Type, Value = ConstantBuilder.DefaultValue(Type) }; } }