public static object Test(int num, object str = null) { LConsole.WriteLine($"Hello {num}, {str} ({str?.GetType()})", ConsoleColor.Blue); if (num == 42) { Console.SwitchFrontend(new PlainTextConsoleFrontend()); } return(new TestClass()); }
private void HandleVariable(string varName, string args) { if (!Config.EnableVariables) { LConsole.WriteLine("Variables are disabled", ConsoleColor.Red); return; } var(exists, value) = _Environment.TryGet(varName); if (string.IsNullOrWhiteSpace(args)) { if (!exists) { LConsole.WriteLine($"No variable found with name '{varName}'", ConsoleColor.Red); return; } } else if (args.StartsWith(":=")) { if (!HandleVariableOperation(varName, args.Substring(2).Trim())) { return; } } else if (args.StartsWith("=")) { value = args.Substring(1).Trim(); if (string.IsNullOrWhiteSpace(value)) { _Environment.Remove(varName); LConsole.WriteLine("Cleared variable"); return; } else { _Environment.Set(varName, value); } } LConsole.BeginLine() .Write(varName, ConsoleColor.DarkYellow) .Write(" = ", ConsoleColor.DarkGray) .Write(value) .End(); }
public void Run(string fileName) { var lexemes = Lexer.Lex(fileName, FileSystem).ToArray(); File ast; try { ast = Parser.ParseFile(lexemes); } catch (ParseException ex) when(!Debugger.IsAttached) { LConsole.WriteLine(ex.Message, ConsoleColor.Red); return; } try { Interpreter.Run(ast); } catch (RuntimeException ex) //when (!Debugger.IsAttached) { LConsole.WriteLine(ex.Message, ConsoleColor.Red); } }
public object DoTest(object asd) { LConsole.WriteLine("Function called: " + asd); return(this); }
///<summary>Executes a single line</summary> /// <exception cref="CommandNotFoundException"></exception> /// <exception cref="ParserException"></exception> public void ExecuteLine(string line, bool addToHistory = true) { if (string.IsNullOrWhiteSpace(line)) { return; } line = line.Trim(); if (addToHistory) { History.AddNewItem(line); } int cmdNameSeparatorIndex = line.IndexOf(' '); string cmdName = cmdNameSeparatorIndex == -1 ? line.Substring(0) : line.Substring(0, cmdNameSeparatorIndex).Trim(); string argsLine = cmdNameSeparatorIndex == -1 ? null : line.Substring(cmdNameSeparatorIndex + 1).Trim(); argsLine = ReplaceVariables(argsLine); if (cmdName.StartsWith("$")) { HandleVariable(cmdName.Substring(1), argsLine); return; } var strArgs = GetArgs(argsLine).ToArray(); var(success, _, commandsWithSameName) = CommandFinder.Find(cmdName, strArgs.Length); if (!success) { if (commandsWithSameName == null || commandsWithSameName.Length == 0) { LConsole.WriteLine("Command not found"); } else { foreach (var item in commandsWithSameName) { item.PrintUsage(); } } return; } object[] cmdArgs = null; Command cmd = null; foreach (var possibleCmd in commandsWithSameName) { if (possibleCmd.RequiredParamCount > strArgs.Length) { continue; } var(transformSuccess, transformError) = TryTransformParameters(argsLine, strArgs, possibleCmd, out cmdArgs); if (transformSuccess) { cmd = possibleCmd; break; } } if (cmd == null) { return; } object result = null; try { CommandExecutor.Execute(cmd, cmdArgs); } catch (TargetInvocationException ex) { if (ex.InnerException.GetType().Name == "SuccessException") //For NUnit { throw ex.InnerException; } HandleException(ex.InnerException); } catch (Exception ex) { HandleException(ex); } if (result != null) { LConsole.WriteLine("Command returned: " + result, ConsoleColor.DarkGray); } void HandleException(Exception ex) { _LastException = ex; LConsole.Frontend.PrintException(ex); } }