Пример #1
0
        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 string CreatePhpScript(StringCollection method)
        {
            MathNode.Trace("{0}.CreatePhpScript", this.GetType().Name);
            string e = this[0].CreatePhpScript(method);

            return(MathNode.FormString("!({0})", e));
        }
Пример #3
0
        protected void OnPhpScriptPrepareVariable(StringCollection method)
        {
            MathNode.Trace("OnPhpScriptPrepareVariable for {0}", this.GetType());
            string s0 = MathNode.FormString("{0} - {1}", this.GetParameterCodePhp(method, 1), this.GetParameterCodePhp(method, 0));
            string s1 = MathNode.FormString("({0}) / {1}", s0, _intervals);
            string s2 = MathNode.FormString("({0}) * ({1})", this.GetParameterCodePhp(method, 5), s1);
            string s3 = MathNode.FormString("{0} + {1}", this.GetParameterCodePhp(method, 0), s2);

            //this[3] is the x, this[2] is the function
            //for all x in the function, the code expression is cp
            //this[2].AssignCodeExp(cp, ((MathNodeVariable)this[3]).CodeVariableName);
            this[2].AssignPhpScriptCodeExp(s3, ((MathNodeVariable)this[3]).CodeVariableName);
        }
 public override string CreatePhpScript(StringCollection method)
 {
     if (_property != null)
     {
         string ownerCode;
         if (_property.IsStatic)
         {
             ownerCode = _property.Holder.TypeString;
         }
         else
         {
             ownerCode = "$this";
         }
         return(MathNode.FormString("{0}->{1}", ownerCode, _property.FieldMemberName));
     }
     return(null);
 }
Пример #5
0
        public override string CreatePhpScript(StringCollection method)
        {
            MathNode.Trace("{0}.CreatePhpScript", this.GetType().Name);
            string sum = ((IVariable)this[4]).CodeVariableName;

            this[2].AssignPhpScriptCodeExp(this.GetParameterCodePhp(method, 0), ((MathNodeVariable)this[3]).CodeVariableName);
            string fa = this.GetParameterCodePhp(method, 2);

            this[2].AssignPhpScriptCodeExp(this.GetParameterCodePhp(method, 1), ((MathNodeVariable)this[3]).CodeVariableName);
            string fb = this.GetParameterCodePhp(method, 2);

            this[2].AssignPhpScriptCodeExp(null, ((MathNodeVariable)this[3]).CodeVariableName);
            string f2 = MathNode.FormString("({0} + {1}) / 2.0", fa, fb);
            string f  = MathNode.FormString("{0} + {1}", f2, sum);
            string ba = MathNode.FormString("({0} - {1}) / {2}", this.GetParameterCodePhp(method, 1), this.GetParameterCodePhp(method, 0), _intervals);

            return(MathNode.FormString("({0}) * ({1})", ba, f));
        }
        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);
        }
Пример #7
0
        public override string CreatePhpScript(StringCollection method)
        {
            if (_methodRef.MethodOwner == null)
            {
                throw new Exception("methodOwner is not assigned to MethodNode.");
            }
            MathNode.Trace("{0}:CreatePhpScript 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();
                parameterCodePhp = new string[n];
                for (int i = 0; i < n; i++)
                {
                    MathNode.Trace("parameter {0}: {1}", i, this[i].TraceInfo);
                    string ce = this[i].CreatePhpScript(method);
                    parameterCodePhp[i] = ce;
                }
                MathNode.IndentDecrement();
            }
            else
            {
                parameterCodePhp = new string[] { };
            }

            if (_methodRef.MethodOwner.Type == ObjectRefType.Type)            //a static function
            {
                MathNode.Trace("Invoke static function {0}.{1}", _methodRef.MethodOwner.Value.LibType, _methodRef.MethodName);
                StringBuilder mCode = new StringBuilder();
                mCode.Append(_methodRef.MethodOwner.Value.LibType.FullName);
                mCode.Append("->");
                mCode.Append(_methodRef.MethodName);
                mCode.Append("(");
                if (parameterCodePhp.Length > 0)
                {
                    mCode.Append(parameterCodePhp[0]);
                    for (int i = 1; i < parameterCodePhp.Length; i++)
                    {
                        mCode.Append(",");
                        mCode.Append(parameterCodePhp[i]);
                    }
                }
                mCode.Append(")");
                return(mCode.ToString());
            }
            else
            {
                if (_methodRef.MethodName == XmlSerialization.CONSTRUCTOR_METHOD)
                {
                    MathNode.Trace("Invoke constructor for {0}", _methodRef.MethodOwner.TypeString);
                    if (parameterCodePhp.Length == 0)
                    {
                        return(MathNode.FormString("new _methodRef.MethodOwner.TypeString()"));
                    }
                    else
                    {
                        string        cn          = MathNode.FormString("_const{0}", _methodRef.MethodOwner.TypeString.Replace(".", "_"));
                        StringBuilder constructor = new StringBuilder("function ");
                        constructor.Append(cn);
                        constructor.Append("(");
                        constructor.Append(_methodRef.Parameters[0].Name);
                        for (int i = 1; i < _methodRef.Parameters.Length; i++)
                        {
                            constructor.Append(",");
                            constructor.Append(_methodRef.Parameters[i].Name);
                        }
                        constructor.Append(")");
                        string cst    = constructor.ToString();
                        string scode  = method.ToString();
                        bool   bExist = (scode.IndexOf(cst) >= 0);
                        if (!bExist)
                        {
                            method.Add(cst);
                            method.Add("\r\n{\r\n");
                            method.Add("$this->");
                            method.Add(_methodRef.Parameters[0].Name);
                            method.Add(";\r\n");
                            for (int i = 1; i < _methodRef.Parameters.Length; i++)
                            {
                                constructor.Append("this->");
                                constructor.Append(_methodRef.Parameters[i].Name);
                                method.Add(";\r\n");
                            }
                            method.Add("\r\n}\r\n");
                        }
                        StringBuilder callC = new StringBuilder("new ");
                        callC.Append(cn);
                        callC.Append("(");
                        callC.Append(parameterCodePhp[0]);
                        for (int i = 1; i < parameterCodePhp.Length; i++)
                        {
                            callC.Append(",");
                            callC.Append(parameterCodePhp[i]);
                        }
                        callC.Append(")");
                        return(callC.ToString());
                    }
                }
                MathNode.Trace("Invoke member function {0} from {1}", FunctionName, _methodRef.MethodOwner.Name);
                if (string.IsNullOrEmpty(TargetObjectPhp))
                {
                    if (_methodRef.IsStatic)
                    {
                        TargetObjectPhp = _methodRef.MethodOwner.TypeString;
                    }
                    else
                    {
                        TargetObjectPhp = this.MethodOwner.CreatePhpScript(method, _methodRef.MethodOwner.XPath);
                    }
                }
                //
                StringBuilder e = new StringBuilder(TargetObjectPhp);
                if (!string.IsNullOrEmpty(TargetObjectPhp))
                {
                    e.Append("->");
                }
                e.Append(_methodRef.MethodName);
                e.Append("(");
                if (parameterCodePhp.Length > 0)
                {
                    e.Append(parameterCodePhp[0]);
                    for (int i = 1; i < parameterCodePhp.Length; i++)
                    {
                        e.Append(",");
                        e.Append(parameterCodePhp[i]);
                    }
                }
                e.Append(")");
                return(e.ToString());
            }
        }
Пример #8
0
        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");
                }
            }
        }
Пример #9
0
 public override void ExportJavaScriptCodeStatements(StringCollection method)
 {
     method.Add(MathNode.FormString("{0}={1};\r\n", this[0].CreateJavaScript(method), this[1].CreateJavaScript(method)));
 }
Пример #10
0
        public string CreatePhpScript(StringCollection method, string methodOwnerXPath)
        {
            MathNode.Trace("ObjectRef.CreatePhpScript for {0}", this);
            switch (Type)
            {
            case ObjectRefType.Argv:
            case ObjectRefType.EventSender:
                return(string.Format(CultureInfo.InvariantCulture, "${0}", _name));

            case ObjectRefType.This:
                return("$this");

            case ObjectRefType.Type:
                return(DataType.FullName);

            case ObjectRefType.Field:
            case ObjectRefType.Property:
                if (Owner == null)
                {
                    throw new MathException("property reference does not have an owner");
                }
                return(MathNode.FormString("{0}->{1}", Owner.CreatePhpScript(method, methodOwnerXPath), _name));

            case ObjectRefType.Const:
                if (_value == null)
                {
                    return("NULL");
                }
                else
                {
                    return(_value.DataValue.ToString());
                }

            case ObjectRefType.XPath:
                if (_xpathNode == null)
                {
                    throw new MathException("ObjectRef.ExportCode is called for XPath with null xpath Node");
                }
                if (string.IsNullOrEmpty(_name))
                {
                    throw new MathException("ObjectRef.ExportCode is called for XPath with null xpath value");
                }
                string codeExp = "$this";
                if (string.CompareOrdinal(methodOwnerXPath, _name) != 0)
                {
                    //object for the reference
                    //_name is the xpath
                    XmlNode thisNode = _xpathNode.OwnerDocument.DocumentElement.SelectSingleNode(_name);
                    if (thisNode == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("ObjectRef.ExportCode is called for XPath with invalid xpath value: {0}", _name));
                    }
                    //
                    if (string.CompareOrdinal(thisNode.Name, XmlSerialization.RAIS_R) == 0)
                    {
                        if (XmlSerialization.IsStaticComponent(thisNode))
                        {
                            codeExp = TypeString;
                        }
                        else
                        {
                            //find all names for each part R|A[@ID='?']R|A[@ID='?']...
                            string xpath0 = _name;
                            string name   = XmlSerialization.GetName(thisNode);
                            while (xpath0.Length > 0 && xpath0 != methodOwnerXPath)
                            {
                                int pos = xpath0.LastIndexOf('/');
                                if (pos > 0)
                                {
                                    xpath0 = xpath0.Substring(0, pos);
                                    if (xpath0 != methodOwnerXPath)
                                    {
                                        XmlNode n0 = XmlSerialization.GetXmlNodeByPath(thisNode.OwnerDocument, xpath0);
                                        if (n0 == null)
                                        {
                                            throw new MathException(XmlSerialization.FormatString("Invalid path {0}", xpath0));
                                        }
                                        name = MathNode.FormString("{0}->{1}", XmlSerialization.GetName(n0), name);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                            codeExp = MathNode.FormString("{0}->{1}", codeExp, name);
                        }
                    }
                }
                else
                {
                    XmlNode thisNode = _xpathNode.OwnerDocument.DocumentElement.SelectSingleNode(_name);
                    if (thisNode == null)
                    {
                        throw new MathException(XmlSerialization.FormatString("ObjectRef.CreateJavaScript is called for XPath with invalid xpath value: {0}", _name));
                    }
                    //use the parent node to generate code
                    if (thisNode.Name == XmlSerialization.RAIS_R)
                    {
                        if (XmlSerialization.IsStaticComponent(thisNode))
                        {
                            codeExp = TypeString;
                        }
                    }
                }
                return(codeExp);
            }
            return("$this");
        }