private void cmdVersion(Repl repl, Command c, string[] args) { Hr(); NewLine(); Write(" " + Software.Name, ConsoleColor.White); var sv = Software.Version; Write($" v{sv.Major}.{sv.Minor}.{sv.Revision} build {sv.Build}\n"); Write(" " + Software.Copyright + "\n"); if (!string.IsNullOrEmpty(Software.License)) { Write(" License: ", ConsoleColor.White); Write(Software.License + "\n"); } if (!string.IsNullOrEmpty(Software.Message)) { Write(Software.Message); NewLine(); } NewLine(); Hr(); NewLine(); var lv = Assembly.GetAssembly(GetType()).GetName().Version; Write(" LibREPL", ConsoleColor.White); Write($" v{lv.Major}.{lv.Minor}.{lv.Revision} build {lv.Build}\n"); Write(" Copyright © 20" + (lv.Build / 1000) + ", piksel bitworks\n"); NewLine(); Hr(); }
private void cmdDebug(Repl repl, Command c, string[] args) { if (args.Length > 0) { var newDebug = Repl.ParseBoolean(args[0]); if (newDebug.HasValue) { PrintInput = newDebug.Value; } else { Write(" Error", ConsoleColor.Red); Write(": Unknown boolean value "); Write(args[0], ConsoleColor.Cyan); Write(".\n"); } } Write(" Debug is "); if (PrintInput) { Write("ENABLED", ConsoleColor.Green); } else { Write("DISABLED", ConsoleColor.Red); } Write(".\n"); }
internal bool GetBoolArgumentOrDefault(string[] args, int index) { // TODO: Perhaps a bit to "clever" of a function? -NM 2016-02-14 var value = GetArgumentOrDefault(args, index); bool parsed; return((Repl.TryParseBoolean(value, out parsed) || (value != Arguments[index].Default && Repl.TryParseBoolean(Arguments[index].Default, out parsed))) ? parsed : false); }
private void cmdHelp(Repl repl, Command c, string[] args) { if (args.Length < 1) { PrintHelp(); } else { switch (args[0].ToLower()) { case "command": Write(" No, as in substitute COMMAND for a command you want help on.\n Use "); Write("help commands", ConsoleColor.White); Write(" to list available commands.\n"); break; case "commands": Write(" Registered commands:\n"); var maxNameLength = 0; foreach (var name in Commands.Keys) { if (maxNameLength < name.Length) { maxNameLength = name.Length; } } foreach (var cmdKvp in Commands) { var cmd = cmdKvp.Value; var name = cmdKvp.Key; Write(" " + name.PadLeft(maxNameLength, ' '), ConsoleColor.Cyan); WriteFormatted(" " + cmd.Description); NewLine(); } break; default: if (Commands.ContainsKey(args[0])) { var cc = Commands[args[0]]; WriteFormatted(" " + cc.Description + "\n\n"); Write(" Usage:\n"); Write(" " + args[0], ConsoleColor.White); var maxArgLength = 0; foreach (var ca in cc.Arguments) { if (ca.Keyword.Length > maxArgLength) { maxArgLength = ca.Keyword.Length; } Write(" " + ca.Keyword, ca.Required ? ConsoleColor.Red : ConsoleColor.Cyan); } Write("\n\n Arguments:\n"); if (cc.Arguments.Count > 0) { foreach (var ca in cc.Arguments) { Write(" " + ca.Keyword.PadLeft(maxArgLength, ' '), ConsoleColor.White); if (true || ca.Type != ArgumentType.String) { Write(" "); Dir(ca.Type); Write(" "); } if (!string.IsNullOrEmpty(ca.Default)) { Write("(Default: "); Write(ca.Default, ConsoleColor.Yellow); Write(") "); } if (ca.Required) { Write("(Required) "); } NewLine(); Write(new string(' ', maxArgLength + 4)); WriteFormatted(ca.Description + "\n\n"); } } else { Write(" (does not take any arguments)\n", ConsoleColor.Yellow); } } break; } } }
static void Main(string[] cmdline_args) { if (cmdline_args.Length != 1) { Console.WriteLine("One argument required: path to script to compile and run."); return; } string program = null; using (var inFile = new StreamReader(cmdline_args[0])) { program = inFile.ReadToEnd(); } var inputStream = new AntlrInputStream(program); var lexer = new CloacaLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); var errorListener = new ParseErrorListener(); var parser = new CloacaParser(commonTokenStream); parser.AddErrorListener(errorListener); if (errorListener.Errors.Count > 0) { Console.WriteLine("There were errors trying to compile the script. We cannot run it."); return; } var antlrVisitorContext = parser.file_input(); var variablesIn = new Dictionary <string, object>(); var visitor = new CloacaBytecodeVisitor(variablesIn); visitor.Visit(antlrVisitorContext); CodeObject compiledProgram = visitor.RootProgram.Build(); var scheduler = new Scheduler(); var interpreter = new Interpreter(scheduler); interpreter.DumpState = true; scheduler.SetInterpreter(interpreter); var context = scheduler.Schedule(compiledProgram).Frame; foreach (string varName in variablesIn.Keys) { context.SetVariable(varName, variablesIn[varName]); } interpreter.StepMode = true; bool traceMode = false; var debugRepl = new Piksel.LibREPL.Repl("dbg> ") { HeaderTitle = "Cloaca Interpreter Debugger", HeaderSubTitle = "Debug Cloaca ByteCode Evaluation" }; debugRepl.Commands.Add("g", new Command() { Action = (repl, cmd, args) => { if (scheduler.Done) { repl.Write("Scheduled programs are all done."); } else { interpreter.StepMode = false; while (!scheduler.Done) { try { scheduler.Tick().Wait(); } catch (AggregateException wrappedEscapedException) { // Given the nature of exception handling, we should normally only have one of these! ExceptionDispatchInfo.Capture(wrappedEscapedException.InnerExceptions[0]).Throw(); } } } }, Description = "Runs until finished" }); debugRepl.Commands.Add("s", new Command() { Action = (repl, cmd, args) => { if (scheduler.Done) { repl.Write("Scheduled programs are all done."); } else { interpreter.StepMode = true; try { scheduler.Tick().Wait(); } catch (AggregateException wrappedEscapedException) { // Given the nature of exception handling, we should normally only have one of these! ExceptionDispatchInfo.Capture(wrappedEscapedException.InnerExceptions[0]).Throw(); } if (traceMode) { DumpState(scheduler); } } }, Description = "Steps one line of bytecode" }); debugRepl.Commands.Add("d", new Command() { Action = (repl, cmd, args) => { if (scheduler.Done) { repl.Write("Scheduled programs are all done."); } else { var currentTasklet = scheduler.LastTasklet; if (currentTasklet != null && currentTasklet.Cursor < currentTasklet.CodeBytes.Bytes.Length) { DumpDatastack(currentTasklet); } } }, Description = "Dumps the data stack" }); debugRepl.Commands.Add("t", new Command() { Action = (repl, cmd, args) => { traceMode = !traceMode; if (traceMode) { repl.Write("Trace mode on."); } else { repl.Write("Trace mode off."); } }, Description = "Toggle trace mode." }); debugRepl.Commands.Add("c", new Command() { Action = (repl, cmd, args) => { if (scheduler.Done) { repl.Write("Scheduled programs are all done."); } else { DumpCode(scheduler); } }, Description = "Disassembles the current code object." }); debugRepl.Commands.Add("l", new Command() { Action = (repl, cmd, args) => { if (scheduler.Done) { repl.Write("Scheduled programs are all done."); } else { var currentTasklet = scheduler.LastTasklet; if (currentTasklet != null && currentTasklet.Cursor < currentTasklet.CodeBytes.Bytes.Length) { if (args.Length == 0) { repl.Write(Dis.dis(currentTasklet.Program, currentTasklet.Cursor, 1)); } else if (args.Length == 1) { int count = Int32.Parse(args[0]); repl.Write(Dis.dis(currentTasklet.Program, currentTasklet.Cursor, count)); } } } }, Description = "Disassembles byte code based on the current location (not implemented yet)." }); debugRepl.Start(); }