Exemple #1
0
        public static void CreateTestMethod(IMathExpression result, CodeTypeDeclaration t, CodeNamespace ns, CodeMemberMethod m, AssemblyRefList imports, VariableList parameters, List <IPropertyPointer> pointerList)
        {
            result.GetAllImports(imports);
            //
            m.ReturnType = new CodeTypeReference(result.DataType.Type);
            //
            m.Comments.Add(new CodeCommentStatement("Variable mapping:"));
            m.Comments.Add(new CodeCommentStatement("In formula:  method parameter"));
            //
            MethodType mt = new MethodType();

            mt.MethodCode = m;
            result.GenerateInputVariables();
            VariableList variables = result.InputVariables;
            Dictionary <string, IPropertyPointer> pointers = new Dictionary <string, IPropertyPointer>();

            result.GetPointers(pointers);
            int n = variables.Count;

            MathNode.Trace("Generate arguments from {0} input variables", n);
            MathNode.IndentIncrement();
            for (int k = 0; k < n; k++)
            {
                IVariable var = variables[k];
                MathNode.Trace(k, var);
                if (!(var is MathNodeVariableDummy) && !var.IsParam && !var.IsConst)
                {
                    string paramName = "";
                    parameters.Add(var);
                    paramName = var.CodeVariableName;
                    CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(new CodeTypeReference(var.VariableType.Type), paramName);
                    m.Parameters.Add(p);
                    //add comment
                    string sub = var.SubscriptName;
                    if (string.IsNullOrEmpty(sub))
                    {
                        sub = " ";
                    }
                    m.Comments.Add(new CodeCommentStatement(string.Format("{0}{1}:\t {2}", var.VariableName, sub, var.CodeVariableName)));
                    MathNode.IndentIncrement();
                    MathNode.Trace("Argument {0} {1} for {2}, {3}", var.VariableType.Type, paramName, var.TraceInfo, var.GetType());
                    MathNode.IndentDecrement();
                    //
                    result.AssignCodeExp(new CodeArgumentReferenceExpression(paramName), var.CodeVariableName);
                }
            }
            MathNode.Trace("Generate arguments from {0} pointers", pointers.Count);
            foreach (KeyValuePair <string, IPropertyPointer> kv in pointers)
            {
                string paramName = "";
                pointerList.Add(kv.Value);
                paramName = kv.Value.CodeName;
                CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(new CodeTypeReference(kv.Value.ObjectType), paramName);
                m.Parameters.Add(p);
                //add comment
                m.Comments.Add(new CodeCommentStatement(string.Format("{0}:\t {1}", kv.Value.ToString(), paramName)));
                MathNode.IndentIncrement();
                MathNode.Trace("Argument {0} {1} for {2}", kv.Value.ObjectType, paramName, kv.Value.ToString());
                MathNode.IndentDecrement();
            }
            MathNode.IndentDecrement();
            //do the compiling
            CodeExpression ce = result.ReturnCodeExpression(mt);

            //
            MathNode.Trace("Test method returns {0}, compiled type: {1}", result.DataType.Type, result.ActualCompileDataType.Type);
            if (result.ActualCompileDataType.Type.Equals(result.DataType.Type))
            {
                CodeMethodReturnStatement mr = new CodeMethodReturnStatement(ce);
                m.Statements.Add(mr);
            }
            else
            {
                if (result.ActualCompileDataType.IsVoid)
                {
                    m.Statements.Add(new CodeExpressionStatement(ce));
                    CodeMethodReturnStatement mr = new CodeMethodReturnStatement(ValueTypeUtil.GetDefaultValueByType(result.DataType.Type));
                    m.Statements.Add(mr);
                }
                else
                {
                    if (result.DataType.IsVoid)
                    {
                        m.Statements.Add(new CodeExpressionStatement(ce));
                    }
                    else
                    {
                        if (result.DataType.Type.Equals(typeof(string)))
                        {
                            CodeMethodReturnStatement mr = new CodeMethodReturnStatement(new CodeMethodInvokeExpression(ce, "ToString", new CodeExpression[] { }));
                            m.Statements.Add(mr);
                        }
                        else
                        {
                            CodeExpression mie = RaisDataType.GetConversionCode(result.ActualCompileDataType, ce, result.DataType, m.Statements);
                            if (mie != null)
                            {
                                CodeMethodReturnStatement mr = new CodeMethodReturnStatement(mie);
                                m.Statements.Add(mr);
                            }
                        }
                    }
                }
            }
        }