/// <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)); }
/// <summary> /// generate local variables and code to do the calculation of the math. /// the calculation result is assigned to the variable this[4] /// </summary> /// <returns></returns> public override void ExportCodeStatements(IMethodCompile method) { MathNode.Trace("ExportCodeStatements for {0}", this.GetType()); //0:function //1:index //2:begin //3:end //4:sum OnPrepareVariable(method); CodeVariableReferenceExpression sum = new CodeVariableReferenceExpression(((IVariable)this[4]).CodeVariableName); CodeVariableDeclarationStatement p; if (((IVariable)this[4]).VariableType.Type.IsValueType) { p = new CodeVariableDeclarationStatement(((IVariable)this[4]).VariableType.Type, ((IVariable)this[4]).CodeVariableName); } else { p = new CodeVariableDeclarationStatement(((IVariable)this[4]).VariableType.Type, ((IVariable)this[4]).CodeVariableName, ValueTypeUtil.GetDefaultCodeByType(((IVariable)this[4]).VariableType.Type)); } method.MethodCode.Statements.Add(p); CodeExpression idx = this[1].ExportCode(method); CodeIterationStatement cis = new CodeIterationStatement( new CodeVariableDeclarationStatement(this[1].DataType.Type, ((IVariable)this[1]).CodeVariableName, this[2].ExportCode(method)), new CodeBinaryOperatorExpression(idx, CodeBinaryOperatorType.LessThanOrEqual, this[3].ExportCode(method)), new CodeAssignStatement(idx, new CodeBinaryOperatorExpression(idx, CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1))), new CodeStatement[] { new CodeAssignStatement(sum, new CodeBinaryOperatorExpression(sum, CodeBinaryOperatorType.Add, this[0].ExportCode(method))) }); method.MethodCode.Statements.Add(cis); }
/// <summary> /// read a value from a child node /// </summary> /// <param name="node"></param> /// <param name="nodeName"></param> /// <param name="valueType">it must be a valu etype</param> /// <returns></returns> public static object ReadValueFromChildNode(XmlNode node, string nodeName, Type valueType) { object v; if (ReadValueFromChildNode(node, nodeName, out v)) { return(v); } return(ValueTypeUtil.GetDefaultValueByTypeCode(Type.GetTypeCode(valueType))); }
/// <summary> /// get a value from an attribute /// </summary> /// <param name="node"></param> /// <param name="name"></param> /// <param name="valueType">it must be a value type</param> /// <returns></returns> public static object GetAttributeValue(XmlNode node, string name, Type valueType) { string s = GetAttribute(node, name); if (!string.IsNullOrEmpty(s)) { return(ValueTypeUtil.ConvertValueByTypeCode(Type.GetTypeCode(valueType), s)); } else { return(ValueTypeUtil.GetDefaultValueByTypeCode(Type.GetTypeCode(valueType))); } }
public override CodeExpression ExportCode(IMethodCompile method) { if (_passin != null) { return(_passin); } string s = method.GetParameterCodeNameById(this.Parameter.ID); if (string.IsNullOrEmpty(s)) { MathNode.Trace("Argument '{0}' is not an argument for method '{1}'", ArgumentName, method.MethodName); return(ValueTypeUtil.GetDefaultCodeByType(Parameter.DataType.Type)); } else { MathNode.Trace("{0}.ExportCode maps {1} to {2}", this.GetType().Name, ArgumentName, s); return(new CodeArgumentReferenceExpression(s)); } }
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)); } }
public static void DeclareJavaScriptVariable(StringCollection statements, IVariable var, string val) { if (var is MathNodeVariableDummy) { return; } //use JavaScript's auto variable declaration string v; if (string.IsNullOrEmpty(val)) { v = ValueTypeUtil.GetDefaultJavaScriptValueByType(var.VariableType.Type); } else { v = val; } string s; s = MathNode.FormString("{0}={1};\r\n", var.CodeVariableName, v); statements.Add(s); }
/// <summary> /// declare a variable and initialize it with default value. /// </summary> /// <param name="supprtStatements"></param> /// <param name="var"></param> public static void DeclareVariable(CodeStatementCollection supprtStatements, IVariable var) { if (var is MathNodeVariableDummy) { return; } if (!VariableDeclared(supprtStatements, var.CodeVariableName)) { MathNode.Trace("Declare variable {0}", var.TraceInfo); CodeVariableDeclarationStatement p; if (var.VariableType.Type.IsValueType) { p = new CodeVariableDeclarationStatement(new CodeTypeReference(var.VariableType.Type), var.CodeVariableName); } else { p = new CodeVariableDeclarationStatement(new CodeTypeReference(var.VariableType.Type), var.CodeVariableName, ValueTypeUtil.GetDefaultCodeByType(var.VariableType.Type)); } supprtStatements.Add(p); } }
public override void ExportPhpScriptCodeStatements(StringCollection method) { MathNode.Trace("ExportPhpScriptCodeStatements for {0}", this.GetType()); //0:function //1:index //2:begin //3:end //4:sum OnPreparePhpScriptVariable(method); string sum = ((IVariable)this[4]).CodeVariableName; string declareSum = FormString("{0}={1};\r\n", ((IVariable)this[4]).CodeVariableName, ValueTypeUtil.GetDefaultPhpScriptCodeByType(((IVariable)this[4]).VariableType.Type)); method.Add(declareSum); string idx = this[1].CreatePhpScript(method); method.Add(FormString("for({0}={1};{2}<={3};({2})++)\r\n{\r\n", ((IVariable)this[1]).CodeVariableName, this[2].CreatePhpScript(method), idx, this[3].CreatePhpScript(method))); method.Add(FormString("{0} = {0} + {1};\r\n", sum, this[0].CreatePhpScript(method))); method.Add("}\r\n"); }
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); } } } } } }