コード例 #1
0
 private void selectInputButton_Click(object sender, EventArgs e)
 {
     InputRequested?.Invoke(this, new InputRequestedEventArgs(InputNumber));
 }
コード例 #2
0
 internal void GetInput()
 {
     InputRequested?.Invoke(this, new ConsolePrintEventArgs("input code"));
 }
コード例 #3
0
        private async Task ExecuteAsync()
        {
            instructionPointer = 0;

            while (memory[instructionPointer] != HCF)
            {
                var(opCode, parameterModes) = CalculateOpCodeAndParameterModes(memory[instructionPointer]);

                switch (opCode)
                {
                case ADD:
                    SetParameterValue(2, GetParameterValue(0) + GetParameterValue(1));
                    instructionPointer += 4;
                    break;

                case MUL:
                    SetParameterValue(2, GetParameterValue(0) * GetParameterValue(1));
                    instructionPointer += 4;
                    break;

                case INP:
                    //throw new InvalidOperationException("Encountered INP while inputs has ran out of data");
                    if (inputOverride.HasValue)
                    {
                        SetParameterValue(0, inputOverride.Value);
                    }
                    else
                    {
                        InputRequested?.Invoke(this, input);
                        SetParameterValue(0, await input.ReceiveAsync());
                    }


                    instructionPointer += 2;
                    break;

                case OUT:
                    var o = GetParameterValue(0);
                    output.Post(o);
                    OutputList.Add(o);
                    OutputHappened?.Invoke(this, o);
                    instructionPointer += 2;
                    break;

                case JNZ:
                    instructionPointer = GetParameterValue(0) != 0 ? GetParameterValue(1) : instructionPointer + 3;
                    break;

                case JZ:
                    instructionPointer = GetParameterValue(0) == 0 ? GetParameterValue(1) : instructionPointer + 3;
                    break;

                case LT:
                    SetParameterValue(2, GetParameterValue(0) < GetParameterValue(1) ? 1 : 0);
                    instructionPointer += 4;
                    break;

                case EQ:
                    SetParameterValue(2, GetParameterValue(0) == GetParameterValue(1) ? 1 : 0);
                    instructionPointer += 4;
                    break;

                case SETREL:
                    relativeBase       += GetParameterValue(0);
                    instructionPointer += 2;
                    break;

                default: throw new InvalidOperationException($"Invalid instruction at {instructionPointer} : {memory[instructionPointer]}");

                    // returns parameter value in both immediate and position mode
                    long GetParameterValue(int parameterIndex)
                    {
                        long parameter = ReadMemory(instructionPointer + parameterIndex + 1);
                        long result;

                        switch (parameterModes[parameterIndex])
                        {
                        case ParameterModes.Position:
                            result = ReadMemory(parameter);
                            break;

                        case ParameterModes.Immediate:
                            result = parameter;
                            break;

                        case ParameterModes.Relative:
                            result = ReadMemory(relativeBase + parameter);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        return(result);
                    }

                    void SetParameterValue(int parameterIndex, long value)
                    {
                        var parameter = ReadMemory(instructionPointer + parameterIndex + 1);
                        int result;

                        switch (parameterModes[parameterIndex])
                        {
                        case ParameterModes.Position:
                            WriteMemory(parameter, value);
                            break;

                        case ParameterModes.Relative:
                            WriteMemory(relativeBase + parameter, value);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }

                if (memory.Length < instructionPointer + 1)
                {
                    throw new InvalidOperationException("Missing HCF");
                }
            }
        }
コード例 #4
0
 public void RequestInput()
 {
     InputRequested?.Invoke();
 }