Exemplo n.º 1
0
        public static BoundProgram BindProgram(BoundGlobalScope globalScope)
        {
            var parentScope = CreateParentScope(globalScope);

            var functionBodies = ImmutableDictionary.CreateBuilder <FunctionSymbol, BoundBlockStatement>();
            var diagnostics    = ImmutableArray.CreateBuilder <Diagnostic>();

            var scope = globalScope;

            while (scope != null)
            {
                foreach (var function in scope.Functions)
                {
                    var binder      = new Binder(parentScope, function);
                    var body        = binder.BindStatement(function.Declaration.Body);
                    var loweredBody = Lowerer.Lower(body);
                    functionBodies.Add(function, loweredBody);

                    diagnostics.AddRange(binder.Diagnostics);
                }

                scope = scope.Previous;
            }

            var statement = Lowerer.Lower(new BoundBlockStatement(globalScope.Statements));

            return(new BoundProgram(diagnostics.ToImmutable(), functionBodies.ToImmutable(), statement));
        }
Exemplo n.º 2
0
        private static LoweredFunction LowerFunction(BoundFunctionDeclaration declaration)
        {
            var loweredBody = Lowerer.Lower(declaration.Body);

            return(new LoweredFunction(
                       declaration: declaration,
                       name: declaration.Name,
                       inputDescription: declaration.InputDescription,
                       outputDescription: declaration.OutputDescription,
                       body: loweredBody));
        }
Exemplo n.º 3
0
        private static void WriteBoundStatementExpression(IndentedTextWriter writer, BoundStatementExpression node)
        {
            var statements = Lowerer.Flatten(FunctionSymbol.Invalid, node.Statement).Statements;

            writer.Indent += 4;
            foreach (var stmt in statements)
            {
                writer.WriteBoundNode(stmt);
                writer.WriteLine();
            }
            writer.Indent -= 4;
        }
Exemplo n.º 4
0
        public static BoundProgram BindProgram(SyntaxTree syntaxTree)
        {
            var binder           = new Binder();
            var boundRoot        = binder.BindRoot(syntaxTree.NullRoot);
            var statements       = ((BoundBlockStatement)boundRoot.File.Body).Statements;
            var functionsBuilder = ImmutableDictionary.CreateBuilder <FunctionSymbol, LoweredFunction>();
            var globalStatements = statements.Where(s => s.Kind != BoundNodeKind.FunctionDeclaration).ToArray();
            var mainFunction     = (FunctionSymbol?)null;
            var scriptFunction   = (FunctionSymbol?)null;

            if (globalStatements.Length > 0)
            {
                // we have to gather all bound expression statements into a "script" function.
                scriptFunction = new FunctionSymbol("%script");
                var body        = Block(globalStatements[0].Syntax, globalStatements);
                var loweredBody = Lowerer.Lower(body);
                var declaration = new BoundFunctionDeclaration(
                    syntax: globalStatements[0].Syntax,
                    name: "%script",
                    inputDescription: ImmutableArray <ParameterSymbol> .Empty,
                    outputDescription: ImmutableArray <ParameterSymbol> .Empty,
                    body: body);
                var loweredFunction = LowerFunction(declaration);
                functionsBuilder.Add(scriptFunction, loweredFunction);
            }

            var functions = statements.OfType <BoundFunctionDeclaration>().ToArray();
            var first     = true;

            foreach (var function in functions)
            {
                var functionSymbol = new FunctionSymbol(
                    name: function.Name);
                var loweredFunction = LowerFunction(function);
                functionsBuilder.Add(functionSymbol, loweredFunction);
                if (first && globalStatements.Length == 0)
                {
                    // the first function in a file will become "main".
                    first        = false;
                    mainFunction = functionSymbol;
                }
            }

            return(new BoundProgram(
                       binder._diagnostics.ToImmutableArray(),
                       mainFunction,
                       scriptFunction,
                       functionsBuilder.ToImmutable()));
        }
Exemplo n.º 5
0
 private BoundBlockStatement GetStatement()
 {
     return(Lowerer.Lower(GlobalScope.Statement));
 }
Exemplo n.º 6
0
 private BoundBlockStatement GetStatement() => Lowerer.Lower(GlobalScope.Statement);
Exemplo n.º 7
0
        private BoundBlockStatement GetStatement()
        {
            var result = GlobalScope.Statement;

            return(Lowerer.Lower(result));
        }