コード例 #1
0
 public static double Divide(double a, ExprBase nextExpr, EventParams envParams)
 {
     try
     {
         return(a / nextExpr.Execute(envParams));
     }
     catch
     {
         Console.WriteLine("参与计算的数据类型不匹配");
         return(0);
     }
 }
コード例 #2
0
 public static double NoEqual(double a, ExprBase nextExpr, EventParams envParams)
 {
     try
     {
         return((a != nextExpr.Execute(envParams)) ? 1 : 0);
     }
     catch
     {
         Console.WriteLine("参与计算的数据类型不匹配");
         return(0);
     }
 }
コード例 #3
0
 public static double Or(double a, ExprBase nextExpr, EventParams envParams)
 {
     try
     {
         if (Convert.ToBoolean(a))
         {
             return(1);
         }
         if (Convert.ToBoolean(nextExpr.Execute(envParams)))
         {
             return(1);
         }
         return(0);
     }
     catch
     {
         Console.WriteLine("参与计算的数据类型不匹配");
         return(0);
     }
 }
コード例 #4
0
ファイル: ExprBuilderTests.cs プロジェクト: danthomas/Parsing
        private void ExprToString(ExprBase expr, StringBuilder stringBuilder, int indent = 0, string prefix = "")
        {
            var textExpr = expr as TextExpr;
            var attrExpr = expr as AttrExpr;
            var blockExpr = expr as BlockExpr;
            var conditionalExpr = expr as ConditionalExpr;

            stringBuilder.AppendLine();
            stringBuilder.Append(new string(' ', indent * 4));
            stringBuilder.Append(prefix);
            stringBuilder.Append(expr.GetType().Name);
            stringBuilder.Append(" : ");

            if (textExpr != null)
            {
                stringBuilder.Append(textExpr.Text);
            }
            else if (attrExpr != null)
            {
                stringBuilder.Append(attrExpr.Name);
            }
            else if (conditionalExpr != null)
            {
                stringBuilder.Append(conditionalExpr.Attr);
                stringBuilder.Append(" ");
                stringBuilder.Append(conditionalExpr.Operator);
                stringBuilder.Append(" ");
                stringBuilder.Append(String.Join("|", conditionalExpr.Values));

                ExprToString(conditionalExpr.TrueExpr, stringBuilder, indent + 1, " Then ");
                if (conditionalExpr.FalseExpr != null)
                {
                    ExprToString(conditionalExpr.FalseExpr, stringBuilder, indent + 1, " Else ");
                }
            }
            else if (blockExpr != null)
            {
                foreach (var exprBase in blockExpr.Exprs)
                {
                    ExprToString(exprBase, stringBuilder, indent + 1);
                }
            }
        }
コード例 #5
0
    //--------------------------------------------------------------------
    #region 内部方法
    ExprBase Parse(Queue <string> stringQueue)
    {
        string   word = null;
        ExprBase first = null, prev = null, last = null;

        while (stringQueue.Count > 0)
        {
            word = stringQueue.Dequeue();
            if (word == "(")
            {
                ExprBase subExpr = Parse(stringQueue);
                if (subExpr == null)
                {
                    return(null);
                }
                last = new SubExpr(subExpr);
            }
            else if (word[0] == '\'')
            {
                int endPos = word.LastIndexOf('\'');
                if (endPos > 0)
                {
                    last = new StrExpr(word.Substring(1, endPos - 1));
                }
                else
                {
                    return(null);
                }
            }
            else if (word[0] == '\"')
            {
                int endPos = word.LastIndexOf('\"');
                if (endPos > 0)
                {
                    last = new StrExpr(word.Substring(1, endPos - 1));
                }
                else
                {
                    return(null);
                }
            }
            else if (word == "true")
            {
                last = new ConstExpr(1);
            }
            else if (word == "false")
            {
                last = new ConstExpr(0);
            }
            else if (word[0] == '-' || Char.IsDigit(word[0]))
            {
                if (word.Contains("."))
                {
                    last = new ConstExpr(float.Parse(word));
                }
                else
                {
                    last = new ConstExpr(int.Parse(word));
                }
            }
            else
            {
                last = ParseFunc(word, stringQueue);
            }

            if (last == null)
            {
                return(null);
            }

            if (first == null)
            {
                first = prev = last;
            }
            else
            {
                prev.next = last;
                prev      = last;
            }

            // 条件连接关系
            if (stringQueue.Count > 0)
            {
                word = stringQueue.Dequeue();
                if (word == ")")
                {
                    break;
                }

                last.connector = ExprConnectors.Get(word);
                if (last.connector == null)
                {
                    return(null);
                }
            }

            last = null;
        }
        return(first);
    }
コード例 #6
0
    List <ExprBase> ParseParams(Queue <string> stringQueue, char endBracket)
    {
        string          word         = null;
        List <ExprBase> parameters   = new List <ExprBase>();
        List <ExprBase> subParams    = null;
        bool            exprComplete = false;

        while (stringQueue.Count > 0)
        {
            word = stringQueue.Dequeue();
            if (word[0] == ',')
            {
                break;
            }
            else if (word[0] == endBracket)
            {
                exprComplete = true;
                break;
            }
            else if (word[0] == '\'')
            {
                int endPos = word.LastIndexOf('\'');
                if (endPos < 0)
                {
                    break;
                }
                parameters.Add(new StrExpr(word.Substring(1, endPos - 1)));
            }
            else if (word[0] == '"')
            {
                int endPos = word.LastIndexOf('"');
                if (endPos < 0)
                {
                    break;
                }
                parameters.Add(new StrExpr(word.Substring(1, endPos - 1)));
            }
            else if (word[0] == '{')
            {
                subParams = ParseParams(stringQueue, '}');
                if (subParams == null)
                {
                    break;
                }
                parameters.Add(new ListExpr(subParams));
            }
            else if (word == "true")
            {
                parameters.Add(new ConstExpr(1));
            }
            else if (word == "false")
            {
                parameters.Add(new ConstExpr(0));
            }
            else if (word[0] == '@' && word[1] == 'B')
            {
                string str = word.Substring(2);
                parameters.Add(new ConstExpr(int.Parse(str)));
            }
            else if (Char.IsDigit(word[0]) || word[0] == '-')
            {
                if (word.Contains("."))
                {
                    parameters.Add(new ConstExpr(float.Parse(word)));
                }
                else
                {
                    parameters.Add(new ConstExpr(int.Parse(word)));
                }
            }
            else
            {
                ExprBase funcExpr = ParseFunc(word, stringQueue);
                if (funcExpr == null)
                {
                    break;
                }
                parameters.Add(funcExpr);
            }

            // 分隔符
            word = stringQueue.Dequeue();
            if (word[0] == ',')
            {
                continue;
            }

            // 结束符
            if (word[0] == endBracket)
            {
                exprComplete = true;
            }
            break;
        }

        if (!exprComplete)
        {
            Console.WriteLine("字串格式不正确:{0}", word);
            return(null);
        }
        return(parameters);
    }
コード例 #7
0
 public SubExpr(ExprBase val)
 {
     this.val = val;
 }