Beispiel #1
0
        static void Main(string[] args)
        {
            string   filename = "../../test.txt";
            FileInfo file     = new FileInfo(filename);

            if (file.Exists)
            {
                string[]    lines         = File.ReadAllLines(filename);
                Map         myMap         = new Map(10, 10, 4, 4);
                Interpreter myInterpreter = new Interpreter(lines);
                myInterpreter.CommandEvent += myMap.CommandHandle;
                try
                {
                    while (!myInterpreter.IsCompleted)
                    {
                        myInterpreter.NextAction();
                        myMap.Display();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("e = " + e.Message);
                }
            }
            else
            {
                Console.WriteLine("File not found");
            }
            Console.WriteLine("!End!");
            Console.ReadKey();
            return;

            string a = " foo* foo - 10 + 5 * 7 +24/(1 + 1  ) ";//данное выражение

            Console.WriteLine(a);
            var b = EquationParser.Tokenize(a);       //список токенов
            var c = new Dictionary <string, float>(); //словарь переменных

            //переменные в словаре переменных
            c.Add("foo", 10);
            c.Add("bar", 0);
            foreach (var i in EquationParser.ConvertToRpn(b, c))//токены в польской нотации
            {
                Console.Write(i + " | ");
            }
            Console.WriteLine();
            foreach (var j in c)//переменные в словаре переменных
            {
                Console.WriteLine(j.Key + " = " + j.Value);
            }
            Console.WriteLine();
            Console.WriteLine(EquationParser.CalculateRpn(EquationParser.ConvertToRpn(b, c), c));//результат

            Console.ReadKey();
            return;
        }
Beispiel #2
0
        /// <summary>
        /// Do all instructions from source untill reaching rover command
        /// </summary>
        public void NextAction()
        {
            if (!IsCompleted)
            {
                Console.WriteLine(_currentPos + 1);
                Regex currRegex;
                //check for end of file
                if (_currentPos > SourceCode.Length - 1)//last row check
                {
                    if (CommandEvent != null)
                    {
                        CommandEvent(new Command("EndOfCode", new int[] { 0 }));
                    }
                    IsCompleted = true;
                }
                //rover motion commands
                else if ((currRegex = new Regex(@"^\s*(right|forward|left|backward)\s*;\s*$")).IsMatch(SourceCode[_currentPos]))
                {
                    Match moveRoverMatch = currRegex.Match(SourceCode[_currentPos]);
                    if (CommandEvent != null)
                    {
                        CommandEvent(new Command("RoverMove", new int[] { _roverCommand[moveRoverMatch.Groups[1].ToString()] }));
                    }
                    _currentPos += 1;
                }
                //goto statement
                else if ((currRegex = new Regex(@"^\s*goto\s*(.+?)\s*;\s*$")).IsMatch(SourceCode[_currentPos]))
                {
                    Match gotoMatch = currRegex.Match(SourceCode[_currentPos]);
                    if (gotoMatch.Success)
                    {
                        _currentPos = int.Parse(gotoMatch.Groups[1].ToString()) - 1;
                    }
                    if ((_currentPos < 0) && (_currentPos >= SourceCode.Length))
                    {
                        throw new Exception("Wrong goto position");
                    }
                    Console.WriteLine("goto " + gotoMatch.Groups[1]);
                }
                //assigment of new var
                else if ((currRegex = new Regex(@"\s*(float|int)\s*([a-zA-Z_\-][a-zA-Z0-9_\-]*)\s*\=\s*(.+?)\s*;")).IsMatch(SourceCode[_currentPos]))
                {
                    Match setVariableMatch = currRegex.Match(SourceCode[_currentPos]);
                    for (int i = 1; i < setVariableMatch.Groups.Count; ++i)
                    {
                        Console.WriteLine(setVariableMatch.Groups[i]);
                    }
                    switch (setVariableMatch.Groups[0].ToString())
                    {
                    case "float":
                        _variablesStorage.Add(setVariableMatch.Groups[1].ToString(), EquationParser.Calculate(setVariableMatch.Groups[2].ToString(), _variablesStorage));
                        Console.WriteLine(setVariableMatch.Groups[1] + " = " + _variablesStorage[setVariableMatch.Groups[1].ToString()]);
                        break;

                    case "int":
                        _variablesStorage.Add(setVariableMatch.Groups[1].ToString(), (int)EquationParser.Calculate(setVariableMatch.Groups[2].ToString(), _variablesStorage));
                        Console.WriteLine(setVariableMatch.Groups[1] + " = " + _variablesStorage[setVariableMatch.Groups[1].ToString()]);
                        break;
                    }
                    Console.WriteLine(setVariableMatch.Groups[1].ToString() + "-" + setVariableMatch.Groups[2].ToString() + "-" + setVariableMatch.Groups[3].ToString());
                    _currentPos += 1;
                }
                //new var w/o assignment
                else if ((currRegex = new Regex(@"\s*(float|int)\s*([a-zA-Z_\-][a-zA-Z0-9_\-]*)\s*;")).IsMatch(SourceCode[_currentPos]))
                {
                    Match createVariableMatch = currRegex.Match(SourceCode[_currentPos]);
                    for (int i = 1; i < createVariableMatch.Groups.Count; ++i)
                    {
                        Console.WriteLine(createVariableMatch.Groups[i]);
                    }
                    switch (createVariableMatch.Groups[1].ToString())
                    {
                    case "float":
                        _variablesStorage.Add(createVariableMatch.Groups[2].ToString(), 0f);
                        break;

                    case "int":
                        _variablesStorage.Add(createVariableMatch.Groups[2].ToString(), 0);
                        break;
                    }
                    Console.WriteLine(createVariableMatch.Groups[2] + " = " + _variablesStorage[createVariableMatch.Groups[2].ToString()]);
                    Console.WriteLine(createVariableMatch.Groups[1].ToString() + "-" + createVariableMatch.Groups[2].ToString());
                    _currentPos += 1;
                }
                //existing value change
                else if ((currRegex = new Regex(@"\s*([a-zA-Z_\-][a-zA-Z0-9_\-]*)\s*([\+\-\*\/]?\=)\s*(.+?)\s*;")).IsMatch(SourceCode[_currentPos]))
                {
                    Match changeVariableMatch = currRegex.Match(SourceCode[_currentPos]);
                    for (int i = 1; i < changeVariableMatch.Groups.Count; ++i)
                    {
                        Console.WriteLine(changeVariableMatch.Groups[i]);
                    }
                    float equationResult = EquationParser.Calculate(changeVariableMatch.Groups[3].ToString(),
                                                                    _variablesStorage);
                    switch (changeVariableMatch.Groups[2].ToString())
                    {
                    case "=":
                        _variablesStorage[changeVariableMatch.Groups[1].ToString()] = equationResult;
                        break;

                    case "+=":
                        _variablesStorage[changeVariableMatch.Groups[1].ToString()] += equationResult;
                        break;

                    case "-=":
                        _variablesStorage[changeVariableMatch.Groups[1].ToString()] -= equationResult;
                        break;

                    case "*=":
                        _variablesStorage[changeVariableMatch.Groups[1].ToString()] *= equationResult;
                        break;

                    case "/=":
                        _variablesStorage[changeVariableMatch.Groups[1].ToString()] /= equationResult;
                        break;
                    }
                    Console.WriteLine(changeVariableMatch.Groups[1] + " = " + _variablesStorage[changeVariableMatch.Groups[1].ToString()]);
                    _currentPos += 1;
                }
                else if (new Regex(@"^\s*$").IsMatch(SourceCode[_currentPos]))
                {
                    _currentPos += 1;
                }
            }
        }