예제 #1
0
 public virtual RCValue Finish(RCRunner runner, RCClosure closure, RCValue result)
 {
     return(RCL.Kernel.Eval.DoFinish(runner, closure, result));
 }
예제 #2
0
 public override void Eval(RCRunner runner, RCClosure closure)
 {
     RCL.Kernel.Eval.DoEval(runner, closure, this);
 }
예제 #3
0
 public override bool IsLastCall(RCClosure closure, RCClosure arg)
 {
     return(RCL.Kernel.Eval.DoIsLastCall(closure, arg, this));
 }
예제 #4
0
 public static RCException Range(RCClosure closure, long i, long count)
 {
     return(new RCException(closure,
                            RCErrors.Range,
                            string.Format("Index {0} is out of range. Count is {1}", i, count)));
 }
예제 #5
0
        public void ToString(StringBuilder builder, int indent, bool firstOnTop)
        {
            RCClosure      closure = this;
            Stack <string> lines   = new Stack <string> ();

            // Do not include the global namespace in the stack trace.
            while (closure != null && closure.Parent != null)
            {
                if (closure.Code != null)
                {
                    RCOperator op = closure.Code as RCOperator;
                    if (op != null)
                    {
                        lines.Push(string.Format("-- {0}", op.ToString()));
                    }
                }
                RCBlock result = closure.Result;
                while (result != null)
                {
                    if (result.Value != null)
                    {
                        RCCube acube = result.Value as RCCube;
                        if (acube != null)
                        {
                            string value = acube.FlatPack().Format(RCFormat.Default);
                            lines.Push(value);
                        }
                        else
                        {
                            string value = result.Value.Format(RCFormat.Default);
                            value = string.Format("{0}:{1}", result.Name, value);
                            value = value.Substring(0, Math.Min(80, value.Length));
                            lines.Push(value);
                        }
                    }
                    result = result.Previous;
                }
                closure = closure.Parent;
            }
            if (firstOnTop)
            {
                builder.AppendFormat("--- BEGIN STACK (bot:{0},fiber:{1},lines:{2}) ---\n",
                                     closure.Bot,
                                     closure.Fiber,
                                     lines.Count);
                while (lines.Count > 0)
                {
                    builder.AppendLine(lines.Pop());
                }
                builder.AppendFormat("--- END STACK ---\n");
            }
            else
            {
                builder.AppendFormat("--- END STACK (bot:{0},fiber:{1},lines:{2}) ---\n",
                                     closure.Bot,
                                     closure.Fiber,
                                     lines.Count);
                string[] linesInOrder = lines.ToArray();
                for (int i = linesInOrder.Length - 1; i >= 0; --i)
                {
                    builder.AppendLine(linesInOrder[i]);
                }
                builder.AppendFormat("--- BEGIN STACK ---\n");
            }
        }
예제 #6
0
 public void EvalEval(RCRunner runner, RCClosure closure, RCBlock left, RCByte right)
 {
     runner.Yield(closure, right);
 }
예제 #7
0
 public RCException(RCClosure closure, RCErrors error, string message)
     : base(message)
 {
     Closure = closure;
     Error   = error;
 }
예제 #8
0
 public void EvalWait(RCRunner runner, RCClosure closure, RCLong right)
 {
     runner.Wait(closure, right);
 }
예제 #9
0
 public void EvalEval(RCRunner runner, RCClosure closure, RCTime right)
 {
     runner.Yield(closure, right);
 }
예제 #10
0
 public void EvalWatch(RCRunner runner, RCClosure closure, RCLong right)
 {
     runner.Watch(closure, right[0]);
 }
예제 #11
0
        public void EvalFiber(RCRunner runner, RCClosure closure, RCBlock right)
        {
            long fiber = DoFiber(runner, closure, right);

            runner.Yield(closure, new RCLong(closure.Bot, fiber));
        }
예제 #12
0
 public void EvalDone(RCRunner runner, RCClosure closure, RCLong right)
 {
     runner.Done(closure, right);
 }
예제 #13
0
 public virtual bool IsBeforeLastCall(RCClosure closure)
 {
     // Yes it matters that this one defaults to false and the other one defaults to
     // true.
     return(false);
 }
예제 #14
0
 public virtual bool IsLastCall(RCClosure closure, RCClosure arg)
 {
     return(true);
 }
예제 #15
0
        public void EvalEval(RCRunner runner, RCClosure closure, RCBlock left, RCReference right)
        {
            RCClosure parent = UserOpClosure(closure, right, new RCArray <RCBlock> (left), noClimb: true);

            DoEval(runner, parent, right);
        }
예제 #16
0
        public void EvalApply(RCRunner runner, RCClosure closure, RCReference left, object right)
        {
            RCClosure parent = UserOpClosure(closure, left, null, null, (RCValue)right);

            DoEval(runner, parent, left);
        }
예제 #17
0
 // Construct the next closure for an operator.
 public static RCClosure DoNext(RCOperator op,
                                RCRunner runner,
                                RCClosure head,
                                RCClosure previous,
                                RCValue result)
 {
     if (op.Left == null)
     {
         if (previous.Index == 0)
         {
             RCValue           userop;
             RCArray <RCBlock> useropContext;
             RCClosure         nextParentOf = NextParentOf(op, previous, out userop, out useropContext);
             RCClosure         next         = new RCClosure(nextParentOf,
                                                            head.Bot,
                                                            op,
                                                            previous.Left,
                                                            new RCBlock(null, "1", ":", result),
                                                            previous.Index + 1,
                                                            userop,
                                                            useropContext);
             return(next);
         }
         else if (previous.Index == 1 && previous.Parent != null)
         {
             return(previous.Parent.Code.Next(runner,
                                              head == null ? previous : head,
                                              previous.Parent,
                                              result));
         }
         else
         {
             return(null);
         }
     }
     else
     {
         if (previous.Index == 0)
         {
             return(new RCClosure(previous.Parent,
                                  head.Bot,
                                  op,
                                  result,
                                  previous.Result,
                                  previous.Index + 1,
                                  previous.UserOp,
                                  previous.UserOpContext));
         }
         else if (previous.Index == 1)
         {
             RCValue           userop;
             RCArray <RCBlock> useropContext;
             RCClosure         next = new RCClosure(NextParentOf(op,
                                                                 previous,
                                                                 out userop,
                                                                 out
                                                                 useropContext),
                                                    head.Bot,
                                                    op,
                                                    // reset "pocket" left to null.
                                                    null,
                                                    // fold it into the current context for the
                                                    // final eval.
                                                    new RCBlock(new RCBlock(null, "0", ":", previous.Left),
                                                                "1",
                                                                ":",
                                                                result),
                                                    previous.Index + 1,
                                                    userop,
                                                    useropContext);
             return(next);
         }
         else if (previous.Index == 2 && previous.Parent != null)
         {
             return(previous.Parent.Code.Next(runner,
                                              head == null ? previous : head,
                                              previous.Parent,
                                              result));
         }
         else if (previous.Parent != null && previous.Parent.Parent != null)
         {
             return(previous.Parent.Parent.Code.Next(runner,
                                                     head == null ? previous : head,
                                                     previous.Parent.Parent,
                                                     result));
         }
         else
         {
             return(null);
         }
     }
 }
예제 #18
0
 // Kicks off evaluation for a block.
 public static void DoEval(RCRunner runner, RCClosure closure, RCBlock block)
 {
     if (block.Count == 0)
     {
         DoYield(runner, closure, block);
     }
     else
     {
         RCBlock current = block.GetName(closure.Index);
         if (current.Evaluator.Invoke)
         {
             string op = ((RCString)current.Value)[0];
             RCSystem.Activator.Invoke(runner, closure, op, closure.Result);
         }
         else if (current.Evaluator.Template)
         {
             try
             {
                 RCString result = ExpandTemplate(new StringBuilder(),
                                                  (RCTemplate)current,
                                                  closure.Result,
                                                  0,
                                                  "");
                 runner.Yield(closure, result);
             }
             catch (Exception ex)
             {
                 RCException rcex = new RCException(closure,
                                                    ex,
                                                    RCErrors.Native,
                                                    "An exception was thrown by the template.");
                 runner.Finish(closure, rcex, (int)RCErrors.Native);
             }
         }
         else if (current.Evaluator.Pass)
         {
             DoYield(runner, closure, current.Value);
         }
         // This means that Value is an operator or a reference.
         else if (current.Value.ArgumentEval)
         {
             current.Value.Eval(runner,
                                new RCClosure(closure,
                                              closure.Bot,
                                              current.Value,
                                              closure.Left,
                                              closure.Result,
                                              0));
         }
         else if (current.Evaluator.Return)
         {
             DoYield(runner, closure, current.Value);
         }
         else
         {
             // I need something different to happen when we are at the top level already.
             // Or maybe I need to inject a wrapper closure when I do Rep this way?
             if ((closure.Index < block.Count - 1) || (closure.Parent != null))
             {
                 DoYield(runner, closure, current.Value);
             }
             else
             {
                 DoYield(runner, closure, current);
             }
         }
     }
 }
예제 #19
0
        public void Invoke(RCRunner runner, RCClosure closure, string name, object left, object right)
        {
            OverloadValue overload;
            Type          ltype = left.GetType();
            Type          rtype = right.GetType();
            Type          ctype = right.GetType();

            try
            {
                if (_dispatch.TryGetValue(new OverloadKey(name, ctype, ltype, rtype), out overload))
                {
                    RCBot  bot   = runner.GetBot(closure.Bot);
                    object state = bot.GetModule(overload.Module);
                    overload.Implementation.Invoke(state, new object[] { runner, closure, left, right });
                }
                else if (_dispatch.TryGetValue(new OverloadKey(name, typeof(object), ltype, rtype),
                                               out overload))
                {
                    RCBot  bot   = runner.GetBot(closure.Bot);
                    object state = bot.GetModule(overload.Module);
                    overload.Implementation.Invoke(state, new object[] { runner, closure, left, right });
                }
                else if (_dispatch.TryGetValue(new OverloadKey(name,
                                                               typeof(object),
                                                               typeof(object),
                                                               rtype),
                                               out overload))
                {
                    RCBot  bot   = runner.GetBot(closure.Bot);
                    object state = bot.GetModule(overload.Module);
                    overload.Implementation.Invoke(state, new object[] { runner, closure, left, right });
                }
                else if (_dispatch.TryGetValue(new OverloadKey(name,
                                                               typeof(object),
                                                               ltype,
                                                               typeof(object)),
                                               out overload))
                {
                    RCBot  bot   = runner.GetBot(closure.Bot);
                    object state = bot.GetModule(overload.Module);
                    overload.Implementation.Invoke(state, new object[] { runner, closure, left, right });
                }
                else if (_dispatch.TryGetValue(new OverloadKey(name,
                                                               typeof(object),
                                                               typeof(object),
                                                               typeof(object)),
                                               out overload))
                {
                    RCBot  bot   = runner.GetBot(closure.Bot);
                    object state = bot.GetModule(overload.Module);
                    overload.Implementation.Invoke(state, new object[] { runner, closure, left, right });
                }
                else if (_dispatch.TryGetValue(new OverloadKey(name,
                                                               typeof(object),
                                                               ltype,
                                                               typeof(RCOperator)),
                                               out overload))
                {
                    RCBot  bot   = runner.GetBot(closure.Bot);
                    object state = bot.GetModule(overload.Module);
                    overload.Implementation.Invoke(state, new object[] { runner, closure, left, right });
                }
                else
                {
                    throw RCException.Overload(closure, name, left, right);
                }
            }
            catch (TargetInvocationException tiex)
            {
                Exception ex = tiex.GetBaseException();
                if (ex is RCException)
                {
                    throw ex;
                }
                else
                {
                    // You have to pass the tiex, not ex here so that the interior stack trace will
                    // be
                    // preserved when/if it is rethrown.
                    throw new RCException(closure, tiex, RCErrors.Native, ThrowMessage(name, ex));
                }
            }
        }
예제 #20
0
 public static void DoEval(RCRunner runner,
                           RCClosure closure,
                           RCReference reference)
 {
     runner.Yield(closure, Resolve(reference._static, closure, reference.Parts, null));
 }
예제 #21
0
        public static RCException Overload(RCClosure closure, RCOperator op, RCValue right)
        {
            string message = string.Format(MONADIC_OVERLOAD_FORMAT, op.Name, right.TypeName);

            return(new RCException(closure, RCErrors.Type, message));
        }
예제 #22
0
 public void EvalEval(RCRunner runner, RCClosure closure, RCBlock left, RCUserOperator right)
 {
     // Invocation using the activator requires a perfect match on the argument type.
     EvalEval(runner, closure, left, (RCOperator)right);
 }
예제 #23
0
 public static RCException LockViolation(RCClosure closure)
 {
     return(new RCException(closure,
                            RCErrors.Lock,
                            "Attempted to mutate a value after it was locked."));
 }
예제 #24
0
 public static void DoEvalInline(RCRunner runner, RCClosure closure, RCInlineOperator op)
 {
     op._code.Eval(runner, UserOpClosure(closure, op._code, null, noClimb: false));
 }
예제 #25
0
 public override void EvalOperator(RCRunner runner, RCClosure closure)
 {
     RCL.Kernel.Eval.DoEvalInline(runner, closure, this);
 }
예제 #26
0
 public static void DoEvalTemplate(RCRunner runner, RCClosure closure, RCTemplate template)
 {
     throw new Exception("Not implemented");
 }
예제 #27
0
 public virtual void EvalOperator(RCRunner runner, RCClosure closure)
 {
     RCL.Kernel.Eval.DoEvalOperator(runner, closure, this);
 }
예제 #28
0
 public void EvalEval(RCRunner runner, RCClosure closure, RCReference right)
 {
     DoEval(runner, closure, right);
 }
예제 #29
0
 public override bool IsBeforeLastCall(RCClosure closure)
 {
     return(RCL.Kernel.Eval.DoIsBeforeLastCall(closure, this));
 }
예제 #30
0
 public virtual void Eval(RCRunner runner, RCClosure closure)
 {
     runner.Yield(closure, this);
 }