Esempio n. 1
0
        public void Execute()
        {
            while (true)
            {
                if (InstructionPointer >= Instructions.Count)
                {
                    return;
                }

                var instruction = Instructions[InstructionPointer++];

                var command = instruction.Code;

                long num = 0;
                long val = 0;

                switch (command)
                {
                case "snd":
                    OutputQueue.Add(GetRegister(instruction.Register[0]));
                    break;

                case "set":
                    if (Int64.TryParse(instruction.Value, out num))
                    {
                        val = num;
                    }
                    else
                    {
                        val = GetRegister(instruction.Value[0]);
                    }

                    Registers[instruction.Register[0]] = val;
                    break;

                case "add":
                    if (Int64.TryParse(instruction.Value, out num))
                    {
                        val = GetRegister(instruction.Register[0]) + num;
                    }
                    else
                    {
                        val = GetRegister(instruction.Register[0]) + GetRegister(instruction.Value[0]);
                    }

                    Registers[instruction.Register[0]] = val;
                    break;

                case "mul":
                    if (Int64.TryParse(instruction.Value, out num))
                    {
                        val = GetRegister(instruction.Register[0]) * num;
                    }
                    else
                    {
                        val = GetRegister(instruction.Register[0]) * GetRegister(instruction.Value[0]);
                    }

                    Registers[instruction.Register[0]] = val;
                    break;

                case "mod":
                    if (Int64.TryParse(instruction.Value, out num))
                    {
                        val = GetRegister(instruction.Register[0]) % num;
                    }
                    else
                    {
                        val = GetRegister(instruction.Register[0]) % GetRegister(instruction.Value[0]);
                    }

                    Registers[instruction.Register[0]] = val;

                    break;

                case "rcv":
                    if (InputQueue.Count > 0)
                    {
                        Registers[instruction.Register[0]] = InputQueue.First();
                        InputQueue.RemoveAt(0);
                    }
                    else
                    {
                        InstructionPointer--;
                        return;
                    }

                    break;

                case "jgz":
                    if (Int64.TryParse(instruction.Register, out num))
                    {
                        val = num;
                    }
                    else
                    {
                        val = GetRegister(instruction.Register[0]);
                    }

                    if (val > 0)
                    {
                        if (Int64.TryParse(instruction.Value, out num))
                        {
                            val = num;
                        }
                        else
                        {
                            val = GetRegister(instruction.Value[0]);
                        }

                        var jumpCount = val;

                        InstructionPointer--;
                        InstructionPointer += (int)jumpCount;
                    }

                    break;
                }
            }
        }
Esempio n. 2
0
        public void Execute()
        {
            while (true)
            {
                if (InstructionPointer >= Instructions.Count)
                {
                    return;
                }

                var instruction      = Instructions[InstructionPointer++];
                var instructionWords = instruction.Split(' ').ToList();

                var command  = instruction.Split(' ').First();
                var register = default(char);
                var value    = string.Empty;

                switch (command)
                {
                case "snd":
                    value = instructionWords[1];

                    OutputQueue.Add(GetValue(value));
                    break;

                case "set":
                    register = instructionWords[1][0];
                    value    = instructionWords[2];

                    Registers[register] = GetValue(value);
                    break;

                case "add":
                    register = instructionWords[1][0];
                    value    = instructionWords[2];

                    Registers[register] += GetValue(value);
                    break;

                case "mul":
                    register = instructionWords[1][0];
                    value    = instructionWords[2];

                    Registers[register] *= GetValue(value);
                    break;

                case "mod":
                    register = instructionWords[1][0];
                    value    = instructionWords[2];

                    Registers[register] %= GetValue(value);
                    break;

                case "rcv":
                    register = instructionWords[1][0];

                    if (InputQueue.Count > 0)
                    {
                        Registers[register] = InputQueue.First();
                        InputQueue.RemoveAt(0);
                    }
                    else
                    {
                        InstructionPointer--;
                        return;
                    }

                    break;

                case "jgz":
                    value = instructionWords[1];

                    if (GetValue(value) > 0)
                    {
                        var jumpCount = GetValue(instructionWords[2]);

                        InstructionPointer--;
                        InstructionPointer += (int)jumpCount;
                    }

                    break;

                default:
                    throw new Exception();
                }
            }
        }