Ejemplo n.º 1
0
        public RamMachineCommand(string type, string argument, uint curLine)
        {
            Line = curLine;
            if (!RamMachineController.Exist(type))
            {
                throw new RamMachineParserException(0, $"Command {type} doesn't exist");
            }

            (Type, Argument) = (type, argument);
        }
Ejemplo n.º 2
0
 private void PrepareInvoke()
 {
     while (Point < Commands.Count)
     {
         if (RamMachineController.GetCommandBehavior(Commands[(int)Point].Type).PreInvoke())
         {
             RamMachineController.Invoke(Commands[(int)Point], this);
         }
         Point++;
     }
     Point = 0;
 }
Ejemplo n.º 3
0
        public static IEnumerable <RamMachineCommand> Parse(string text)
        {
            Regex regex = new Regex(@"#.*?$", RegexOptions.Multiline);//getting rid of comments

            text = regex.Replace(text, "");
            int  index   = 0;
            uint curLine = 0;

            foreach (string comLine in text.Split('\n'))
            {
                curLine++;
                foreach (string line in Regex.Split(comLine, "$|(?<=:)"))
                {
                    var trimed = line.Trim();
                    if (trimed == string.Empty)
                    {
                        continue;
                    }
                    string[] splited  = Regex.Split(trimed, " ").Where(item => item != string.Empty).Select(item => item.Trim()).ToArray();
                    string   type     = splited[0];
                    string   argument = "";

                    if (splited.Length > 1)
                    {
                        argument = splited[1];
                        if ((RamMachineController.GetCommandBehavior(type)?.IsArgumentCorrect(argument) == false))
                        {
                            throw new RamMachineParserException(curLine, $"\"{argument}\" is not correct argument for \"{type}\" at line {curLine} ");
                        }
                    }

                    if (RamMachine.RamMachineController.Exist((type)) == false)
                    {
                        throw new RamMachineParserException(curLine, $"Unknown command ({type}) at {curLine} line");
                    }
                    yield return(new RamMachineCommand(type, argument, curLine));

                    index++;
                }
            }
        }
Ejemplo n.º 4
0
 public bool DoNext()
 {
     if (Point >= Commands.Count)
     {
         if (!registerEnd)
         {
             OnEnded(this, EventArgs.Empty);
             registerEnd = true;
         }
         return(false);
     }
     registerEnd = false;
     if (RamMachineController.GetCommandBehavior(Commands[(int)Point].Type).PreInvoke() == false)
     {
         RamMachineController.Invoke(Commands[(int)Point], this);
     }
     Point++;
     LineInvoked++;
     if (InvokedLimit > 0 && LineInvoked > InvokedLimit)
     {
         throw new RamMachineRuntimeException(Point, $"Limit of {InvokedLimit} commands has been approched, the ram machine code can have infinity loop at {Point+1} line", null);
     }
     return(true);
 }
Ejemplo n.º 5
0
 public bool IsCorrectArgument(string argument)
 {
     return(RamMachineController.GetCommandBehavior(this.Argument).IsArgumentCorrect(argument));
 }
Ejemplo n.º 6
0
 public void Invoke(IRamMachine ram)
 {
     RamMachineController.Invoke(this, ram);
 }