示例#1
0
文件: Program.cs 项目: pipe01/LICC
        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());
        }
示例#2
0
        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();
        }
示例#3
0
文件: LsfRunner.cs 项目: pipe01/LICC
        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);
            }
        }
示例#4
0
文件: Program.cs 项目: pipe01/LICC
 public object DoTest(object asd)
 {
     LConsole.WriteLine("Function called: " + asd);
     return(this);
 }
示例#5
0
        ///<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);
            }
        }