Exemple #1
0
        public static string TranslateToPrintF(this MemberReferenceExpression mre, object data)
        {
            TextWriter        output  = new StringWriter();
            CUDAOutputVisitor visitor = new CUDAOutputVisitor(new TextWriterOutputFormatter(output), new CSharpFormattingOptions());
            var ex = data as InvocationExpression;

            if (ex == null)
            {
                throw new ArgumentNullException("data as InvocationExpression");
            }

            bool isWriteLine            = ((MemberReferenceExpression)ex.Target).MemberName.StartsWith("WriteLine");
            bool hasIfCondition         = ((MemberReferenceExpression)ex.Target).MemberName.EndsWith("If");
            List <Expression> arguments = ex.Arguments.ToList();
            int i = 0;

            if (hasIfCondition)
            {
                i = -1;
                output.Write("if(");
                arguments[0].AcceptVisitor(visitor, data);
                output.Write(") ");
            }

            output.Write("printf(");
            foreach (var arg in arguments)
            {
                if (i == -1) /* skip it, it was an if condition */ } {
        public static string TranslateAssert(this MemberReferenceExpression mre, object data)
        {
            TextWriter output = new StringWriter();
            CUDAOutputVisitor visitor = new CUDAOutputVisitor(new TextWriterOutputFormatter(output), new CSharpFormattingOptions());
            var ex = data as InvocationExpression;
            if (ex == null)
                throw new ArgumentNullException("data as InvocationExpression");

            bool isWriteLine = ((MemberReferenceExpression)ex.Target).MemberName == "WriteLine";
            int i = 0;
            List<Expression> arguments = ex.Arguments.ToList();
            if (arguments.Count > 1)
            {
                // An If statement and Debug.WriteLine should go along with this assert
                output.Write("if(!(");
                arguments[0].AcceptVisitor(visitor, data);
                output.Write(")) {");

                bool shortMessageIsNull = arguments[1] is NullReferenceExpression;
                // Argument #2 is a unformatted short message, print it out without any parameters
                if (!(arguments[1] is PrimitiveExpression || shortMessageIsNull))
                    throw new CudafyLanguageException("When using Debug.Assert() the second parameter must be a string literal");
                string shortMessage = arguments[1].ToString();
                if (!shortMessageIsNull && !shortMessage.StartsWith("\""))
                    throw new CudafyLanguageException("When using Debug.Assert() the second parameter must be a string literal");

                // Skip printing this first message if the value is NULL. 
                // Why? Because I assume the user wants to print the message with parameters without the hassle of two messages
                if (!shortMessageIsNull)
                {
                    // Insert a newline into the end of the message
                    shortMessage = shortMessage.Insert(shortMessage.Length - 1, "\\n");
                    // Since this message does not support parameters, escape any % to avoid ErrorUnknown
                    shortMessage = shortMessage.Replace("%", "%%");

                    output.Write("printf(" + shortMessage + ");");
                }

                if (arguments.Count > 2)
                {
                    // Argument #3 is an un/formated detailed message with or without parameters
                    if (!(arguments[2] is PrimitiveExpression))
                        throw new CudafyLanguageException("When using Debug.Assert() the third parameter must be a string literal");
                    string detailedMessageFormat = arguments[2].ToString();
                    if (!detailedMessageFormat.StartsWith("\""))
                        throw new CudafyLanguageException("When using Debug.Assert() the third parameter must be a string literal");

                    // Insert a newline into the end of the message
                    detailedMessageFormat = detailedMessageFormat.Insert(detailedMessageFormat.Length - 1, "\\n");
                    if (arguments.Count > 3)
                    {
                        output.Write("printf(" + detailedMessageFormat);
                        if (arguments[3] is ArrayCreateExpression)
                        {
                            var arrayCreateExpression = arguments[3] as ArrayCreateExpression;
                            foreach (var arrayArg in arrayCreateExpression.Initializer.Elements)
                            {
                                output.Write(',');
                                arrayArg.AcceptVisitor(visitor, data);
                            }
                        }
                        else
                        {
                            output.Write(',');
                            arguments[3].AcceptVisitor(visitor, data);
                        }
                        output.Write(");");
                    }
                    else if (arguments.Count == 3)
                    {
                        // Since this message does not support parameters, escape any % to avoid ErrorUnknown
                        detailedMessageFormat = detailedMessageFormat.Replace("%", "%%");
                        output.Write("printf(" + detailedMessageFormat + ");");
                    }
                }
                output.Write("assert(0);");
                output.Write("}");
            }
            else
            {
                output.Write("assert(");
                arguments[0].AcceptVisitor(visitor, data);
                output.Write(")");
            }
            return output.ToString();
        }
        public static string TranslateToPrintF(this MemberReferenceExpression mre, object data)
        {
            TextWriter output = new StringWriter();
            CUDAOutputVisitor visitor = new CUDAOutputVisitor(new TextWriterOutputFormatter(output), new CSharpFormattingOptions());
            var ex = data as InvocationExpression;
            if (ex == null)
                throw new ArgumentNullException("data as InvocationExpression");

            bool isWriteLine = ((MemberReferenceExpression)ex.Target).MemberName.StartsWith("WriteLine");
            bool hasIfCondition = ((MemberReferenceExpression)ex.Target).MemberName.EndsWith("If");
            List<Expression> arguments = ex.Arguments.ToList();
            int i = 0;

            if (hasIfCondition)
            {
                i = -1;
                output.Write("if(");
                arguments[0].AcceptVisitor(visitor, data);
                output.Write(") ");
            }

            output.Write("printf(");
            foreach (var arg in arguments)
            {
                if (i == -1) { /* skip it, it was an if condition */ }
                else if (i == 0)
                {
                    if (!(arg is PrimitiveExpression))
                        throw new CudafyLanguageException("When using Debug.Write" + (isWriteLine ? "Line" : "") + (hasIfCondition ? "If" : "") + "() the first parameter must be a string literal");

                    string strFormat = arg.ToString();
                    if (!strFormat.StartsWith("\""))
                        throw new CudafyLanguageException("When using Debug.Write" + (isWriteLine ? "Line" : "") + "() the first parameter must be a string literal");

                    if (hasIfCondition)
                    {
                        // Since debug if condition messages don't support parameters, escape any % to avoid ErrorUnknown
                        strFormat = strFormat.Replace("%", "%%");
                    }

                    // NOTE: unless you know the type of each parameter passed to printf, 
                    // then I don't see a good way to convert String.Format() rules to printf() rules
                    //strFormat = CUDALanguage.TranslateStringFormat(strFormat);

                    if (isWriteLine)
                        strFormat = strFormat.Insert(strFormat.Length - 1, "\\n");

                    output.Write(strFormat);
                }
                else if (hasIfCondition)
                {
                    // Skip any other parameters if they are there, debug if conditions don't support formatting
                }
                else if (arg is ArrayCreateExpression)
                {
                    var arrayCreateExpression = arg as ArrayCreateExpression;
                    foreach (var arrayArg in arrayCreateExpression.Initializer.Elements)
                    {
                        output.Write(',');
                        arrayArg.AcceptVisitor(visitor, data);
                    }
                }
                else
                {
                    output.Write(',');
                    arg.AcceptVisitor(visitor, data);
                }
                i++;
            }
            output.Write(")");
            return output.ToString();
        }