Example #1
0
        protected string ToString(IfOperation operation)
        {
            switch (operation)
            {
            case IfOperation.Equals:
                return("==");

            case IfOperation.NotEquals:
                return("!=");

            case IfOperation.GreaterThan:
                return(">");

            case IfOperation.LessThan:
                return("<");

            case IfOperation.GreaterThanEquals:
                return(">=");

            case IfOperation.LessThanEquals:
                return("<=");

            default:
                return(string.Empty);
            }
        }
Example #2
0
        private static bool TryReadIfOperation(string op, out IfOperation operation)
        {
            switch (op.Trim())
            {
            case "==":
                operation = IfOperation.Equals;
                return(true);

            case "!=":
                operation = IfOperation.NotEquals;
                return(true);

            case "<":
                operation = IfOperation.LessThan;
                return(true);

            case ">":
                operation = IfOperation.GreaterThan;
                return(true);

            case "<=":
                operation = IfOperation.LessThanEquals;
                return(true);

            case ">=":
                operation = IfOperation.GreaterThanEquals;
                return(true);

            default:
                operation = default(IfOperation);
                return(false);
            }
        }
Example #3
0
 public IfCommand(string leftExpression, IfOperation operation, string rightExpression, params ParseException[] exceptions)
     : base(exceptions)
 {
     this.leftExpression  = leftExpression;
     this.operation       = operation;
     this.rightExpression = rightExpression;
 }
Example #4
0
 public IfCommand(string leftExpression, IfOperation operation, string rightExpression)
     : base()
 {
     this.leftExpression  = leftExpression;
     this.operation       = operation;
     this.rightExpression = rightExpression;
 }
Example #5
0
 public override IBuildIntention <IIfOperation> GetBuildIntention(IConversionContext context)
 {
     var(toBuild, maker) = IfOperation.Create();
     return(new BuildIntention <IIfOperation>(toBuild, () =>
     {
         maker.Build(Left.GetOrThrow().ConvertElementOrThrow(context), Right.GetOrThrow().ConvertElementOrThrow(context));
     }));
 }
Example #6
0
        protected VisitResult VisitIf(IfOperation operation)
        {
            var condition = Visit(operation.Condition);

            if (condition.ResultSchema.Kind != SchemaNodeKind.Boolean)
            {
                throw new InvalidOperationException("The condition in an if operation must be a boolean");
            }
            var trueOperation  = Visit(operation.TrueOperation);
            var falseOperation = Visit(operation.FalseOperation);

            if (trueOperation.ResultSchema.Kind != falseOperation.ResultSchema.Kind)
            {
                throw new InvalidOperationException("The result cases in a condition must be of the same type");
            }
            return(new VisitResult
            {
                ResultSchema = trueOperation.ResultSchema,
                Expression = Expression.Condition(
                    condition.Expression,
                    trueOperation.Expression,
                    falseOperation.Expression),
            });
        }
Example #7
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")
                    );
        }
Example #8
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>())
                    );
        }