Пример #1
0
 /// <summary>
 /// Anade una nueva funcion o procedimiento al scope
 /// </summary>
 /// <param name="c"></param>
 public void AddCallable(Callable c)
 {
     if (DefinedCallables.ContainsKey(c.Name))
     {
         throw new Exception("El procedimiento o funcion ya existe");
     }
     DefinedCallables.Add(c.Name, c);
 }
Пример #2
0
        public override void CheckSemantic(Scope scope, IList<Error> errors)
        {
            //Comprobamos que no exista una variable/funcion en este mismo scope definida
            if (scope.ExistsVariableOrCallable(Name, false) || scope.IsCallableNameInvalid(Name)) {
                errors.Add(new AlreadyDefinedError(Line, Column, Name));
                return;
            }

            int errorsCount = errors.Count;
            IList<string> parameters = new List<string>();
            //Creamos el scope de la nueva funcion
            Scope = new Scope(scope);

            //Chequeamos los parametros...
            foreach (var field in Fields) {
                bool errorInField = false;
                //Comprobar que el tipo exista
                if (!scope.ExistsType(field.TypeId)) {
                    errors.Add(new UndefinedTypeError(Line, Column, field.TypeId));
                    errorInField = true;
                }
                //Si existe un parametro con ese mismo nombre
                if (parameters.Contains(field.Id))
                {
                    errors.Add(new DuplicateParameterError(Line, Column, field.Id));
                    errorInField = true;
                }
                else {
                    parameters.Add(field.Id);
                }

                //Anadimos este parametro al scope de la funcion
                if (!errorInField) {
                    Scope.Add(field.Id, scope.GetType(field.TypeId));
                }
            }

            //Si me especificaron un tipo y este no existe
            if (Type != null && !scope.ExistsType(Type)) {
                errors.Add(new UndefinedTypeError(Line, Column, Type));
            }

            //Si hubo errores en los parametros no seguimos o en la definicion explicita del tipo de la funcion...
            if (errorsCount != errors.Count) {
                return;
            }

            //Si estamos aqui es porque no hubo ningun error anteriormente

            //OJO: No chequeamos la semantica del cuerpo de la funcion. Lo haremos en una segunda pasada

            //Si no hubo ningun problema...
            if (errorsCount == errors.Count) {
                Callable = new Callable(Name, Fields, Type);
                scope.AddCallable(Callable);
            }
        }
Пример #3
0
 private void AssignReturnType(Scope scope, Callable callable)
 {
     if (callable.IsFunction()) {
         ReturnType = scope.GetType(callable.Type);
     }
 }