Пример #1
0
        public override void VisitStringSyntax(StringSyntax syntax)
        => AssignTypeWithCaching(syntax, () => {
            if (!syntax.IsInterpolated())
            {
                // uninterpolated strings have a known type
                return(new StringLiteralType(syntax.GetLiteralValue()));
            }

            var errors = new List <ErrorDiagnostic>();

            foreach (var interpolatedExpression in syntax.Expressions)
            {
                var expressionType = VisitAndReturnType(interpolatedExpression);
                CollectErrors(errors, expressionType);
            }

            if (errors.Any())
            {
                return(new ErrorTypeSymbol(errors));
            }

            // normally we would also do an assignability check, but we allow "any" type in string interpolation expressions
            // so the assignability check cannot possibly fail (we already collected type errors from the inner expressions at this point)
            return(LanguageConstants.String);
        });
Пример #2
0
        private TypeSymbol GetStringType(TypeManagerContext context, StringSyntax @string)
        {
            if (@string.IsInterpolated() == false)
            {
                // uninterpolated strings have a known type
                return(new StringLiteralType(@string.GetLiteralValue()));
            }

            var errors = new List <ErrorDiagnostic>();

            foreach (var interpolatedExpression in @string.Expressions)
            {
                var expressionType = this.GetTypeInfoInternal(context, interpolatedExpression);
                CollectErrors(errors, expressionType);
            }

            if (errors.Any())
            {
                return(new ErrorTypeSymbol(errors));
            }

            // normally we would also do an assignability check, but we allow "any" type in string interpolation expressions
            // so the assignability check cannot possibly fail (we already collected type errors from the inner expressions at this point)
            return(LanguageConstants.String);
        }
Пример #3
0
        private LanguageExpression ConvertString(StringSyntax syntax)
        {
            if (!syntax.IsInterpolated())
            {
                // no need to build a format string
                return(new JTokenExpression(syntax.GetLiteralValue()));;
            }

            var formatArgs = new LanguageExpression[syntax.Expressions.Length + 1];

            var formatString = StringFormatConverter.BuildFormatString(syntax);

            formatArgs[0] = new JTokenExpression(formatString);

            for (var i = 0; i < syntax.Expressions.Length; i++)
            {
                formatArgs[i + 1] = ConvertExpression(syntax.Expressions[i]);
            }

            return(new FunctionExpression("format", formatArgs, Array.Empty <LanguageExpression>()));
        }