Example #1
0
File: Pair.cs Project: tspring/ioke
        public override void Init(IokeObject obj)
        {
            Runtime runtime = obj.runtime;

            obj.Kind = "Pair";
            obj.Mimics(IokeObject.As(IokeObject.FindCell(runtime.Mixins, "Enumerable"), null), runtime.nul, runtime.nul);
            obj.Mimics(IokeObject.As(IokeObject.FindCell(runtime.Mixins, "Comparing"), null), runtime.nul, runtime.nul);

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a hash for the pair",
                                                           new NativeMethod.WithNoArguments("hash", (method, context, message, on, outer) => {
                outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
                var one = ((Pair)IokeObject.dataOf(on)).first.GetHashCode();
                var two = ((Pair)IokeObject.dataOf(on)).second.GetHashCode();
                return(context.runtime.NewNumber(one + 13 * two));
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side pair is equal to the right hand side pair.",
                                                       new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
                                                                                    .ReceiverMustMimic(runtime.Pair)
                                                                                    .WithRequiredPositional("other")
                                                                                    .Arguments,
                                                                                    (method, on, args, keywords, context, message) => {
                Pair d       = (Pair)IokeObject.dataOf(on);
                object other = args[0];

                return(((other is IokeObject) &&
                        (IokeObject.dataOf(other) is Pair) &&
                        d.first.Equals(((Pair)IokeObject.dataOf(other)).first) &&
                        d.second.Equals(((Pair)IokeObject.dataOf(other)).second)) ? context.runtime.True : context.runtime.False);
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("Returns the first value",
                                                       new TypeCheckingNativeMethod.WithNoArguments("first", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                return(((Pair)IokeObject.dataOf(on)).first);
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("Returns the first value",
                                                       new TypeCheckingNativeMethod.WithNoArguments("key", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                return(((Pair)IokeObject.dataOf(on)).first);
            })));


            obj.RegisterMethod(runtime.NewNativeMethod("Returns the second value",
                                                       new TypeCheckingNativeMethod.WithNoArguments("second", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                return(((Pair)IokeObject.dataOf(on)).second);
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("Returns the second value",
                                                       new TypeCheckingNativeMethod.WithNoArguments("value", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                return(((Pair)IokeObject.dataOf(on)).second);
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("Returns a text inspection of the object",
                                                       new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                return(method.runtime.NewText(Pair.GetInspect(on)));
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("Returns a brief text inspection of the object",
                                                       new TypeCheckingNativeMethod.WithNoArguments("notice", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                return(method.runtime.NewText(Pair.GetNotice(on)));
            })));
        }