public void ExecuteInstructions()
        {
            var stringWriter = new StringWriter();

            Console.SetOut(stringWriter);
            var instructions = new List <string>
            {
                "6 3",
                "1 2 N",
                "LMLMLMLMM",
                "3 3 E",
                "MMRMMRMRRM",
                "1 2 N",
                "LMLMLMLMM",
                "1 9 N",
                "LRLRMMMRL"
            };
            var roverPaths = InstructionParser.Parse(instructions);

            InstructionParser.Execute(roverPaths);

            Plateau.RoverAtPosition(new Position(1, 3)).Should().Be(true);

            /*
             * stringWriter.ToString().Should().Be(
             *  "1 3 N\r\n" +
             *  "5 1 E\r\n" +
             *  "1 3 N CRASHED\r\n" +
             *  "1 9 N CRASHED\r\n");
             */
        }
Esempio n. 2
0
        private void btnSendCommands_Click(object sender, EventArgs e)
        {
            StringBuilder allResults = new StringBuilder();

            try
            {
                var builder = new PlanetBuilder();
                IParser parser = new InstructionParser();
                PlanetDescription planetDescription = parser.Parse(tbCommands.Text);
                Queue<Robot> robots = builder.BuildPlanet(planetDescription);

                while (robots.Count > 0)
                {
                    Robot robot = robots.Dequeue();
                    robot.ExecuteCommands();
                    string result = String.Format("{0} {1} {2}{3}", robot.CurrentPosition.X,
                    robot.CurrentPosition.Y, robot.CurrentOrientation.ToString()[0], ((robot.IsLost) ? " LOST" : ""));

                    allResults.Append(result.Trim() + "\r\n");
                }

            }
            catch (MartianException ex)
            {
                allResults.Append(ex.Message + "\r\n");
            }
            catch (Exception ex)
            {
                allResults.Append("Unhandled Exception: " + ex.Message + "\r\n");
            }
            tbResult.Text = allResults.ToString();
        }
Esempio n. 3
0
        public void DecompileCommand(UInt160 contractHash)
        {
            var script = _scriptTable.GetScript(contractHash.ToArray(), false);

            if (script == null)
            {
                throw (new ArgumentNullException("Contract not found"));
            }

            var parser = new InstructionParser();

            foreach (var i in parser.Parse(script))
            {
                _consoleWriter.Write(i.Location.ToString() + " ", ConsoleOutputStyle.Information);

                if (i is InstructionWithPayload ip)
                {
                    _consoleWriter.Write(i.OpCode.ToString() + " ");
                    _consoleWriter.WriteLine("{" + ip.Payload.ToHexString(true) + "}", ConsoleOutputStyle.DarkGray);
                }
                else
                {
                    _consoleWriter.WriteLine(i.OpCode.ToString());
                }
            }
        }
Esempio n. 4
0
        public string Solve(string[] input)
        {
            var parser       = new InstructionParser();
            var instructions = input.Select(line => parser.Parse(line));
            var layout       = new SwarmScript(instructions).Compile();

            return((layout.Outputs[0] * layout.Outputs[1] * layout.Outputs[2]).ToString());
        }
        public void Parse_ShouldParseCharacterToEnum(char inputValue, Instruction expectedValue)
        {
            var parser = new InstructionParser();

            var result = parser.Parse(inputValue);

            result.Should().Be(expectedValue);
        }
Esempio n. 6
0
        public void RowRotationGetsParsed()
        {
            var parser = new InstructionParser();
            var parsed = parser.Parse("rotate row y=0 by 4");
            var proper = new Instruction(Instruction.Operation.RowRotation, 0, 4);

            Assert.Equal(proper, parsed);
        }
Esempio n. 7
0
        public void ColumnRotationGetsParsed()
        {
            var parser = new InstructionParser();
            var parsed = parser.Parse("rotate column x=14 by 31");
            var proper = new Instruction(Instruction.Operation.ColRotation, 14, 31);

            Assert.Equal(proper, parsed);
        }
Esempio n. 8
0
        public void RectangleGetsParsed()
        {
            var parser = new InstructionParser();
            var parsed = parser.Parse("rect 3x2");
            var proper = new Instruction(Instruction.Operation.Rectangle, 3, 2);

            Assert.Equal(proper, parsed);
        }
Esempio n. 9
0
        public string Solve(string[] input)
        {
            var registers = new RegisterMap(new[] { 'a', 'b', 'c', 'd' });
            var parser    = new InstructionParser(registers);
            var interpret = new Interpreter(registers);

            interpret.Execute(input.Select(i => parser.Parse(i)).ToArray());
            return(interpret.Registers[0].ToString());
        }
Esempio n. 10
0
        private IEnumerable <Instruction> ConvertToNodes(Stream stream)
        {
            var reader            = new XmlCompatibilityReader(stream);
            var runtimeTypeSource = RuntimeTypeSource;
            var pullParser        = new InstructionParser(runtimeTypeSource);
            var protoParser       = new ProtoInstructionParser(runtimeTypeSource);

            return(pullParser.Parse(protoParser.Parse(reader)).ToList());
        }
        void TestInvalidInstructionCall(byte[] byteCode)
        {
            Int16 instructionCode = BitConverter.ToInt16(byteCode);
            InstructionConfiguratorStub configurator = new InstructionConfiguratorStub();
            InstructionParser           parser       = new InstructionParser(configurator);

            parser.UnknownInstructionFoundEvent += (Int16 code) => throw new InvalidInstructionException();
            Assert.Throws <InvalidInstructionException>(() => parser.Parse(instructionCode));
        }
Esempio n. 12
0
        public string Solve(string[] input)
        {
            var parser       = new InstructionParser();
            var instructions = input.Select(line => parser.Parse(line));
            var layout       = new SwarmScript(instructions).Compile();
            var bot          = layout.Bots.Values.Single(b => b.Values.Max() == 61 && b.Values.Min() == 17);

            return(bot.Id.ToString());
        }
        void TestParseInstruction <T>(byte[] byteCode, IComparable <T> expected) where T : class
        {
            Int16 instructionCode = BitConverter.ToInt16(byteCode);
            InstructionConfiguratorStub configurator = new InstructionConfiguratorStub();
            InstructionParser           parser       = new InstructionParser(configurator);
            var parsed = parser.Parse(instructionCode) as T;

            Assert.Equal(0, expected.CompareTo(parsed));
        }
Esempio n. 14
0
        private ICollection <Instruction> ExtractNodesFromPullParser(string xml)
        {
            var pullParser = new InstructionParser(RuntimeTypeSource);

            using (var stream = new StringReader(xml))
            {
                var reader = new XmlCompatibilityReader(stream);
                return(pullParser.Parse(new ProtoInstructionParser(RuntimeTypeSource).Parse(reader)).ToList());
            }
        }
Esempio n. 15
0
 void EnsureInstructionsParsed()
 {
     if (m_instructions == null && m_pRow->RVA != 0)
     {
         var header       = GetMethodHeader();
         var instructions = InstructionParser.Parse(new BufferWrapper(header.RawHeader + header.Size, header.CodeSize), this);
         #pragma warning disable 420
         Interlocked.CompareExchange(ref m_instructions, instructions, null);
         #pragma warning restore 420
     }
 }
Esempio n. 16
0
        public Property InputInstructionGetsParsed()
        {
            var inputInstructions = from value in Arb.Generate <byte>()
                                    from bot in Arb.Generate <byte>()
                                    select new { value, bot, instruction = $"value {value} goes to bot {bot}" };
            var parser  = new InstructionParser();
            var parsing = inputInstructions.Select(i => new { original = i, parsed = parser.Parse(i.instruction) as InputInstruction });

            return(Prop.ForAll(parsing.ToArbitrary(),
                               p => p.parsed != null && p.parsed.Value == p.original.value && p.parsed.Bot == p.original.bot));
        }
Esempio n. 17
0
        public void ShouldCreateTheCorrectInstructionTypes(string instructionBody, Type expectedInstructionType)
        {
            var instructionsList = scriptParser.Parse(instructionBody);

            Assert.IsNotNull(instructionsList);
            Assert.IsTrue(instructionsList.Count == 1);
            Assert.IsTrue(instructionsList.First().GetType() == expectedInstructionType);
        }
Esempio n. 18
0
        private static IEnumerable <Instruction> ReadNodes(Type underlyingType)
        {
            var resourceProvider = new InflatableTranslator();

            using (var stream = resourceProvider.GetInflationSourceStream(underlyingType))
            {
                var reader            = new XmlCompatibilityReader(stream);
                var runtimeTypeSource = new WpfRuntimeTypeSource();
                var loader            = new InstructionParser(runtimeTypeSource);
                var protoParser       = new ProtoInstructionParser(runtimeTypeSource);

                return(loader.Parse(protoParser.Parse(reader)));
            }
        }
        public void ParseInstructions()
        {
            var instructions = new List <string>
            {
                "6 3",
                "1 2 N",
                "LMLMLMLMM",
                "3 3 E",
                "MMRMMRMRRM",
                "1 2 N",
                "LMLMLMLMM"
            };
            var roverPaths = InstructionParser.Parse(instructions);

            roverPaths.Count().Should().Be(3);
            Plateau.GetPlateauBounds().ShouldBeEquivalentTo(new Position(6, 3));
        }
Esempio n. 20
0
        /// <summary>
        /// Compiles a given source to a list of instructions
        /// </summary>
        /// <param name="source">The source code</param>
        /// <returns>The instructions parsed out of the source.</returns>
        public static List <Instruction> Compile(string source)
        {
            var tokens = InstructionParser.Parse(source);
            var result = new List <Instruction>();

            tokens.ForEach(token =>
            {
                if (token is InstructionToken instructionToken)
                {
                    result.Add(instructionToken.Instruction);
                }

                // TODO: Do further processing - Aka optimize and pre-process and labels.
            });

            return(result);
        }
Esempio n. 21
0
        public void TestFromFiles()
        {
            foreach (TestDescription description in Tests)
            {
                IParser parser = new InstructionParser();
                PlanetDescription planetDescription = parser.Parse(description.Test);
                Queue<Robot> robots = new PlanetBuilder().BuildPlanet(planetDescription);

                StringBuilder allResults = new StringBuilder();

                while (robots.Count > 0)
                {
                    Robot robot = robots.Dequeue();
                    robot.ExecuteCommands();
                    string result = String.Format("{0} {1} {2}{3}", robot.CurrentPosition.X,
                        robot.CurrentPosition.Y, robot.CurrentOrientation.ToString()[0], ((robot.IsLost) ? " LOST" : ""));

                    allResults.Append(result.Trim()+"\r\n");
                }

                Assert.AreEqual(allResults.ToString().Trim(), description.ExpectedResult.Trim());
            }
        }
Esempio n. 22
0
 public void ParseTest()
 {
     InstructionParser.Parse("nop +0").Should().BeEquivalentTo(new NopInstruction(0));
     InstructionParser.Parse("acc +1").Should().BeEquivalentTo(new AccInstruction(1));
     InstructionParser.Parse("jmp +4").Should().BeEquivalentTo(new JmpInstruction(4));
 }
Esempio n. 23
0
        private static IEnumerable<Instruction> ReadNodes(Type underlyingType)
        {
            var resourceProvider = new InflatableTranslator();

            using (var stream = resourceProvider.GetInflationSourceStream(underlyingType))
            {
                var reader = new XmlCompatibilityReader(stream);
                var runtimeTypeSource = new WpfRuntimeTypeSource();
                var loader = new InstructionParser(runtimeTypeSource);
                var protoParser = new ProtoInstructionParser(runtimeTypeSource);

                return loader.Parse(protoParser.Parse(reader));
            }
        }
Esempio n. 24
0
        public void SampleInstructionsGetParsed(string instruction, IBotInstruction parsed)
        {
            var parser = new InstructionParser();

            Assert.Equal(parsed, parser.Parse(instruction));
        }