Example #1
0
 public void FinalizeExpression()
 {
     for (int i = stacks.Count - 1; i >= 0; i--)
     {
         RusticStack stack = stacks[i];
         stack.displayId = i;
         stack.Prepare();
     }
 }
Example #2
0
        private void PutOperationOfLowerPriority(RusticOperation operation)
        {
            nextOperation = operation;
            while (currentStack.priority > operation.GetPriorityWithOffset())
            {
                currentStack = currentStack.parent;
            }

            if (currentStack.priority < operation.GetPriorityWithOffset())
            {
                PutOperationOfHigherPriority(operation);
            }
        }
Example #3
0
 private void ChangePriority(int addition)
 {
     priorityOffset += addition;
     if (addition > 0)
     {
         RusticStack newStack = new RusticStack(stacks.Count, currentStack, priorityOffset + RusticOperation.FirstPriority);
         nextOperation.parameter = new Evaluators.StackReference(newStack);
         currentStack.operations.Add(nextOperation);
         int index = stacks.IndexOf(currentStack);
         stacks.Insert(index + 1, newStack);
         currentStack  = newStack;
         nextOperation = new Operations.Set();
     }
 }
Example #4
0
        public RusticExprBuilder(RusticExpr expression, RusticContext context)
        {
            if (expression?.stacks.Count > 0)
            {
                throw new Exception("RusticExpr instance was already built and should not be built again. Did you intend to ResetExpression?");
            }

            this.expression = expression;
            this.context    = context;
            nextOperation   = new Operations.Set();
            priorityOffset  = 0;
            stacks?.Add(new RusticStack(0, null, 1));
            currentStack = stacks?[0];
        }
Example #5
0
        private void PutOperationOfHigherPriority(RusticOperation operation)
        {
            nextOperation = operation;
            RusticOperation      lastOperationFromStack = currentStack.operations[currentStack.operations.Count - 1];
            RusticValueEvaluator lastOperationValue     = lastOperationFromStack.parameter;
            RusticStack          newStack = new RusticStack(stacks.Count, currentStack, operation.GetPriorityWithOffset());

            lastOperationFromStack.parameter = new Evaluators.StackReference(newStack);
            RusticOperation newSetOperation = new Operations.Set();

            newSetOperation.parameter = lastOperationValue;
            newStack.operations.Add(newSetOperation);
            int index = stacks.IndexOf(currentStack);

            stacks.Insert(index + 1, newStack);
            currentStack = newStack;
        }
Example #6
0
        private void PutLeftOperationToken(RusticOperation operation)
        {
            if (currentStack.priority == operation.GetPriorityWithOffset())
            {
                currentStack.operations.Add(operation);
            }
            else
            {
                RusticStack newStack = new RusticStack(stacks.Count, currentStack, operation.GetPriorityWithOffset());
                newStack.operations.Add(operation);
                int index = stacks.IndexOf(currentStack);
                stacks.Insert(index + 1, newStack);

                PutCustomValueToken(new Evaluators.StackReference(newStack));
                currentStack  = newStack;
                nextOperation = new Operations.Set();
            }
        }
Example #7
0
        public void SimplifyExpression()
        {
            HashSet <RusticStack> discard = null;

            foreach (RusticStack stack in stacks)
            {
                foreach (RusticOperation operation in stack.operations)
                {
                    Evaluators.StackReference reference = null;
                    bool hasRefParam     = operation.parameter != null && (reference = operation.parameter as Evaluators.StackReference) != null;
                    bool canBeSimplified = hasRefParam && reference.stack.CanBeSimplified;
                    if (canBeSimplified)
                    {
                        operation.parameter = reference.stack.operations[0].parameter;
                        discard             = discard ?? new HashSet <RusticStack>();
                        discard.Add(reference.stack);
                    }
                }
            }

            if (discard != null)
            {
                foreach (RusticStack item in discard)
                {
                    stacks.Remove(item);
                }
            }

            // Simplify the stack register R0
            if (stacks.Count > 0 && stacks[0].operations.Count == 1)
            {
                RusticOperation set;
                if ((set = stacks[0].operations[0] as Operations.Set) != null)
                {
                    stacks[0].operations.Clear();
                    RusticStack stack = ((Evaluators.StackReference)set.parameter).stack;
                    stacks[0].operations.AddRange(stack.operations);
                    stacks[0].priority = stack.priority;
                    stacks.Remove(stack);
                }
            }
            FinalizeExpression();
        }
Example #8
0
 public RusticStack(int id, RusticStack parent, int priority)
 {
     this.displayId = id;
     this.parent    = parent;
     this.priority  = priority;
 }