예제 #1
0
        public virtual IokeObject ConvertToDecimal(IokeObject self, IokeObject m, IokeObject context, bool signalCondition)
        {
            if (signalCondition)
            {
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                             m,
                                                                             context,
                                                                             "Error",
                                                                             "Type",
                                                                             "IncorrectType"), context).Mimic(m, context);
                condition.SetCell("message", m);
                condition.SetCell("context", context);
                condition.SetCell("receiver", self);
                condition.SetCell("expectedType", context.runtime.GetSymbol("Decimal"));

                object[] newCell = new object[] { self };

                context.runtime.WithRestartReturningArguments(() => { context.runtime.ErrorCondition(condition); },
                                                              context,
                                                              new IokeObject.UseValue("decimal", newCell));

                return(IokeObject.ConvertToDecimal(newCell[0], m, context, signalCondition));
            }
            return(null);
        }
예제 #2
0
파일: Text.cs 프로젝트: tspring/ioke
        public static object ToDecimal(object on, IokeObject context, IokeObject message)
        {
            string tvalue = GetText(on);

            try {
                return(context.runtime.NewDecimal(tvalue));
            } catch (Exception) {
                Runtime    runtime   = context.runtime;
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                             message,
                                                                             context,
                                                                             "Error",
                                                                             "Arithmetic",
                                                                             "NotParseable"), context).Mimic(message, context);
                condition.SetCell("message", message);
                condition.SetCell("context", context);
                condition.SetCell("receiver", on);
                condition.SetCell("text", on);

                object[] newCell = new object[] { null };

                runtime.WithRestartReturningArguments(() => { runtime.ErrorCondition(condition); },
                                                      context,
                                                      new IokeObject.UseValue("decimal", newCell),
                                                      new TakeLongestDecimal(tvalue, newCell));
                return(newCell[0]);
            }
        }
예제 #3
0
        public object ConvertTo(IokeObject self, object mimic, bool signalCondition, string conversionMethod, IokeObject message, IokeObject context)
        {
            if (IokeObject.IsMimic(self, IokeObject.As(mimic, context), context))
            {
                return(self);
            }
            if (signalCondition)
            {
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                             message,
                                                                             context,
                                                                             "Error",
                                                                             "Type",
                                                                             "IncorrectType"), context).Mimic(message, context);
                condition.SetCell("message", message);
                condition.SetCell("context", context);
                condition.SetCell("receiver", self);
                condition.SetCell("expectedType", mimic);

                object[] newCell = new object[] { self };

                context.runtime.WithRestartReturningArguments(() => { context.runtime.ErrorCondition(condition); },
                                                              context,
                                                              new IokeObject.UseValue("object", newCell));

                return(IokeObject.ConvertTo(mimic, newCell[0], signalCondition, conversionMethod, message, context));
            }
            return(null);
        }
예제 #4
0
파일: Locals.cs 프로젝트: tspring/ioke
 public static string GetContextMessageName(IokeObject ctx) {
     if("Locals".Equals(ctx.GetKind())) {
         return ":in `" + IokeObject.As(ctx.body.Get("currentMessage"), ctx).Name + "'";
     } else {
         return "";
     }
 }
예제 #5
0
        public static object AssignCell(IokeObject context, IokeObject message, object on, object first, object val)
        {
            string name = Text.GetText(Interpreter.Send(context.runtime.asText, context, first));

            if (val is IokeObject)
            {
                if ((IokeObject.dataOf(val) is Named) && ((Named)IokeObject.dataOf(val)).Name == null)
                {
                    ((Named)IokeObject.dataOf(val)).Name = name;
                }
                else if (name.Length > 0 && char.IsUpper(name[0]) && !IokeObject.As(val, context).HasKind)
                {
                    if (on == context.runtime.Ground)
                    {
                        IokeObject.As(val, context).Kind = name;
                    }
                    else
                    {
                        IokeObject.As(val, context).Kind = IokeObject.As(on, context).GetKind(message, context) + " " + name;
                    }
                }
            }

            return(IokeObject.SetCell(on, message, context, name, val));
        }
예제 #6
0
        public static object ActivateWithDataFixed(IokeObject self, IokeObject dynamicContext, IokeObject message, object on, IDictionary <string, object> data)
        {
            LexicalMacro lm = (LexicalMacro)self.data;

            if (lm.code == null)
            {
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(dynamicContext.runtime.Condition,
                                                                             message,
                                                                             dynamicContext,
                                                                             "Error",
                                                                             "Invocation",
                                                                             "NotActivatable"), dynamicContext).Mimic(message, dynamicContext);
                condition.SetCell("message", message);
                condition.SetCell("context", dynamicContext);
                condition.SetCell("receiver", on);
                condition.SetCell("method", self);
                condition.SetCell("report", dynamicContext.runtime.NewText("You tried to activate a method without any code - did you by any chance activate the LexicalMacro kind by referring to it without wrapping it inside a call to cell?"));
                dynamicContext.runtime.ErrorCondition(condition);
                return(null);
            }

            IokeObject c = self.runtime.NewLexicalContext(on, "Lexical macro activation context", lm.context);

            c.SetCell("outerScope", lm.context);
            c.SetCell("call", dynamicContext.runtime.NewCallFrom(c, message, dynamicContext, IokeObject.As(on, dynamicContext)));
            foreach (var d in data)
            {
                string s = d.Key;
                c.SetCell(s.Substring(0, s.Length - 1), d.Value);
            }

            return(self.runtime.interpreter.Evaluate(lm.code, c, on, c));
        }
예제 #7
0
 private static bool IsApplicable(object pass, IokeObject message, IokeObject ctx)
 {
     if (pass != null && pass != ctx.runtime.nul && IokeObject.FindCell(IokeObject.As(pass, ctx), "applicable?") != ctx.runtime.nul)
     {
         return(IokeObject.IsObjectTrue(Send(ctx.runtime.isApplicableMessage, ctx, pass, ctx.runtime.CreateMessage(Message.Wrap(message)))));
     }
     return(true);
 }
예제 #8
0
        public static object ActivateWithDataFixed(IokeObject self, IokeObject context, IokeObject message, object on, IDictionary <string, object> data)
        {
            DefaultMethod dm = (DefaultMethod)self.data;

            if (dm.code == null)
            {
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                             message,
                                                                             context,
                                                                             "Error",
                                                                             "Invocation",
                                                                             "NotActivatable"), context).Mimic(message, context);
                condition.SetCell("message", message);
                condition.SetCell("context", context);
                condition.SetCell("receiver", on);
                condition.SetCell("method", self);
                condition.SetCell("report", context.runtime.NewText("You tried to activate a method without any code - did you by any chance activate the DefaultMethod kind by referring to it without wrapping it inside a call to cell?"));
                context.runtime.ErrorCondition(condition);
                return(null);
            }


            IokeObject c = context.runtime.Locals.Mimic(message, context);

            c.SetCell("self", on);
            c.SetCell("@", on);
            c.RegisterMethod(c.runtime.NewNativeMethod("will return the currently executing method receiver",
                                                       new NativeMethod.WithNoArguments("@@",
                                                                                        (method, _context, _message, _on, outer) => {
                outer.ArgumentsDefinition.GetEvaluatedArguments(_context, _message, _on, new SaneArrayList(), new SaneDictionary <string, object>());
                return(self);
            })));
            c.SetCell("currentMessage", message);
            c.SetCell("surroundingContext", context);
            foreach (var d in data)
            {
                string s = d.Key;
                c.SetCell(s.Substring(0, s.Length - 1), d.Value);
            }

            c.SetCell("super", CreateSuperCallFor(self, context, message, on, dm.name));

            dm.arguments.AssignArgumentValues(c, context, message, on);

            try {
                return(context.runtime.interpreter.Evaluate(dm.code, c, on, c));
            } catch (ControlFlow.Return e) {
                if (e.context == c)
                {
                    return(e.Value);
                }
                else
                {
                    throw e;
                }
            }
        }
예제 #9
0
        private object ExpandWithCall(IokeObject self, IokeObject context, IokeObject message, object on, object call, IDictionary <string, object> data)
        {
            if (code == null)
            {
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                             message,
                                                                             context,
                                                                             "Error",
                                                                             "Invocation",
                                                                             "NotActivatable"), context).Mimic(message, context);
                condition.SetCell("message", message);
                condition.SetCell("context", context);
                condition.SetCell("receiver", on);
                condition.SetCell("method", self);
                condition.SetCell("report", context.runtime.NewText("You tried to activate a method without any code - did you by any chance activate the DefaultSyntax kind by referring to it without wrapping it inside a call to cell?"));
                context.runtime.ErrorCondition(condition);
                return(null);
            }

            IokeObject c = context.runtime.Locals.Mimic(message, context);

            c.SetCell("self", on);
            c.SetCell("@", on);
            c.RegisterMethod(c.runtime.NewNativeMethod("will return the currently executing syntax receiver", new NativeMethod.WithNoArguments("@@",
                                                                                                                                               (method, _context, _message, _on, outer) => {
                outer.ArgumentsDefinition.GetEvaluatedArguments(_context, _message, _on, new SaneArrayList(), new SaneDictionary <string, object>());
                return(self);
            })));
            c.SetCell("currentMessage", message);
            c.SetCell("surroundingContext", context);
            c.SetCell("call", call);
            if (data != null)
            {
                foreach (var d in data)
                {
                    string s = d.Key;
                    c.SetCell(s.Substring(0, s.Length - 1), d.Value);
                }
            }

            object result = null;

            try {
                result = context.runtime.interpreter.Evaluate(code, c, on, c);
            } catch (ControlFlow.Return e) {
                if (e.context == c)
                {
                    result = e.Value;
                }
                else
                {
                    throw e;
                }
            }

            return(result);
        }
예제 #10
0
        public static object Documentation(IokeObject context, IokeObject message, object on)
        {
            string docs = IokeObject.As(on, context).Documentation;

            if (null == docs)
            {
                return(context.runtime.nil);
            }
            return(context.runtime.NewText(docs));
        }
예제 #11
0
 public string Notice(object self)
 {
     if (IokeObject.As(self, (IokeObject)self).IsActivatable)
     {
         return("fnx(...)");
     }
     else
     {
         return("fn(...)");
     }
 }
예제 #12
0
 public string CodeString(object self)
 {
     if (IokeObject.As(self, null).IsActivatable)
     {
         return("lecro(" + Message.Code(code) + ")");
     }
     else
     {
         return("lecrox(" + Message.Code(code) + ")");
     }
 }
예제 #13
0
 public string FormattedCode(object self)
 {
     if (IokeObject.As(self, (IokeObject)self).IsActivatable)
     {
         return("lecro(\n  " + Message.FormattedCode(code, 2, (IokeObject)self) + ")");
     }
     else
     {
         return("lecrox(\n  " + Message.FormattedCode(code, 2, (IokeObject)self) + ")");
     }
 }
예제 #14
0
파일: Arity.cs 프로젝트: tspring/ioke
        public static IokeObject GetArity(IokeObject self, DefaultArgumentsDefinition def)
        {
            if (def == null || def.IsEmpty)
            {
                return(IokeObject.As(TakingNothing(self), self.runtime.Arity));
            }
            IokeObject obj = self.runtime.Arity.AllocateCopy(null, null);

            obj.MimicsWithoutCheck(self.runtime.Arity);
            obj.Data = new Arity(def);
            return(obj);
        }
예제 #15
0
        public string Inspect(object self)
        {
            string args = arguments.GetCode();

            if (IokeObject.As(self, (IokeObject)self).IsActivatable)
            {
                return("fnx(" + args + Message.Code(message) + ")");
            }
            else
            {
                return("fn(" + args + Message.Code(message) + ")");
            }
        }
예제 #16
0
 public static object SetDocumentation(IokeObject context, IokeObject message, object on, object arg)
 {
     if (arg == context.runtime.nil)
     {
         IokeObject.As(on, context).SetDocumentation(null, message, context);
     }
     else
     {
         string s = Text.GetText(arg);
         IokeObject.As(on, context).SetDocumentation(s, message, context);
     }
     return(arg);
 }
예제 #17
0
        public string FormattedCode(object self)
        {
            string args = arguments == null ? "" : arguments.GetCode();

            if (IokeObject.As(self, (IokeObject)self).IsActivatable)
            {
                return("fnx(" + args + "\n  " + Message.FormattedCode(message, 2, (IokeObject)self) + ")");
            }
            else
            {
                return("fn(" + args + "\n  " + Message.FormattedCode(message, 2, (IokeObject)self) + ")");
            }
        }
예제 #18
0
            public override void CheckMimic(IokeObject obj, IokeObject m, IokeObject context)
            {
                IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                             m,
                                                                             context,
                                                                             "Error",
                                                                             "CantMimicOddball"), context).Mimic(m, context);

                condition.SetCell("message", m);
                condition.SetCell("context", context);
                condition.SetCell("receiver", obj);
                context.runtime.ErrorCondition(condition);
            }
예제 #19
0
파일: Hook.cs 프로젝트: tspring/ioke
        public static void Init(Runtime runtime)
        {
            IokeObject obj = new IokeObject(runtime, "A hook allow you to observe what happens to a specific object. All hooks have Hook in their mimic chain.");

            obj.Kind = "Hook";
            obj.MimicsWithoutCheck(runtime.Origin);
            runtime.IokeGround.RegisterCell("Hook", obj);

            obj.RegisterMethod(runtime.NewNativeMethod("Takes one or more arguments to hook into and returns a new Hook connected to them.",
                                                       new TypeCheckingNativeMethod("into", TypeCheckingArgumentsDefinition.builder()
                                                                                    .ReceiverMustMimic(obj)
                                                                                    .WithRequiredPositional("firstConnected")
                                                                                    .WithRest("restConnected")
                                                                                    .Arguments,
                                                                                    (method, on, args, keywords, context, message) => {
                IokeObject hook = obj.AllocateCopy(context, message);
                hook.MimicsWithoutCheck(obj);

                IList objs = new SaneArrayList();
                foreach (object o in args)
                {
                    objs.Add(IokeObject.As(o, context));
                }
                Hook h    = new Hook(objs);
                hook.Data = h;
                h.Rewire(hook);
                return(hook);
            })));


            obj.RegisterMethod(runtime.NewNativeMethod("returns the objects this hook is connected to",
                                                       new TypeCheckingNativeMethod.WithNoArguments("connectedObjects", obj,
                                                                                                    (method, on, args, keywords, context, message) => {
                Hook h  = (Hook)IokeObject.dataOf(on);
                IList l = new SaneArrayList(h.connected);
                return(method.runtime.NewList(l));
            })));

            obj.RegisterMethod(runtime.NewNativeMethod("Takes one argument and will add that to the list of connected objects",
                                                       new TypeCheckingNativeMethod("hook!", TypeCheckingArgumentsDefinition.builder()
                                                                                    .ReceiverMustMimic(obj)
                                                                                    .WithRequiredPositional("objectToHookInto")
                                                                                    .Arguments,
                                                                                    (method, on, args, keywords, context, message) => {
                Hook h = (Hook)IokeObject.dataOf(on);
                h.connected.Add(IokeObject.As(args[0], context));
                h.Rewire(IokeObject.As(on, context));
                return(on);
            })));
        }
예제 #20
0
        private static void report(object self, IokeObject context, IokeObject message, string name)
        {
            IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                         message,
                                                                         context,
                                                                         "Error",
                                                                         "Invocation",
                                                                         "NotActivatable"), context).Mimic(message, context);

            condition.SetCell("message", message);
            condition.SetCell("context", context);
            condition.SetCell("receiver", self);
            condition.SetCell("methodName", context.runtime.GetSymbol(name));
            context.runtime.ErrorCondition(condition);
        }
예제 #21
0
        private static object NoActivator(IokeObject self, object on, IList args, IDictionary <string, object> keywords, IokeObject context, IokeObject message)
        {
            IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
                                                                         message,
                                                                         context,
                                                                         "Error",
                                                                         "Invocation",
                                                                         "NotActivatable"), context).Mimic(message, context);

            condition.SetCell("message", message);
            condition.SetCell("context", context);
            condition.SetCell("receiver", on);
            condition.SetCell("method", self);
            condition.SetCell("report", context.runtime.NewText("You tried to activate a method without any code - did you by any chance activate the JavaMethod kind by referring to it without wrapping it inside a call to cell?"));
            context.runtime.ErrorCondition(condition);
            return(self.runtime.nil);
        }
예제 #22
0
파일: IokeObject.cs 프로젝트: tspring/ioke
        public IokeObject ConvertToDecimal(IokeObject m, IokeObject context, bool signalCondition)
        {
            IokeObject result = data.ConvertToDecimal(this, m, context, false);

            if (result == null)
            {
                if (FindCell(this, "asDecimal") != context.runtime.nul)
                {
                    return(IokeObject.As(Interpreter.Send(context.runtime.asDecimalMessage, context, this), context));
                }
                if (signalCondition)
                {
                    return(data.ConvertToDecimal(this, m, context, true));
                }
                return(context.runtime.nil);
            }
            return(result);
        }
예제 #23
0
        public string Inspect(object self)
        {
            string type = "lecro";

            if (!IokeObject.As(self, null).IsActivatable)
            {
                type = "lecrox";
            }

            if (name == null)
            {
                return(type + "(" + Message.Code(code) + ")");
            }
            else
            {
                return(name + ":" + type + "(" + Message.Code(code) + ")");
            }
        }
예제 #24
0
        public string Notice(object self)
        {
            string type = "lecro";

            if (!IokeObject.As(self, null).IsActivatable)
            {
                type = "lecrox";
            }

            if (name == null)
            {
                return(type + "(...)");
            }
            else
            {
                return(name + ":" + type + "(...)");
            }
        }
예제 #25
0
        public static object GetEvaluatedArgument(object argument, IokeObject context)
        {
            if (!(argument is IokeObject))
            {
                return(argument);
            }

            IokeObject o = IokeObject.As(argument, context);

            if (!o.IsMessage)
            {
                return(o);
            }

            var xx = context.runtime.interpreter.Evaluate(o, context, context.RealContext, context);

            return(xx);
        }
예제 #26
0
        public int CheckArgumentCount(IokeObject context, IokeObject message, object on)
        {
            Runtime runtime   = context.runtime;
            IList   arguments = message.Arguments;
            int     argCount  = arguments.Count;
            int     keySize   = keywords.Count;

            if (argCount < min || (max != -1 && argCount > (max + keySize)))
            {
                int finalArgCount = argCount;
                if (argCount < min)
                {
                    IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                 message,
                                                                                 context,
                                                                                 "Error",
                                                                                 "Invocation",
                                                                                 "TooFewArguments"), context).Mimic(message, context);
                    condition.SetCell("message", message);
                    condition.SetCell("context", context);
                    condition.SetCell("receiver", on);
                    condition.SetCell("missing", runtime.NewNumber(min - argCount));

                    runtime.ErrorCondition(condition);
                }
                else
                {
                    IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                                 message,
                                                                                 context,
                                                                                 "Error",
                                                                                 "Invocation",
                                                                                 "TooManyArguments"), context).Mimic(message, context);
                    condition.SetCell("message", message);
                    condition.SetCell("context", context);
                    condition.SetCell("receiver", on);
                    condition.SetCell("extra", runtime.NewList(ArrayList.Adapter(arguments).GetRange(max, finalArgCount - max)));

                    runtime.WithReturningRestart("ignoreExtraArguments", context, () => { runtime.ErrorCondition(condition); });
                    argCount = max;
                }
            }
            return(argCount);
        }
예제 #27
0
        public static IokeObject Signal(object datum, IList positionalArgs, IDictionary <string, object> keywordArgs, IokeObject message, IokeObject context)
        {
            IokeObject newCondition = null;

            if (Text.IsText(datum))
            {
                newCondition = IokeObject.As(context.runtime.Condition.GetCell(message, context, "Default"), context).Mimic(message, context);
                newCondition.SetCell("context", context);
                newCondition.SetCell("text", datum);
            }
            else
            {
                if (keywordArgs.Count == 0)
                {
                    newCondition = IokeObject.As(datum, context);
                }
                else
                {
                    newCondition = IokeObject.As(datum, context).Mimic(message, context);
                    newCondition.SetCell("context", context);
                    foreach (var val in keywordArgs)
                    {
                        string s = val.Key;
                        newCondition.SetCell(s.Substring(0, s.Length - 1), val.Value);
                    }
                }
            }

            Runtime.RescueInfo rescue = context.runtime.FindActiveRescueFor(newCondition);

            IList <Runtime.HandlerInfo> handlers = context.runtime.FindActiveHandlersFor(newCondition, (rescue == null) ? new Runtime.BindIndex(-1, -1) : rescue.index);

            foreach (Runtime.HandlerInfo rhi in handlers)
            {
                Interpreter.Send(context.runtime.callMessage, context, Interpreter.Send(context.runtime.handlerMessage, context, rhi.handler), newCondition);
            }

            if (rescue != null)
            {
                throw new ControlFlow.Rescue(rescue, newCondition);
            }

            return(newCondition);
        }
예제 #28
0
        public static object ActivateFixed(IokeObject self, IokeObject context, IokeObject message, object on)
        {
            object cell = IokeObject.FindCell(self, "activate");

            if (cell == context.runtime.nul)
            {
                report(self, context, message, "activate");
                return(context.runtime.nil);
            }
            else
            {
                IokeObject newMessage = Message.DeepCopy(message);
                newMessage.Arguments.Clear();
                newMessage.Arguments.Add(context.runtime.CreateMessage(Message.Wrap(context)));
                newMessage.Arguments.Add(context.runtime.CreateMessage(Message.Wrap(message)));
                newMessage.Arguments.Add(context.runtime.CreateMessage(Message.Wrap(IokeObject.As(on, context))));
                return(Interpreter.GetOrActivate(cell, context, newMessage, self));
            }
        }
예제 #29
0
        public static object SignalNoSuchCell(IokeObject message, IokeObject ctx, object obj, string name, object cell, IokeObject recv)
        {
            Runtime    runtime   = ctx.runtime;
            IokeObject condition = IokeObject.As(IokeObject.GetCellChain(runtime.Condition,
                                                                         message,
                                                                         ctx,
                                                                         "Error",
                                                                         "NoSuchCell"), ctx).Mimic(message, ctx);

            condition.SetCell("message", message);
            condition.SetCell("context", ctx);
            condition.SetCell("receiver", obj);
            condition.SetCell("cellName", runtime.GetSymbol(name));

            object[] newCell = new object[] { cell };
            runtime.WithRestartReturningArguments(() => { runtime.ErrorCondition(condition); }, ctx,
                                                  new IokeObject.UseValue(name, newCell),
                                                  new IokeObject.StoreValue(name, newCell, recv));
            return(newCell[0]);
        }
예제 #30
0
        public object Evaluate(IokeObject self, IokeObject ctx, object ground, object receiver)
        {
            Runtime    runtime  = self.runtime;
            object     current  = receiver;
            object     tmp      = null;
            string     name     = null;
            object     lastReal = runtime.nil;
            IokeObject m        = self;
            Message    msg;

            while (m != null)
            {
                msg = (Message)m.data;
                tmp = msg.cached;
                if (tmp != null)
                {
                    lastReal = current = tmp;
                }
                else if ((name = msg.name).Equals("."))
                {
                    current = ctx;
                }
                else if (name.Length > 0 && msg.arguments.Count == 0 && name[0] == ':')
                {
                    lastReal = msg.cached = current = runtime.GetSymbol(name.Substring(1));
                }
                else
                {
                    IokeObject recv = IokeObject.As(current, ctx);
                    lastReal = current = tmp = Perform(recv, recv, ctx, m, name);
                }

                m = Message.GetNext(m);
            }
            return(lastReal);
        }