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); } }
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."); } }
public virtual void Help() { BetterConsole.WriteOnNextLine(CommandInfo); if (!string.IsNullOrEmpty(Usage)) { BetterConsole.WriteOnNextLine(Usage); } }
public void Call(List <string> args) { if (args.Count < RequiredArgNum) { if (!string.IsNullOrEmpty(Usage)) { BetterConsole.WriteOnNextLine(Usage); } } else { ExecuteCmd(args); } }
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>()); }
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(); } } }
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); }) ); } }
protected override void ExecuteCmd(List <string> args) { BetterConsole.WriteOnNextLine($"Available commands: {string.Join(", ", _commands.Values.Select(x => x.Name).ToArray())}"); }