public void EvalFlip(RCRunner runner, RCClosure closure, RCBlock right) { RCBlock prototype = (RCBlock)right.Get(0); string[] names = new string[prototype.Count]; RCVectorBase[] columns = new RCVectorBase[prototype.Count]; for (int i = 0; i < prototype.Count; ++i) { RCBlock name = prototype.GetName(i); RCVectorBase scalar = (RCVectorBase)name.Value; columns[i] = RCVectorBase.FromScalar(scalar.Child(0)); names[i] = name.Name; } for (int i = 1; i < right.Count; ++i) { RCBlock row = (RCBlock)right.Get(i); for (int j = 0; j < row.Count; ++j) { RCBlock name = (RCBlock)row.GetName(j); int n = Array.IndexOf(names, name.Name); if (n >= 0) { RCVectorBase scalar = (RCVectorBase)name.Value; columns[n].Write(scalar.Child(0)); } } } RCBlock result = RCBlock.Empty; for (int i = 0; i < names.Length; ++i) { result = new RCBlock(result, names[i], ":", columns[i]); } runner.Yield(closure, result); }
public void EvalDyad(RCRunner runner, RCClosure closure, RCString left, object right) { RCBlock block = (RCBlock)right; RCOperator op = RCSystem.Activator.New(left[0], block.Get("l"), block.Get("r")); runner.Yield(closure, op); }
public override TcpSendState Send(RCRunner runner, RCClosure closure, RCBlock message) { long cid = Interlocked.Increment(ref _cid); RCSymbolScalar id = new RCSymbolScalar(null, _handle); id = new RCSymbolScalar(id, cid); StringBuilder address = new StringBuilder(); // HttpVerb verb = (HttpVerb) Enum.Parse ( // typeof(HttpVerb), // ((RCSymbol) message.Get ("verb"))[0].Part (0).ToString ()); object[] resource = ((RCSymbol)message.Get("resource"))[0].ToArray(); address.Append("http://"); address.Append(_host); if (_port > 0) { address.Append(":"); address.Append(_port); } address.Append("/"); for (int i = 0; i < resource.Length; ++i) { address.Append(resource[i].ToString()); if (i < resource.Length - 1) { address.Append("/"); } } RCBlock query = (RCBlock)message.Get("query"); if (query != null) { address.Append("?"); for (int i = 0; i < query.Count; ++i) { RCBlock variable = query.GetName(i); address.Append(variable.Name); address.Append("="); address.Append(((RCString)variable.Value)[0]); if (i < query.Count - 1) { address.Append("&"); } } } // byte[] payload = _client.Encoding.GetBytes (message.Get ("body").ToString ()); Uri uri = new Uri(address.ToString()); System.Console.Out.WriteLine(address.ToString()); _client.DownloadDataAsync(uri, new RCAsyncState(runner, closure, id)); // runner.Yield (closure, new RCSymbol(id)); return(new TcpSendState(_handle, cid, message)); }
protected RCArray <T> DoAppend <T> (RCBlock right) { RCVector <T> current = (RCVector <T>)right.Get(0); RCArray <T> result = new RCArray <T> (current.Count * 3); result.Write(current.Data); for (int i = 1; i < right.Count; ++i) { current = (RCVector <T>)right.Get(i); result.Write(current.Data); } return(result); }
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); }
public void EvalEach(RCRunner runner, RCClosure closure, RCBlock left, RCBlock right) { if (right.Count == 0) { runner.Yield(closure, right); return; } long i = closure.Index - 2; if (i < right.Count) { RCBlock name = right.GetName(i); RCBlock result = new RCBlock(closure.Result, "L", ":", new RCString(name.Name)); result = new RCBlock(result, "I", ":", new RCLong(i)); result = new RCBlock(result, "R", ":", right.Get(i)); closure = new RCClosure(closure.Parent, closure.Bot, closure.Code, closure.Left, result, closure.Index); left.Eval(runner, new RCClosure(closure, closure.Bot, left, closure.Left, RCBlock.Empty, 0)); } else { runner.Yield(closure, closure.Parent.Result); } }
public void EvalUnwrap(RCRunner runner, RCClosure closure, RCBlock right) { if (right.Count != 1) { throw new Exception("monadic unwrap unwraps a block containing a single element"); } runner.Yield(closure, right.Get(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); }
public virtual void EvalHas(RCRunner runner, RCClosure closure, RCBlock left, RCString right) { RCArray <bool> result = new RCArray <bool> (); for (int i = 0; i < right.Count; ++i) { result.Write(left.Get(right[i]) != null); } runner.Yield(closure, new RCBoolean(result)); }
public void EvalGet(RCRunner runner, RCClosure closure, RCBlock left, RCSymbol right) { if (right.Count != 1) { throw new Exception("right argument may only contain a single name"); } object obj = right[0].Part(0); if (obj is long) { runner.Yield(closure, left.Get((long)obj)); } else if (obj is string) { runner.Yield(closure, left.Get((string)obj)); } else { throw new Exception("invalid symbol: " + right[0].ToString()); } }
public void Putms(RCRunner runner, RCClosure closure, RCSymbol keys, RCBlock values) { lock (_lock) { Dictionary <RCSymbolScalar, RCValue> store = GetSection(keys); for (int i = 0; i < values.Count; ++i) { store[keys[i]] = values.Get(i); } } runner.Yield(closure, keys); }
public override TcpSendState Send(RCRunner runner, RCClosure closure, RCBlock message) { string verb = (string)((RCSymbol)message.Get("verb"))[0].Part(0); if (verb.Equals("read")) { RCSymbol symbol = ((RCSymbol)message.Get("symbol")); RCLong rows = ((RCLong)message.Get("rows")); long id = Interlocked.Increment(ref _id); ReadFromFiles(runner, closure, symbol, rows); return(new TcpSendState(_handle, id, message)); } else if (verb.Equals("write")) { throw new NotImplementedException(); /* * RCCube cube = (RCCube) message.Get ("cube"); * if (cube != null) * { * long id = Interlocked.Increment (ref _id); * WriteToFiles (runner, closure, cube); * return new SendState (_handle, id, message); * } * RCBlock block = (RCBlock) message.Get ("block"); * if (block != null) * { * RCSymbol symbol = (RCSymbol) message.Get ("symbol"); * long id = Interlocked.Increment (ref _id); * WriteToFiles (runner, closure, symbol, block); * return new SendState (_handle, id, message); * } * throw new Exception ("verb:#write requires a block or a cube"); */ } else { throw new Exception("Unknown verb:" + verb); } }
public void Postw(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { if (left.Count != 1) { throw new Exception("postw can only put to one resource at a time."); } RCString body = (RCString)right.Get("body"); RCBlock head = (RCBlock)right.Get("head", RCBlock.Empty); RestAsyncState state = new RestAsyncState(runner, closure, left[0], "POST", head, body, false, Interlocked.Increment(ref _client), _timeout, _retry, true); ThreadPool.QueueUserWorkItem(state.BeginWebRequest, null); }
public void EvalGet(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { if (left.Count != 1) { throw new Exception("left argument may only contain a single name"); } RCValue result = right.Get(left[0]); if (result == null) { throw new RCException(closure, RCErrors.Name, "Unable to resolve name " + left[0]); } runner.Yield(closure, result); }
public void EvalGet(RCRunner runner, RCClosure closure, RCLong left, RCBlock right) { if (left.Count != 1) { throw new Exception("left argument may only contain a single index"); } RCValue result = right.Get(left[0]); if (result == null) { throw new RCException(closure, RCErrors.Range, "Index " + left[0] + " is out of range"); } runner.Yield(closure, result); }
protected virtual RCArray <T> CoerceBlock <T> (RCBlock right) { RCArray <T> result = new RCArray <T> (); for (int i = 0; i < right.Count; ++i) { RCVector <T> value = (RCVector <T>)right.Get(i); for (int j = 0; j < value.Count; ++j) { result.Write(value[j]); } } return(result); }
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); }
protected void ReceiveCompleted(IAsyncResult result) { int count = 0; try { count = _socket.EndReceive(result); if (count > 0) { long sid = -1; long ignore; RCBlock message = _buffer.CompleteReceive(_openState.Runner, count, _handle, sid, out ignore); RCSymbol id = (RCSymbol)message.Get("id"); // Console.Out.WriteLine ("Client receiving {0}, message:{1}", id, message); if (message != null) { _inbox.Add(id[0], message); } _socket.BeginReceive( _buffer.RecvBuffer, _buffer.Read, _buffer.RecvBuffer.Length - _buffer.Read, SocketFlags.None, new AsyncCallback(ReceiveCompleted), null); } } catch (Exception ex) { _openState.Runner.Report(_openState.Closure, ex); } finally { if (count == 0) { _socket.Close(1000); RCSystem.Log.Record(_openState.Closure, "socket", _handle, "closed", ""); } } }
public void EvalNetformat(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { RCVectorBase[] columns = new RCVectorBase[right.Count]; for (int i = 0; i < right.Count; ++i) { columns[i] = (RCVectorBase)right.Get(i); if (columns[i].Count != columns[0].Count) { throw new Exception(string.Format( "All columns must have the same count. Expected: {0}, Actual: {1}", columns[0].Count, columns[i].Count)); } } RCArray <object[]> formatParams = new RCArray <object[]> (columns[0].Count); RCArray <string> result = new RCArray <string> (columns[0].Count); for (int i = 0; i < columns[0].Count; ++i) { formatParams.Write(new object[right.Count]); for (int j = 0; j < right.Count; ++j) { formatParams[i][j] = columns[j].Child(i); RCTimeScalar?time = formatParams[i][j] as RCTimeScalar ?; if (time.HasValue) { if (time.Value.Type == RCTimeType.Timespan) { throw new Exception( "netformat does not handle Timespans, please pass a specific date and time."); } formatParams[i][j] = new DateTime(time.Value.Ticks); } } } for (int i = 0; i < formatParams.Count; ++i) { result.Write(string.Format(left[0], formatParams[i])); } runner.Yield(closure, new RCString(result)); }
public void EvalFirst(RCRunner runner, RCClosure closure, RCBlock right) { runner.Yield(closure, right.Get(0)); }
public void EvalSort(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right) { SortDirection direction = ToDir(left); string col = (string)left[0].Part(1); RCVectorBase column = (RCVectorBase)right.Get(col); // It would be nice if there was an easy way to call one operator from another. // I tried to add one but found I would have to create a weird closure for this // purpose. // So I decided to wait and see and live with the switch statement for now. RCLong rank; switch (column.TypeCode) { case 'x': rank = new RCLong(RankUtils.DoRank <byte> (direction, (RCByte)column)); break; case 'l': rank = new RCLong(RankUtils.DoRank <long> (direction, (RCLong)column)); break; case 'd': rank = new RCLong(RankUtils.DoRank <double> (direction, (RCDouble)column)); break; case 'm': rank = new RCLong(RankUtils.DoRank <decimal> (direction, (RCDecimal)column)); break; case 's': rank = new RCLong(RankUtils.DoRank <string> (direction, (RCString)column)); break; case 'b': rank = new RCLong(RankUtils.DoRank <bool> (direction, (RCBoolean)column)); break; case 'y': rank = new RCLong(RankUtils.DoRank <RCSymbolScalar> (direction, (RCSymbol)column)); break; case 't': rank = new RCLong(RankUtils.DoRank <RCTimeScalar> (direction, (RCTime)column)); break; default: throw new Exception("Type:" + column.TypeCode + " is not supported by sort"); } RCBlock result = RCBlock.Empty; for (int i = 0; i < right.Count; ++i) { RCBlock name = right.GetName(i); column = (RCVectorBase)name.Value; RCValue reordered; switch (column.TypeCode) { case 'x': reordered = ReorderColumn <byte> (rank, (RCVector <byte>)column); break; case 'l': reordered = ReorderColumn <long> (rank, (RCVector <long>)column); break; case 'd': reordered = ReorderColumn <double> (rank, (RCVector <double>)column); break; case 'm': reordered = ReorderColumn <decimal> (rank, (RCVector <decimal>)column); break; case 's': reordered = ReorderColumn <string> (rank, (RCVector <string>)column); break; case 'b': reordered = ReorderColumn <bool> (rank, (RCVector <bool>)column); break; case 'y': reordered = ReorderColumn <RCSymbolScalar> (rank, (RCVector <RCSymbolScalar>)column); break; case 't': reordered = ReorderColumn <RCTimeScalar> (rank, (RCVector <RCTimeScalar>)column); break; default: throw new Exception("Type:" + column.TypeCode + " is not supported by sort"); } result = new RCBlock(result, name.Name, ":", reordered); } runner.Yield(closure, result); }
protected void DoHttpSend(RCRunner runner, RCClosure closure, RCLong left, RCBlock right) { if (left.Count > 1) { throw new Exception( "httpsend only allows one request per call. Maybe this can change though."); } RequestInfo info; lock (_lock) { info = _contexts[(int)left[0]]; } try { RCLong status = (RCLong)right.Get("status"); RCBlock headers = (RCBlock)right.Get("headers"); RCString body = (RCString)right.Get("body"); if (status != null) { if (status[0] == 0) { info.Context.Response.StatusCode = 200; } else if (status[0] == 1) { info.Context.Response.StatusCode = 400; } else { info.Context.Response.StatusCode = (int)status[0]; } } if (headers != null) { for (int i = 0; i < headers.Count; ++i) { RCBlock header = headers.GetName(i); RCString val = (RCString)header.Value; info.Context.Response.AppendHeader(header.RawName, val[0]); } } if (body == null) { body = new RCString(""); } byte[] bytes = Encoding.UTF8.GetBytes(body[0]); byte[] buffer = new byte[1024 * 16]; MemoryStream stream = new MemoryStream(bytes); int nbytes; while ((nbytes = stream.Read(buffer, 0, buffer.Length)) > 0) { info.Context.Response.OutputStream.Write(buffer, 0, nbytes); } } catch (Exception) { info.Context.Response.StatusCode = 400; } finally { info.Context.Response.OutputStream.Close(); runner.Yield(closure, left); } }
public void EvalOption(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, _options.Get(right[0])); }
public void EvalAppend(RCRunner runner, RCClosure closure, RCBlock right) { if (right.Count == 0) { runner.Yield(closure, RCBlock.Empty); } RCValue first = right.Get(0); RCVectorBase vector = first as RCVectorBase; if (vector != null) { RCVectorBase result; switch (vector.TypeCode) { case 'x': result = new RCByte(DoAppend <byte> (right)); break; case 'l': result = new RCLong(DoAppend <long> (right)); break; case 'd': result = new RCDouble(DoAppend <double> (right)); break; case 'm': result = new RCDecimal(DoAppend <decimal> (right)); break; case 's': result = new RCString(DoAppend <string> (right)); break; case 'b': result = new RCBoolean(DoAppend <bool> (right)); break; case 'y': result = new RCSymbol(DoAppend <RCSymbolScalar> (right)); break; case 't': result = new RCTime(DoAppend <RCTimeScalar> (right)); break; default: throw new Exception("Type:" + vector.TypeCode + " is not supported by sort"); } runner.Yield(closure, result); return; } RCCube cube = first as RCCube; if (cube != null) { RCCube result = new RCCube(cube); for (int i = 1; i < right.Count; ++i) { Writer writer = new Writer(result, null, true, true, 0); writer.Write((RCCube)right.Get(i)); } runner.Yield(closure, result); return; } // Individual values are now appended to the result just like the children of // blocks. { RCBlock result = RCBlock.Empty; for (int i = 0; i < right.Count; ++i) { RCBlock top = right.GetName(i); if (top.Value is RCBlock) { RCBlock list = (RCBlock)right.Get(i); for (int j = 0; j < list.Count; ++j) { RCBlock item = list.GetName(j); result = new RCBlock(result, item.Name, ":", item.Value); } } else { result = new RCBlock(result, top.Name, ":", top.Value); } } runner.Yield(closure, result); } }