Example #1
0
        protected virtual void DoSwitch <T> (RCRunner runner,
                                             RCClosure closure,
                                             RCVector <T> left,
                                             RCBlock right,
                                             Picker <T> picker)
        {
            int i = closure.Index - 2;

            if (i < left.Count)
            {
                bool    eval;
                RCValue code = picker(left[i], out eval);
                if (eval)
                {
                    RCClosure child = new RCClosure(closure,
                                                    closure.Bot,
                                                    code,
                                                    closure.Left,
                                                    RCBlock.Empty,
                                                    0);
                    code.Eval(runner, child);
                }
                else
                {
                    // In this case the "code" can be a value, normally part of a generated program
                    runner.Yield(closure, code);
                }
            }
            else
            {
                runner.Yield(closure, closure.Parent.Result);
            }
        }
Example #2
0
        public void EvalUnflip(RCRunner runner, RCClosure closure, RCBlock right)
        {
            // Take a block of arrays and turn them into a block of rows.
            RCBlock[] blocks = new RCBlock[right.Get(0).Count];
            for (int i = 0; i < blocks.Length; ++i)
            {
                blocks[i] = RCBlock.Empty;
            }
            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock      name   = right.GetName(i);
                RCVectorBase vector = (RCVectorBase)name.Value;
                for (int j = 0; j < vector.Count; ++j)
                {
                    RCValue box = RCVectorBase.FromScalar(vector.Child(j));
                    blocks[j] = new RCBlock(blocks[j], name.Name, ":", box);
                }
            }
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < blocks.Length; ++i)
            {
                result = new RCBlock(result, "", ":", blocks[i]);
            }
            runner.Yield(closure, result);
        }
Example #3
0
        public override RCValue Finish(RCValue result)
        {
            RCBlock wrapper = new RCBlock("status", ":", new RCLong(0));

            wrapper = new RCBlock(wrapper, "data", ":", result);
            return(wrapper);
        }
Example #4
0
        protected RCBlock RecursiveWhere(RCBlock left, RCBlock right)
        {
            if (left.Count != right.Count)
            {
                throw new Exception(string.Format(
                                        "Left count was {0} but right count was {1}. Counts must match",
                                        left.Count,
                                        right.Count));
            }
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < left.Count; ++i)
            {
                RCBlock leftName   = left.GetName(i);
                RCValue rightValue = right.Get(i);
                if (rightValue is RCBlock)
                {
                    RCBlock rightBlock  = (RCBlock)rightValue;
                    RCBlock leftBlock   = left.GetBlock(i);
                    RCBlock childResult = RecursiveWhere(leftBlock, rightBlock);
                    if (childResult.Count > 0)
                    {
                        result = new RCBlock(result, leftName.Name, leftName.Evaluator, childResult);
                    }
                }
                else if (rightValue is RCBoolean)
                {
                    if (rightValue.Count == 1)
                    {
                        if (right.GetBoolean(i))
                        {
                            result = new RCBlock(result, leftName.Name, ":", leftName.Value);
                        }
                    }
                    else
                    {
                        RCBoolean rightVector = (RCBoolean)rightValue;
                        RCBlock   leftBlock   = left.GetBlock(i);
                        RCBlock   childResult = RCBlock.Empty;
                        for (int j = 0; j < rightVector.Count; ++j)
                        {
                            RCBlock leftVar = leftBlock.GetName(j);
                            if (rightVector[j])
                            {
                                childResult = new RCBlock(childResult, leftVar.Name, ":", leftVar.Value);
                            }
                        }
                        if (childResult.Count > 0)
                        {
                            result = new RCBlock(result, leftName.Name, leftName.Evaluator, childResult);
                        }
                    }
                }
            }
            return(result);
        }
Example #5
0
        public override bool IsLastCall(RCClosure closure, RCClosure arg)
        {
            RCValue right = closure.Result.Get("1");

            if (right == null)
            {
                return(base.IsLastCall(closure, arg));
            }
            return(closure.Index == right.Count + 2);
        }
Example #6
0
        public void EvalOptions(RCRunner runner, RCClosure closure, RCString right)
        {
            RCValue result = RCSystem.Args.Options.Get(right[0]);

            if (result == null)
            {
                throw new Exception("No such option:" + right[0]);
            }
            runner.Yield(closure, result);
        }
Example #7
0
        public void EvalOptions(RCRunner runner, RCClosure closure, RCString left, RCString right)
        {
            RCValue result = RCSystem.Args.Options.Get(right[0]);

            if (result == null)
            {
                result = left;
            }
            runner.Yield(closure, result);
        }
Example #8
0
        protected virtual void SetTimer(RCRunner runner, RCClosure closure, RCValue right, int millis)
        {
            RCAsyncState state = new RCAsyncState(runner, closure, right);
            // Using the single argument ctor causes the the timer to be
            // passed in the "state" parameter, and the state parameter
            // is always included in the gc handle table, so in this convoluted
            // way, the timer is protected from being gc'd.
            Timer timer = new Timer(new Wakeup(state).Continue);

            timer.Change(millis, Timeout.Infinite);
        }
Example #9
0
        public override RCClosure Next(RCRunner runner,
                                       RCClosure tail,
                                       RCClosure previous,
                                       RCValue
                                       result)
        {
            bool done = Fiber.CheckFiberDone(previous);

            if (done)
            {
                return(null);
            }
            return(base.Next(runner, tail, previous, result));
        }
Example #10
0
        public virtual RCBlock CompleteReceive(RCRunner runner,
                                               int count,
                                               long handle,
                                               long sid,
                                               out
                                               long cid)
        {
            RCBlock message = null;

            if (_read == 0)
            {
                // Inspect the header to see if we have enough buffer for the message
                // that is going to arrive.  If not create a bigger buffer.
                _reading = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(_recvBuffer, 0));
                _cid     = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(_recvBuffer, 4));
                if (_reading > _recvBuffer.Length - HEADER_SIZE)
                {
                    Console.Out.WriteLine("  replacing buffer...", _read, _reading);
                    byte[] replacement = new byte[_reading + HEADER_SIZE];
                    _recvBuffer.CopyTo(replacement, count);
                    _recvBuffer = replacement;
                }
            }
            _read += count;
            cid    = _cid;
            if (_read >= _reading + HEADER_SIZE)
            {
                string text     = Encoding.ASCII.GetString(_recvBuffer, HEADER_SIZE, _read - HEADER_SIZE);
                bool   fragment = false;
                // This one creates a new parser an so it is threadsafe for multiple calls.
                RCValue body = RCSystem.Parse(text, out fragment);
                if (body != null)
                {
                    // throw new Exception("failed to parse:" + text);
                    RCSymbolScalar correlation = new RCSymbolScalar(null, handle);
                    if (sid > -1)
                    {
                        correlation = new RCSymbolScalar(correlation, sid);
                    }
                    correlation = new RCSymbolScalar(correlation, _cid);
                    message     = new RCBlock(null, "id", ":", new RCSymbol(correlation));
                    message     = new RCBlock(message, "body", ":", body);
                }
                _read    = 0;
                _reading = 0;
                _cid     = -1;
            }
            return(message);
        }
Example #11
0
        public static RCBlock DoAt(RCClosure closure, RCBlock left, RCSymbol right)
        {
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < right.Count; ++i)
            {
                RCValue next = left.Get(right[i], null);
                if (next == null)
                {
                    string message = string.Format("at: Name '{0}' not found within block", right[i]);
                    throw new RCException(closure, RCErrors.Name, message);
                }
                string name = right[i].Key as string;
                result = new RCBlock(result, name, ":", next);
            }
            return(result);
        }
Example #12
0
 public override RCClosure Next(RCRunner runner,
                                RCClosure tail,
                                RCClosure previous,
                                RCValue
                                result)
 {
     if (previous.Index < 2)
     {
         return(base.Next(runner, tail, previous, result));
     }
     else
     {
         RCBot bot    = runner.GetBot(tail.Bot);
         Take  module = (Take)bot.GetModule(typeof(Take));
         module.Untake(runner, tail);
         return(base.Next(runner, tail, previous, result));
     }
 }
Example #13
0
        public override RCClosure Handle(RCRunner runner,
                                         RCClosure closure,
                                         Exception exception,
                                         long status,
                                         out RCValue result)
        {
            // When the runner finds out about an exception it will try to call this
            // Handle method on all of the parents in the stack until it gets to
            // one that returns a closure to eval next.
            RCBlock     wrapper = new RCBlock("status", ":", new RCLong(status));
            RCException rcex    = exception as RCException;
            string      message;

            if (rcex != null)
            {
                if (RCSystem.Args.OutputEnum == RCOutput.Test)
                {
                    message = rcex.ToTestString();
                }
                else
                {
                    message = rcex.ToString();
                }
            }
            else
            {
                message = exception.Message;
            }
            RCBlock report      = new RCBlock("", ":", new RCString(message + "\n"));
            int     escapeCount = RCTemplate.CalculateReportTemplateEscapeLevel(message);

            if (RCSystem.Args.OutputEnum == RCOutput.Test)
            {
                wrapper = new RCBlock(wrapper, "data", ":", new RCTemplate(report, escapeCount, true));
            }
            else
            {
                wrapper = new RCBlock(wrapper, "data", ":", new RCTemplate(report, escapeCount, true));
            }
            result = wrapper;
            return(base.Next(runner, closure, closure, result));
        }
Example #14
0
        public void Remove(RCRunner runner, RCClosure closure)
        {
            RCValue message = null;

            lock (_lock)
            {
                if (_messages.Count > 0)
                {
                    message = _messages.Dequeue();
                }
                else
                {
                    _requests.Enqueue(new RCAsyncState(runner, closure, null));
                }
            }
            if (message != null)
            {
                runner.Yield(closure, message);
            }
        }
Example #15
0
        public void Add(RCRunner runner, RCValue message)
        {
            RCAsyncState state = null;

            lock (_lock)
            {
                if (_requests.Count > 0)
                {
                    state = _requests.Dequeue();
                }
                else
                {
                    _messages.Enqueue(message);
                }
            }
            if (state != null)
            {
                runner.Yield(state.Closure, message);
            }
        }
Example #16
0
        protected RCBlock Name(string nameProperty, RCBlock right)
        {
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock name = right.GetName(i);
                RCBlock val;
                if (name.Value is RCBlock)
                {
                    val = (RCBlock)name.Value;
                }
                else
                {
                    throw new Exception(string.Format("Expected a block. Actual type was {0}",
                                                      name.Value.TypeName));
                }
                RCValue  newName = val.Get(nameProperty);
                string   nameStr;
                RCString str = newName as RCString;
                if (str != null)
                {
                    nameStr = str[0];
                }
                else
                {
                    RCSymbol sym = newName as RCSymbol;
                    if (sym != null)
                    {
                        nameStr = (string)sym[0].Part(0);
                    }
                    else
                    {
                        throw new Exception(
                                  "The name must be a string or a symbol with a name in the first part");
                    }
                }
                result = new RCBlock(result, nameStr, name.Evaluator, val);
            }
            return(result);
        }
Example #17
0
        public void DoTake(RCRunner runner, RCClosure closure, RCSymbol symbols, RCValue section)
        {
            RCClosure next = new RCClosure(closure.Bot,
                                           closure.Fiber,
                                           symbols,
                                           closure,
                                           section,
                                           closure.Left,
                                           closure.Parent != null ? closure.Parent.Result : null,
                                           0,
                                           closure.UserOp,
                                           closure.UserOpContext,
                                           noClimb: false,
                                           noResolve: false);

            lock (_takeLock)
            {
                if (TryGetLocks(next))
                {
                    // Start evaluating the critical section.
                    runner.Continue(null, next);
                }
                else
                {
                    // Record the order in which the waiters arrived.
                    _takeOrder.Add(next);

                    // Remember that we want the lock when the other guy releases it.
                    for (int i = 0; i < symbols.Count; ++i)
                    {
                        HashSet <long> fibers;
                        if (!_takeFibers.TryGetValue(symbols[i], out fibers))
                        {
                            fibers = new HashSet <long> ();
                            _takeFibers[symbols[i]] = fibers;
                        }
                        fibers.Add(closure.Fiber);
                    }
                }
            }
        }
Example #18
0
 public override RCClosure Next(RCRunner runner,
                                RCClosure tail,
                                RCClosure previous,
                                RCValue
                                result)
 {
     if (previous.Index == 1)
     {
         long status = 0;
         if (result is RCNative)
         {
             status = 1;
         }
         RCBlock wrapper = new RCBlock("status", ":", new RCLong(status));
         wrapper = new RCBlock(wrapper, "data", ":", result);
         RCClosure next = base.Next(runner, tail, previous, wrapper);
         return(next);
     }
     else
     {
         return(base.Next(runner, tail, previous, result));
     }
 }