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 BoundScope CreateParentScope(BoundGlobalScope previous)
        {
            var stack = new Stack <BoundGlobalScope>();

            while (previous != null)
            {
                stack.Push(previous);
                previous = previous.Previous;
            }

            var parent = CreateRootScope();

            while (stack.Count > 0)
            {
                previous = stack.Pop();
                var scope = new BoundScope(parent);

                foreach (var f in previous.Functions)
                {
                    scope.TryDeclareFunction(f);
                }

                foreach (var v in previous.Variables)
                {
                    scope.TryDeclareVariable(v);
                }

                parent = scope;
            }

            return(parent);
        }
Exemplo n.º 3
0
        public static BoundGlobalScope BindGlobalScope(BoundGlobalScope previous, CompilationUnitSyntax syntax)
        {
            var parentScope = CreateParentScope(previous);
            var binder      = new Binder(parentScope, function: null);

            foreach (var function in syntax.Members.OfType <FunctionDeclarationSyntax>())
            {
                binder.BindFunctionDeclaration(function);
            }

            var statements = ImmutableArray.CreateBuilder <BoundStatement>();

            foreach (var globalStatement in syntax.Members.OfType <GlobalStatementSyntax>())
            {
                var statement = binder.BindStatement(globalStatement.Statement);
                statements.Add(statement);
            }

            var functions   = binder._scope.GetDeclaredFunctions();
            var variables   = binder._scope.GetDeclaredVariables();
            var diagnostics = binder.Diagnostics.ToImmutableArray();

            if (previous != null)
            {
                diagnostics = diagnostics.InsertRange(0, previous.Diagnostics);
            }

            return(new BoundGlobalScope(previous, diagnostics, functions, variables, statements.ToImmutable()));
        }
Exemplo n.º 4
0
 public BoundGlobalScope(BoundGlobalScope previous, ImmutableArray <Diagnostic> diagnostics, ImmutableArray <VariableSymbol> variables, BoundStatement statement)
 {
     Previous    = previous;
     Diagnostics = diagnostics;
     Variables   = variables;
     Statement   = statement;
 }
Exemplo n.º 5
0
 public BoundGlobalScope(BoundGlobalScope previous, ImmutableArray <Diagnostic> diagnostics, ImmutableArray <FunctionSymbol> functions, ImmutableArray <VariableSymbol> variables, ImmutableArray <BoundStatement> statements)
 {
     Previous    = previous;
     Diagnostics = diagnostics;
     Functions   = functions;
     Variables   = variables;
     Statements  = statements;
 }
Exemplo n.º 6
0
        public static BoundGlobalScope BindGlobalScope(BoundGlobalScope previous, CompilationUnitSyntax syntax)
        {
            var parentScope = CreateParentScopes(previous);
            var binder      = new Binder(parentScope);
            var expression  = binder.BindStatement(syntax.Statement);
            var variables   = binder._scope.GetDeclaredVariables();
            var diagnostics = binder.Diagnostics.ToImmutableArray();

            if (previous != null)
            {
                diagnostics = diagnostics.InsertRange(0, previous.Diagnostics);
            }
            return(new BoundGlobalScope(previous, diagnostics, variables, expression));
        }
Exemplo n.º 7
0
 public BoundGlobalScope(
     BoundGlobalScope previous,
     DiagnosticBag diagnostics,
     FunctionSymbol mainFunction,
     FunctionSymbol scriptFunction,
     IEnumerable <FunctionSymbol> functions,
     IEnumerable <VariableSymbol> variables,
     IEnumerable <BoundStatement> statements
     ) : this(
         previous,
         diagnostics,
         mainFunction,
         scriptFunction,
         functions.ToImmutableArray(),
         variables.ToImmutableArray(),
         statements.ToImmutableArray())
 {
 }
Exemplo n.º 8
0
        public BoundGlobalScope(
            BoundGlobalScope previous,
            DiagnosticBag diagnostics,
            FunctionSymbol mainFunction,
            FunctionSymbol scriptFunction,
            ImmutableArray <FunctionSymbol> functions,
            ImmutableArray <VariableSymbol> variables,
            ImmutableArray <BoundStatement> statements)
        {
            Previous = previous;

            Diagnostics = diagnostics
                          ?? throw new ArgumentNullException(nameof(diagnostics));

            MainFunction   = mainFunction;
            ScriptFunction = scriptFunction;
            Functions      = functions;
            Variables      = variables;
            Statements     = statements;
        }