public void Visit(WhereStatement statement)
        {
            var statementDomArg = VisitChild(statement.BooleanExpression, new CodeDomArg() { Scope = _codeStack.Peek().Scope });

            CodeMemberMethod method = new CodeMemberMethod();
            method.Name = "Where_" + statementDomArg.MethodIdentifier;
            method.Attributes = MemberAttributes.Private;
            method.ReturnType = _codeStack.Peek().Scope.CodeDomReference;
            method.Parameters.Add(new CodeParameterDeclarationExpression(_codeStack.Peek().Scope.CodeDomReference, "table"));
            GenerateCallStatement(method.Statements, statement.Line.Line);

            _mainType.Type.Members.Add(method);

            method.Statements.Add(new CodeVariableDeclarationStatement(_codeStack.Peek().Scope.CodeDomReference, "newTable",
                new CodeObjectCreateExpression(_codeStack.Peek().Scope.CodeDomReference)));

            var boolean = new CodeSnippetExpression("row => {" + GenerateCodeFromStatement(new CodeMethodReturnStatement(statementDomArg.CodeExpression)) + "}");
            var rowType = new CodeTypeReference("IList", new CodeTypeReference(_codeStack.Peek().Scope.CodeDomReference.TypeArguments[0].BaseType));

            method.Statements.Add(new CodeVariableDeclarationStatement(rowType, "rows",
                new CodeMethodInvokeExpression(
                    new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(
                        new CodeTypeReferenceExpression("table"), "Where"), boolean), "ToList"))
                );

            method.Statements.Add(new CodeMethodInvokeExpression(
                new CodeVariableReferenceExpression("newTable"), "SetRows", new CodeVariableReferenceExpression("rows")));

            if(statement.NodesBooleanExpression != null)
            {
                method.Statements.Add(new CodeVariableDeclarationStatement(
                    new CodeTypeReference("IEnumerator",
                        new CodeTypeReference(_codeStack.Peek().Scope.CodeDomReference.TypeArguments[0].BaseType)), "i",
                        new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("newTable"), "GetEnumerator")));

                var loop = new CodeIterationStatement();
                method.Statements.Add(loop);
                loop.InitStatement = new CodeSnippetStatement();
                loop.IncrementStatement = new CodeSnippetStatement();
                loop.TestExpression = new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("i"), "MoveNext");

                loop.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(_codeStack.Peek().Scope.CodeDomReference.TypeArguments[0].BaseType), "row",
                    new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("i"), "Current")));

                var aliases = Scope.Current.AliasType<DownloadPage>();
                if(aliases.Length == 1)
                {
                    loop.Statements.Add(new CodeAssignStatement(
                        new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("row"), aliases[0]),
                        new CodeMethodInvokeExpression(
                        new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("row"), aliases[0]), "CssWhere",
                        new CodePrimitiveExpression(statement.NodesBooleanExpression.Selector))));

                }
                else if(aliases.Length > 0) //more than one
                {
                    throw new InvalidOperationException("AmbiguousSelectVariable");
                    //Errors.Add(new AmbiguousSelectVariable(new Semantic.LineInfo(statement.NodesBooleanExpression.Line.Line, statement.NodesBooleanExpression.Line.CharacterPosition), "nodes"));
                }
            }

            method.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("newTable")));

            var methodcall = new CodeMethodInvokeExpression(
             new CodeMethodReferenceExpression(null, method.Name), new CodeArgumentReferenceExpression("fromTable"));

            _codeStack.Peek().CodeExpression = methodcall;
        }
示例#2
0
 public void Visit(WhereStatement statement, CommonTree tree)
 {
     Parent(tree).Children.Add(statement);
     SetLine(statement, tree);
     VisitChildren(tree);
 }