Example #1
0
        private InnerItem Inner(string current, char quotchar)
        {
            var inner = new InnerItem
            {
                Value     = current,
                Quote     = quotchar,
                InnerType = InnerType.TYPE_STRING
            };

            if (inner.Quote != '\'' && inner.Quote != '"')
            {
                if (current == "true" || current == "false")
                {
                    inner.InnerType = InnerType.TYPE_BOOLEAN;
                    if (current == "true")
                    {
                        inner.Value = true;
                    }
                    else
                    {
                        inner.Value = false;
                    }
                }
                else if (inner.Quote == '\0' && current.IsNumeric())
                {
                    inner.InnerType = InnerType.TYPE_NUMERIC;
                    inner.Value     = double.Parse(current.ToString());
                }
                else
                {
                    inner.InnerType = InnerType.TYPE_VARIABLE;
                }
            }
            return(inner);
        }
Example #2
0
        public static object GetPropValue(InnerItem item, object vars, object localvars = null)
        {
            object res  = null;
            var    name = item.Value.ToString();

            if (localvars != null)
            {
                res = GetProp(name, localvars);
            }
            if (res == null)
            {
                res = GetProp(name, vars);
            }
            return(res);
        }
Example #3
0
        public ComputeResult Compute(object vars = null, InnerItem sender = null, object localvars = null)
        {
            var       cr            = new ComputeResult();
            object    lastvalue     = null;
            InnerItem xoperator     = null;
            InnerItem previtem      = null;
            InnerItem waititem      = null;
            InnerItem waititem2     = null;
            string    waitop        = "";
            object    waitvalue     = null;
            string    waitop2       = "";
            object    waitvalue2    = null;
            string    waitkey       = "";
            bool      unlemused     = false;
            bool      stopdoubledot = false;

            if (this.IsObject())
            {
                cr.Result.AddObject(new ExpandoObject());
            }
            for (int i = 0; i < this.InnerItems.Count; i++)
            {
                object    currentitemvalue = null;
                InnerItem current          = this.InnerItems[i];
                ParItem   paritem          = null;
                if (current.IsParItem())
                {
                    paritem = current as ParItem;
                }
                if (stopdoubledot)
                {
                    if (current.IsOperator && current.Value.ToString() == ":")
                    {
                        break;
                    }
                }
                InnerItem next   = null;
                string    nextop = "";
                if (i + 1 < this.InnerItems.Count)
                {
                    next = this.InnerItems[i + 1];
                }
                if (next != null && next.IsOperator)
                {
                    nextop = next.Value.ToString();
                }
                if (current.IsParItem())
                {
                    var    subresult = paritem.Compute(vars, this, localvars);
                    string prevvalue = "";
                    bool   previsvar = false;
                    if (previtem != null && !previtem.IsOperator && previtem.Value != null)
                    {
                        previsvar = previtem.InnerType == InnerType.TYPE_VARIABLE;
                        prevvalue = previtem.Value.ToString();
                    }
                    object varnew = null;
                    if (lastvalue != null)
                    {
                        varnew = lastvalue;
                    }
                    else
                    {
                        varnew = vars;
                    }
                    if (prevvalue != "")
                    {
                        if (paritem.ParName == "(")
                        {
                            currentitemvalue = ComputeActions.CallMethod(prevvalue, subresult.Result.GetObjects(), varnew, localvars);
                        }
                        else if (paritem.ParName == "[")
                        {
                            var prop = ComputeActions.GetProp(prevvalue, varnew);
                            if (PhpFuctions.is_array(prop))
                            {
                                int indis  = (int)Convert.ChangeType(subresult.Result[0], TypeCode.Int32);
                                var aritem = prop as IList;

                                currentitemvalue = aritem[indis];
                            }
                            else if (PhpFuctions.is_indis(prop))
                            {
                                var indisProp = prop.GetType().GetProperty("Item");
                                var newParams = ParamUtil.MatchParams(subresult.Result.GetObjects(), indisProp.GetIndexParameters());
                                currentitemvalue = indisProp.GetValue(prop, newParams);
                            }
                            else
                            {
                                int indis = (int)Convert.ChangeType(subresult.Result[0], TypeCode.Int32);
                                currentitemvalue = ((string)prop)[indis];
                            }
                        }
                    }
                    else
                    {
                        if (paritem.ParName == "(")
                        {
                            currentitemvalue = subresult.Result[0];
                        }
                        else if (paritem.ParName == "[")
                        {
                            if (subresult.Result.KeysIncluded())
                            {
                                currentitemvalue = subresult.Result.ToDictionary();
                            }
                            else
                            {
                                currentitemvalue = subresult.Result.GetObjects();
                            }
                        }
                        else if (paritem.ParName == "{")
                        {
                            currentitemvalue = subresult.Result.First();
                        }
                    }
                }
                else
                {
                    if (!current.IsOperator && current.InnerType == InnerType.TYPE_VARIABLE && next != null && next.IsParItem())
                    {
                        currentitemvalue = null;
                    }
                    else
                    {
                        currentitemvalue = current.Value;
                    }

                    if (current.InnerType == InnerType.TYPE_VARIABLE && (next == null || !next.IsParItem()) && (xoperator == null || xoperator.Value.ToString() != "."))
                    {
                        if (currentitemvalue == null || currentitemvalue.ToString() == "null")
                        {
                            currentitemvalue = null;
                        }
                        else if (currentitemvalue.ToString() == "false")
                        {
                            currentitemvalue = false;
                        }
                        else if (currentitemvalue.ToString() == "true")
                        {
                            currentitemvalue = true;
                        }
                        else if (!this.IsObject())
                        {
                            currentitemvalue = ComputeActions.GetPropValue(current, vars, localvars);
                        }
                    }
                }
                if (unlemused)
                {
                    currentitemvalue = !PhpFuctions.not_empty(currentitemvalue);
                    unlemused        = false;
                }
                if (current.IsOperator)
                {
                    if (current.Value.ToString() == "!")
                    {
                        unlemused = !unlemused;
                        previtem  = current;
                        continue;
                    }
                    if ((this.IsParItem() && current.Value.ToString() == ",") || (this.IsArray() && current.Value.ToString() == "=>" && (waitvalue == null || waitvalue.ToString() == "")) || (this.IsObject() && current.Value.ToString() == ":" && (waitvalue == null || waitvalue.ToString() == "")))
                    {
                        if (waitop2 != "")
                        {
                            lastvalue  = ComputeActions.OperatorResult(waitvalue2, lastvalue, waitop2);
                            waitvalue2 = null;
                            waitop2    = "";
                        }
                        if (waitop != "")
                        {
                            lastvalue = ComputeActions.OperatorResult(waitvalue, lastvalue, waitop);
                            waitvalue = null;
                            waitop    = "";
                        }
                        if (current.Value.ToString() == ",")
                        {
                            if (this.IsObject())
                            {
                                var exp = cr.Result.First <IDictionary <string, object> >();
                                exp.Add(waitkey, lastvalue);
                            }
                            else
                            {
                                cr.Result.AddObject(waitkey, lastvalue);
                            }

                            waitkey = "";
                        }
                        else
                        {
                            waitkey = lastvalue.ToString();
                        }
                        lastvalue = null;
                        xoperator = null;
                        previtem  = current;
                        continue;
                    }
                    string opstr = current.Value.ToString();
                    if (opstr == "||" || opstr == "|" || opstr == "or" || opstr == "&&" || opstr == "&" || opstr == "and" || opstr == "?")
                    {
                        if (waitop2 != "")
                        {
                            lastvalue  = ComputeActions.OperatorResult(waitvalue2, lastvalue, waitop2);
                            waitvalue2 = null;
                            waitop2    = "";
                        }
                        if (waitop != "")
                        {
                            lastvalue = ComputeActions.OperatorResult(waitvalue, lastvalue, waitop);
                            waitvalue = null;
                            waitop    = "";
                        }

                        bool state = PhpFuctions.not_empty(lastvalue);
                        xoperator = null;
                        if (opstr == "?")
                        {
                            if (state)
                            {
                                stopdoubledot = true;
                            }
                            else
                            {
                                for (int j = i + 1; j < this.InnerItems.Count; j++)
                                {
                                    var item = this.InnerItems[j];
                                    if (item.IsOperator && item.Value.ToString() == ":")
                                    {
                                        i = j;
                                        break;
                                    }
                                }
                            }
                            lastvalue = null;
                            previtem  = current;
                            continue;
                        }



                        if (opstr == "||" || opstr == "|" || opstr == "or")
                        {
                            if (state)
                            {
                                lastvalue = true;
                                if (opstr != "|")
                                {
                                    if (this.IsObject())
                                    {
                                        var exp = cr.Result.First <IDictionary <string, object> >();
                                        exp.Add(waitkey, true);
                                    }
                                    else
                                    {
                                        cr.Result.AddObject(waitkey, true);
                                    }
                                    return(cr);
                                }
                            }
                            else
                            {
                                lastvalue = false;
                            }
                        }
                        else
                        {
                            if (!state)
                            {
                                lastvalue = false;
                                if (opstr != "&")
                                {
                                    if (this.IsObject())
                                    {
                                        var exp = cr.Result.First <IDictionary <string, object> >();
                                        exp.Add(waitkey, false);
                                    }
                                    else
                                    {
                                        cr.Result.AddObject(waitkey, false);
                                    }
                                    return(cr);
                                }
                            }
                            else
                            {
                                lastvalue = true;
                            }
                        }
                        xoperator = current;
                    }
                    else
                    {
                        xoperator = current;
                    }

                    previtem = current;
                    continue;
                }
                else
                {
                    if (xoperator != null)
                    {
                        if (ComputeActions.PriotiryStop.Contains(xoperator.Value.ToString()))
                        {
                            if (waitop2 != "")
                            {
                                lastvalue  = ComputeActions.OperatorResult(waitvalue2, lastvalue, waitop2);
                                waitvalue2 = null;
                                waitop2    = "";
                            }
                            if (waitop != "")
                            {
                                lastvalue = ComputeActions.OperatorResult(waitvalue, lastvalue, waitop);
                                waitvalue = null;
                                waitop    = "";
                            }
                        }

                        if (next != null && next.IsParItem())
                        {
                            if (xoperator.Value.ToString() == ".")
                            {
                                if (currentitemvalue != null && !string.IsNullOrEmpty(currentitemvalue.ToString()))
                                {
                                    lastvalue = ComputeActions.GetProp(currentitemvalue.ToString(), lastvalue);
                                }
                            }
                            else
                            {
                                if (waitop == "")
                                {
                                    waitop    = xoperator.Value.ToString();
                                    waititem  = current;
                                    waitvalue = lastvalue;
                                }
                                else if (waitop2 == "")
                                {
                                    waitop2    = xoperator.Value.ToString();
                                    waititem2  = current;
                                    waitvalue2 = lastvalue;
                                }
                                lastvalue = null;
                            }
                            xoperator = null;
                            previtem  = current;
                            continue;
                        }
                        if (xoperator.Value.ToString() == ".")
                        {
                            lastvalue = ComputeActions.GetProp(currentitemvalue.ToString(), lastvalue);
                        }
                        else if (nextop != "." && ((xoperator.Value.ToString() != "+" && xoperator.Value.ToString() != "-") || nextop == "" || (ComputeActions.PriotiryStop.Contains(nextop))))
                        {
                            var opresult = ComputeActions.OperatorResult(lastvalue, currentitemvalue, xoperator.Value.ToString());
                            lastvalue = opresult;
                        }
                        else
                        {
                            if (waitop == "")
                            {
                                waitop    = xoperator.Value.ToString();
                                waititem  = current;
                                waitvalue = lastvalue;
                                lastvalue = currentitemvalue;
                            }
                            else if (waitop2 == "")
                            {
                                waitop2    = xoperator.Value.ToString();
                                waititem2  = current;
                                waitvalue2 = lastvalue;
                                lastvalue  = currentitemvalue;
                            }

                            continue;
                        }
                    }
                    else
                    {
                        lastvalue = currentitemvalue;
                    }
                }

                previtem = current;
            }
            if (waitop2 != "")
            {
                lastvalue  = ComputeActions.OperatorResult(waitvalue2, lastvalue, waitop2);
                waitvalue2 = null;
                waitop2    = "";
            }
            if (waitop != "")
            {
                lastvalue = ComputeActions.OperatorResult(waitvalue, lastvalue, waitop);
                waitvalue = null;
                waitop    = "";
            }
            if (this.IsObject())
            {
                var exp = cr.Result.First <IDictionary <string, object> >();
                exp.Add(waitkey, lastvalue);
            }
            else
            {
                cr.Result.AddObject(waitkey, lastvalue);
            }
            return(cr);
        }
Example #4
0
        public void Decode()
        {
            InnerItem parentItem = this.Items;
            bool      isopened   = false;

            for (int i = 0; i < this.TextLength; i++)
            {
                var cur  = this.Text[i];
                var prev = '\0';
                if (i - 1 >= 0)
                {
                    prev = this.Text[i - 1];
                }
                if ((prev != ')' && prev != ']' && prev != '}') && (cur == '=' || cur == '>' || cur == '<' || cur == '?' || cur == ':'))
                {
                    if (isopened)
                    {
                        InnerItem item = new InnerItem();
                        item.IsOperator = true;
                        if ((prev == '>' && cur == '=') || (prev == '<' && cur == '=') || (prev == '!' && cur == '=') || (prev == '=' && cur == '>'))
                        {
                            item.Value = prev.ToString() + cur.ToString();
                        }
                        else

                        {
                            item.Value = cur;
                        }
                        parentItem = parentItem.Parent;
                        isopened   = false;
                        parentItem.InnerItems.Add(item);
                        i--;
                    }
                    else
                    {
                        var item = new ParItem
                        {
                            Parent  = parentItem,
                            ParName = "("
                        };
                        parentItem.InnerItems.Add(item);
                        parentItem = item;
                        isopened   = true;
                    }
                    continue;
                }
                if (cur == '(' || cur == '[' || cur == '{')
                {
                    if (isopened)
                    {
                        //isopened = false;
                    }
                    var item = new ParItem
                    {
                        Parent  = parentItem,
                        ParName = cur.ToString(),
                    };
                    parentItem.InnerItems.Add(item);
                    parentItem = item;
                    continue;
                }
                else if (cur == ')' || cur == ']' || cur == '}')
                {
                    parentItem = parentItem.Parent;
                    if (parentItem == null)
                    {
                        throw new Exception("Syntax Error");
                    }
                    continue;
                }
                var result = this.DecodeText(i, isopened);
                parentItem.InnerItems.AddRange(result);
                i = this.pos;
            }
        }
Example #5
0
        private InnerItemsList DecodeText(int start, bool autopar = false)
        {
            var           inspec     = false;
            var           inquot     = false;
            char          qutochar   = '\0';
            var           innerItems = new InnerItemsList();
            StringBuilder value      = new StringBuilder();

            for (int i = start; i < this.TextLength; i++)
            {
                var  cur  = this.Text[i];
                char next = '\0';
                if (i + 1 < this.TextLength)
                {
                    next = this.Text[i + 1];
                }
                if (inspec)
                {
                    value.Append(cur);
                    inspec = false;
                    continue;
                }
                if (cur == '\\')
                {
                    inspec = true;
                    continue;
                }
                if (!inquot)
                {
                    if (cur == ' ' || cur == '\t')
                    {
                        continue;
                    }
                    if (cur == '\'' || cur == '\"')
                    {
                        inquot   = true;
                        qutochar = cur;
                        continue;
                    }
                    if (cur == '+' || cur == '-' || cur == '*' ||
                        cur == '/' || cur == '%' || cur == '!' ||
                        cur == '=' || cur == '&' || cur == '|' ||
                        cur == ')' || cur == '(' || cur == ',' ||
                        cur == '[' || cur == ']' || cur == '^' ||
                        cur == '<' || cur == '>' || cur == '{' ||
                        cur == '}' || (cur == ':' && next != ':') || cur == '?' || cur == '.')
                    {
                        if (value.Length > 0)
                        {
                            innerItems.Add(this.Inner(value.ToString(), qutochar));
                            value.Clear();
                        }
                        if (cur == '[' || cur == '(' || cur == '{')
                        {
                            this.pos = i - 1;
                            return(innerItems);
                        }
                        if (autopar && (cur == '?' || cur == ':' || cur == '=' || cur == '<' || cur == '>' || (cur == '!' && next == '=')))
                        {
                            if ((cur == '=' && next == '>') || (cur == '!' && next == '=') || (cur == '>' && next == '=') || (cur == '<' && next == '='))
                            {
                                this.pos = i;
                            }
                            else
                            {
                                this.pos = i - 1;
                            }
                            return(innerItems);
                        }

                        if (cur != '(' && cur != ')' && cur != '[' && cur != ']' && cur != '{' && cur != '}')
                        {
                            var inner2 = new InnerItem
                            {
                                IsOperator = true
                            };
                            if ((cur == '=' && next == '>') || (cur == '!' && next == '=') || (cur == '>' && next == '=') || (cur == '<' && next == '='))
                            {
                                inner2.Value = cur.ToString() + next.ToString();
                                i++;
                            }
                            else if ((cur == '=' || cur == '&' || cur == '|') && cur == next)
                            {
                                inner2.Value = cur.ToString() + next.ToString();
                                i++;
                            }
                            else
                            {
                                inner2.Value = cur.ToString();
                            }
                            string valuestr = (string)inner2.Value;
                            innerItems.Add(inner2);
                            qutochar = '\0';
                            if (valuestr == "=" || valuestr == "<=" || valuestr == ">=" || valuestr == "<" || valuestr == ">" || valuestr == "!=" || valuestr == "==")
                            {
                                this.pos = i - 1;
                                return(innerItems);
                            }
                        }
                        else
                        {
                            this.pos = i - 1;
                            return(innerItems);
                        }
                        continue;
                    }
                }
                else
                {
                    if (cur == qutochar)
                    {
                        inquot = false;
                        continue;
                    }
                }

                if (cur == ':' && next == ':')
                {
                    value.Append(':');
                    i++;
                }
                value.Append(cur);
            }
            if (value.Length > 0)
            {
                innerItems.Add(this.Inner(value.ToString(), qutochar));
            }
            this.pos = this.TextLength;

            return(innerItems);
        }