Ejemplo n.º 1
0
        public override FunctionResult Invoke(InvocContext context, Arguments args)
        {
            if (args.Length <= 0)
                return FunctionResult.Fail("Function requires at least one argument");

            if (args.AllCouldBeCastAsInt())
            {
                var result = Enumerable.Range(0, args.Length).Sum(x => args.GetAsInt(x));

                return FunctionResult.Success(result, Core.Token.Int);
            }

            if (args.AllCouldBeCastAsDouble())
            {
                var result = Enumerable.Range(0, args.Length).Sum(x => args.GetAsDouble(x));

                return FunctionResult.Success(result, Core.Token.Float);
            }

            return FunctionResult.Fail("Cannot call method with arguments");
        }
Ejemplo n.º 2
0
        public override FunctionResult Invoke(InvocContext context, Arguments args)
        {
            if (args.Length != 1)
            {
                return FunctionResult.Fail("Unrecognised number of params");
            }

            if (args.CouldBeCastAsInt(0))
            {
                var result = _rand.Next(args.GetAsInt(0));

                return FunctionResult.Success(result, Core.Token.Int);
            }

            if (args.CouldBeCastAsDouble(0))
            {
                var result = _rand.NextDouble() * args.GetAsDouble(0);

                return FunctionResult.Success(result, Core.Token.Float);
            }

            return FunctionResult.Fail("Type cannot be used as argument");
        }