/// <summary>
 /// On all blocknodes, all types are added. No other scope information exists once this finishes
 /// </summary>
 public static AstData CollectTypes(AstData data, SideeffectHelper helper)
 {
     return(new AstData(
                data.TokenStream,
                data.Filename,
                new FirstScopePassVisitor().Visit(data.Tree.RootNode).OwningTree));
 }
 /// <summary>
 /// Adds everything else to scope (Variables, methods)
 /// </summary>
 internal static AstData SecondScopePass(AstData withoutScope, SideeffectHelper notused)
 {
     return(new AstData(
                withoutScope.TokenStream,
                withoutScope.Filename,
                new AddScopeVisitor().Visit(withoutScope.Tree.RootNode).OwningTree));
 }
        /// <summary>
        /// Changes parameters to ref where possible to avoid deep cloning.
        /// </summary>
        internal static AstData RefWherePossible(AstData astData, SideeffectHelper helper)
        {
            var visitor = new RefParametersVisitor();
            var newTree = visitor.Visit(astData.Tree.RootNode).OwningTree;

            return(new AstData(astData.TokenStream, astData.Filename, newTree));
        }
 /// <summary>
 /// Adds type to all typenodes. Outputs error messages if types are not found. No regard for private types atm
 /// </summary>
 internal static AstData PutTypes(AstData arg, SideeffectHelper helper)
 {
     return(new AstData(
                arg.TokenStream,
                arg.Filename,
                new PutTypeVisitor(helper.CompilationMessages, arg.TokenStream, arg.Filename).Visit(arg.Tree.RootNode)
                .OwningTree));
 }
        internal static AstData FoldConstants(AstData astData, SideeffectHelper helper)
        {
            var constantFoldingVisitor = new ConstantFoldingVisitor();
            var operandSortingVisitor  = new OperandSortingVisitor();

            var newTree = astData.Tree.RootNode;

            do
            {
                constantFoldingVisitor.OptimizationsWereMade = false;
                newTree = operandSortingVisitor.Visit(newTree);
                newTree = constantFoldingVisitor.Visit(newTree);
            } while (constantFoldingVisitor.OptimizationsWereMade);

            return(new AstData(astData.TokenStream, astData.Filename, newTree.OwningTree));
        }
        public static ParseTreeData ReadFileToPt(string path, SideeffectHelper helper)
        {
            ICharStream  charStream  = new AntlrFileStream(path, Encoding.UTF8);
            CrawlLexer   tokenSource = new CrawlLexer(charStream);
            ITokenStream tokenStream = new CommonTokenStream(tokenSource);

            CompilationMessageErrorListner cm = new CompilationMessageErrorListner(helper.CompilationMessages, path);

            CrawlParser parser = new CrawlParser(tokenStream);

            parser.RemoveErrorListeners();
            parser.AddErrorListener(cm);

            CrawlParser.Translation_unitContext translationUnit = parser.translation_unit();

            return(new ParseTreeData(tokenStream, translationUnit, path));
        }
            public AstData AddExport(AstData arg, SideeffectHelper helper)
            {
                var tu = ((TranslationUnitNode)arg.Tree.RootNode);

                try
                {
                    return(new AstData(
                               arg.TokenStream,
                               arg.Filename,
                               tu
                               .WithImportedNamespaces(
                                   Namespace.Merge(tu.Imports.Select(TryGetNamespace).ToArray())
                                   )
                               .OwningTree
                               ));
                }
                catch (NamespaceNotFoundException nsnfe)
                {
                    throw helper.FailWith(CompilationMessage.Create(arg.TokenStream, nsnfe.FaultyNode.Interval,
                                                                    MessageCode.NamespaceNotFound, arg.Filename));
                }
            }
        public static AstData CreateAst(ParseTreeData pt, SideeffectHelper helper)
        {
            CrawlSyntaxTree tree = CrawlSyntaxTree.ParseTree(pt.ParseTree, pt.Filename);

            return(new AstData(pt.TokenStream, pt.Filename, tree));
        }
 /// <summary>
 /// Checks that variables are only used after they are declared
 /// </summary>
 internal static AstData DeclerationOrderCheck(AstData arg, SideeffectHelper helper)
 {
     new CheckDeclerationOrderVisitor(helper.CompilationMessages, arg).Visit(arg.Tree.RootNode);
     return(arg);
 }
 /// <summary>
 /// Finishes parsing types
 /// </summary>
 public static AstData FinishTypes(AstData arg1, SideeffectHelper arg2)
 {
     new FinishTypesVisitor().Visit(arg1.Tree.RootNode);
     return(arg1);
 }
        /// <summary>
        /// Check that expressions are used on correct types.
        /// </summary>
        internal static AstData TypeCheck(AstData tree, SideeffectHelper notused)
        {
            var newTree = new TypeVisitor().Visit(tree.Tree.RootNode).OwningTree;

            return(new AstData(tree.TokenStream, tree.Filename, newTree));
        }