Esempio n. 1
0
 private void CompareParameterTypes(IExpression value, IParameterCallable option, int parameter, DocRange errorRange)
 {
     if (!CodeType.TypeMatches(option.Parameters[parameter].Type, value.Type()))
     {
         // The parameter type does not match.
         string msg = string.Format("Expected a value of type {0}.", option.Parameters[parameter].Type.Name);
         optionDiagnostics[option].Add(new Diagnostic(msg, errorRange, Diagnostic.Error));
     }
     else if (value.Type() != null && option.Parameters[parameter].Type == null && value.Type().Constant() == TypeSettable.Constant)
     {
         string msg = string.Format($"The type '{value.Type().Name}' cannot be used here.");
         optionDiagnostics[option].Add(new Diagnostic(msg, errorRange, Diagnostic.Error));
     }
 }
        private void CompareParameterTypes(IExpression value, IParameterCallable option, int parameter, DocRange errorRange)
        {
            CodeType parameterType = option.Parameters[parameter].Type;

            if (parameterType != null && ((parameterType.IsConstant() && value.Type() == null) || (value.Type() != null && !value.Type().Implements(parameterType))))
            {
                // The parameter type does not match.
                string msg = string.Format("Expected a value of type {0}.", option.Parameters[parameter].Type.Name);
                optionDiagnostics[option].Add(new Diagnostic(msg, errorRange, Diagnostic.Error));
            }
            else if (value.Type() != null && parameterType == null && value.Type().IsConstant())
            {
                string msg = string.Format($"The type '{value.Type().Name}' cannot be used here.");
                optionDiagnostics[option].Add(new Diagnostic(msg, errorRange, Diagnostic.Error));
            }
        }
 public OverloadMatch(IParameterCallable option)
 {
     Option = option;
 }
        private OverloadMatch MatchOverload(IParameterCallable option, PickyParameter[] inputParameters, DeltinScriptParser.Call_parametersContext context)
        {
            PickyParameter lastPicky = null;

            OverloadMatch match = new OverloadMatch(option);

            match.OrderedParameters = new PickyParameter[option.Parameters.Length];

            // Iterate through the option's parameters.
            for (int i = 0; i < inputParameters.Length; i++)
            {
                if (inputParameters[i].ParameterOrdered(option.Parameters[i]))
                {
                    // If the picky parameters end but there is an additional picky parameter, throw a syntax error.
                    if (lastPicky != null && inputParameters[i].Name == null)
                    {
                        match.Error($"Named argument '{lastPicky.Name}' is used out-of-position but is followed by an unnamed argument", lastPicky.NameRange);
                    }
                    else
                    {
                        match.OrderedParameters[i] = inputParameters[i];
                        // Next contextual parameter
                        if (i == inputParameters.Length - 1 && i < option.Parameters.Length - 1)
                        {
                            match.LastContextualParameterIndex = i + 1;
                        }
                    }
                }
                else
                {
                    // Picky parameter ends.
                    lastPicky = inputParameters[i];

                    // Set relevant picky parameter.
                    bool nameFound = false;
                    for (int p = 0; p < option.Parameters.Length && !nameFound; p++)
                    {
                        if (inputParameters[i].Name == option.Parameters[p].Name)
                        {
                            match.OrderedParameters[p] = inputParameters[i];
                            nameFound = true;
                        }
                    }

                    // If the named argument's name is not found, throw an error.
                    if (!nameFound)
                    {
                        match.Error($"Named argument '{lastPicky.Name}' does not exist in the function '{option.GetLabel(false)}'.", inputParameters[i].NameRange);
                    }
                }
            }

            // Compare parameter types.
            for (int i = 0; i < match.OrderedParameters.Length; i++)
            {
                match.CompareParameterTypes(i);
            }

            // Get the missing parameters.
            match.GetMissingParameters(genericErrorRange, ErrorMessages, context, CallRange);

            return(match);
        }