public void Should_not_scope_atom_nodes()
        {
            var fooNode = new NumberNode("123");
            var body    = new ListNode(fooNode);
            var methodDefinitionNode = new MethodDefinitionNode("sayMessage", new ParameterDefinitionNode[] { new ParameterDefinitionNode("message"), }, body);

            var globalScope = new GlobalScope <SymbolNode>();

            var builder     = CreateScopeBuilder(globalScope);
            var resultScope = builder.GetScope(methodDefinitionNode, globalScope);

            resultScope.Descendants().OfType <BoundScope <SymbolNode> >().Any(scope => scope.Node == fooNode).ShouldBe(false);
        }
        public void Should_scope_parameters()
        {
            var body = new ListNode();
            var methodDefinitionNode = new MethodDefinitionNode("sayMessage", new ParameterDefinitionNode[] { new ParameterDefinitionNode("message"), }, body);

            var globalScope = new GlobalScope <SymbolNode>();

            var builder     = CreateScopeBuilder(globalScope);
            var resultScope = builder.GetScope(methodDefinitionNode, globalScope);

            resultScope.Resolve("message").ShouldNotBe(null);
            resultScope.Resolve("message").ShouldBeOfType <ParameterDefinitionNode>();
        }
        public IBoundScope <TItem> GetScope(MethodDefinitionNode node, IScope <TItem> parentScope)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            var            methodName  = node.MethodName;
            IScope <TItem> methodScope = new MethodScope <TItem>(methodName, parentScope);


            // Define the method itself
            _scopingStrategy.Define(node, parentScope);

            // Bind the parameters
            foreach (var parameter in node.Parameters)
            {
                _scopingStrategy.Define(parameter, methodScope);
            }

            // Bind the variables
            var body = node.MethodBody;

            if (body == null)
            {
                return(new BoundScope <TItem>(methodScope, node));
            }

            var variableDeclarations = body.Children
                                       .OfType <VariableDefinitionNode>()
                                       .Where(n => n != null)
                                       .ToArray();

            foreach (var declaration in variableDeclarations)
            {
                _scopingStrategy.Define(declaration, methodScope);
            }

            // Bind the method body
            var subBuilder = new ScopeBuilder <TItem>(methodScope, _scopingStrategy);
            var bodyScope  = subBuilder.Visit(body);

            var boundScope = new BoundScope <TItem>(methodScope, node, new[] { bodyScope });

            return(boundScope);
        }
        public void Should_scope_variable_definitions()
        {
            var body = new ListNode(new VariableDefinitionNode("someBooleanValue", new FalseNode()));
            var methodDefinitionNode = new MethodDefinitionNode("sayMessage", new ParameterDefinitionNode[] { new ParameterDefinitionNode("message"), }, body);

            var globalScope = new GlobalScope <SymbolNode>();

            var builder     = CreateScopeBuilder(globalScope);
            var resultScope = builder.GetScope(methodDefinitionNode, globalScope);

            resultScope.Resolve("message").ShouldNotBe(null);
            resultScope.Resolve("message").ShouldBeOfType <ParameterDefinitionNode>();
            resultScope.Resolve("someBooleanValue").ShouldNotBe(null);

            var symbol = resultScope.Resolve("someBooleanValue") as VariableDefinitionNode;

            symbol.ShouldNotBe(null);
            symbol.Value.ShouldBeOfType <FalseNode>();
        }
Esempio n. 5
0
        private static void Walk([NotNull] Agent agent, [NotNull] MethodDefinitionNode methodDefinition, bool isStrict)
        {
            //TODO
            if (methodDefinition.Kind != PropertyKind.Constructor)
            {
                Walk(agent, methodDefinition.Key, isStrict);
                Walk(agent, methodDefinition.Value, isStrict);
            }
            else
            {
                Walk(agent, methodDefinition.Key, isStrict);
                if (methodDefinition.Value.Id != null)
                {
                    Walk(agent, methodDefinition.Value.Id, isStrict);
                }

                foreach (var parameter in methodDefinition.Value.Parameters)
                {
                    Walk(agent, parameter, isStrict);
                }

                Walk(agent, methodDefinition.Value.Body, isStrict);
            }
        }
 public void Define(MethodDefinitionNode node, IScope <SymbolNode> parentScope)
 {
     parentScope.Define(node.Symbol, node);
 }