コード例 #1
0
ファイル: AlgoLibraryManager.cs プロジェクト: c272/algo-lang
        //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);
            }
        }