Exemple #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 v in previous.Variables)
                {
                    scope.TryDeclare(v);
                }

                parent = scope;
            }

            return(parent);
        }
Exemple #2
0
        private BoundStatement BindVariableDeclaration(VariableDeclarationSyntax syntax)
        {
            var name       = syntax.Identifier.Text;
            var isReadOnly = syntax.Keyword.Kind == SyntaxKind.LetKeyword;
            var expression = BindExpression(syntax.Initializer);
            var variable   = new VariableSymbol(name, isReadOnly, expression.Type);

            if (!_scope.TryDeclare(variable))
            {
                _diagnostics.ReportVariableAlreadyDeclared(syntax.Identifier.Span, name);
            }
            return(new BoundVariableDeclaration(variable, expression));
        }
Exemple #3
0
        private BoundStatement BindForStatement(ForStatementSyntax syntax)
        {
            var name     = syntax.Identifier.Text;
            var variable = new VariableSymbol(name, true, typeof(int));

            _scope = new BoundScope(_scope);

            if (!_scope.TryDeclare(variable))
            {
                _diagnostics.ReportVariableAlreadyDeclared(syntax.Identifier.Span, name);
            }
            var lowerBound = BindExpression(syntax.LowerBound, typeof(int));
            var upperBound = BindExpression(syntax.UpperBound, typeof(int));
            var body       = BindStatement(syntax.Body);

            _scope = _scope.Parent;
            return(new BoundForStatement(variable, lowerBound, upperBound, body));
        }