Exemple #1
0
        /*private IEnumerable<TInput> Next(IEnumerable<TInput> input)
         * {
         *  foreach(TInput t in input)
         *  {
         *      if (!Ignore.Contains(t))
         *      {
         *          yield return t;
         *      }
         *  }
         * }*/

        private TOutput Close(Evaluator quantity)
        {
            for (int j = 0; j < quantity.Operations.Length; j++)
            {
                LinkedList <LinkedListNode <object> > stack = quantity.Operations[j];

                while (stack?.Count > 0)
                {
                    LinkedListNode <object> node = stack.Dequeue().Value;
                    Operator <TOutput>      op   = quantity.Operators[j].Dequeue().Value;

                    // Operator was removed by someone else
                    if (node.List == null)
                    {
                        continue;
                    }
                    //Operator<TOutput> op = (Operator<TOutput>)node.Value;

                    IEditEnumerator <object>[] operandNodes = new IEditEnumerator <object> [op.Targets.Length];

                    for (int k = 0; k < op.Targets.Length; k++)
                    {
                        operandNodes[k] = new Parse.Collections.Generic.LinkedList <object> .Enumerator(node);

                        //op.Targets[k](operandNodes[k]);
                    }

                    TOutput[] operands = new TOutput[operandNodes.Length];
                    for (int k = 0; k < operands.Length; k++)
                    {
                        operands[k] = (TOutput)operandNodes[k].Current;
                        operandNodes[k].Remove(0);
                    }

                    Print.Log("operating", op.GetType(), operands.Length);
                    foreach (object o in operands)
                    {
                        Print.Log(o, o.GetType());
                    }
                    Print.Log("done");

                    node.Value = op.Operate(operands);
                }
            }

            if (quantity.Input.Count == 0)
            {
                throw new Exception();
            }

            //IOrdered<object> itr = new LinkedListBiEnumerator<object>(input);
            //itr.MoveNext();
            //Other.LinkedList<object> list = input;
            return(Juxtapose(quantity.Input.GetEnumerator()));
        }
Exemple #2
0
        public TOutput Parse(IEditEnumerable <TInput> input)
        {
            string parsing = "parsing |";

            foreach (TInput s in input)
            {
                parsing += s + "|";
            }
            Print.Log(parsing);

            Stack <Evaluator> quantities = new Stack <Evaluator>();

            IEditEnumerator <TInput> itr = input.GetEnumerator();

            quantities.Push(new Evaluator(new Parse.Collections.Generic.LinkedList <object>(), new LinkedList <LinkedListNode <object> > [Count], new LinkedList <Operator <TOutput> > [Count]));

            while (true)
            {
                Evaluator quantity = quantities.Peek();

                bool done = !itr.MoveNext();

                if (done || Closing.Contains(itr.Current))
                {
                    Evaluator e = quantities.Pop();
                    Print.Log("close", e.Input.Count);
                    LinkedListNode <object> a = e.Input.First;
                    while (a != null)
                    {
                        Print.Log(a.Value, a.Value?.GetType());
                        a = a.Next;
                    }
                    Print.Log("\n");

                    TOutput answer = Close(e);

                    if (quantities.Count == 0)
                    {
                        if (done)
                        {
                            return(answer);
                        }
                        else
                        {
                            Parse.Collections.Generic.LinkedList <object> front = new Parse.Collections.Generic.LinkedList <object>();
                            front.AddFirst(answer);
                            quantities.Push(new Evaluator(front, new LinkedList <LinkedListNode <object> > [Count], new LinkedList <Operator <TOutput> > [Count]));
                        }
                    }
                    else
                    {
                        quantities.Peek().Input.AddLast(answer);
                    }
                }
                else if (Opening.Contains(itr.Current))
                {
                    Print.Log("open");
                    quantities.Push(new Evaluator(new Parse.Collections.Generic.LinkedList <object>(), new LinkedList <LinkedListNode <object> > [Count], new LinkedList <Operator <TOutput> > [Count]));
                }
                else
                {
                    Operator <TOutput> operation;
                    if (Operations.TryGetValue(itr.Current, out operation))
                    {
                        Print.Log("found operator", itr.Current);

                        int index = IndexOf(itr.Current);

                        // Put the operator in the linked list as a node
                        LinkedListNode <object> node = new LinkedListNode <object>(itr.Current);

                        // Get the list of all of this type of operator (e.g. all instances of "+")
                        if (quantity.Operations[index] == null)
                        {
                            quantity.Operations[index] = new LinkedList <LinkedListNode <object> >();
                            quantity.Operators[index]  = new LinkedList <Operator <TOutput> >();
                        }

                        if (operation.Order == ProcessingOrder.RightToLeft)
                        {
                            quantity.Operations[index].AddFirst(node);
                            quantity.Operators[index].AddFirst(operation);
                        }
                        else
                        {
                            quantity.Operations[index].AddLast(node);
                            quantity.Operators[index].AddLast(operation);
                        }

                        quantity.Input.AddLast(node);
                    }
                    else
                    {
                        Print.Log("found operand", itr.Current);

                        foreach (TOutput o in ParseOperand(itr.Current))
                        {
                            Print.Log("\t" + o);
                            quantity.Input.AddLast(o);
                        }
                    }
                }
            }
        }
Exemple #3
0
 public Evaluator(Parse.Collections.Generic.LinkedList <object> input, LinkedList <LinkedListNode <object> >[] operations, LinkedList <Operator <TOutput> >[] operators)
 {
     Input      = input;
     Operations = operations;
     Operators  = operators;
 }