//Fatal error, with token context. public static void Fatal(ParserRuleContext context, string errMessage) { //Is error catching on? if (AlgoRuntimeInformation.CatchExceptions) { //Don't actually perform any error tasks, just set the caught message. AlgoRuntimeInformation.SetExceptionMessage(errMessage); throw new Exception("Error caught, you shouldn't be seeing this message."); } //Set console colours. Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Red; //Check the loaded file is right. if (AlgoRuntimeInformation.FileLoaded == "") { AlgoRuntimeInformation.FileLoaded = "No File"; } //Print the actual error message. if (context == null) { Console.WriteLine("Algo Runtime Error: " + AlgoRuntimeInformation.FileLoaded + ", NOCONTEXT - " + errMessage); } else { Console.WriteLine("Algo Runtime Error: " + AlgoRuntimeInformation.FileLoaded + ", Line " + context.Start.Line + ":" + context.Start.StartIndex + " - " + errMessage); } Console.ResetColor(); //Only print the scopes in developer mode. if (AlgoRuntimeInformation.DeveloperMode) { ANTLRDebug.PrintScopes(); } //Test mode? Throw instead of exit. if (AlgoRuntimeInformation.UnitTestMode) { throw new Exception(errMessage); } //Check if we're in continuous mode. If so, just restart (no header). if (AlgoRuntimeInformation.ContinuousMode) { //If in continuous mode, just keep going and run the interpreter again. Program.Main(new string[] { "--nohead" }); Environment.Exit(0); } else { //Normal mode, just exit. Environment.Exit(-1); } }
/// <summary> /// Runs Algo in "inline" mode (no package manager, etc). Returns an exit code. /// </summary> private static int RunAlgoInline(InlineCLIOptions opts, string[] originalArgs) { //Set developer mode on if necessary. if (opts.DeveloperMode) { AlgoRuntimeInformation.DeveloperMode = true; } if (opts.TestMode) { AlgoRuntimeInformation.UnitTestMode = true; } //Displaying any generic info and then shutting off? if (opts.ShowVersionOnly) { PrintVersionInfo(); return(0); } //Compiling a file? if (opts.Compile != null) { //Attempt to compile. ALEC.Compile(opts.Compile); return(0); } //Running the script interpreter, or the live interpreter? if (opts.ScriptFile == null) { //Run live. //Print version info. If --nohead is on, then the header info for the interpreter is skipped. if (!opts.NoHeader) { PrintVersionInfo(); Console.WriteLine("Starting interpreter...\n"); } //Create a visitor. if (visitor == null) { visitor = new algoVisitor(); //Load core library. visitor.LoadCoreLibrary(); } //Interactive interpreter. while (true) { Console.Write(">> "); string line = Console.ReadLine(); //Catch keywords and null strings. if (line == "quit" || line == "exit" || line == "stop") { break; } if (line == "help") { PrintHelp(); continue; } if (line == "clear") { Console.Clear(); continue; } if (line == "") { continue; } //Parse line. var s_chars = new AntlrInputStream(line); var s_lexer = new algoLexer(s_chars); var s_tokens = new CommonTokenStream(s_lexer); var s_parse = new algoParser(s_tokens); //Turn on continuous mode. AlgoRuntimeInformation.ContinuousMode = true; //Execute. s_parse.BuildParseTree = true; var s_tree = s_parse.compileUnit(); try { visitor.VisitCompileUnit(s_tree); } catch (Exception e) { //Internal exception. if (!AlgoRuntimeInformation.UnitTestMode) { Error.Internal(e.Message); } else { throw e; } } } return(0); } else { //Run normal script. //Does the given file location exist? string fullPath = CPFilePath.GetPlatformFilePath(new string[] { Environment.CurrentDirectory, opts.ScriptFile }); if (!File.Exists(fullPath)) { Error.FatalNoContext("No file with the name '" + opts.ScriptFile + "' exists relative to your current directory."); return(-1); } //Loading in the file arguments. List <string> args = originalArgs.ToList(); args.RemoveAll(x => x.StartsWith("-")); algoVisitor.SetConsoleArguments(args.Skip(1).ToArray()); //Read in the input. AlgoRuntimeInformation.FileLoaded = opts.ScriptFile; string input = File.ReadAllText(fullPath); var chars = new AntlrInputStream(input); var lexer = new algoLexer(chars); var tokens = new CommonTokenStream(lexer); //Debug print. if (AlgoRuntimeInformation.DeveloperMode) { ANTLRDebug.PrintTokens(lexer); } //Debug print tree. var parser = new algoParser(tokens); parser.BuildParseTree = true; var tree = parser.compileUnit(); if (AlgoRuntimeInformation.DeveloperMode) { ANTLRDebug.PrintParseList(tree, parser); //Add a gap. Console.WriteLine(" --------------------\n | BEGIN EVALUATION |\n --------------------\n"); } //Walking the tree. visitor = new algoVisitor(); visitor.LoadCoreLibrary(); visitor.VisitCompileUnit(tree); if (AlgoRuntimeInformation.DeveloperMode) { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("\n ------------------\n | END EVALUATION |\n ------------------\n"); //Print variables. ANTLRDebug.PrintScopes(); } return(0); } }