private static Expression Load(SymbolTable symbols, Object obj, String name) { if (name.StartsWithInvariant(CtsPrefix)) { return(Expression.Constant(((IEnumerable <IGrouping <String, Type> >)obj) .Select(g => g.Key.Apply(k => symbols[k] = YacqExpression.TypeCandidate(symbols, g))) .ToArray() )); } else { var stream = (Stream)obj; switch (Path.GetExtension(name).ToLowerInvariant()) { case ".dll": #if SILVERLIGHT throw new NotImplementedException("Loading DLL is not implemented in Sliverlight environment."); #else return(Expression.Constant(new Byte[stream.Length].Apply(b => stream.Read(b, 0, b.Length)) .Let(Assembly.Load) .Apply(a => a.GetTypes() .Where(t => t.IsPublic) .ForEach(symbols.Import) ) )); #endif case ".yacb": throw new NotImplementedException("YACQ Binary code is not implemented."); default: return(new StreamReader(stream, true) .Dispose(r => YacqServices.Parse(symbols, r.ReadToEnd()))); } } }
/// <summary> /// Reduces this node to a simpler expression with additional symbol tables. /// </summary> /// <param name="symbols">The additional symbol table for reducing.</param> /// <param name="expectedType">The type which is expected as the type of reduced expression.</param> /// <returns>The reduced expression.</returns> protected override Expression ReduceImpl(SymbolTable symbols, Type expectedType) { return(this._codes.Any() ? (Expression)TypeCandidate(typeof(String)).Method(symbols, "Format", this._codes .Select(c => YacqServices.Parse(symbols, c)) .StartWith(Constant(this.Value)) ) : Constant(this.Value)); }
public void Run() { this.AddReplInterface("console", new ConsoleReplInterface()); new FileInfo("replrc.yacq").If(f => f.Exists, f => YacqServices.Parse(File.ReadLines(f.FullName).SelectMany(l => l)) .Evaluate() ); this._replInterfaces.Values.ForEach(i => i.Initialize(this)); this._replInterfaces.Values.ForEach(i => i.Run()); }
public void Run() { this.Output = ""; Observable.Start(() => { try { this.WriteOutput("Started."); var expr = YacqServices.Parse( new SymbolTable() { { DispatchTypes.Method, typeof(Object), "print", (e, s, t) => YacqExpression.Dispatch( s, DispatchTypes.Method, Expression.Constant(this), "Print", e.Left ) }, }, this.Body ); this.WriteOutput("Parsed.\nGenerated Expression:\n" + expr); var func = Expression.Lambda(expr).Compile(); this.WriteOutput("Compiled."); var ret = func.DynamicInvoke(); this.WriteOutput("Finished.\nReturned Type: " + (ret != null ? ret.GetType().Name : "null")); if (ret != null) { this.Output += "Returned Value:\n" + (ret is IEnumerable && !(ret is String) ? String.Join(", ", ((IEnumerable)ret).OfType <Object>().Select(e => e.ToString())) : ret ); } } catch (Exception ex) { this.WriteOutput(ex.ToString()); } }).Subscribe(_ => { }); }
public Object EvaluateWithoutContext(IEnumerable <Char> code, params Object[] args) { return(YacqServices.Parse(this.Symbols, code).Evaluate(null, args)); }
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Up: var previous = this.historyPos != null ? this.historyPos.Previous : this.history.Last; if (previous != null) { var lines = this.textBox.Text.Split('\n'); lines[lines.Length - 1] = ">>> " + previous.Value; this.textBox.Text = string.Join("\n", lines); this.textBox.Select(this.textBox.Text.Length, 0); this.historyPos = previous; } e.Handled = true; break; case Key.Down: if (this.historyPos != null && this.historyPos.Next != null) { this.historyPos = this.historyPos.Next; var lines = this.textBox.Text.Split('\n'); lines[lines.Length - 1] = ">>> " + this.historyPos.Value; this.textBox.Text = string.Join("\n", lines); this.textBox.Select(this.textBox.Text.Length, 0); } e.Handled = true; break; case Key.Left: case Key.Back: e.Handled = this.textBox.SelectionStart <= (this.textBox.Text .Split('\n') .Select(s => s.Length + 1) .Reverse() .Skip(1) .Sum() + ">>> ".Length); break; case Key.Enter: var code = this.textBox.Text.Split('\n').Last().Substring(">>> ".Length); if (!string.IsNullOrWhiteSpace(code)) { this.history.AddLast(code); if (this.historyPos != null && this.historyPos.Value != code) { this.historyPos = null; } this.textBox.AppendText("\r\n"); try { var exp = YacqExpression.Lambda( YacqServices.Parse(this.symbolTable, code) ); var ret = exp.Compile().DynamicInvoke(); if (ret == null) { this.textBox.AppendText("null"); } else if (ret is IEnumerable && !(ret is String)) { var sb = new StringBuilder("[ "); var data = ((IEnumerable)ret) .Cast <Object>() .Select(_ => (_ ?? "(null)").ToString()) .Take(Symbols.ReplDumpLimit + 1) .ToArray(); if (data.Any(s => s.Length > 40)) { sb.Append("\n"); sb.Append(String.Join( "\n", data.Take(Symbols.ReplDumpLimit) .Select(s => " " + s) )); sb.Append(data.Length > Symbols.ReplDumpLimit ? " (more...)\n]" : "\n]" ); } else { sb.Append(String.Join(" ", data.Take(Symbols.ReplDumpLimit))); sb.Append(data.Length > Symbols.ReplDumpLimit ? " (more...) ]" : " ]" ); } this.textBox.AppendText(sb.ToString()); } else { this.textBox.AppendText(ret.ToString()); } } catch (Exception ex) { this.textBox.AppendText(ex.GetType().Name + ":" + ex); } } this.textBox.AppendText("\r\n>>> "); this.textBox.Select(this.textBox.Text.Length, 0); e.Handled = true; break; default: if ( e.KeyboardDevice.Modifiers == ModifierKeys.None || e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.KeyboardDevice.Modifiers == ModifierKeys.Control && (e.Key == Key.V || e.Key == Key.X || e.Key == Key.Back || e.Key == Key.Delete) ) { var inputStartPos = this.textBox.Text .Split('\n') .Select(s => s.Length + 1) .Reverse() .Skip(1) .Sum() + ">>> ".Length; if (this.textBox.SelectionStart < inputStartPos) { this.textBox.Select(this.textBox.Text.Length, 0); } } break; } }