Пример #1
0
        public static string ExecuteMond(string source)
        {
            outputBuffer.Clear();
            try
            {
                MondValue result = MondState.Run(source, "Halbmondbox");

                if (result != MondValue.Undefined)
                {
                    Console.WriteLine("Result: ");
                    Console.WriteLine(result.Serialize());
                    output.WriteLine();

                    if (result["moveNext"])
                    {
                        output.WriteLine("sequence (15 max):");
                        foreach (var i in result.Enumerate(MondState).Take(15))
                        {
                            output.WriteLine(i.Serialize());
                        }
                    }
                    else
                    {
                        output.WriteLine(result.Serialize());
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                output.WriteLine(e.Message);
            }
            return(outputBuffer.ToString());
        }
Пример #2
0
            public static double SumOrDouble(MondState state, MondValue range)
            {
                if (range.Type == MondValueType.Number)
                {
                    return(range * 2d);
                }

                return(range.Enumerate(state).Sum(n => (double)n));
            }
Пример #3
0
        public void Slice()
        {
            var state = new MondState();

            var arr = new MondValue(MondValueType.Array);
            var str = new MondValue("HelloWorld");

            arr.ArrayValue.AddRange(new MondValue[] { 1, 2, 3, 4, 5 });

            Assert.True(str.Slice(1, 3, 1).Equals(new MondValue("ell")));

            Assert.True(arr.Slice().Enumerate(state).SequenceEqual(arr.Enumerate(state)), "clone");

            Assert.True(arr.Slice(step: -1).Enumerate(state).SequenceEqual(new MondValue[] { 5, 4, 3, 2, 1 }), "reverse");

            Assert.True(arr.Slice(1, 3).Enumerate(state).SequenceEqual(new MondValue[] { 2, 3, 4 }), "range");
            Assert.True(arr.Slice(3, 1).Enumerate(state).SequenceEqual(new MondValue[] { 4, 3, 2 }), "reverse range");

            Assert.True(arr.Slice(0, 0).Enumerate(state).SequenceEqual(new MondValue[] { 1 }), "same start and end");

            Assert.True(arr.Slice(-4, -2).Enumerate(state).SequenceEqual(new MondValue[] { 2, 3, 4 }), "negative range");
            Assert.True(arr.Slice(-2, -4).Enumerate(state).SequenceEqual(new MondValue[] { 4, 3, 2 }), "negative range reverse");

            Assert.True(arr.Slice(step: 2).Enumerate(state).SequenceEqual(new MondValue[] { 1, 3, 5 }), "skip");
            Assert.True(arr.Slice(step: -2).Enumerate(state).SequenceEqual(new MondValue[] { 5, 3, 1 }), "skip negative");

            Assert.Throws <MondRuntimeException>(() => arr.Slice(-6, 0, "out of bounds 1"));
            Assert.Throws <MondRuntimeException>(() => arr.Slice(0, 5, "out of bounds 2"));

            Assert.Throws <MondRuntimeException>(() => arr.Slice(step: 0), "invalid step");

            Assert.Throws <MondRuntimeException>(() => arr.Slice(4, 0, 1), "invalid range");
            Assert.Throws <MondRuntimeException>(() => arr.Slice(0, 4, -1), "invalid range negative");

            Assert.Throws <MondRuntimeException>(() => MondValue.Undefined.Slice(), "slice non-array");

            var empty = new MondValue(MondValueType.Array);

            Assert.True(!empty.Slice().Enumerate(state).Any(), "clone empty");
        }
Пример #4
0
            public static double SumOrDouble(MondState state, MondValue range)
            {
                if (range.Type == MondValueType.Number)
                    return range * 2d;

                return range.Enumerate(state).Sum(n => (double)n);
            }