Ejemplo n.º 1
0
 /// <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.");
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds a new cAssignIndex command
        /// </summary>
        /// <param name="array">Array to be assigned.</param>
        /// <param name="indexes">The indexes</param>
        private void DoAssignIndex(VariableArray array, List <DirectValueSymbol> indexes)
        {
            if (array.Lengths.Count != indexes.Count)
            {
                SemErr("Cantidad de dimensiones incorrecta.");
                return;
            }

            var cmd = new AssignIndex
            {
                Array   = array,
                Indexes = indexes
            };

            _currentScope.CommandList.Commands.Add(cmd);
        }
Ejemplo n.º 3
0
        void Variable(out Variable variable, out List <DirectValueSymbol> indexes)
        {
            indexes = new List <DirectValueSymbol>();
            Expect(1);
            string name = t.val; CheckVariableExists(name); var symbol = _currentScope.Search(name) ?? (new Variable()); variable = symbol as Variable;

            if (la.kind == 15)
            {
                Get();
                Expresion();
                indexes.Add(_symbolStack.Pop() as DirectValueSymbol);
                while (la.kind == 7)
                {
                    Get();
                    Expresion();
                    indexes.Add(_symbolStack.Pop() as DirectValueSymbol);
                }
                Expect(16);
                CheckIsArray(name); VariableArray array = (symbol as VariableArray); DoAssignIndex(array, indexes); variable = array;
            }
        }