Ejemplo n.º 1
0
        public override void ExportCodeStatements(IMethodCompile method)
        {
            RaisDataType targetType = this[0].DataType;
            RaisDataType sourceType = this[1].DataType;
            Type         target     = targetType.Type;
            Type         source     = sourceType.Type;

            if (target.Equals(source))
            {
                MathNode.Trace("MathNodeAssign, code 1: same type:{0}", target);
                method.MethodCode.Statements.Add(new CodeAssignStatement(this[0].ExportCode(method), this[1].ExportCode(method)));
            }
            else
            {
                MathNode.Trace("MathNodeAssign");
                CodeExpression code = RaisDataType.GetConversionCode(sourceType, this[1].ExportCode(method), targetType, method.MethodCode.Statements);
                if (code != null)
                {
                    method.MethodCode.Statements.Add(new CodeAssignStatement(this[0].ExportCode(method), code));
                }
                else
                {
                    if (!target.Equals(typeof(void)))
                    {
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override CodeExpression ExportCode(IMethodCompile method)        //)
        {
            CodeStatementCollection supprtStatements = method.MethodCode.Statements;

            if (this.UseDefaultValue)
            {
                if (_default == null)
                {
                    MathNode.Trace("MathNodeParameter.ExportCode: Use default case 0:null");
                    return(ValueTypeUtil.GetDefaultCodeByType(this.DataType.Type));
                }
                else
                {
                    MathNode.Trace("MathNodeParameter.ExportCode: Use default case 1:{0}", _default);
                    return(ObjectCreationCodeGen.ObjectCreationCode(_default));
                }
            }
            else
            {
                if (this.InPort != null && this.InPort.LinkedPortID != 0)
                {
                    MathNode.Trace("MathNodeParameter.ExportCode: call linked item");
                    IMathExpression rootContainer = this.root.RootContainer;
                    if (rootContainer == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("Parameter {0} not associated with a root container", this.TraceInfo));
                    }
                    MathExpItem LinkedItem = rootContainer.GetItemByID(this.InPort.LinkedPortID);
                    if (LinkedItem == null)
                    {
                        throw new MathException(string.Format("Linked Port ID {0} from ({1}) does not match an item", InPort.LinkedPortID, this.TraceInfo));
                    }
                    CodeExpression ce = LinkedItem.ReturnCodeExpression(method);
                    return(RaisDataType.GetConversionCode(LinkedItem.MathExpression.DataType, ce, this.DataType, supprtStatements));
                }
                //
                MathNode.Trace("MathNodeParameter.ExportCode: call MathNodeVariable.ExportCode");
                return(base.ExportCode(method));
            }
        }
Ejemplo n.º 3
0
 public override CodeExpression ExportCode(IMethodCompile method)
 {
     if (_methodRef.MethodOwner == null)
     {
         throw new Exception("methodOwner is not assigned to MethodNode.");
     }
     MathNode.Trace("{0}:ExportCode for {1}", this.GetType().Name, this.TraceInfo);
     if (_methodRef.Parameters != null)
     {
         int n = ChildNodeCount;
         if (_methodRef.ParameterCount != n)
         {
             throw new MathException("method parameters are not initialized for {0}.", this.GetType().Name);
         }
         MathNode.Trace("{0} parameter(s)", n);
         MathNode.IndentIncrement();
         parameterCode = new CodeExpression[n];
         for (int i = 0; i < n; i++)
         {
             MathNode.Trace("parameter {0}: {1}", i, this[i].TraceInfo);
             CodeExpression ce = this[i].ExportCode(method);
             if (!_methodRef.Parameters[i].DataType.IsSameType(this[i].DataType))
             {
                 ce = RaisDataType.GetConversionCode(this[i].DataType, ce, _methodRef.Parameters[i].DataType, method.MethodCode.Statements);
             }
             parameterCode[i] = ce;
             if (_methodRef.Parameters[i].Direction != FieldDirection.In)
             {
                 parameterCode[i] = new CodeDirectionExpression(_methodRef.Parameters[i].Direction, parameterCode[i]);
             }
         }
         MathNode.IndentDecrement();
     }
     else
     {
         parameterCode = new CodeExpression[] { };
     }
     if (_methodRef.MethodOwner.Type == ObjectRefType.Type)            //a static function
     {
         MathNode.Trace("Invoke static function {0}.{1}", _methodRef.MethodOwner.Value.LibType, _methodRef.MethodName);
         CodeMethodInvokeExpression e = new CodeMethodInvokeExpression(
             new CodeTypeReferenceExpression(_methodRef.MethodOwner.Value.LibType),
             _methodRef.MethodName, parameterCode);
         return(e);
     }
     else
     {
         if (_methodRef.MethodName == XmlSerialization.CONSTRUCTOR_METHOD)
         {
             MathNode.Trace("Invoke constructor for {0}", _methodRef.MethodOwner.TypeString);
             CodeObjectCreateExpression oc = new CodeObjectCreateExpression(_methodRef.MethodOwner.TypeString, parameterCode);
             return(oc);
         }
         MathNode.Trace("Invoke member function {0} from {1}", FunctionName, _methodRef.MethodOwner.Name);
         if (TargetObject == null)
         {
             if (_methodRef.IsStatic)
             {
                 TargetObject = new CodeTypeReferenceExpression(_methodRef.MethodOwner.TypeString);
             }
             else
             {
                 TargetObject = this.MethodOwner.ExportCode(_methodRef.MethodOwner.XPath);
             }
         }
         //
         CodeMethodInvokeExpression e = new CodeMethodInvokeExpression(
             TargetObject, _methodRef.MethodName, parameterCode);
         return(e);
     }
 }
Ejemplo n.º 4
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);
                            }
                        }
                    }
                }
            }
        }