コード例 #1
0
ファイル: ExpressionPrecedence.cs プロジェクト: bartwe/plukc
 internal static string operatorName(OperatorKind kind, string name)
 {
     switch (kind)
     {
         case OperatorKind.Prefix:
             return name + "_prefix";
         case OperatorKind.Infix:
             return name + "_infix";
         case OperatorKind.Postfix:
             return name + "_postfix";
         default:
             {
                 Require.NotCalled();
                 return null;
             }
     }
 }
コード例 #2
0
ファイル: ExpressionPrecedence.cs プロジェクト: bartwe/plukc
        public Expression Reduce()
        {
            int level = 0;
            while (list.Count > 1)
            {
                Require.True(level <= precedence.highestLevel);
                // needs to be a for loop, the list gets modified in action
                //forward
                Opera_nd_tor item;
                for (int j = 0; j < 2; ++j)
                {
                    bool leftToRight = j == 0;
                    int i;
                    if (leftToRight)
                        i = -1;
                    else
                        i = list.Count;                        
                    while (true)
                    {
                        if (leftToRight)
                        {
                            i++;
                            if (i == list.Count)
                                break;
                        }
                        else
                        {
                            if (i == 0)
                                break;
                            i--;
                        }
                        item = list[i];
                        if (item.IsOperator && (item.Precedence == level) && (item.LeftToRight == leftToRight))
                        {
                            switch (item.Kind)
                            {
                                case OperatorKind.Infix:
                                    {
                                        i = i - 1;
                                        Expression left = list[i].Expression;
                                        Expression right = list[i + 2].Expression;
                                        list.RemoveRange(i, 3);
                                        list.Insert(i, item.ReduceInfix(left, right));
                                        break;
                                    }
                                case OperatorKind.Prefix:
                                    {
                                        Expression right = list[i + 1].Expression;
                                        list.RemoveRange(i, 2);
                                        list.Insert(i, item.ReducePrefix(right));
                                        break;
                                    }
                                case OperatorKind.Postfix:
                                    {
                                        i = i - 1;
                                        Expression left = list[i].Expression;
                                        list.RemoveRange(i, 2);
                                        list.Insert(i, item.ReducePostFix(left));
                                        break;
                                    }
                                default:
                                    {
                                        Require.NotCalled();
                                        break;
                                    }
                            }
                        }
                    }
                }

                level++;
            }
            Expression result = list[0].Expression;
            list.Clear();
            return result;
        }