Beispiel #1
0
        private static BoundScope CreateParentScope(BoundGlobalScope previous)
        {
            var stack = new Stack <BoundGlobalScope>();

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

                previous = previous.Previous;
            }

            BoundScope parent = null;

            while (stack.Count > 0)
            {
                previous = stack.Pop();

                var scope = new BoundScope(parent);

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

                parent = scope;
            }

            return(parent);
        }
Beispiel #2
0
        private BoundStatement BindBlockStatement(BlockStatementSyntax syntax)
        {
            _scope = new BoundScope(_scope);

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

            foreach (var statementSyntax in syntax.Statements)
            {
                var statement = BindStatement(statementSyntax);

                statements.Add(statement);
            }

            _scope = _scope.Parent;

            return(new BoundBlockStatement(statements.ToImmutable()));
        }
Beispiel #3
0
        private BoundStatement BindForStatement(ForStatementSyntax syntax)
        {
            var lowerBound = BindExpression(syntax.LowerBoundExpression, TypeSymbol.Int);
            var upperBound = BindExpression(syntax.UpperBoundExpression, TypeSymbol.Int);

            _scope = new BoundScope(_scope);

            var name = syntax.IdentifierToken.Text;

            var variable = new VariableSymbol(name, TypeSymbol.Int, true);

            if (!_scope.TryDeclareVariable(variable))
            {
                _diagnostics.ReportVariableAlreadyDeclared(syntax.IdentifierToken.Span, name);
            }

            var body = BindStatement(syntax.Body);

            _scope = _scope.Parent;

            return(new BoundForStatement(variable, lowerBound, upperBound, body));
        }
Beispiel #4
0
 public BoundScope(BoundScope parent)
 {
     Parent = parent;
 }
Beispiel #5
0
 private Binder(BoundScope parent)
 {
     _scope = new BoundScope(parent);
 }