예제 #1
0
    public void RunProgram(bool stopWhenRecover)
    {
        SentMsgToQ = false;
        IsOnHold   = false;
        for (var lineNo = _nextLineToProcess; lineNo < _commands.Length; lineNo++)
        {
            var m    = Regex.Match(_commands[lineNo], "(\\w+)\\s(\\w)\\s*(-*\\w*)");
            var cmd  = m.Groups[1].Value;
            var arg1 = m.Groups[2].Value;
            var arg2 = m.Groups.Count > 2 ? GetValue(m.Groups[3].Value) : 0;
            if (cmd != "snd" && !_registers.ContainsKey(m.Groups[2].Value))
            {
                _registers.Add(arg1, 0);
            }

            switch (cmd)
            {
            case "snd":
                SendQ.Add(GetValue(arg1));
                LastFrequencySent = GetValue(arg1);
                SentMsgToQ        = true;
                MessagesSent++;
                break;

            case "set":
                _registers[arg1] = arg2;
                break;

            case "add":
                _registers[arg1] += arg2;
                break;

            case "mul":
                _registers[arg1] *= arg2;
                break;

            case "mod":
                _registers[arg1] %= arg2;
                break;

            case "rcv":
                if (GetValue(arg1) != 0 && stopWhenRecover)
                {
                    return;
                }

                if (ReceiveQ.Count == 0)
                {
                    _nextLineToProcess = lineNo;
                    IsOnHold           = true;
                    return;
                }
                else
                {
                    _registers[arg1] = ReceiveQ[0];
                    ReceiveQ.RemoveAt(0);
                }

                break;

            case "jgz":
                if (GetValue(arg1) > 0)
                {
                    lineNo += arg2 - 1;
                }
                break;
            }
        }

        IsRunning = false;
    }
예제 #2
0
        public void RunProgram(bool stopWhenRecover)
        {
            SentMsgToQ = false;
            IsOnHold   = false;
            var count = 0;

            for (var i = 106500; i <= 123500; i += 17)
            {
                if (!IsPrime(i))
                {
                    count++;
                }
            }

            for (var lineNo = _nextLineToProcess; lineNo < _commands.Length; lineNo++)
            {
                var m    = Regex.Match(_commands[lineNo], "(\\w+)\\s(\\w)\\s*(-*\\w*)");
                var cmd  = m.Groups[1].Value;
                var arg1 = m.Groups[2].Value;
                var arg2 = m.Groups.Count > 2 ? GetValue(m.Groups[3].Value) : 0;
                if (cmd != "snd" && !Registers.ContainsKey(m.Groups[2].Value))
                {
                    Registers.Add(arg1, 0);
                }

                switch (cmd)
                {
                case "sub":
                    Registers[arg1] -= arg2;
                    break;

                case "snd":
                    SendQ.Add(GetValue(arg1));
                    LastFrequencySent = GetValue(arg1);
                    SentMsgToQ        = true;
                    MessagesSent++;
                    break;

                case "set":
                    Registers[arg1] = arg2;
                    break;

                case "add":
                    Registers[arg1] += arg2;
                    break;

                case "mul":
                    Registers[arg1] *= arg2;
                    MulInvoked++;
                    break;

                case "mod":
                    Registers[arg1] %= arg2;
                    break;

                case "rcv":
                    if (GetValue(arg1) != 0 && stopWhenRecover)
                    {
                        return;
                    }

                    if (ReceiveQ.Count == 0)
                    {
                        _nextLineToProcess = lineNo;
                        IsOnHold           = true;
                        return;
                    }
                    else
                    {
                        Registers[arg1] = ReceiveQ[0];
                        ReceiveQ.RemoveAt(0);
                    }

                    break;

                case "jgz":
                    if (GetValue(arg1) > 0)
                    {
                        lineNo += arg2 - 1;
                    }
                    break;

                case "jnz":
                    if (GetValue(arg1) != 0)
                    {
                        lineNo += arg2 - 1;
                    }
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            IsRunning = false;
        }