Esempio n. 1
0
        public override void AcceptMarkdownHeader(RCToken token)
        {
            // Console.Out.WriteLine ("AcceptMarkdownHeader({0}): '{1}'", _state, token.Text);
            // Console.Out.WriteLine ("_liLength: " + _liLength);
            FinishList();
            FinishRuns(true);
            // Do break out of a p for a header
            int    space            = token.Text.IndexOf(' ');
            int    headerTextStart  = space + 1;
            int    headerTextLength = token.Text.TrimEnd().Length - headerTextStart;
            string headerText       = token.Text.Substring(headerTextStart, headerTextLength);
            int    headerLevel      = 0;

            while (token.Text[headerLevel] == '#')
            {
                ++headerLevel;
            }
            AppendRun();
            bool    reentered;
            RCBlock text = ParseEmbeddedRun(headerText, MarkdownState.Link, out reentered);

            _name = "h" + headerLevel.ToString();
            StartBlock();
            _value = text;
            EndBlock();
        }
Esempio n. 2
0
 public override void VisitScalar <T> (string name, Column <T> column, int row)
 {
     _row = new RCBlock(_row,
                        name,
                        ":",
                        RCVectorBase.FromArray(new RCArray <T> (column.Data[row])));
 }
Esempio n. 3
0
        public override void AcceptMarkdownLiteralLink(RCToken token)
        {
            // Do not break out of a p for a link
            FinishRuns(false);
            if (_state == MarkdownState.None)
            {
                _name = "p";
                StartBlock();
                _parsingParagraph = true;
            }
            _state = MarkdownState.Link;
            int    openBracket    = 0;
            int    closeBracket   = token.Text.Length - 1;
            int    linkTextStart  = openBracket + 1;
            int    linkTextLength = closeBracket - linkTextStart;
            string linkText       = token.Text.Substring(linkTextStart, linkTextLength);

            _name = "a";
            StartBlock();
            RCBlock textBlock = new RCBlock("", ":", new RCString(linkText));

            _value = new RCBlock(RCBlock.Empty, "text", ":", textBlock);
            _value = new RCBlock(_value, "href", ":", new RCString(linkText));
            EndBlock();
        }
Esempio n. 4
0
 public static RCValue DoFinish(RCRunner runner,
                                RCClosure closure,
                                RCValue result)
 {
     while (closure != null)
     {
         RCBlock obj = closure.Code as RCBlock;
         if (obj != null)
         {
             if (obj.Count > 0 && obj.Evaluator.FinishBlock && result != closure.Code)
             {
                 result = NextBlock(runner, obj, closure, result);
             }
         }
         RCOperator op = closure.Code as RCOperator;
         if (op != null)
         {
             result = op.Finish(result);
         }
         if (closure.Parent != null &&
             (closure.Parent.Bot != closure.Bot ||
              closure.Parent.Fiber != closure.Fiber))
         {
             break;
         }
         closure = closure.Parent;
     }
     return(result);
 }
Esempio n. 5
0
 /// <summary>
 /// Find and return the value referenced by name. Throw if not found.
 /// </summary>
 public static RCValue Resolve(RCBlock context,
                               RCClosure closure,
                               RCArray <string> name,
                               RCArray <RCBlock> @this)
 {
     return(Resolve(context, closure, name, @this, false));
 }
Esempio n. 6
0
        public RCValue Run(RCValue program, bool restoreStateOnError)
        {
            // Shouldn't this be an exception?
            if (program == null)
            {
                return(null);
            }
            RCBlock   wrapper = new RCBlock(RCBlock.Empty, "", "<-", program);
            RCClosure parent  = new RCClosure(_bots[0].Id,
                                              0,
                                              null,
                                              null,
                                              wrapper,
                                              null,
                                              _state,
                                              0,
                                              null,
                                              null,
                                              noClimb: false,
                                              noResolve: false);
            RCClosure closure = new RCClosure(parent,
                                              _bots[0].Id,
                                              program,
                                              null,
                                              RCBlock.Empty,
                                              0,
                                              null,
                                              null);
            RCValue result = Run(closure, restoreStateOnError);

            return(result);
        }
Esempio n. 7
0
        public RCBlock ToBlock()
        {
            RCBlock result = RCBlock.Empty;

            result = new RCBlock(result, "SymbolLimit", ":", SymbolLimit);
            result = new RCBlock(result, "SymbolUnlimited", ":", SymbolUnlimited);
            result = new RCBlock(result, "Forward", ":", Forward);
            result = new RCBlock(result, "Start", ":", (long)Start);
            result = new RCBlock(result, "IgnoreDispatchedRows", ":", IgnoreDispatchedRows);
            result = new RCBlock(result, "SkipFirst", ":", (long)SkipFirst);
            result = new RCBlock(result, "TotalLimit", ":", (long)TotalLimit);
            result = new RCBlock(result, "Force", ":", Force);
            result = new RCBlock(result, "Fill", ":", Fill);
            result = new RCBlock(result, "RecordCount", ":", RecordCount);
            RCBlock records = RCBlock.Empty;

            foreach (KeyValuePair <RCSymbolScalar, SpecRecord> specRecord in _records)
            {
                records = new RCBlock(records,
                                      specRecord.Key.ToString(),
                                      ":",
                                      specRecord.Value.ToBlock());
            }
            result = new RCBlock(result, "Records", ":", records);
            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// This method creates an identical closure where the left and right
        /// arguments can be accessed in user space.
        /// This has to be done by operators that evaluate user provided code.
        /// </summary>
        public static RCClosure UserOpClosure(RCClosure previous,
                                              RCValue code,
                                              RCArray <RCBlock> @this,
                                              RCValue left,
                                              RCValue right,
                                              bool noClimb)
        {
            left  = left != null ? left : previous.Result.Get("0");
            right = right != null ? right : previous.Result.Get("1");
            RCBlock result = null;

            if (@this != null && @this.Count > 0)
            {
                result = @this[0];
                // This is only for when the this context contains more than one object.
                // I'm not even sure whether to support this, I guess I should.
                // But this is not going to be the fastest solution possible.
                for (int i = 1; i < @this.Count; ++i)
                {
                    for (int j = 0; j < @this[i].Count; ++j)
                    {
                        RCBlock block = @this[i].GetName(j);
                        result = new RCBlock(result, block.Name, ":", block.Value);
                    }
                }
            }
            if (left == null)
            {
                result = new RCBlock(result, "R", ":", right);
            }
            else
            {
                result = new RCBlock(result, "L", ":", left);
                result = new RCBlock(result, "R", ":", right);
            }
            // Passing code and @this here is important. NextParentOf will look
            // for the body of the executing function in that variable to detect tail calls.
            RCClosure replacement = new RCClosure(previous.Bot,
                                                  previous.Fiber,
                                                  previous.Locks,
                                                  previous.Parent,
                                                  previous.Code,
                                                  previous.Left,
                                                  result,
                                                  previous.Index,
                                                  code,
                                                  @this,
                                                  noClimb,
                                                  noResolve: false);
            RCClosure child = new RCClosure(replacement,
                                            previous.Bot,
                                            code,
                                            previous.Left,
                                            RCBlock.Empty,
                                            0,
                                            code,
                                            @this);

            return(child);
        }
Esempio n. 9
0
        public RCBlock Serialize()
        {
            RCBlock result = RCBlock.Empty;

            result = new RCBlock(result, "bot", ":", Bot);
            result = new RCBlock(result, "code", ":", this.Code);
            result = new RCBlock(result, "depth", ":", this.Depth);
            result = new RCBlock(result, "fiber", ":", this.Fiber);
            result = new RCBlock(result, "index", ":", this.Index);
            result = new RCBlock(result, "result", ":", this.Result);
            if (this.Left != null)
            {
                result = new RCBlock(result, "left", ":", this.Left);
            }
            if (this.Locks != null)
            {
                result = new RCBlock(result, "locks", ":", this.Locks);
            }
            if (this.Parent != null)
            {
                result = new RCBlock(result, "parent", ":", this.Parent.Serialize());
            }
            if (this.UserOpContext != null)
            {
                // TODO: make this List serialize
                // result = new RCBlock (result, "userOpContext", ":", this.Parent.Serialize ());
            }
            result = new RCBlock(result, "noClimb", ":", this.NoClimb);
            result = new RCBlock(result, "noResolve", ":", this.NoResolve);
            return(result);
        }
Esempio n. 10
0
 public override void AcceptEvaluator(RCToken token)
 {
     FinishValue(true);
     MakeExpression();
     // When this applies there is no actual opening bracket defining the block.
     // This happens in the shell.
     // I call it an "implied" block.
     if (_block == null)
     {
         _block = RCBlock.Empty;
     }
     FinishBlock();
     if (_maybeOperator != null)
     {
         // _maybeOp is not an operator, its a variable.
         _variable      = _maybeOperator;
         _maybeOperator = null;
     }
     if (_variable == null)
     {
         // It is an unnamed block.
         _variable = "";
     }
     // If anything was left over on the right, it doesn't belong here.
     // Should this be an invalid state for the parser?
     _result    = null;
     _evaluator = RCEvaluator.For(token.Text);
 }
Esempio n. 11
0
        public override RCValue Parse(RCArray <RCToken> tokens, out bool fragment, bool canonical)
        {
            _initialState = _state;
            for (int i = 0; i < tokens.Count; ++i)
            {
                tokens[i].Type.Accept(this, tokens[i]);
            }
            fragment = false;

            // Console.Out.WriteLine ("Done parsing. Doing cleanup ({0})", _state);
            FinishQuote(true);
            while (_values.Count > 0)
            {
                EndBlock();
            }
            if (_run.Length > 0)
            {
                _value = new RCBlock(_value, "", ":", new RCString(_run.ToString()));
            }
            else if (_initialState == MarkdownState.Link && _value == null)
            {
                _value = new RCBlock(null, "", ":", new RCString(""));
            }
            return(_value);
        }
Esempio n. 12
0
 public override void BeforeRow(long e, RCTimeScalar t, RCSymbolScalar symbol, int row)
 {
     if (_source.Axis.Has("G"))
     {
         _row = new RCBlock(RCBlock.Empty, "G", ":", new RCLong(_source.Axis.Global[row]));
     }
     if (_source.Axis.Has("E"))
     {
         _row = new RCBlock(_row, "E", ":", new RCLong(_source.Axis.Event[row]));
     }
     if (_source.Axis.Has("T"))
     {
         _row = new RCBlock(_row, "T", ":", new RCTime(_source.Axis.Time[row]));
     }
     if (_source.Axis.Has("S"))
     {
         // Include S as both a field on the row blocks and as the names of the rows
         // themselves
         // This is so that you can get back to what you had with "flip block $my_cube"
         // While also being able to treat the result of "block $my_cube" as a dictionary
         // if you wish
         _row     = new RCBlock(_row, "S", ":", new RCSymbol(_source.Axis.Symbol[row]));
         _rowName = _source.Axis.Symbol[row].Key.ToString();
     }
 }
Esempio n. 13
0
 public static bool DoIsLastCall(RCClosure closure,
                                 RCClosure arg,
                                 RCBlock block)
 {
     // Costly call to GetName, will want to address this at some point.
     // return block.Evaluator.Return;
     return(block.GetName(closure.Index).Evaluator.Return);
 }
Esempio n. 14
0
        protected static string CreateMessage(object info)
        {
            // Not the most efficient solution possible, but it looks pretty good on the
            // console.
            string  message;
            RCBlock block = info as RCBlock;

            if (block != null)
            {
                if (block.Count == 1 && block.Evaluator == RCEvaluator.Yield)
                {
                    message = block.Format(RCFormat.Default, _colmap);
                }
                else
                {
                    message = block.Format(RCFormat.Pretty, _colmap);
                }
            }
            else if (info is RCCube)
            {
                message = ((RCValue)info).Format(RCFormat.Pretty, _colmap);
            }
            else if (info is RCException && (_level == RCOutput.Test || _level == RCOutput.Single))
            {
                message = ((RCException)info).ToTestString();
            }
            else if (info is Exception && (_level == RCOutput.Test || _level == RCOutput.Single))
            {
                if (RCSystem.Args.FullStack)
                {
                    message = string.Format("<<Reported, {0}>>", info.ToString());
                }
                else
                {
                    message = "<<Reported>>";
                }
            }
            else if (info is RCString)
            {
                RCString      strings = (RCString)info;
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < strings.Count; ++i)
                {
                    builder.AppendLine(strings[i]);
                }
                if (builder.Length > 0)
                {
                    builder.Remove(builder.Length - 1, 1);
                }
                message = builder.ToString();
            }
            else
            {
                message = info.ToString();
            }
            return(message);
        }
Esempio n. 15
0
 protected void AppendChild(RCValue scalar)
 {
     _value = new RCBlock(_value, _name, ":", scalar);
     if (_state == JSONState.Value)
     {
         _name  = "";
         _state = JSONState.Name;
     }
 }
Esempio n. 16
0
 public RCClosure(RCClosure parent,
                  long bot,
                  RCValue code,
                  RCValue left,
                  RCBlock result,
                  int index)
     : this(parent, bot, code, left, result, index, null, null)
 {
 }
Esempio n. 17
0
        public RCValue Get(string name)
        {
            RCBlock obj = GetName(name);

            if (obj == null)
            {
                return(null);
            }
            return(obj.Value);
        }
Esempio n. 18
0
        public RCValue Get(long index)
        {
            RCBlock obj = GetName(index);

            if (obj == null)
            {
                return(null);
            }
            return(obj.Value);
        }
Esempio n. 19
0
        public void SetStatic(RCBlock context)
        {
            if (IsLocked)
            {
                throw new Exception(
                          "Attempted to modify a locked instance of RCReference.");
            }

            _static = context;
        }
Esempio n. 20
0
 protected void EndBlock()
 {
     if (_values.Count > 0)
     {
         RCBlock child = _value;
         _name  = _names.Pop();
         _value = _values.Pop();
         _state = _states.Pop();
         AppendChild(child);
     }
 }
Esempio n. 21
0
 protected void StartBlock()
 {
     if (_value != null)
     {
         _values.Push(_value);
         _names.Push(_name);
         _states.Push(_state);
     }
     _value = RCBlock.Empty;
     _name  = "";
 }
Esempio n. 22
0
        public static RCBlock Append(RCBlock left, RCBlock right)
        {
            RCBlock result = left;

            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock current = right.GetName(i);
                result = new RCBlock(result, current.Name, current.Evaluator, current.Value);
            }
            return(result);
        }
Esempio n. 23
0
        public void EvalEval(RCRunner runner, RCClosure closure, RCBlock right)
        {
            RCClosure parent = new RCClosure(closure.Parent,
                                             closure.Bot,
                                             right,
                                             closure.Left,
                                             RCBlock.Empty,
                                             0);

            DoEval(runner, parent, right);
        }
Esempio n. 24
0
        public override void Lock()
        {
            base.Lock();
            RCBlock current = this;

            while (current.Count > 0)
            {
                current.Value.Lock();
                current = current.Previous;
            }
        }
Esempio n. 25
0
        public RCValue Rep(RCValue program, bool restoreStateOnError)
        {
            RCValue result = Run(program, restoreStateOnError);
            RCBlock state  = result as RCBlock;

            if (state != null)
            {
                _state = state;
            }
            return(result);
        }
Esempio n. 26
0
        public override void AcceptMarkdownContent(RCToken token)
        {
            // Console.Out.WriteLine ("AcceptMarkdownContent({0}): '{1}'", _state, token.Text);
            // Console.Out.WriteLine ("_parsingParagraph: {0}", _parsingParagraph);
            // Console.Out.WriteLine ("_parsingList: {0}", _parsingList);
            // Console.Out.WriteLine ("_liLength: {0}", _liLength);
            // Console.Out.WriteLine ("_blankLine: {0}", _blankLine);
            string text = token.Text;

            if (_state == MarkdownState.ListItem)
            {
                WrapLITextIfNeeded(_state);
            }
            if (_liLength > -1)
            {
                bool indentedlip = true;
                for (int i = 0; i < _liLength; ++i)
                {
                    if (i < text.Length && text[i] != ' ')
                    {
                        indentedlip = false;
                    }
                }
                if (indentedlip)
                {
                    WrapLITextIfNeeded(_state);
                }
            }
            FinishList();
            if (_parsingList && _parsingParagraph &&
                _blankLine && _state == MarkdownState.None)
            {
                EndBlock();
                _parsingParagraph = false;
            }
            // _parsingParagraph should be sufficient, shouldn't need _run.Length check.
            if ((!_parsingParagraph && _blankLine && _parsingList) ||
                (!_parsingList && !_parsingParagraph && _run.Length == 0 &&
                 (_state == MarkdownState.None || _state == MarkdownState.Newline1)))
            {
                _state = MarkdownState.Paragraph;
                _name  = "p";
                StartBlock();
                _name             = "";
                _value            = RCBlock.Empty;
                text              = text.TrimStart();
                _parsingParagraph = true;
            }
            else if (_state == MarkdownState.Newline1 || _state == MarkdownState.MaybeBR)
            {
                text = text.TrimStart();
            }
            UpdateTextRun(_run, text);
        }
Esempio n. 27
0
        protected void AppendRun()
        {
            if (_run.Length == 0)
            {
                return;
            }
            RCString text = new RCString(_run.ToString());

            _run.Clear();
            _value = new RCBlock(_value, _name, ":", text);
            _name  = "";
        }
Esempio n. 28
0
 protected bool FinishBlock()
 {
     // instantiate blocks
     if (_variable != null && _result != null)
     {
         _block    = new RCBlock(_block, _variable, _evaluator, _result);
         _variable = null;
         return(true);
         // _evaluator = null;
     }
     return(false);
 }
Esempio n. 29
0
        protected RCValue Run(RCClosure root, bool restoreStateOnError)
        {
            lock (_queueLock)
            {
                if (_root == null)
                {
                    _root = root;
                    RCBot rootBot = GetBot(_root.Bot);
                    // keeping this inside the lock because it should happen before the call to
                    // Enqueue.
                    // But only during the very first call to run for this runner.
                    // Log.Record (this, root, root.BotId, "bot", root.BotId, "start", root.Code);
                    rootBot.ChangeFiberState(root.Fiber, "start");
                    RCSystem.Log.Record(root, "fiber", root.Fiber, "start", root.Code);
                }
                _queue.Enqueue(root);
            }
            // Trigger a worker (don't care which) to take it.
            _wait.Set();
            // Wait for the work to be completed.
            _done.WaitOne();
            // If an exception was thrown, rethrow it on this thread.
            if (_exception != null)
            {
                Exception exception = _exception;
                _exception = null;
                if (restoreStateOnError)
                {
                    // Make the successfully computed values into the effective state of the
                    // environment
                    RCClosure top      = _exceptionClosure;
                    RCClosure underTop = null;
                    while (top.Parent != null && top.Parent.Parent != null)
                    {
                        underTop = top;
                        top      = top.Parent;
                    }
                    if (underTop != null && top.Code.IsOperator)
                    {
                        top = underTop;
                    }
                    _state = RCBlock.Append(_state, top.Result);
                }
                _runnerUnhandled = true;
                throw exception;
            }
            // The final result is assigned by the worker in Finish ().
            RCValue result = _result;

            _result = null;
            return(result);
        }
Esempio n. 30
0
        public RCBlock GetBlock(string name)
        {
            RCBlock val = (RCBlock)Get(name);

            if (val == null)
            {
                throw new Exception("Required value " + name + " not found in block");
            }
            else
            {
                return(val);
            }
        }