Пример #1
0
        static async Task Main(string[] args)
        {
            var lines = new List <string>(In.ReadToEnd().Split(separator: NewLine)).ConvertAll(l => l.ToLower());

            foreach (var line in lines)
            {
                foreach (var symbol in line)
                {
                    if (symbol.IsLetterLowercase())
                    {
                        Count[symbol] += 1;
                    }
                    else if (symbol.IsNumber())
                    {
                        Count[symbol - 48] += 1;
                    }
                }
            }

            using var file = File.CreateText("result.txt");

            foreach (var item in Count.OrderByDescending(item => item.Value).Take(10))
            {
                if (item.Key.ToChar().IsLetterLowercase())
                {
                    await file.WriteLineAsync($"{item.Key.ToChar()}: {item.Value}");
                }
                else
                {
                    await file.WriteLineAsync($"{item.Key}: {item.Value}");
                }
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            // TODO: remove as many try/catches as possible, this isn't Python, nor is it Java
            // TODO: do we even need row/col in ParseNode
            string file;
            bool   getHelp = false, useREPL = args.Length == 0;

            var options = new OptionSet {
                {
                    "h|help", "Show help", n => getHelp = n != null
                }, {
                    "r|repl", "Use REPL", n => useREPL = n != null
                }, {
                    "c|cauliflower", "Use Cauliflower", n => IsCauliflower = n != null
                }
            };
            IEnumerable <string> argv = options.Parse(args);
            var interpreter           = IsCauliflower ? new CauliflowerInterpreter(new BList(argv.Skip(1).Select(i => new BString(i)))) : new Interpreter();
            var prompt             = IsCauliflower ? "cauliflower> " : "broccoli> ";
            var continuationPrompt = IsCauliflower ? "           > " : "        > ";

            file = argv.FirstOrDefault();
            if (file is null)
            {
                useREPL = true;
            }
            argv = argv.Skip(1);

            if (useREPL)
            {
                while (true)
                {
                    if (CursorLeft != 0)
                    {
                        WriteLine();
                    }
                    ForegroundColor = ConsoleColor.Green;
                    Write(prompt);
                    ForegroundColor = ConsoleColor.White;

                    string ReadOrDie()
                    {
                        var line = ReadLine();

                        if (line is null)
                        {
                            Environment.Exit(0);
                        }
                        return(line);
                    }

                    ParseNode parsed = null;
                    try {
                        parsed = interpreter.Parse(ReadOrDie() + '\n');
                    } catch (Exception e) {
                        WriteLine($"{e.GetType().ToString().Split('.').Last()}: {e.Message}");
                        continue;
                    }
                    while (!parsed.Finished)
                    {
                        ForegroundColor = ConsoleColor.Green;
                        Write(continuationPrompt);
                        ForegroundColor = ConsoleColor.White;
                        try {
                            parsed = interpreter.Parse(ReadOrDie() + '\n', parsed);
                        } catch (Exception e) {
                            WriteLine($"{e.GetType().ToString().Split('.').Last()}: {e.Message}");
                            continue;
                        }
                    }

                    try {
                        var result = interpreter.Run(parsed);
                        if (result != null)
                        {
                            WriteLine(result.Inspect());
                        }
                    } catch (Exception e) {
                        WriteLine($"{e.GetType().ToString().Split('.').Last()}: {e.Message}");
                    }
                }
            }

            if (file == null || getHelp)
            {
                GetHelp(options);
            }

            interpreter.Run(file == "-" ? In.ReadToEnd() : File.ReadAllText(file));
        }