示例#1
0
文件: ALEC.cs 项目: c272/algo-lang
        /// <summary>
        /// Performs a syntax check on a provided Algo script. Throws a compile error if failing.
        /// </summary>
        private static void SyntaxCheck(string script, int dependencyLineCount)
        {
            //Read in the input.
            var chars  = new AntlrInputStream(script);
            var lexer  = new algoLexer(chars);
            var tokens = new CommonTokenStream(lexer);

            //Attempt to parse.
            var parser = new algoParser(tokens);

            parser.BuildParseTree = true;
            var tree = parser.compileUnit();

            //Walk the tree, check if there are any compile exceptions.
            //Is there any content?
            if (tree.block() == null)
            {
                Error.FatalCompile("No valid content was found to compile in the provided Algo script.");
            }

            //Walk all children.
            foreach (var child in tree.block().statement())
            {
                //Does this one have an exception?
                if (child.exception != null && child.exception != default(RecognitionException))
                {
                    Error.FatalCompile("Syntax error in provided Algo script, at original file line " + (child.Start.Line - dependencyLineCount + 1) + ".");
                }
            }

            Log("Script statement syntax check passed successfully.", ALECEvent.Success);
        }
示例#2
0
        //Runs an Algo script, given a file path.
        public void RunAlgoScript(string path, string newScopeName = "")
        {
            //Read the entire text file into a lexer and tokens.
            string input  = File.ReadAllText(path);
            var    chars  = new AntlrInputStream(input);
            var    lexer  = new algoLexer(chars);
            var    tokens = new CommonTokenStream(lexer);

            //Parse the file.
            var parser = new algoParser(tokens);

            parser.BuildParseTree = true;
            var tree = parser.compileUnit();

            //Set the currently loaded file.
            FileInfo fi      = new FileInfo(path);
            string   oldFile = AlgoRuntimeInformation.FileLoaded;

            AlgoRuntimeInformation.FileLoaded = fi.Name;

            //If this is being placed in a separate scope, switch out now.
            AlgoScopeCollection oldScope = null;

            if (newScopeName != "")
            {
                oldScope = Scopes;
                Scopes   = new AlgoScopeCollection();
            }

            //Visit this tree, and fully execute.
            VisitCompileUnit(tree);

            //Set the currently loaded file back.
            AlgoRuntimeInformation.FileLoaded = oldFile;

            //If it was executed in a separate scope, save as a library with this name.
            if (newScopeName != "")
            {
                AlgoScopeCollection importScope = Scopes;
                Scopes = oldScope;
                Scopes.AddLibrary(newScopeName, importScope);
            }
        }
示例#3
0
        /// <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);
            }
        }