/// <summary> /// find linked node and use that node's code. /// if not found then use default value /// </summary> /// <returns></returns> public override System.CodeDom.CodeExpression ExportCode(IMethodCompile method) { MathNode.Trace("{0}.ExportCode", this.GetType().Name); if (this.InPort != null) { if (this.InPort.LinkedPortID != 0) { MathExpItem mathParent = root.ContainerMathItem; if (mathParent == null) { throw new MathException(XmlSerialization.FormatString("ExportCode for Default value: Linked port ID {0}. MathNodeRoot missing container.", this.InPort.LinkedPortID)); } if (mathParent.Parent == null) { throw new MathException(XmlSerialization.FormatString("ExportCode for Default value: Linked port ID {0}. MathExpItem missing container.", this.InPort.LinkedPortID)); } MathExpItem item = mathParent.Parent.GetItemByID(this.InPort.LinkedPortID); if (item == null) { throw new MathException(XmlSerialization.FormatString("ExportCode for Default value: Linked port ID {0} does not point to a valid port", this.InPort.LinkedPortID)); } return(item.ReturnCodeExpression(method)); } } return(ValueTypeUtil.GetDefaultValueByType(this.DataType.Type)); }
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); } } } } } }