Exemplo n.º 1
0
        public Factorial()
        {
            var ifBlockScope = Scope.CreateAndBuild(new List <Scope.IsStatic> {
            });
            var elseBlock    = Scope.CreateAndBuild(new List <Scope.IsStatic> {
            });

            var inputKey = new NameKey("input");
            var input    = MemberDefinition.CreateAndBuild(inputKey, new NumberType(), false);

            var facKey = new NameKey("fac");
            var fac    = MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(new NumberType(), new NumberType()), false);


            var methodScope = Scope.CreateAndBuild(new List <Scope.IsStatic> {
                new Scope.IsStatic(input, false)
            });


            Module =
                ModuleDefinition.CreateAndBuild(
                    Scope.CreateAndBuild(
                        new List <Scope.IsStatic> {
                new Scope.IsStatic(MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(new NumberType(), new NumberType()), false), false)
            }),
                    new ICodeElement[] {
                AssignOperation.CreateAndBuild(
                    MethodDefinition.CreateAndBuild(
                        new NumberType(),
                        new NumberType(),
                        input,
                        methodScope,
                        new ICodeElement[] {
                    ElseOperation.CreateAndBuild(
                        IfOperation.CreateAndBuild(
                            LessThanOperation.CreateAndBuild(
                                MemberReference.CreateAndBuild(input),
                                ConstantNumber.CreateAndBuild(2)),
                            BlockDefinition.CreateAndBuild(
                                ifBlockScope,
                                new ICodeElement[] {
                        ReturnOperation.CreateAndBuild(
                            ConstantNumber.CreateAndBuild(1))
                    },
                                new ICodeElement[0])),
                        BlockDefinition.CreateAndBuild(
                            elseBlock,
                            new ICodeElement[] {
                        ReturnOperation.CreateAndBuild(
                            MultiplyOperation.CreateAndBuild(
                                NextCallOperation.CreateAndBuild(
                                    SubtractOperation.CreateAndBuild(
                                        MemberReference.CreateAndBuild(input),
                                        ConstantNumber.CreateAndBuild(1)),
                                    MemberReference.CreateAndBuild(fac)),
                                MemberReference.CreateAndBuild(input)))
                    },
                            new ICodeElement[0]))
                },
                        new ICodeElement[0],
                        false),
                    MemberReference.CreateAndBuild(fac)
                    )
            },
                    new NameKey("factorial")
                    );
        }
Exemplo n.º 2
0
        public Factorial()
        {
            var ifBlockScope = Scope.CreateAndBuild(new List <IsStatic> {
            });
            var elseBlock    = Scope.CreateAndBuild(new List <IsStatic> {
            });

            var inputKey = new NameKey("input");
            var input    = MemberDefinition.CreateAndBuild(inputKey, new NumberType(), Access.ReadWrite);

            var facKey = new NameKey("fac");
            var fac    = MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(new NumberType(), new NumberType()), Access.ReadWrite);

            var methodScope = Scope.CreateAndBuild(new List <IsStatic> {
                new IsStatic(input, false)
            });

            RootScope =
                Model.Instantiated.RootScope.CreateAndBuild(
                    Scope.CreateAndBuild(
                        new List <IsStatic> {
                new IsStatic(MemberDefinition.CreateAndBuild(facKey, MethodType.CreateAndBuild(
                                                                 new NumberType(),
                                                                 new NumberType()), Access.ReadWrite), false)
            }),
                    new [] {
                AssignOperation.CreateAndBuild(
                    MethodDefinition.CreateAndBuild(
                        new NumberType(),
                        input,
                        methodScope,
                        new ICodeElement[] {
                    ElseOperation.CreateAndBuild(
                        IfOperation.CreateAndBuild(
                            LessThanOperation.CreateAndBuild(
                                MemberReference.CreateAndBuild(input),
                                ConstantNumber.CreateAndBuild(2)),
                            BlockDefinition.CreateAndBuild(
                                ifBlockScope,
                                new ICodeElement[] {
                        ReturnOperation.CreateAndBuild(
                            ConstantNumber.CreateAndBuild(1))
                    },
                                Array.Empty <ICodeElement>())),
                        BlockDefinition.CreateAndBuild(
                            elseBlock,
                            new ICodeElement[] {
                        ReturnOperation.CreateAndBuild(
                            MultiplyOperation.CreateAndBuild(
                                NextCallOperation.CreateAndBuild(
                                    SubtractOperation.CreateAndBuild(
                                        MemberReference.CreateAndBuild(input),
                                        ConstantNumber.CreateAndBuild(1)),
                                    MemberReference.CreateAndBuild(fac)),
                                MemberReference.CreateAndBuild(input)))
                    },
                            Array.Empty <ICodeElement>()))
                },
                        Array.Empty <ICodeElement>()),
                    MemberReference.CreateAndBuild(fac)
                    )
            },
                    EntryPointDefinition.CreateAndBuild(new EmptyType(), MemberDefinition.CreateAndBuild(new NameKey("input"), new NumberType(), Access.ReadWrite), Scope.CreateAndBuild(Array.Empty <IsStatic>()), Array.Empty <ICodeElement>(), Array.Empty <ICodeElement>())
                    );
        }