public override void ExportPhpScriptCodeStatements(StringCollection method) { MathNode.Trace("ExportPhpScriptCodeStatements for {0}", this.GetType()); //0: start //1: end //2: function //3: dx IsParam //4: sum IsLocal //5: summing index IsLocal IsParam base.ExportPhpScriptCodeStatements(method); //assign code expression to all x in the function OnPreparePhpScriptVariable(method); MathNodeVariable.DeclarePhpScriptVariable(method, (IVariable)this[4]); string sum = ((IVariable)this[4]).CodeVariableName; method.Add(MathNode.FormString("{0}=0;\r\n", sum)); string c5 = this.GetParameterCodePhp(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.GetParameterCodePhp(method, 2))); method.Add("}\r\n"); //clear code expression to all x in the function this[2].AssignPhpScriptCodeExp(null, ((MathNodeVariable)this[3]).CodeVariableName); }
public override void ExportPhpScriptCodeStatements(StringCollection method) { int n = Branches; MathNode.Trace("ExportPhpScriptCodeStatements for {0}, branches: {1}", this.GetType(), n); string resultName = ((IVariable)this[n + 1]).CodeVariableName; OnPreparePhpScriptVariable(method); MathNode.Trace("result is varibale {0}", resultName); string result = resultName; MathNodeVariable.DeclarePhpScriptVariable(method, (IVariable)this[n + 1]); MathNode.Trace("default value: {0}", this[n].ToString()); this[n].ExportPhpScriptCodeStatements(method); string eDefault = this[n].CreatePhpScript(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].CreatePhpScript(method); string ce = this[0][0].CreatePhpScript(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].CreatePhpScript(method); ce = this[i][0].CreatePhpScript(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"); } } }