Пример #1
0
        public static void Init(IokeObject obj)
        {
            Runtime runtime = obj.runtime;

            obj.Kind = "DefaultBehavior Definitions";

            obj.RegisterMethod(runtime.NewNativeMethod("expects any number of unevaluated arguments. if no arguments at all are given, will just return nil. creates a new method based on the arguments. this method will be evaluated using the context of the object it's called on, and thus the definition can not refer to the outside scope where the method is defined. (there are other ways of achieving this). all arguments except the last one is expected to be names of arguments that will be used in the method. there will possible be additions to the format of arguments later on - including named parameters and optional arguments. the actual code is the last argument given.",
                                                       new NativeMethod("method", DefaultArgumentsDefinition.builder()
                                                                        .WithOptionalPositionalUnevaluated("documentation")
                                                                        .WithRestUnevaluated("argumentsAndBody")
                                                                        .Arguments,
                                                                        (method, context, message, on, outer) => {
                outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);

                var args = message.Arguments;

                if (args.Count == 0)
                {
                    Message mx     = new Message(context.runtime, "nil", null, false);
                    mx.File        = Message.GetFile(message);
                    mx.Line        = Message.GetLine(message);
                    mx.Position    = Message.GetPosition(message);
                    IokeObject mmx = context.runtime.CreateMessage(mx);
                    return(runtime.NewMethod(null, runtime.DefaultMethod, new DefaultMethod(context, DefaultArgumentsDefinition.Empty(), mmx)));
                }

                string doc = null;

                int start = 0;
                if (args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText"))
                {
                    start++;
                    string s = (string)((IokeObject)args[0]).Arguments[0];
                    doc      = s;
                }

                DefaultArgumentsDefinition def = DefaultArgumentsDefinition.CreateFrom(args, start, args.Count - 1, message, on, context);

                return(runtime.NewMethod(doc, runtime.DefaultMethod, new DefaultMethod(context, def, (IokeObject)args[args.Count - 1])));
            })));



            obj.RegisterMethod(runtime.NewNativeMethod("expects one code argument, optionally preceeded by a documentation string. will create a new DefaultMacro based on the code and return it.",
                                                       new NativeMethod("macro", DefaultArgumentsDefinition.builder()
                                                                        .WithOptionalPositionalUnevaluated("documentation")
                                                                        .WithOptionalPositionalUnevaluated("body")
                                                                        .Arguments,
                                                                        (method, context, message, on, outer) => {
                outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
                var args = message.Arguments;

                if (args.Count == 0)
                {
                    Message mx     = new Message(context.runtime, "nil", null, false);
                    mx.File        = Message.GetFile(message);
                    mx.Line        = Message.GetLine(message);
                    mx.Position    = Message.GetPosition(message);
                    IokeObject mmx = context.runtime.CreateMessage(mx);

                    return(runtime.NewMacro(null, runtime.DefaultMacro, new DefaultMacro(context, mmx)));
                }

                string doc = null;

                int start = 0;
                if (args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText"))
                {
                    start++;
                    string s = (string)(((IokeObject)args[0]).Arguments[0]);
                    doc      = s;
                }

                return(runtime.NewMacro(doc, runtime.DefaultMacro, new DefaultMacro(context, (IokeObject)args[start])));
            })));



            obj.RegisterMethod(runtime.NewNativeMethod("creates a new lexical block that can be executed at will, while retaining a reference to the lexical closure it was created in. it will always update variables if they exist. there is currently no way of introducing shadowing variables in the local context. new variables can be created though, just like in a method. a lexical block mimics LexicalBlock, and can take arguments. at the moment these are restricted to required arguments, but support for the same argument types as DefaultMethod will come.",
                                                       new NativeMethod("fn", DefaultArgumentsDefinition.builder()
                                                                        .WithOptionalPositionalUnevaluated("documentation")
                                                                        .WithRestUnevaluated("argumentsAndBody")
                                                                        .Arguments,
                                                                        (method, context, message, on, outer) => {
                outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
                var args = message.Arguments;

                if (args.Count == 0)
                {
                    return(runtime.NewLexicalBlock(null, runtime.LexicalBlock, new LexicalBlock(context, DefaultArgumentsDefinition.Empty(), method.runtime.nilMessage)));
                }

                string doc = null;

                int start = 0;
                if (args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText"))
                {
                    start++;
                    string s = ((string)((IokeObject)args[0]).Arguments[0]);
                    doc      = s;
                }

                IokeObject code = IokeObject.As(args[args.Count - 1], context);

                DefaultArgumentsDefinition def = DefaultArgumentsDefinition.CreateFrom(args, start, args.Count - 1, message, on, context);
                return(runtime.NewLexicalBlock(doc, runtime.LexicalBlock, new LexicalBlock(context, def, code)));
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("expects one code argument, optionally preceeded by a documentation string. will create a new LexicalMacro based on the code and return it.",
                                                       new NativeMethod("lecro", DefaultArgumentsDefinition.builder()
                                                                        .WithOptionalPositionalUnevaluated("documentation")
                                                                        .WithOptionalPositionalUnevaluated("body")
                                                                        .Arguments,
                                                                        (method, context, message, on, outer) => {
                outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
                var args = message.Arguments;

                if (args.Count == 0)
                {
                    Message mx     = new Message(context.runtime, "nil", null, false);
                    mx.File        = Message.GetFile(message);
                    mx.Line        = Message.GetLine(message);
                    mx.Position    = Message.GetPosition(message);
                    IokeObject mmx = context.runtime.CreateMessage(mx);

                    return(runtime.NewMacro(null, runtime.LexicalMacro, new LexicalMacro(context, mmx)));
                }

                string doc = null;

                int start = 0;
                if (args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText"))
                {
                    start++;
                    string s = ((string)((IokeObject)args[0]).Arguments[0]);
                    doc      = s;
                }

                return(runtime.NewMacro(doc, runtime.LexicalMacro, new LexicalMacro(context, (IokeObject)args[start])));
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("expects one code argument, optionally preceeded by a documentation string. will create a new DefaultSyntax based on the code and return it.",
                                                       new NativeMethod("syntax", DefaultArgumentsDefinition.builder()
                                                                        .WithOptionalPositionalUnevaluated("documentation")
                                                                        .WithOptionalPositionalUnevaluated("body")
                                                                        .Arguments,
                                                                        (method, context, message, on, outer) => {
                outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
                var args = message.Arguments;

                if (args.Count == 0)
                {
                    Message mx     = new Message(context.runtime, "nil", null, false);
                    mx.File        = Message.GetFile(message);
                    mx.Line        = Message.GetLine(message);
                    mx.Position    = Message.GetPosition(message);
                    IokeObject mmx = context.runtime.CreateMessage(mx);

                    return(runtime.NewMacro(null, runtime.DefaultSyntax, new DefaultSyntax(context, mmx)));
                }

                string doc = null;

                int start = 0;
                if (args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText"))
                {
                    start++;
                    string s = (string)((IokeObject)args[0]).Arguments[0];
                    doc      = s;
                }

                return(runtime.NewMacro(doc, runtime.DefaultSyntax, new DefaultSyntax(context, (IokeObject)args[start])));
            })));
            obj.RegisterMethod(runtime.NewNativeMethod("Takes two evaluated text or symbol arguments that name the method to alias, and the new name to give it. returns the receiver.",
                                                       new NativeMethod("aliasMethod", DefaultArgumentsDefinition.builder()
                                                                        .WithRequiredPositional("oldName")
                                                                        .WithRequiredPositional("newName")
                                                                        .Arguments,
                                                                        (method, context, message, on, outer) => {
                var args = new SaneArrayList();
                outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary <string, object>());
                string fromName = Text.GetText(Interpreter.Send(runtime.asText, context, args[0]));
                string toName   = Text.GetText(Interpreter.Send(runtime.asText, context, args[1]));
                IokeObject.As(on, context).AliasMethod(fromName, toName, message, context);
                return(on);
            })));
        }
Пример #2
0
        public override void Init(IokeObject obj)
        {
            obj.Kind = "Arity";

            obj.SetCell("taking:nothing", GetArity(obj, Taking.Nothing));
            obj.SetCell("taking:everything", GetArity(obj, Taking.Everything));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("Create an Arity object from the given messages. The list of unevaluated messages given to this method will be used as if they were the arguments part of a DefaultMethod definition.",
                                                           new TypeCheckingNativeMethod("from", TypeCheckingArgumentsDefinition.builder()
                                                                                        .ReceiverMustMimic(obj)
                                                                                        .WithRestUnevaluated("arguments")
                                                                                        .Arguments,
                                                                                        (self, on, args, keywords, context, message) => {
                if (message.Arguments.Count == 0)
                {
                    return(TakingNothing(self));
                }
                DefaultArgumentsDefinition def = DefaultArgumentsDefinition.CreateFrom(message.Arguments, 0, message.Arguments.Count, message, on, context);
                return(GetArity(self, def));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the names for positional arguments",
                                                           new TypeCheckingNativeMethod("positionals", TypeCheckingArgumentsDefinition.builder()
                                                                                        .WithOptionalPositional("includeOptionals", "true")
                                                                                        .Arguments,
                                                                                        (method, on, args, keywords, context, message) => {
                Arity a              = (Arity)IokeObject.dataOf(on);
                var names            = new SaneArrayList();
                bool includeOptional = args.Count == 0 ? true : IokeObject.IsObjectTrue(args[0]);
                if (a.argumentsDefinition != null)
                {
                    foreach (DefaultArgumentsDefinition.Argument argument in a.argumentsDefinition.Arguments)
                    {
                        if (argument is DefaultArgumentsDefinition.KeywordArgument)
                        {
                            continue;
                        }
                        if (argument is DefaultArgumentsDefinition.OptionalArgument)
                        {
                            if (includeOptional)
                            {
                                names.Add(method.runtime.GetSymbol(argument.Name));
                            }
                        }
                        else
                        {
                            names.Add(method.runtime.GetSymbol(argument.Name));
                        }
                    }
                }
                return(method.runtime.NewList(names));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the names for keyword arguments",
                                                           new TypeCheckingNativeMethod.WithNoArguments("keywords", obj,
                                                                                                        (method, on, args, keywords, context, message) => {
                Arity a   = (Arity)IokeObject.dataOf(on);
                var names = new SaneArrayList();
                if (a.argumentsDefinition != null)
                {
                    foreach (string name in a.argumentsDefinition.Keywords)
                    {
                        names.Add(method.runtime.GetSymbol(name.Substring(0, name.Length - 1)));
                    }
                }
                return(method.runtime.NewList(names));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the symbol name for the krest argument.",
                                                           new TypeCheckingNativeMethod.WithNoArguments("krest", obj,
                                                                                                        (method, on, args, keywords, context, message) => {
                Arity a = (Arity)IokeObject.dataOf(on);
                if (a.argumentsDefinition != null)
                {
                    string name = a.argumentsDefinition.KrestName;
                    if (name == null)
                    {
                        return(method.runtime.nil);
                    }
                    else
                    {
                        return(method.runtime.GetSymbol(name));
                    }
                }
                else
                {
                    return(method.runtime.nil);
                }
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the symbol name for the rest argument.",
                                                           new TypeCheckingNativeMethod.WithNoArguments("rest", obj,
                                                                                                        (method, on, args, keywords, context, message) => {
                Arity a = (Arity)IokeObject.dataOf(on);
                if (a.argumentsDefinition != null)
                {
                    string name = a.argumentsDefinition.RestName;
                    if (name == null)
                    {
                        return(method.runtime.nil);
                    }
                    else
                    {
                        return(method.runtime.GetSymbol(name));
                    }
                }
                else
                {
                    return(method.runtime.nil);
                }
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the text representation of this arity",
                                                           new TypeCheckingNativeMethod.WithNoArguments("asText", obj,
                                                                                                        (method, on, args, keywords, context, message) => {
                Arity a = (Arity)IokeObject.dataOf(on);
                if (a.taking == Taking.Everything)
                {
                    return(method.runtime.NewText("..."));
                }
                else if (a.taking == Taking.Nothing)
                {
                    return(method.runtime.NewText(""));
                }
                return(method.runtime.NewText(a.argumentsDefinition.GetCode(false)));
            })));
        }
Пример #3
0
        public override void Init(IokeObject obj)
        {
            obj.Kind = "LexicalBlock";

            obj.RegisterMethod(obj.runtime.NewNativeMethod("takes two evaluated arguments, where this first one is a list of messages which will be used as the arguments and the code, and the second is the context where this lexical scope should be created in",
                                                           new NativeMethod("createFrom", DefaultArgumentsDefinition.builder()
                                                                            .WithRequiredPositional("messageList")
                                                                            .WithRequiredPositional("lexicalContext")
                                                                            .Arguments,
                                                                            (method, _context, _message, on, outer) => {
                Runtime runtime    = _context.runtime;
                var positionalArgs = new SaneArrayList();
                outer.ArgumentsDefinition.GetEvaluatedArguments(_context, _message, on, positionalArgs, new SaneDictionary <string, object>());
                var args          = IokeList.GetList(positionalArgs[0]);
                IokeObject ground = IokeObject.As(positionalArgs[1], _context);

                IokeObject code = IokeObject.As(args[args.Count - 1], _context);

                DefaultArgumentsDefinition def = DefaultArgumentsDefinition.CreateFrom(args, 0, args.Count - 1, _message, on, _context);
                return(runtime.NewLexicalBlock(null, runtime.LexicalBlock, new LexicalBlock(ground, def, code)));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("invokes the block with the arguments provided, returning the result of the last expression in the block",
                                                           new NativeMethod("call", DefaultArgumentsDefinition.builder()
                                                                            .WithRestUnevaluated("arguments")
                                                                            .Arguments,
                                                                            (method, _context, _message, on, outer) => {
                return(Interpreter.Activate(IokeObject.As(on, _context), _context, _message, on));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the full code of this lexical block, as a Text",
                                                           new TypeCheckingNativeMethod.WithNoArguments("code", obj,
                                                                                                        (method, on, args, keywords, _context, _message) => {
                IokeObject objx = IokeObject.As(on, _context);
                string x        = objx.IsActivatable ? "x" : "";

                string argstr = ((LexicalBlock)IokeObject.dataOf(on)).arguments.GetCode();
                return(_context.runtime.NewText("fn" + x + "(" + argstr + Message.Code(((LexicalBlock)IokeObject.dataOf(on)).message) + ")"));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the code for the argument definition",
                                                           new TypeCheckingNativeMethod.WithNoArguments("argumentsCode", obj,
                                                                                                        (method, on, args, keywords, _context, _message) => {
                return(_context.runtime.NewText(((AssociatedCode)IokeObject.dataOf(on)).ArgumentsCode));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a list of the keywords this block takes",
                                                           new TypeCheckingNativeMethod.WithNoArguments("keywords", obj,
                                                                                                        (method, on, args, keywords, _context, _message) => {
                var keywordList = new SaneArrayList();

                foreach (string keyword in ((LexicalBlock)IokeObject.dataOf(on)).arguments.Keywords)
                {
                    keywordList.Add(_context.runtime.GetSymbol(keyword.Substring(0, keyword.Length - 1)));
                }

                return(_context.runtime.NewList(keywordList));
            })));

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a list of the argument names the positional arguments this block takes",
                                                           new TypeCheckingNativeMethod.WithNoArguments("argumentNames", obj,
                                                                                                        (method, on, args, keywords, _context, _message) => {
                var names = new SaneArrayList();

                foreach (var arg in ((LexicalBlock)IokeObject.dataOf(on)).arguments.Arguments)
                {
                    if (!(arg is DefaultArgumentsDefinition.KeywordArgument))
                    {
                        names.Add(_context.runtime.GetSymbol(arg.Name));
                    }
                }

                return(_context.runtime.NewList(names));
            })));
            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the message chain for this block",
                                                           new TypeCheckingNativeMethod.WithNoArguments("message", obj,
                                                                                                        (method, on, args, keywords, _context, _message) => {
                return(((AssociatedCode)IokeObject.dataOf(on)).Code);
            })));

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

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

            obj.RegisterMethod(obj.runtime.NewNativeMethod("returns idiomatically formatted code for this lexical block",
                                                           new TypeCheckingNativeMethod.WithNoArguments("formattedCode", obj,
                                                                                                        (method, on, args, keywords, _context, message) => {
                return(_context.runtime.NewText(((AssociatedCode)IokeObject.dataOf(on)).FormattedCode(method)));
            })));
        }