示例#1
0
        public static IFilterToken IsTrue <T1>(this IFor <T1> ifor,
                                               Expression <Func <T1, bool> > filter,
                                               [CallerFilePath] string file     = "",
                                               [CallerLineNumber] int line      = 0,
                                               [CallerMemberName] string member = "")
        {
            var diag = DiagInfoExpressionFactory.CreateDiag(file, line, member, filter);

            return(ifor.IsTrue(diag, filter.Compile()));
        }
示例#2
0
        public static IRegister Not <T1, T2>(this IFor <T1, T2> ifor,
                                             [CallerFilePath] string file     = "",
                                             [CallerLineNumber] int line      = 0,
                                             [CallerMemberName] string member = "")
        {
            IRegister output = null;

            ifor.Not(x => output = x);
            return(output);
        }
示例#3
0
        public static void IsNot <T1, T2>(this IFor <T1> ifor,
                                          string name,
                                          Expression <Func <T1, T2> > srcFunc,
                                          [CallerFilePath] string file     = "",
                                          [CallerLineNumber] int line      = 0,
                                          [CallerMemberName] string member = "")
        {
            var diag = DiagInfoExpressionFactory.CreateDiag(file, line, member, srcFunc);

            ifor.DoesNotExist(diag, name, TestingContextLimitedInterface.ExpressionalInterfaceExtension.SingleFunc(srcFunc));
        }
示例#4
0
        public static void Each <T1, T2>(this IFor <T1> ifor,
                                         string name,
                                         Expression <Func <T1, IEnumerable <T2> > > srcFunc,
                                         [CallerFilePath] string file     = "",
                                         [CallerLineNumber] int line      = 0,
                                         [CallerMemberName] string member = "")
        {
            var diag = DiagInfoExpressionFactory.CreateDiag(file, line, member, srcFunc);

            ifor.Each(diag, name, srcFunc.Compile());
        }
示例#5
0
        public void VisitFor(IFor @for, Tensor <float> data)
        {
            var f     = (Tensor <float> .For)@for;
            var other = data as Tensor <float> .For;

            if (other == null)
            {
                Arent();
            }
            else
            {
                var loop1 = f.Loop;
                var loop2 = other.Loop;
                And(
                    () => VisitFloatArray(f.Expression, other.Expression),
                    () => VisitFloatArray(f.OutputInfo, other.OutputInfo),
                    () => VisitFloatArrayList(loop1.Sequences.Cast <Tensor <float> >().ToList(), loop2.Sequences.Cast <Tensor <float> >().ToList()),
                    () => VisitFloatArrayList(loop1.Variables.Cast <Tensor <float> >().ToList(), loop2.Variables.Cast <Tensor <float> >().ToList())
                    );
            }
        }
示例#6
0
 public ManagePermissions(IFor item) => _item = item;
示例#7
0
 public void VisitFor(IFor @for, Scalar <float> data)
 {
     throw new NotImplementedException();
 }
示例#8
0
        public virtual void VisitFor(IFor @for, Compiler compiler)
        {
            compiler.Scope.Declare(@for, compiler);
            var loop = @for.Loop;

            if (loop.Compiler != compiler)
            {
                loop.Compiler = compiler;

                // Loop-invariant code motion: sequences variables are not yet declared, so expressions containing the latter will not be compiled, only constant expression will
                // https://en.wikipedia.org/wiki/Loop-invariant_code_motion
                using (LoopInvariant(loop.Variables))
                {
                    for (int o = 0; o < loop.Fors.Count; o++)
                    {
                        var expr = loop.Fors[o].Expression;
                        compiler.CompileExpr(expr, this);
                    }
                }

                // compile sequences
                foreach (var sequence in loop.Sequences)
                {
                    compiler.CompileExpr(sequence, this);
                }

                // compile seeds
                foreach (var outputInfo in loop.RecursiveFors.Select(f => f.OutputInfo))
                {
                    compiler.CompileExpr(outputInfo, this);
                    compiler.DecCount(outputInfo);
                }

                // declare storage variables for expressions
                var length = loop.Length;
                length.Comment = $"length of loop '{loop.Name}'";
                compiler.CompileExpr(length, this);

                foreach (var f in loop.Fors)
                {
                    var expr = f.Expression;
                    compiler.CompileExpr(expr.Shape, this);
                    compiler.Scope.Declare(f, compiler);

                    compiler.DecCount(length);
                    foreach (var axis in expr.Shape)
                    {
                        compiler.DecCount(axis);
                    }

                    string comment = null;
                    if (compiler.Verbose)
                    {
                        expr.Comment = expr.Comment ?? $"result for loop {f} = {expr}";
                        comment      = compiler.RefComment(f, f.Shape, expr.Comment);
                    }

                    var buff = compiler.GetBuffer(f);
                    if (buff == null)
                    {
                        var innerType = ((ITensor)f).Match(
                            (Tensor <float> tf) => "float",
                            (Tensor <int> ti) => "int",
                            null
                            );
                        var shape = compiler.Scope.GetVar(length);
                        if (expr.NDim > 0)
                        {
                            shape += ", " + string.Join(", ", expr.Shape.Select(axis => compiler.Scope.GetVar(axis)));
                        }
                        compiler.EmitAssign(f, $"new Array<{innerType}>({shape})", comment);
                    }
                    else
                    {
                        compiler.EmitAssign(f, compiler.GetBufferName(buff), comment);
                    }
                }

                // create a scope for the variable private to the loop (recursive for example)
                compiler.EmitStartBlock(null, $"Start of {loop}.");

                // declare recursion variables (outputsInfo != null)
                foreach (var rec in loop.RecursiveFors)
                {
                    var outputInfo = rec.OutputInfo;
                    var variable   = rec.RecursiveVariable;
                    compiler.Scope.Declare(variable, compiler, variable.Name);
                    compiler.EmitAssign(variable, compiler.Scope.GetVar(outputInfo), compiler.RefComment(variable, outputInfo));
                    compiler.DecCount(rec.RecursiveVariable);
                }

                // write the beginning of the `for` loop.
                compiler.DecCount(length);
                compiler.EmitStartBlock($"for (int i = 0; i < {compiler.Scope.GetVar(length)}; i++)", $"= ({compiler.GetCount(length)})");

                // extract items from sequences
                for (int i = 0; i < loop.Sequences.Count; i++)
                {
                    var seq      = loop.Sequences[i];
                    var variable = loop.Variable(seq);
                    compiler.Scope.Declare(variable, compiler);

                    compiler.DecCount(seq);
                    var comment = compiler.RefComment(variable, seq);
                    var axis    = loop.SequenceAxes[i];
                    if (axis == 0)
                    {
                        compiler.EmitAssign(variable, $"{compiler.Scope.GetVar(seq)}[i]", comment);
                    }
                    else
                    {
                        compiler.EmitAssign(variable, $"SliceAlong({compiler.Scope.GetVar(seq)}, {axis}, i)", comment);
                    }
                }

                // compile and store expressions
                for (int o = 0; o < loop.Fors.Count; o++)
                {
                    var expr = loop.Fors[o].Expression;
                    compiler.CompileExpr(expr, this);

                    compiler.DecCount(expr);
                    var comment = compiler.RefComment(loop.Fors[o], expr);
                    compiler.EmitStore(loop, o, expr, comment);
                }

                // copy recursion variable for next iteration
                foreach (var rec in loop.RecursiveFors)
                {
                    var variable = loop.Variable(rec);
                    compiler.DecCount(rec.Expression);
                    var comment = compiler.RefComment(variable, rec.Expression);
                    compiler.EmitAliasing(variable, rec.Expression, comment);
                }

                compiler.EmitEndBlock();
                compiler.CheckShape(@for, this);

#if REF
                // the recursive variable aren'tvisible outside the scope, so they shouldn't be referenced anymore
                foreach (var v in loop.Fors.Select(f => f.RecursiveVariable))
                {
                    if (v != null && compiler.GetCount(v) != 0)
                    {
                        throw new Exception("Recursive variable not decremented");
                    }
                }
#endif
                compiler.EmitEndBlock();
            }
        }
示例#9
0
 internal static void Owner(this IFor <Pet> petFor, Person expectedOwner)
 {
     petFor.Compose(pet => Expect(pet.Owner).To.Deep.Equal(expectedOwner));
 }