/// <inheritdoc/> /// <summary> /// See <see cref="IContextObject.Retrieve{T}(string)"/> /// </summary> public T Retrieve <T>(string identifier) { if (NamedStorage.TryGetValue(identifier, out object value)) { return((T)value); } return(default(T)); }
/// <inheritdoc/> /// <summary> /// See <see cref="IContextObject.TryRetrieve{T}(string, out T)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="identifier"></param> /// <param name="value"></param> /// <returns></returns> public bool TryRetrieve <T>(string identifier, out T value) { if (NamedStorage.TryGetValue(identifier, out object val)) { value = (T)val; return(true); } value = default(T); return(false); }
public bool TryGetVariable(string name, out NamedStorage variable) { // Do we have it? if (Variables.TryGetValue(name, out variable)) { return(true); } // Search the parent scope: if (Parent != null) { return(Parent.TryGetVariable(name, out variable)); } // Not found: return(false); }
static void Main_(string[] args) { string dirname = "J:/Users/Дмитрий/Documents/testdb"; if (Directory.Exists(dirname)) foreach (var file in Directory.EnumerateFiles(dirname)) File.Delete(file); Directory.CreateDirectory(dirname); using (var bdata = new NamedStorage<string>(dirname)) { var sw = new Stopwatch(); sw.Start(); for (int i = 1000000; i-- > 0; ) { var t = i.ToString(); bdata.Add(t, t); } sw.Stop(); Console.WriteLine(sw.Elapsed); sw.Restart(); foreach (var o in bdata) { Console.WriteLine(o.Value); } sw.Stop(); Console.WriteLine(sw.Elapsed); } }
internal void Add(NamedStorage variable) { Variables.Add(variable.Name, variable); }
public object Eval(SExpr sexpr, ExternEvaluate customEval) { if (customEval == null) { customEval = defaultEval; } sexpr.ThrowIfError(); if (sexpr.Kind == SExprKind.ScopedIdentifier) { // Try to resolve the identifier: var identExpr = (ScopedIdentifierExpr)sexpr; NamedStorage variable = TryResolve(identExpr); if (variable == null) { throw new Exception("Cannot find variable named '{0}' in scope".F(identExpr.Name)); } return(variable.Value); } else if (sexpr.Kind == SExprKind.Invocation) { return(Invoke((InvocationExpr)sexpr)); } else if (sexpr.Kind == SExprKind.List) { var le = (ListExpr)sexpr; var items = new object[le.Count]; for (int i = 0; i < items.Length; ++i) { items[i] = customEval(this, le[i], customEval); } return(items); } else if (sexpr.Kind == SExprKind.Quote) { return(sexpr); } else if (sexpr.Kind == SExprKind.Integer) { return(((IntegerExpr)sexpr).Value); } else if (sexpr.Kind == SExprKind.Boolean) { return(((BooleanExpr)sexpr).Value); } else if (sexpr.Kind == SExprKind.Null) { return(null); } else if (sexpr.Kind == SExprKind.String) { return(sexpr.StartToken.Text); } else if (sexpr.Kind == SExprKind.Decimal) { return(((DecimalExpr)sexpr).Value); } else if (sexpr.Kind == SExprKind.Double) { return(((DoubleExpr)sexpr).Value); } else if (sexpr.Kind == SExprKind.Float) { return(((FloatExpr)sexpr).Value); } throw new Exception("Unknown expression kind: '{0}'".F(sexpr.Kind)); }
public void AddGlobal(NamedStorage variable) { globalScope.Add(variable); }
/// <inheritdoc/> /// <summary> /// See <see cref="IContextObject.Store(string, object)"/> /// </summary> public void Store(string name, object obj) { //Replace the old object with the new if an old object exists, or add a new object NamedStorage.AddOrUpdate(name, obj, (n, existing) => obj); }