Exemple #1
0
        public void Start()
        {
            ActivationRecord newAR = new ActivationRecord();

            newAR.Name          = "main";
            newAR.ReturnAddress = -1;
            InstructionPointer  = Labels["main"];
            ActivationRecords.Add(newAR);
            updateWallAndCoinRegister();
            InstructionPointer++;
            //Execute();
        }
Exemple #2
0
 public void Reset()
 {
     //Labels.Clear();
     Stack.Clear();
     ActivationRecords.Clear();
 }
Exemple #3
0
        public void Execute()
        {
            String instruction = Instructions[InstructionPointer].Trim().ToLower();

            String[] args = instruction.Split(' ');

            if (ActivationRecords.Count == 0)
            {
                return;
            }
            else if (ActivationRecords.Count > 50)
            {
                throw new Exception("Настана активациски overflow.");
            }


            if (instruction.Contains(':'))
            {
            }
            else if (args[0] == "ret")
            {
                InstructionPointer = getCurrentActivationRecord().ReturnAddress;
                ActivationRecords.RemoveAt(ActivationRecords.Count - 1);
            }
            else if (args[0] == "inc")
            {
                RegN++;
                if (RegN > 9999999)
                {
                    throw new Exception("Настана overflow во regN. Максимална дозволена големина на податоци од тип бро е 9999999.");
                }
            }
            else if (args[0] == "push")
            {
                Stack.Push(RegN);
            }
            else if (args[0] == "pop")
            {
                RegN = Convert.ToInt32(Stack.Pop());
            }
            else if (args[0] == "move")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();
                if (args[1] == "regn")
                {
                    if (currentAR.ContainsVariable(args[2]))
                    {
                        RegN = Convert.ToInt32(currentAR.GetVariableValue(args[2]));
                    }
                    else
                    {
                        RegN = Convert.ToInt32(args[2]);
                    }
                }
                else
                {
                    if (args[2] == "regn")
                    {
                        currentAR.SetVariableValue(args[1], RegN.ToString());
                    }
                    else if (currentAR.ContainsVariable(args[2]))
                    {
                        currentAR.SetVariableValue(args[1], currentAR.GetVariableValue(args[2]));
                    }
                    else
                    {
                        currentAR.SetVariableValue(args[1], args[2]);
                    }
                }
            }
            else if (args[0] == "cmp")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();

                if (args[1] == "regn")
                {
                    if (currentAR.ContainsVariable(args[2]))
                    {
                        RegFlag = RegN.CompareTo(Convert.ToInt32(currentAR.GetVariableValue(args[2])));
                    }
                    else
                    {
                        RegFlag = RegN.CompareTo(Convert.ToInt32(args[2]));
                    }
                }
                else if (args[1] == "regd")
                {
                    if (currentAR.ContainsVariable(args[2]))
                    {
                        RegFlag = RegD.CompareTo(currentAR.GetVariableValue(args[2]));
                    }
                    else
                    {
                        RegFlag = RegD.CompareTo(Convert.ToChar(args[2]));
                    }
                }
                else if (args[1] == "regc")
                {
                    if (args[2] == "$w")
                    {
                        if (RegC >= 10)
                        {
                            RegFlag = 0;
                        }
                        else
                        {
                            RegFlag = 1;
                        }
                    }
                    else if (args[2] == "$c")
                    {
                        if (RegC % 10 == 1)
                        {
                            RegFlag = 0;
                        }
                        else
                        {
                            RegFlag = 1;
                        }
                    }
                    else
                    {
                        throw new Exception("Невалидна наредба");
                    }
                }
                else
                {
                    throw new Exception("Невалидна наредба");
                }
            }
            else if (args[0] == "add")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();

                if (currentAR.ContainsVariable(args[2]))
                {
                    RegN += Convert.ToInt32(currentAR.GetVariableValue(args[2]));
                }
                else
                {
                    RegN += Convert.ToInt32(args[2]);
                }
            }
            else if (args[0] == "mul")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();

                if (currentAR.ContainsVariable(args[2]))
                {
                    RegN *= Convert.ToInt32(currentAR.GetVariableValue(args[2]));
                }
                else
                {
                    RegN *= Convert.ToInt32(args[2]);
                }
            }
            else if (args[0] == "sub")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();

                if (currentAR.ContainsVariable(args[2]))
                {
                    RegN -= Convert.ToInt32(currentAR.GetVariableValue(args[2]));
                }
                else
                {
                    RegN -= Convert.ToInt32(args[2]);
                }
            }
            else if (args[0] == "div")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();

                if (currentAR.ContainsVariable(args[2]))
                {
                    RegN /= Convert.ToInt32(currentAR.GetVariableValue(args[2]));
                }
                else
                {
                    RegN /= Convert.ToInt32(args[2]);
                }
            }
            else if (args[0] == "go")
            {
                Go();
            }
            else if (args[0] == "rr")
            {
                turnRight();
            }
            else if (args[0] == "rl")
            {
                turnLeft();
            }
            else if (args[0] == "tk")
            {
                if (Enviorment[ConvertToInternalPosition(X)][ConvertToInternalPosition(Y)] == Cell.Coin)
                {
                    Enviorment[ConvertToInternalPosition(X)][ConvertToInternalPosition(Y)] = Cell.Empty;
                    RegTC++;
                }
                else
                {
                    throw new Exception("Нема жетон на оваа позиција.");
                }
            }
            else if (args[0] == "lv")
            {
                if (Enviorment[ConvertToInternalPosition(X)][ConvertToInternalPosition(Y)] == Cell.Coin)
                {
                    throw new Exception("Неможе да се остави жетон на оваа позиција бидејќи веќе има еден.");
                }
                else if (RegTC == 0)
                {
                    throw new Exception("Неможе да се остави жетон бидејќи немате жетони на располагање.");
                }
                else
                {
                    RegTC--;
                    Enviorment[ConvertToInternalPosition(X)][ConvertToInternalPosition(Y)] = Cell.Coin;
                }
            }
            else if (args[0] == "jie")
            {
                int line = Labels[args[1]];
                if (RegFlag == 0)
                {
                    InstructionPointer = line;
                }
            }
            else if (args[0] == "jne")
            {
                int line = Labels[args[1]];
                if (RegFlag != 0)
                {
                    InstructionPointer = line;
                }
            }
            else if (args[0] == "jl")
            {
                int line = Labels[args[1]];
                if (RegFlag == -1)
                {
                    InstructionPointer = line;
                }
            }
            else if (args[0] == "jle")
            {
                int line = Labels[args[1]];
                if (RegFlag <= 0)
                {
                    InstructionPointer = line;
                }
            }
            else if (args[0] == "jm")
            {
                int line = Labels[args[1]];
                if (RegFlag == 1)
                {
                    InstructionPointer = line;
                }
            }
            else if (args[0] == "jme")
            {
                int line = Labels[args[1]];
                if (RegFlag >= 0)
                {
                    InstructionPointer = line;
                }
            }
            else if (args[0] == "jmp")
            {
                int line = Labels[args[1]];
                InstructionPointer = line;
            }
            else if (args[0] == "data")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();
                currentAR.AddVariable(args[1]);
            }
            else if (args[0] == "call")
            {
                ActivationRecord currentAR = getCurrentActivationRecord();
                ActivationRecord newAR     = new ActivationRecord();
                newAR.Name          = args[1];
                newAR.ReturnAddress = InstructionPointer + 1;
                ActivationRecords.Add(newAR);

                for (int i = 2; i < args.Length; i++)
                {
                    if (currentAR.ContainsVariable(args[i]))
                    {
                        newAR.AddArgument(currentAR.GetVariableValue(args[i]));
                    }
                    else
                    {
                        newAR.AddArgument(args[i]);
                    }
                }

                InstructionPointer = Labels[args[1]];
            }
            else if (args[0] == "data")
            {
                ActivationRecord currAR = getCurrentActivationRecord();
                currAR.AddVariable(args[0]);
            }
            else
            {
                throw new Exception("Непозната инструкција.");
            }

            updateWallAndCoinRegister();
            InstructionPointer++;
        }