示例#1
0
 private void FoldLabels()
 {
     for (int i = 0; i < oldInstructions.Length; i++)
     {
         if (oldInstructions[i] is JumpIfLessThanOrEqual)
         {
             JumpIfLessThanOrEqual instr = (JumpIfLessThanOrEqual)oldInstructions[i];
             string label = instr.labelName;
             int    index = new List <Instruction>(oldInstructions).FindIndex(
                 instruction => (instruction as Label) != null &&
                 (instruction as Label).label == label);
             instr.position = index;
             newInstructions.Add(instr);
         }
         else if (oldInstructions[i] is JumpIfFalse)
         {
             JumpIfFalse instr = (JumpIfFalse)oldInstructions[i];
             string      label = instr.labelName;
             int         index = new List <Instruction>(oldInstructions).FindIndex(
                 instruction => (instruction as Label) != null &&
                 (instruction as Label).label == label);
             instr.position = index;
             newInstructions.Add(instr);
         }
         else
         {
             newInstructions.Add(oldInstructions[i]);
         }
     }
 }
示例#2
0
    private static int[] Compute(int[] input)
    {
        int i      = 0;
        int opcode = -1;

        while (i < input.Length && opcode != 99)
        {
            Instruction instruction = new NullInstruction(input, i);
            opcode = input[i] % 100;
            var fullOpCode = input[i];
            switch (opcode)
            {
            case 99: break;

            case 1:
                instruction = new Add(input, i);
                break;

            case 2:
                instruction = new Multiply(input, i);
                break;

            case 3:
                instruction = new Input(input, i);
                break;

            case 4:
                instruction = new Output(input, i);
                break;

            case 5:
                instruction = new JumpIfTrue(input, i);
                break;

            case 6:
                instruction = new JumpIfFalse(input, i);
                break;

            case 7:
                instruction = new LessThan(input, i);
                break;

            case 8:
                instruction = new Equal(input, i);
                break;

            default: throw new ArgumentException();
            }
            (var newInput, var instructionPointer) = instruction.Compute();

            i     = instructionPointer;
            input = newInput;
        }
        return(input);
    }
示例#3
0
        private IOpcode GetOpCodeFromCurrentPointer()
        {
            var     opcodeValue = Convert.ToUInt32(new string(LocalComputerMemory.PointerValue().ToString().TakeLast(2).ToArray()));
            IOpcode opcode      = null;

            switch ((OpCode)opcodeValue)
            {
            case OpCode.Adds:
                opcode = new Add();
                break;

            case OpCode.Multiplie:
                opcode = new Multiply();
                break;

            case OpCode.Inputstuff:
                opcode = new Input();
                break;

            case OpCode.OutputStuff:
                opcode = new Output();
                break;

            case OpCode.JumpIfTrue:
                opcode = new JumpIfTrue();
                break;

            case OpCode.JumpIfFalse:
                opcode = new JumpIfFalse();
                break;

            case OpCode.isLessThen:
                opcode = new LessThan();
                break;

            case OpCode.isEqual:
                opcode = new Equal();
                break;

            case OpCode.RelativeBase:
                opcode = new RelativeBase();
                break;

            case OpCode.Exit:
                opcode = new Exit();
                break;

            default:
                break;
            }
            return(opcode);
        }
 protected JumpIfFalseTests()
 {
     TestedInstruction = new JumpIfFalse();
 }
 public void Visit(JumpIfFalse code)
 {
     AddReference(code.Reference);
     AddLabel(code.Label);
 }