Esempio n. 1
0
        private static int EvaluateLine(string line, int pc, MachineState state)
        {
            var tokens = Tokenizer.TryTokenize(line);

            if (!tokens.HasValue)
            {
                Error(() => {
                    ErrorSpan(tokens.Location);
                    Console.WriteLine();

                    Console.Error.WriteLine(tokens.FormatErrorMessageFragment());
                });

                Console.WriteLine("Press any key to try this line again");
                Console.ReadKey(true);
                return(pc);
            }

            var parsed = Parser.TryParse(tokens.Value);

            if (!parsed.HasValue)
            {
                Error(() => {
                    ErrorSpan(tokens.Location);
                    Console.WriteLine();

                    Console.Error.WriteLine(parsed.FormatErrorMessageFragment());
                });

                Console.WriteLine("Press any key to try this line again");
                Console.ReadKey(true);
                return(pc);
            }

            if (parsed.Remainder.Any())
            {
                Error(() => {
                    Console.WriteLine("Failed to parse entire line");
                    foreach (var token in parsed.Remainder)
                    {
                        Console.WriteLine($" - {token}");
                    }
                });

                Console.WriteLine("Press any key to try this line again");
                Console.ReadKey(true);
                return(pc);
            }

            return(parsed.Value.Evaluate(pc, state));
        }
Esempio n. 2
0
        private static int EvaluateLine(string line, int pc, MachineState state)
        {
            var tokens = Tokenizer.TryTokenize(line);

            if (!tokens.HasValue)
            {
                Error(() => {
                    ErrorSpan(tokens.Location);
                    Console.WriteLine();

                    Console.Error.WriteLine(tokens.FormatErrorMessageFragment());
                });

                Console.WriteLine("Press any key to try this line again");
                Console.ReadKey(true);
                return(pc);
            }

            var parsed = Parser.TryParseLine(tokens.Value);

            if (!parsed.HasValue)
            {
                Error(() => {
                    ErrorSpan(tokens.Location);
                    Console.WriteLine();

                    Console.Error.WriteLine(parsed.FormatErrorMessageFragment());
                });

                Console.WriteLine("Press any key to try this line again");
                Console.ReadKey(true);
                return(pc);
            }

            if (parsed.Remainder.Any())
            {
                Error(() => {
                    Console.WriteLine("Failed to parse entire line");
                    foreach (var token in parsed.Remainder)
                    {
                        Console.WriteLine($" - {token}");
                    }
                });

                Console.WriteLine("Press any key to try this line again");
                Console.ReadKey(true);
                return(pc);
            }

            try
            {
                return(parsed.Value.Evaluate(pc, state));
            }
            catch (ExecutionException ee)
            {
                var c = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Runtime Error: {ee.Message}");
                Console.ForegroundColor = c;
                return(pc + 1);
            }
        }