public void EvalDiff(RCRunner runner, RCClosure closure, RCString left, RCString right) { diff_match_patch dmp = new diff_match_patch(); List <Diff> diffs = dmp.diff_main(left[0], right[0], true); List <Patch> patch = dmp.patch_make(diffs); RCCube result = new RCCube(new RCArray <string> ()); for (int i = 0; i < patch.Count; ++i) { for (int j = 0; j < patch[i].diffs.Count; ++j) { string operation = patch[i].diffs[j].operation.ToString(); string text = patch[i].diffs[j].text; result.WriteCell("op", null, operation); result.WriteCell("text", null, text); result.Axis.Write(null); } } runner.Yield(closure, result); }
public void EvalSavebin(RCRunner runner, RCClosure closure, RCString left, RCByte right) { // BRIAN READ THIS WHEN YOU GET BACK HERE. // Should not be doing sync io like this. // The least I can do is use a thread pool thread. try { File.WriteAllBytes(left[0], right.ToArray()); } catch (UnauthorizedAccessException ex) { throw new RCException(closure, RCErrors.Access, ex.Message); } catch (FileNotFoundException ex) { throw new RCException(closure, RCErrors.File, ex.Message); } RCSystem.Log.Record(closure, "save", Interlocked.Increment(ref _handle), left[0], right); runner.Yield(closure, new RCString(left[0])); }
public void Getw(RCRunner runner, RCClosure closure, RCString right) { if (right.Count != 1) { throw new Exception("getw can only get from one resource at a time."); } RestAsyncState state = new RestAsyncState(runner, closure, right[0], "GET", RCBlock.Empty, new RCString(), false, Interlocked.Increment(ref _client), _timeout, _retry, true); ThreadPool.QueueUserWorkItem(state.BeginWebRequest, state); }
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)); }
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); }
public void WriteLineToInput(object obj) { RCAsyncState state = (RCAsyncState)obj; RCString right = (RCString)state.Other; try { StringBuilder builder = new StringBuilder(); for (int i = 0; i < right.Count; ++i) { builder.AppendLine(right[i]); } string text = builder.ToString(); byte[] message = Encoding.UTF8.GetBytes(text); lock (this) { if (_outputDone || _errorDone || _exited) { Exception ex = new Exception( "Cannot write to standard input, process has already exited or is in the process of exiting."); state.Runner.Finish(state.Closure, ex, 1); } _process.StandardInput.BaseStream.BeginWrite(message, 0, message.Length, EndWrite, state); } // Using right instead of text here ensures that the output is consistent // with the usual formatting procedure. // example single lines will appear on the header line not their own line. // for purposes of writing to stdin it is imperative that every line ends // cleanly. RCSystem.Log.Record(state.Closure, "exec", Handle, "writex", right); } catch (Exception ex) { state.Runner.Report(state.Closure, ex); } }
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 EvalHttpGet(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { // You can send the same request to multiple sites. // I just had an idea, you can use method to propogate data to // other nodes, then consistency becomes the responsiblity of the // process that inserts or produces the data. // It's a sort-of/maybe idea. string query = ToQueryString(right); for (int i = 0; i < left.Count; ++i) { // Why doesn't this follow the same Begin/End idea as every other API? // Is there some other lower level thing I should use? WebClient client = new WebClient(); long handle = Interlocked.Increment(ref _client); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); Uri uri = new Uri(left[i] + query); RCSystem.Log.Record(closure, "httpc", handle, "get", new RCString(left[i])); client.DownloadStringAsync(uri, new RCAsyncState(runner, closure, handle)); } }
public void EvalSwitch(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { Picker <string> picker = delegate(string val, out bool eval) { RCBlock variable = right.GetName(val); RCValue code; // This behavior is sketchy and should be reevaluated - this should be an // exception if (variable == null) { code = RCBlock.Empty; eval = true; } else { code = variable.Value; eval = !variable.Evaluator.Pass; } return(code); }; DoSwitch <string> (runner, closure, left, right, picker); }
public void EvalWritef(RCRunner runner, RCClosure closure, RCLong left, RCString right) { if (left.Count != 1) { throw new Exception("writef requires exactly one handle"); } lock (_lock) { FileState f; if (!_filesByHandle.TryGetValue(left[0], out f)) { throw new Exception("Bad file handle: " + left[0]); } StringBuilder builder = new StringBuilder(); for (int i = 0; i < right.Count; ++i) { builder.Append(right[i]); } byte[] message = Encoding.UTF8.GetBytes(builder.ToString()); RCAsyncState state = new RCAsyncState(runner, closure, left); f._w.BaseStream.BeginWrite(message, 0, message.Length, EndWrite, state); } }
public void EvalSubstring(RCRunner runner, RCClosure closure, RCLong left, RCString right) { RCArray <string> result = new RCArray <string> (); if (left.Count == 1) { int start = (int)left[0]; for (int i = 0; i < right.Count; ++i) { result.Write(right[i].Substring(start)); } } else if (left.Count == 2) { int start = (int)left[0]; int length = (int)left[1]; for (int i = 0; i < right.Count; ++i) { result.Write(right[i].Substring(start, length)); } } runner.Yield(closure, new RCString(result)); }
public ChildProcess(Exec module, long handle, RCAsyncState state, bool yieldWhenDone) { Handle = handle; Module = module; _state = state; _yieldOnExit = yieldWhenDone; RCString command = (RCString)_state.Other; // It seems that this is the one api in the entire bcl that does // not offer some way to pass custom arguments asyncly. string line = command[0].TrimStart(' '); int split = line.IndexOf(' '); if (split >= 0) { _program = line.Substring(0, split); _arguments = line.Substring(split + 1); } else { _program = line; _arguments = ""; } _process = new Process(); _process.StartInfo = new ProcessStartInfo(_program, _arguments); _process.EnableRaisingEvents = true; _process.StartInfo.CreateNoWindow = true; _process.StartInfo.UseShellExecute = false; _process.StartInfo.ErrorDialog = false; _process.StartInfo.RedirectStandardOutput = true; _process.OutputDataReceived += HandleProcessOutputDataReceived; _process.ErrorDataReceived += HandleProcessErrorDataReceived; _process.StartInfo.RedirectStandardError = true; _process.StartInfo.RedirectStandardInput = true; _process.Exited += process_Exited; _timer = new Timer(TimerCallback, null, 200, Timeout.Infinite); }
public void EvalRepeat(RCRunner runner, RCClosure closure, RCLong left, RCString right) { runner.Yield(closure, new RCString(DoRepeat <string> (left, right))); }
public void EvalCount(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCLong(right.Count)); }
public void EvalIsolate(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { DoIsolate(runner, closure, left.ToArray(), right); }
public void EvalCompile(RCRunner runner, RCClosure closure, RCString right) { string code = right[0]; CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerParameters parameters = new CompilerParameters(); Uri codebase = new Uri(Assembly.GetExecutingAssembly().CodeBase); DirectoryInfo dir = new FileInfo(codebase.LocalPath).Directory; parameters.ReferencedAssemblies.Add(dir.FullName + "/RCL.Kernel.dll"); parameters.GenerateInMemory = true; parameters.GenerateExecutable = false; CompilerResults results = null; try { RCSystem.Log.Record(closure, "compile", 0, "code", code); results = provider.CompileAssemblyFromSource(parameters, code); } catch (Exception) { throw; } finally { if (results != null) { for (int i = 0; i < results.Errors.Count; ++i) { CompilerError error = results.Errors[i]; Console.Out.WriteLine(error.ToString()); RCSystem.Log.Record(closure, "compile", 0, "error", error.ToString()); /* * error.Column; * error.ErrorNumber; * error.ErrorText; * error.FileName; * error.IsWarning; * error.Line; */ } } } if (results.Errors.Count > 0) { throw new Exception("compilation failed, show compile:error for details"); } Type[] types = results.CompiledAssembly.GetTypes(); RCArray <string> modules = new RCArray <string> (); RCBlock result = RCBlock.Empty; for (int i = 0; i < types.Length; ++i) { bool isModule; RCBlock typeVerbs = RCSystem.Activator.CreateVerbTable(types[i], out isModule); result = new RCBlock(result, types[i].Name, ":", typeVerbs); if (isModule) { modules.Write(types[i].Name); RCBot bot = runner.GetBot(closure.Bot); bot.PutModule(types[i]); } } runner.Yield(closure, result); }
public void EvalShuffle(RCRunner runner, RCClosure closure, RCLong left, RCString right) { runner.Yield(closure, new RCString(DoShuffle <string> (new Random((int)left[0]), right))); }
public void EvalSort(RCRunner runner, RCClosure closure, RCSymbol left, RCString right) { runner.Yield(closure, new RCString(DoSort <string> (ToDir(left), right))); }
public void EvalSort(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCString(DoSort <string> (SortDirection.asc, right))); }
public void EvalExcept(RCRunner runner, RCClosure closure, RCString left, RCString right) { runner.Yield(closure, new RCString(DoExcept <string> (left, right))); }
public void EvalNextDayOfWeek(RCRunner runner, RCClosure closure, RCTime left, RCString right) { if (right.Count != 1) { throw new Exception("Only one day of week allowed"); } RCArray <RCTimeScalar> result = new RCArray <RCTimeScalar> (left.Count); DayOfWeek dayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), right[0], ignoreCase: true); for (int i = 0; i < left.Count; ++i) { DateTime date = new DateTime(left[i].Ticks); int daysUntil = dayOfWeek - date.DayOfWeek; DateTime targetDate = date; if (daysUntil < 0) { daysUntil += 7; } targetDate = date.AddDays(daysUntil); result.Write(new RCTimeScalar(targetDate, RCTimeType.Date)); } runner.Yield(closure, new RCTime(result)); }
public void EvalPop(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCString(DoPop <string> (right.Data))); }
public void EvalOperator(RCRunner runner, RCClosure closure, RCString left, RCString right) { runner.Yield(closure, DoFind <string> (left, right)); }
public void EvalUnique(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCString(DoUnique <string> (right))); }
public void EvalLast(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCString(right[right.Count - 1])); }
public void EvalRank(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCLong(RankUtils.DoRank <string> (SortDirection.asc, right))); }
public void EvalFirst(RCRunner runner, RCClosure closure, RCString right) { runner.Yield(closure, new RCString(right[0])); }
public void EvalRank(RCRunner runner, RCClosure closure, RCSymbol left, RCString right) { runner.Yield(closure, new RCLong(RankUtils.DoRank <string> (Sort.ToDir(left), right))); }
public void EvalUnion(RCRunner runner, RCClosure closure, RCString left, RCString right) { runner.Yield(closure, new RCString(DoUnion <string> (left, right))); }
public void EvalRange(RCRunner runner, RCClosure closure, RCLong left, RCString right) { runner.Yield(closure, DoRange <string> (left, right)); }