Exemplo n.º 1
0
        /// <summary>
        /// evaluate expression
        /// </summary>
        /// <param name="args"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool eval(string args, out string result)
        {
            int i = 0;
            int k = 0;

            char[] buf = new char[5000];
            while (k < args.Length)
            {
                if (k < args.Length - 1 && ((args[k] == '{' && args[k + 1] == '{') || (args[k] == '}' && args[k + 1] == '}')))
                {
                    buf[i++] = args[k++];
                }
                else if (args[k] == '{')
                {
                    k++;
                    int    index = 0; //index of expr[]
                    char[] expr  = new char[4000];
                    while (k < args.Length && args[k] != '}')
                    {
                        expr[index++] = args[k];
                        k++;
                    }

                    if (k == args.Length)
                    {
                        cerr.WriteLine("Unclosed expression character }");
                        result = string.Empty;
                        return(false);
                    }

                    string code = new string(expr, 0, index);
                    if (IsFormatString(code))
                    {
                        buf[i++] = '{';
                        foreach (char ch in code)
                        {
                            buf[i++] = ch;
                        }
                        buf[i++] = '}';
                    }
                    else
                    {
                        string text = string.Empty;
                        try
                        {
                            VAL val = Script.Evaluate(code, Context.DS);
                            text = val.ToSimpleString();
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine($"error in {code}, {ex.Message}");
                        }

                        foreach (char ch in text)
                        {
                            buf[i++] = ch;
                        }
                    }
                }
                else if (args[k] == '}')
                {
                    cerr.WriteLine("Unclosed expression character }");
                    result = string.Empty;
                    return(false);
                }
                else
                {
                    buf[i++] = args[k];
                }

                k++;
            }

            result = new string(buf, 0, i);

            return(true);
        }