示例#1
0
        public static void Main(string[] args)
        {
            Console.Clear();

            while (true)  //handle switching prompts
            {
                Prompt(Constants.ReplPromptName, x => { REPLCommandInterpreter.Execute(x); }, _cmdBuffer);
                Prompt(LanguageName, x => {
                    if (_languageInterp != null)
                    {
                        var results = _languageInterp(x);
                        if (results.Item1.Count > 0)
                        {
                            BetterConsole.WriteOnNextLine($"Result: ({string.Join(", ", results.Item1)})", ConsoleColor.Green);
                        }
                        if (results.Item2.Count > 0)
                        {
                            BetterConsole.WriteOnNextLine($"Errors: \n{string.Join("\n\n", results.Item2.Select(y => TreePrinter.ToString(y, z => z.SubErrors)))}", ConsoleColor.Red);
                        }
                    }
                    else
                    {
                        BetterConsole.WriteOnNextLine("Language not loaded. Use REPL Command {Language} to load a language assembly.");
                    }
                }, _langBuffer);
            }
        }
示例#2
0
        protected override void ExecuteCmd(List <string> args)
        {
            if (_languagedLoaded)
            {
                BetterConsole.WriteOnNextLine("Language already loaded, unloading languages is not currently supported.");
                return;
            }

            if (!File.Exists(args[0]))
            {
                BetterConsole.WriteOnNextLine($"No file named {args[0]}");
                return;
            }

            try {
                var assem        = Assembly.LoadFile(args[0]);
                var handlers     = assem.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ISymbolHandler))).Select(x => (ISymbolHandler)Activator.CreateInstance(x)).ToArray();
                var languageInfo = (ILanguageInfo)Activator.CreateInstance(assem.GetTypes().First(x => x.GetInterfaces().Contains(typeof(ILanguageInfo))));
                Program.LanguageName = languageInfo.Name;

                var results = LoadLanguageInterpreter.MakeTemporaryParser(languageInfo.Grammar);
                if (results.Item2.Count > 0)
                {
                    BetterConsole.WriteOnNextLine($"Failure to load language: {string.Join("\n\n", results.Item2.Select(y => TreePrinter.ToString(y, z => z.SubErrors)))}", ConsoleColor.Red);
                    return;
                }

                Program._languageInterp = code => results.Item1(code, handlers);
                _languagedLoaded        = true;
                BetterConsole.WriteOnNextLine($"{languageInfo.Name} loaded.");
            } catch (Exception ex) {
                BetterConsole.WriteOnNextLine($"{args[0]} is not a path to a valid language assembly.");
            }
        }
示例#3
0
 public virtual void Help()
 {
     BetterConsole.WriteOnNextLine(CommandInfo);
     if (!string.IsNullOrEmpty(Usage))
     {
         BetterConsole.WriteOnNextLine(Usage);
     }
 }
示例#4
0
 public void Call(List <string> args)
 {
     if (args.Count < RequiredArgNum)
     {
         if (!string.IsNullOrEmpty(Usage))
         {
             BetterConsole.WriteOnNextLine(Usage);
         }
     }
     else
     {
         ExecuteCmd(args);
     }
 }
示例#5
0
        public static List <object> CallREPLCommand(List <object> args)
        {
            var strArgs  = args.Select(x => (string)x).Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim('"')).ToList();
            var maybeCmd = _replCommands.TryGetValue(strArgs[0].ToLower());

            if (maybeCmd.IsNone)
            {
                BetterConsole.WriteOnNextLine(Constants.UnknownCmdFmtStr, strArgs[0]);
            }
            else
            {
                maybeCmd.Value.Call(strArgs.Skip(1).ToList());
            }
            return(new List <object>());
        }
示例#6
0
 protected override void ExecuteCmd(List <string> args)
 {
     if (args.Count == 0)
     {
         Help();
     }
     else
     {
         var maybeCmd = _commands.TryGetValue((args[0]).ToLower());
         if (maybeCmd.IsNone)
         {
             BetterConsole.WriteOnNextLine(Constants.UnknownCmdFmtStr, args[0]);
         }
         else
         {
             maybeCmd.Value.Help();
         }
     }
 }
示例#7
0
        private static void Prompt(string promptText, Action <string> handler, List <string> buffer)
        {
            var done = false;

            while (!done)
            {
                BetterConsole.Prompt(
                    $"{promptText}>",
                    buffer,
                    BetterConsole.MakeKeyHandler(ConsoleKey.Escape, v => done = true),
                    BetterConsole.MakeKeyHandler(ConsoleKey.Enter, v => {
                    if (!string.IsNullOrEmpty(v))
                    {
                        buffer.Remove(v);
                        buffer.Add(v);
                        handler(v);
                    }

                    BetterConsole.WriteLine();
                    return(true);
                })
                    );
            }
        }
示例#8
0
 protected override void ExecuteCmd(List <string> args)
 {
     BetterConsole.WriteOnNextLine($"Available commands: {string.Join(", ", _commands.Values.Select(x => x.Name).ToArray())}");
 }