Esempio n. 1
0
        public static string Convert(CodeExpression expr)
        {
            CodePrimitiveExpression primitiveExpr = expr as CodePrimitiveExpression;

            if (primitiveExpr != null)
            {
                return(primitiveExpr.Value.ToString());
            }

            CodeFieldReferenceExpression fieldExpr = expr as CodeFieldReferenceExpression;

            if (fieldExpr != null)
            {
                return(string.Format("{0}.{1}", Convert(fieldExpr.TargetObject), fieldExpr.FieldName));
            }

            CodeTypeReferenceExpression typeExpr = expr as CodeTypeReferenceExpression;

            if (typeExpr != null)
            {
                return(ConvertNoNamespace(typeExpr.Type));
            }

            CodeBinaryOperatorExpression opExpr = expr as CodeBinaryOperatorExpression;

            if (opExpr != null)
            {
                return(string.Format("{0}({1})({2})", opExpr.Operator, Convert(opExpr.Left), Convert(opExpr.Right)));
            }

            return(expr.ToString());
        }
Esempio n. 2
0
 private static string GetExpression(CodeExpression expr)
 {
     if (expr is CodeFieldReferenceExpression expression)
     {
         return(expression.FieldName);
     }
     if (expr is CodePropertyReferenceExpression expression1)
     {
         return(expression1.PropertyName);
     }
     if (expr is CodeMethodReferenceExpression cmre)
     {
         return(cmre.MethodName);
     }
     if (expr is CodePrimitiveExpression cpe)
     {
         return(cpe.Value.ToString());
     }
     return(expr.ToString());
 }
Esempio n. 3
0
 private static string getExpression(CodeExpression expr)
 {
     if (expr is CodeFieldReferenceExpression)
     {
         return(((CodeFieldReferenceExpression)expr).FieldName);
     }
     if (expr is CodePropertyReferenceExpression)
     {
         return(((CodePropertyReferenceExpression)expr).PropertyName);
     }
     if (expr is CodeMethodReferenceExpression)
     {
         return(((CodeMethodReferenceExpression)expr).MethodName);
     }
     if (expr is CodePrimitiveExpression)
     {
         return(((CodePrimitiveExpression)expr).Value.ToString());
     }
     return(expr.ToString());
 }
        internal static void GenerateCodeFromExpression(CodeExpression e, TextWriter w, CodeGeneratorOptions o)
        {
            if (e == null)
            {
                return;
            }

            var argumentReferenceExpression = e as CodeArgumentReferenceExpression;

            if (argumentReferenceExpression != null)
            {
                w.Write(argumentReferenceExpression.ParameterName);
                return;
            }

            if (WriteCodeArrayCreateExpression(e as CodeArrayCreateExpression, w, o))
            {
                return;
            }

            if (WriteCodeArrayIndexerExpression(e as CodeArrayIndexerExpression, w, o))
            {
                return;
            }

            var baseReferenceExpression = e as CodeBaseReferenceExpression;

            if (baseReferenceExpression != null)
            {
                w.Write("super");
                return;
            }

            if (WriteCodeBinaryOperatorExpression(e as CodeBinaryOperatorExpression, w, o))
            {
                return;
            }

            if (WriteCodeFieldReferenceExpression(e as CodeFieldReferenceExpression, w, o))
            {
                return;
            }

            if (WriteCodeIndexerExpression(e as CodeIndexerExpression, w, o))
            {
                return;
            }

            if (WriteCodeMethodInvokeExpression(e as CodeMethodInvokeExpression, w, o))
            {
                return;
            }


            var methodReferenceExpression = e as CodeMethodReferenceExpression;

            if (methodReferenceExpression != null)
            {
                WriteCodeMethodReferenceExpression(methodReferenceExpression, w, o);
                return;
            }

            if (WriteCodeObjectCreateExpression(e as CodeObjectCreateExpression, w, o))
            {
                return;
            }

            var parameterDeclarationExpression = e as CodeParameterDeclarationExpression;

            if (parameterDeclarationExpression != null)
            {
                w.Write($"{parameterDeclarationExpression.Name}: {TypeMapper.MapCodeTypeReferenceToTsText(parameterDeclarationExpression.Type)}");
                return;
            }

            if (WriteCodePrimitiveExpression(e as CodePrimitiveExpression, w))
            {
                return;
            }


            if (WriteCodePropertyReferenceExpression(e as CodePropertyReferenceExpression, w, o))
            {
                return;
            }

            var snippetExpression = e as CodeSnippetExpression;

            if (snippetExpression != null)
            {
                w.Write(snippetExpression.Value);
                return;
            }

            var thisReferenceExpression = e as CodeThisReferenceExpression;

            if (thisReferenceExpression != null)
            {
                w.Write("this");
                return;
            }

            var typeOfExpression = e as CodeTypeOfExpression;

            if (typeOfExpression != null)
            {
                w.Write("typeof " + TypeMapper.MapCodeTypeReferenceToTsText(typeOfExpression.Type));
                return;
            }

            var typeReferenceExpression = e as CodeTypeReferenceExpression;

            if (typeReferenceExpression != null)
            {
                w.Write(TypeMapper.MapCodeTypeReferenceToTsText(typeReferenceExpression.Type));
                return;
            }

            var variableReferenceExpression = e as CodeVariableReferenceExpression;

            if (variableReferenceExpression != null)
            {
                w.Write(variableReferenceExpression.VariableName);
                return;
            }

            Trace.TraceWarning($"CodeExpression not supported: {e.ToString()}");
        }
        /// <summary>
        /// knowing the code is of different type, make conversion code
        /// </summary>
        /// <param name="sourceValue"></param>
        /// <param name="targetType"></param>
        /// <param name="csc"></param>
        /// <returns></returns>
        static CodeExpression CodeExpConvertTo(CodeExpression sourceCode, Type sourceType, Type targetType, CodeStatementCollection supprtStatements)
        {
            if (targetType.Equals(typeof(void)))
            {
                if (sourceCode == null)
                {
                    MathNode.Trace("code 00: target type is void. Input code is null. Return null for code.");
                }
                else
                {
                    MathNode.Trace("code 00: target type is void. Input code becomes a statement (0). Return null for code.", sourceCode.ToString());
                    supprtStatements.Add(new CodeExpressionStatement(sourceCode));
                }
                return(null);
            }
            CodeExpression codeRet;
            TypeConverter  converter = TypeDescriptor.GetConverter(targetType);

            if (converter.CanConvertFrom(sourceType))
            {
                MathNode.AddImportLocation(typeof(TypeConverter).Assembly.Location);
                string converterName = "conv" + targetType.Name;
                if (!MathNodeVariable.VariableDeclared(supprtStatements, converterName))
                {
                    CodeVariableDeclarationStatement cs1 = new CodeVariableDeclarationStatement(
                        typeof(TypeConverter), converterName,
                        new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression(typeof(TypeDescriptor)), "GetConverter",
                            new CodeExpression[] { new CodeSnippetExpression("typeof(" + targetType.FullName + ")") }));
                    supprtStatements.Add(cs1);
                }
                //=================================================
                codeRet = new CodeCastExpression(targetType,
                                                 new CodeMethodInvokeExpression(
                                                     new CodeVariableReferenceExpression(converterName), "ConvertFrom",
                                                     new CodeExpression[] { sourceCode }));
                MathNode.Trace("code 2: source type:{0}, target type:{1} result=converter.ConvertFrom(code);", sourceType, targetType);
            }
            else
            {
                if (sourceCode is CodePrimitiveExpression)
                {
                    CodePrimitiveExpression codep = sourceCode as CodePrimitiveExpression;
                    if (targetType.Equals(typeof(string)))
                    {
                        if (codep.Value == null)
                        {
                            codeRet = new CodeCastExpression(targetType, sourceCode);
                            MathNode.Trace("code 3: source type:{0}, target type:{1} result=null;", sourceType, targetType);
                        }
                        else
                        {
                            if (codep.Value is string)
                            {
                                codeRet = sourceCode;
                                //
                                MathNode.Trace("code 4: source type:{0}, target type:{1} result=code;", sourceType, targetType);
                            }
                            else
                            {
                                codeRet = new CodeMethodInvokeExpression(sourceCode, "ToString", new CodeExpression[] { });
                                MathNode.Trace("code 5: source type:{0}, target type:{1} result=code.ToString();", sourceType, targetType);
                            }
                        }
                    }
                    else                     //none-string primative
                    {
                        codeRet = new CodeCastExpression(targetType, sourceCode);
                        MathNode.Trace("code 6: source type:{0}, target type:{1}, result=({1})code;", sourceType, targetType);
                    }
                }
                else
                {
                    if (sourceType.Equals(typeof(void)))
                    {
                        codeRet = null;
                        if (sourceCode == null)
                        {
                            MathNode.Trace("code 7a: source type:{0}, target type:{1} code is null;", sourceType, targetType);
                        }
                        else
                        {
                            supprtStatements.Add(new CodeExpressionStatement(sourceCode));
                            MathNode.Trace("code 7: source type:{0}, target type: {1}. code used as a statement; return null as CodeExpression.", sourceType, targetType);
                        }
                    }
                    else if (targetType.Equals(typeof(string)))
                    {
                        codeRet = new CodeMethodInvokeExpression(sourceCode, "ToString", new CodeExpression[] { });
                        MathNode.Trace("code 8: source type:{0}, target type:{1} result=code.ToString();", sourceType, targetType);
                    }
                    else
                    {
                        //check to see if casting can be done
                        codeRet = new CodeCastExpression(targetType, sourceCode);
                        MathNode.Trace("code 9: source type:{0}, target type:{1}. cast it: result=({1})code;", sourceType, targetType);
                    }
                }
            }
            return(codeRet);
        }