public override void ExportJavaScriptCodeStatements(StringCollection method) { MathNode.Trace("ExportJavaScriptCodeStatements for {0}", this.GetType()); //0: start //1: end //2: function //3: dx IsParam //4: sum IsLocal //5: summing index IsLocal IsParam base.ExportJavaScriptCodeStatements(method); //assign code expression to all x in the function OnPrepareJavaScriptVariable(method); MathNodeVariable.DeclareJavaScriptVariable(method, (IVariable)this[4], "0"); string sum = ((IVariable)this[4]).CodeVariableName; method.Add(MathNode.FormString("{0}=0;\r\n", sum)); string c5 = this.GetParameterCodeJS(method, 5); string f1 = MathNode.FormString("for({0}=1;{1}<={2}-1;({1})++)\r\n{\r\n"); method.Add(f1); method.Add(MathNode.FormString("\t{0}={0} + {1};\r\n", sum, this.GetParameterCodeJS(method, 2))); method.Add("}\r\n"); //clear code expression to all x in the function this[2].AssignJavaScriptCodeExp(null, ((MathNodeVariable)this[3]).CodeVariableName); }
public override void ExportJavaScriptCodeStatements(StringCollection method) { int n = Branches; MathNode.Trace("ExportJavaScriptCodeStatements for {0}, branches: {1}", this.GetType(), n); string resultName = ((IVariable)this[n + 1]).CodeVariableName; OnPrepareJavaScriptVariable(method); MathNode.Trace("result is varibale {0}", resultName); string result = resultName; MathNodeVariable.DeclareJavaScriptVariable(method, (IVariable)this[n + 1], null); MathNode.Trace("default value: {0}", this[n].ToString()); this[n].ExportJavaScriptCodeStatements(method); string eDefault = this[n].CreateJavaScript(method); string stDefault = MathNode.FormString("{0}={1};\r\n", result, eDefault); if (n == 0) { MathNode.Trace("no condition given"); method.Add(stDefault); } else { string e = this[0][1].CreateJavaScript(method); string ce = this[0][0].CreateJavaScript(method); bool isSameVariable = false; IVariable ve = this[0][0] as IVariable; if (ve != null) { if (string.CompareOrdinal(ve.VariableName, resultName) == 0) { isSameVariable = true; } } StringBuilder trueStatements = new StringBuilder(); StringBuilder falseStatements = new StringBuilder(); if (!isSameVariable) { trueStatements.Append(MathNode.FormString("{0}={1};\r\n", result, e)); } int indent = 0; string tabs = string.Empty; method.Add(MathNode.FormString("if({0}) {{\r\n\t", ce)); method.Add(trueStatements.ToString()); method.Add("}\r\n"); for (int i = 1; i < n; i++) { e = this[i][1].CreateJavaScript(method); ce = this[i][0].CreateJavaScript(method); method.Add(tabs); method.Add("else {\r\n"); indent++; tabs = new string('\t', indent); method.Add(tabs); method.Add(MathNode.FormString("if({0}) {{\r\n", ce)); method.Add(tabs); method.Add(MathNode.FormString("\t{0}={1};\r\n", result, e)); method.Add(tabs); method.Add("}\r\n"); } method.Add(tabs); method.Add("else {\r\n\t"); method.Add(tabs); method.Add(stDefault); method.Add("\r\n"); method.Add(tabs); method.Add("}\r\n"); for (int i = 1; i < n; i++) { indent--; tabs = new string('\t', indent); method.Add(tabs); method.Add("}\r\n"); } } }