Beispiel #1
0
        private BoundExpression BindCallExpression(CallExpressionSyntax syntax)
        {
            var arguments = ImmutableArray.CreateBuilder <BoundExpression>();

            foreach (var argument in syntax.Arguments)
            {
                var boundArgument = BindExpression(argument);

                arguments.Add(boundArgument);
            }

            var functions = BuiltinFunctions.GetAll();

            var function = functions.SingleOrDefault(f => f.Name == syntax.IdentifierToken.Text);

            if (function == null)
            {
                _diagnostics.ReportUndefinedFunction(syntax.IdentifierToken.Span, syntax.IdentifierToken.Text);

                return(new BoundErrorExpression());
            }

            if (syntax.Arguments.Count != function.Parameters.Length)
            {
                _diagnostics.ReportIncorrectArgumentCount(syntax.Span, function.Name, function.Parameters.Length, syntax.Arguments.Count);

                return(new BoundErrorExpression());
            }

            for (var i = 0; i < function.Parameters.Length; i++)
            {
                var parameter = function.Parameters[i];
                var argument  = arguments[i];

                if (parameter.Type != argument.Type)
                {
                    _diagnostics.ReportTypeMismatch(syntax.Span, argument.Type, function.Parameters[i].Type);

                    return(new BoundErrorExpression());
                }
            }

            return(new BoundCallExpression(function, arguments.ToImmutable()));
        }