public IntepreterInstruction(InterpreterInstructionType type, int value, int sourceIndex)
     : this()
 {
     this.Type         = type;
     this.Value        = value;
     this.SourceIndex  = sourceIndex;
     this.SourceLength = 1;
 }
Esempio n. 2
0
        private static void HandlePointerAndValues(List <IntepreterInstruction> instructions, ref int pointerPosition, bool knownPosition, int i, char c, InterpreterInstructionType lastType, ref IntepreterInstruction nextInsn)
        {
            var movePointer = c == '<' || c == '>';
            var changeValue = c == '+' || c == '-';

            if (movePointer || changeValue)
            {
                var step = c == '-' || c == '<' ? -1 : +1;
                if (movePointer)
                {
                    pointerPosition += step;
                }

                if ((movePointer && (lastType == InterpreterInstructionType.SetPointer || lastType == InterpreterInstructionType.MovePointer)) ||
                    (changeValue && (lastType == InterpreterInstructionType.SetValue || lastType == InterpreterInstructionType.AddValue)))
                {
                    nextInsn        = instructions.Pop();
                    nextInsn.Value += step;
                    nextInsn.SourceLength++;
                }
                else if (movePointer)
                {
                    if (knownPosition)
                    {
                        nextInsn = new IntepreterInstruction(InterpreterInstructionType.SetPointer, pointerPosition, i);
                    }
                    else
                    {
                        nextInsn = new IntepreterInstruction(InterpreterInstructionType.MovePointer, pointerPosition, i);
                    }
                }
                else
                {
                    // if (changeValue)
                    nextInsn = new IntepreterInstruction(InterpreterInstructionType.AddValue, step, i);
                }
            }
        }
Esempio n. 3
0
        private static void HandleBeginLoop(string source, ref bool knownPosition, ref int i, InterpreterInstructionType lastType, ref IntepreterInstruction nextInsn)
        {
            // skip loop, after loop
            if (lastType == InterpreterInstructionType.EndLoop)
            {
                i = ScannLoop(source, i, true);
            }
            else
            {
                // check if pointer movements in loops are balanced, so we can know the absolute position of the pointer
                if (knownPosition && !BalancedPointerInLoop(source, i))
                {
                    knownPosition = false;
                }

                nextInsn = new IntepreterInstruction(InterpreterInstructionType.BeginLoop, 0, i);
            }
        }