protected override bool Run() { if (input == null) { Computation input_lookup = new Lookup(task_master, source_reference, new [] { "arg" }, context); input_lookup.Notify(input_result => { if (input_result is Stringish) { input = input_result.ToString(); if (Interlocked.Decrement(ref interlock) == 0) { task_master.Slot(this); } } else { task_master.ReportOtherError(source_reference, "Input argument must be a string."); } }); if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } var frame = new MutableFrame(task_master, source_reference, context, container); for (int it = 0; it < input.Length; it++) { frame.Set(it + 1, (long)Char.ConvertToUtf32(input, it)); } result = frame; return(true); }
private void HandleTransformation(object result) { if (result is Frame) { var frame = (Frame)result; var lookup = new Lookup(task_master, source_ref, new [] { "type" }, Context.Prepend(frame, null)); lookup.Notify(type_result => { if (type_result is long) { switch ((long)type_result) { case 0: HandleSubstition(frame); return; case 1: HandleRange(frame); return; } } task_master.ReportOtherError(source_ref, "Illegal transformation specified."); }); } else { task_master.ReportOtherError(source_ref, "Non-frame in transformation list."); } }
private void HandleFrameResult(object result) { if (result is Frame) { var lookup = new Lookup(task_master, null, new[] { "value" }, ((Frame)result).Context); lookup.Notify(HandleFinalResult); } }
protected override bool Run() { if (!state) { var input_lookup = new Lookup(master, source_ref, new [] { "args" }, context); input_lookup.Notify(HandleArgs); master.Slot(input_lookup); var transformation_lookup = new Lookup(master, source_ref, new [] { "transformations" }, context); transformation_lookup.Notify(HandleTransformations); master.Slot(transformation_lookup); state = true; if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } var output_frame = new Frame(master, master.NextId(), source_ref, context, self); for (var index = 0; index < input.Length; index++) { var buffer = new StringBuilder(); for (var it = 0; it < input[index].Length; it += Char.IsSurrogatePair(input[index], it) ? 2 : 1) { var c = Char.ConvertToUtf32(input[index], it); var is_surrogate = Char.IsSurrogatePair(input[index], it); string replacement; if (single_substitutions.TryGetValue(c, out replacement)) { buffer.Append(replacement); } else { bool matched = false; foreach (var range in ranges.Values) { if (c >= range.start && c <= range.end) { var utf8 = new byte[4]; Encoding.UTF8.GetBytes(input[index], it, is_surrogate ? 2 : 1, utf8, 0); buffer.Append(String.Format(range.replacement, c, (int)input[index][it], is_surrogate ? (int)input[index][it + 1] : 0, (int)utf8[0], (int)utf8[1], (int)utf8[2], (int)utf8[3])); matched = true; break; } } if (!matched) { buffer.Append(Char.ConvertFromUtf32(c)); } } } output_frame[TaskMaster.OrdinalNameStr(index)] = new SimpleStringish(buffer.ToString()); } result = output_frame; return(true); }
protected override bool Run() { if (input == null) { var input_lookup = new Lookup(master, source_reference, new String[] { "arg" }, context); input_lookup.Notify(input_result => { if (input_result is Stringish) { input = input_result.ToString(); if (Interlocked.Decrement(ref interlock) == 0) { master.Slot(this); } } else { master.ReportOtherError(source_reference, "Input argument must be a string."); } }); master.Slot(input_lookup); var radix_lookup = new Lookup(master, source_reference, new String[] { "radix" }, context); radix_lookup.Notify(radix_result => { if (radix_result is Int64) { radix = (int)(long)radix_result; if (Interlocked.Decrement(ref interlock) == 0) { master.Slot(this); } } else { master.ReportOtherError(source_reference, "Input argument must be a string."); } }); master.Slot(radix_lookup); if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } try { result = Convert.ToInt64(input, radix); return(true); } catch (Exception e) { master.ReportOtherError(source_reference, e.Message); return(false); } }
protected override bool Run() { if (input == null) { Computation input_lookup = new Lookup(task_master, source_reference, new [] { "arg" }, context); input_lookup.Notify(input_result => { if (input_result is byte[]) { input = (byte[])input_result; if (Interlocked.Decrement(ref interlock) == 0) { task_master.Slot(this); } } else { task_master.ReportOtherError(source_reference, "Input argument must be a Bin."); } }); Computation encoding_lookup = new Lookup(task_master, source_reference, new [] { "encoding" }, context); encoding_lookup.Notify(encoding_result => { if (encoding_result is long) { var index = (long)encoding_result; if (index >= 0 && index < encodings.Length) { encoding = encodings[index]; if (Interlocked.Decrement(ref interlock) == 0) { task_master.Slot(this); return; } } } task_master.ReportOtherError(source_reference, "Invalid encoding."); }); if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } try { result = new SimpleStringish(encoding.GetString(input)); return(true); } catch (DecoderFallbackException e) { task_master.ReportOtherError(source_reference, String.Format("Cannot decode byte {0}.", e.Index)); return(false); } }
private void HandleFrameResult(object result) { var frame = result as Frame; if (frame != null) { var lookup = new Lookup(task_master, new NativeSourceReference("printer"), new[] { "value" }, frame.Context); lookup.Notify(HandleFinalResult); } else { Console.Error.WriteLine("File did not contain a frame. That should be impossible."); } }
protected override bool Run() { if (mappings.Count == 0) { interlock = categories.Count + 2; Computation input_lookup = new Lookup(master, source_reference, new [] { "arg" }, context); input_lookup.Notify(input_result => { if (input_result is Stringish) { input = input_result.ToString(); if (Interlocked.Decrement(ref interlock) == 0) { master.Slot(this); } } else { master.ReportOtherError(source_reference, "Input argument must be a string."); } }); master.Slot(input_lookup); foreach (var entry in categories) { var lookup = new Lookup(master, source_reference, new [] { entry.Value }, context); lookup.Notify(cat_result => { mappings[entry.Key] = cat_result; if (Interlocked.Decrement(ref interlock) == 0) { master.Slot(this); } }); master.Slot(lookup); } if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } var frame = new Frame(master, master.NextId(), source_reference, context, container); for (int it = 0; it < input.Length; it++) { frame[TaskMaster.OrdinalNameStr(it + 1)] = mappings[Char.GetUnicodeCategory(input[it])]; } result = frame; return(true); }
void LookupString(Frame frame, string name, ConsumeString consume) { var lookup = new Lookup(task_master, source_ref, new [] { name }, Context.Prepend(frame, null)); lookup.Notify(result => { if (result is Stringish) { var str = result.ToString(); consume(str); } else { task_master.ReportOtherError(source_ref, String.Format("Expected “{0}” to be a string. Got {1} instead.", name, Stringish.NameForType(result.GetType()))); } }); }
protected override bool Run() { if (input == null) { var input_lookup = new Lookup(task_master, source_reference, new String[] { "arg" }, context); input_lookup.Notify(input_result => { if (input_result is byte[]) { input = (byte[])input_result; if (Interlocked.Decrement(ref interlock) == 0) { task_master.Slot(this); } } else { task_master.ReportOtherError(source_reference, "Input argument must be a Bin."); } }); if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } try { using (var stream = new GZipStream(new MemoryStream(input), CompressionMode.Decompress)) using (var memory = new MemoryStream()) { byte[] buffer = new byte[4096]; int count; while ((count = stream.Read(buffer, 0, buffer.Length)) > 0) { memory.Write(buffer, 0, count); } result = memory.ToArray(); return(true); } } catch (Exception e) { task_master.ReportOtherError(source_reference, e.Message); return(false); } }
protected override bool Run() { if (input == null) { var input_lookup = new Lookup(task_master, source_ref, new [] { "arg" }, context); input_lookup.Notify(HandleArg); if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } try { var json_value = JsonValue.Parse(input); var tmpl = new Template(source_ref, context, self); tmpl["json_root"] = Dispatch(Unit.NULL, json_value); result = tmpl; return(true); } catch (Exception e) { task_master.ReportOtherError(source_ref, e.Message); return(false); } }
protected override bool Run() { if (input == null) { var input_lookup = new Lookup(task_master, source_reference, new String[] { "arg" }, context); input_lookup.Notify(input_result => { if (input_result is Stringish) { input = input_result.ToString(); if (Interlocked.Decrement(ref interlock) == 0) { task_master.Slot(this); } } else { task_master.ReportOtherError(source_reference, "Input argument must be a string."); } }); if (Interlocked.Decrement(ref interlock) > 0) { return(false); } } try { result = Convert.ToDouble(input); return(true); } catch (Exception e) { task_master.ReportOtherError(source_reference, e.Message); return(false); } }