Example #1
0
        public static ArgumentSyntax ConstantArgument(string typeSignature, string value)
        {
            var type = MyVisualScriptingProxy.GetType(typeSignature);

            // First handle special cases as Color and Enums
            if (type == typeof(Color) || type.IsEnum)
            {
                return(SyntaxFactory.Argument(
                           SyntaxFactory.MemberAccessExpression(
                               SyntaxKind.SimpleMemberAccessExpression,
                               SyntaxFactory.IdentifierName(typeSignature),
                               SyntaxFactory.IdentifierName(value)
                               )));
            }

            if (type == typeof(Vector3D))
            {
                return(SyntaxFactory.Argument(
                           MySyntaxFactory.NewVector3D(value)
                           ));
            }

            return(SyntaxFactory.Argument(
                       Literal(typeSignature, value)
                       ));
        }
Example #2
0
        public static LiteralExpressionSyntax Literal(string typeSignature, string val)
        {
            Debug.Assert(typeSignature != null, "Type signature cannot be null.");
            var type = MyVisualScriptingProxy.GetType(typeSignature);

            if (type != null)
            {
                if (type == typeof(float))
                {
                    var floatVal = string.IsNullOrEmpty(val) ? 0 : float.Parse(val);
                    return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(floatVal)));
                }

                if (type == typeof(int) || type == typeof(long))
                {
                    return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(val)));
                }

                if (type == typeof(bool))
                {
                    var value = MySyntaxFactory.NormalizeBool(val);
                    if (value == "true")
                    {
                        return(SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression));
                    }

                    return(SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
                }
            }

            return(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(val)));
        }
Example #3
0
        public static MethodDeclarationSyntax MethodDeclaration(MethodInfo method)
        {
            var parametersSyntax = new List <SyntaxNodeOrToken>();

            var @params = method.GetParameters();

            for (var index = 0; index < @params.Length; index++)
            {
                var parameter = @params[index];
                var syntax    = SyntaxFactory.Parameter(
                    SyntaxFactory.Identifier(parameter.Name)
                    ).WithType(GenericTypeSyntax(parameter.ParameterType));

                parametersSyntax.Add(syntax);

                // Add comma
                if (index < @params.Length - 1)
                {
                    parametersSyntax.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                }
            }

            // Todo: Make it more generic

            return(SyntaxFactory.MethodDeclaration(
                       MySyntaxFactory.GenericTypeSyntax(method.ReturnType),
                       SyntaxFactory.Identifier(method.Name)
                       ).WithModifiers(
                       SyntaxFactory.TokenList(
                           SyntaxFactory.Token(SyntaxKind.PublicKeyword)
                           )
                       ).WithParameterList(
                       SyntaxFactory.ParameterList(
                           SyntaxFactory.SeparatedList <ParameterSyntax>(parametersSyntax)
                           )
                       ).WithBody(
                       SyntaxFactory.Block()
                       ));
        }