Expression GenDlrForFile(GenContext context) { EnsureTypeBuilt(context); //ConstructorInfo ctorInfo = _ctorInfo; ConstructorInfo ctorInfo = _fnType.GetConstructors()[0]; // The incoming context holds info on the containing function. // That is the one that holds the closed-over variable values. List <Expression> args = new List <Expression>(_closes.count()); for (ISeq s = RT.keys(_closes); s != null; s = s.next()) { LocalBinding lb = (LocalBinding)s.first(); if (lb.PrimitiveType != null) { args.Add(context.FnExpr.GenUnboxedLocal(context, lb)); } else { args.Add(context.FnExpr.GenLocal(context, lb)); } } return(Expression.New(ctorInfo, args)); }
public void CreateOnEmptyDictionaryReturnsEmptyMap() { Dictionary <int, string> d = new Dictionary <int, string>(); IPersistentMap m = PersistentArrayMap.create(d); Expect(m.count()).To.Equal(0); }
public void CountOnEmptyReturns0() { Dictionary <int, string> d = new Dictionary <int, string>(); IPersistentMap m = PersistentArrayMap.create(d); Expect(m.count(), EqualTo(0)); }
public void CreateOnEmptyListReturnsEmptyMap() { ArrayList a = new ArrayList(); IPersistentMap m = PersistentHashMap.create1(a); Expect(m.count()).To.Equal(0); }
public void ConsOnIPersistentMapAddsOrReplacesMany() { Dictionary <int, string> d1 = new Dictionary <int, string>(); d1[1] = "a"; d1[2] = "b"; IPersistentMap m1 = PersistentArrayMap.create(d1); Dictionary <int, string> d2 = new Dictionary <int, string>(); d2[2] = "c"; d2[3] = "d"; IPersistentMap m2 = PersistentArrayMap.create(d2); IPersistentMap m3 = m1.cons(m2); Expect(m1.count(), EqualTo(2)); Expect(m1.valAt(1), EqualTo("a")); Expect(m1.valAt(2), EqualTo("b")); Expect(m2.count(), EqualTo(2)); Expect(m2.valAt(2), EqualTo("c")); Expect(m2.valAt(3), EqualTo("d")); Expect(m3.count(), EqualTo(3)); Expect(m3.valAt(1), EqualTo("a")); Expect(m3.valAt(2), EqualTo("c")); Expect(m3.valAt(3), EqualTo("d")); }
public void CreateOnEmptyDictionaryReturnsEmptyMap() { Dictionary <int, string> d = new Dictionary <int, string>(); IPersistentMap m = PersistentTreeMap.create(d); Expect(m.count(), EqualTo(0)); }
public void CreateOnEmptyISeqReturnsEmptyMap() { object[] items = new object[] { }; ArrayList a = new ArrayList(items); ISeq s = PersistentList.create(a).seq(); IPersistentMap m = PersistentTreeMap.create(s); Expect(m.count(), EqualTo(0)); }
internal Type[] CtorTypes() { int i = !SupportsMeta ? 0 : 1; Type[] ret = new Type[_closes.count() + i]; if (SupportsMeta) { ret[0] = typeof(IPersistentMap); } for (ISeq s = RT.keys(_closes); s != null; s = s.next(), i++) { LocalBinding lb = (LocalBinding)s.first(); ret[i] = lb.PrimitiveType ?? typeof(object); } return(ret); }
public void CountOnNonEmptyReturnsCount() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); Expect(m.count(), EqualTo(2)); }
public void CreateOnISeqReturnsMap() { object[] items = new object[] { 1, "a", 2, "b" }; ArrayList a = new ArrayList(items); ISeq s = PersistentList.create(a).seq(); IPersistentMap m = PersistentHashMap.create(s); Expect(m.count()).To.Equal(2); Expect(m.valAt(1)).To.Equal("a"); Expect(m.valAt(2)).To.Equal("b"); Expect(m.containsKey(3)).To.Be.False(); }
public void CreateOnListReturnsMap() { object[] items = new object[] { 1, "a", 2, "b" }; ArrayList a = new ArrayList(items); IPersistentMap m = PersistentHashMap.create1(a); Expect(m.count(), EqualTo(2)); Expect(m.valAt(1), EqualTo("a")); Expect(m.valAt(2), EqualTo("b")); Expect(m.containsKey(3), False); }
public void GetClojureFnMappingsWorks() { IProxy ip = _obj as IProxy; IPersistentMap map = ip.__getClojureFnMappings(); Expect(map.count()).To.Equal(6); Expect(map.containsKey("m1")); Expect(map.containsKey("m2")); Expect(map.containsKey("m2s")); Expect(map.containsKey("m2v")); Expect(map.containsKey("m3")); Expect(map.containsKey("m4")); }
public void CreateOnISeqReturnsMap() { object[] items = new object[] { 1, "a", 2, "b" }; ArrayList a = new ArrayList(items); ISeq s = PersistentList.create(a).seq(); IPersistentMap m = PersistentTreeMap.create(s); Expect(m.count(), EqualTo(2)); Expect(m.valAt(1), EqualTo("a")); Expect(m.valAt(2), EqualTo("b")); Expect(m.containsKey(3), False); }
void GenerateMethod(ObjExpr objx, GenContext context) { if (Prim != null) { GeneratePrimMethod(objx, context); } TypeBuilder tb = objx.TypeBuilder; MethodBuilder mb = tb.DefineMethod(MethodName, MethodAttributes.ReuseSlot | MethodAttributes.Public | MethodAttributes.Virtual, ReturnType, ArgTypes); //Console.Write("InMd: {0} {1}(", ReturnType.Name, MethodName); //foreach (Type t in ArgTypes) // Console.Write("{0}", t.Name); //Console.WriteLine(")"); GenInterface.SetCustomAttributes(mb, _methodMeta); if (_parms != null) { for (int i = 0; i < _parms.count(); i++) { IPersistentMap meta = GenInterface.ExtractAttributes(RT.meta(_parms.nth(i))); if (meta != null && meta.count() > 0) { ParameterBuilder pb = mb.DefineParameter(i + 1, ParameterAttributes.None, ((Symbol)_parms.nth(i)).Name); GenInterface.SetCustomAttributes(pb, meta); } } } ILGen gen = new ILGen(mb.GetILGenerator()); gen.EmitLoadArg(0); for (int i = 1; i <= _argLocals.count(); i++) { gen.EmitLoadArg(i); } gen.EmitCall(_staticMethodBuilder); if (ReturnType != StaticReturnType) { gen.Emit(OpCodes.Castclass, ReturnType); } gen.Emit(OpCodes.Ret); if (IsExplicit) { tb.DefineMethodOverride(mb, _explicitMethodInfo); } }
public void CreateOnDictionaryReturnsMap() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentTreeMap.create(d); Expect(m.count(), EqualTo(2)); Expect(m.valAt(1), EqualTo("a")); Expect(m.valAt(2), EqualTo("b")); Expect(m.containsKey(3), False); }
public void CreateOnDictionaryReturnsMap() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); Expect(m.count()).To.Equal(2); Expect(m.valAt(1)).To.Equal("a"); Expect(m.valAt(2)).To.Equal("b"); Expect(m.containsKey(3)).To.Be.False(); }
public void AssocAddsOnNewKey() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m1 = PersistentArrayMap.create(d); IPersistentMap m2 = m1.assoc(3, "c"); Expect(m1.count()).To.Equal(2); Expect(m1.containsKey(3)).To.Be.False(); Expect(m2.count()).To.Equal(3); Expect(m2.valAt(3)).To.Equal("c"); }
public void AssocModifiesOnExistingKey() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m1 = PersistentArrayMap.create(d); IPersistentMap m2 = m1.assoc(2, "c"); Expect(m1.count(), EqualTo(2)); Expect(m1.valAt(2), EqualTo("b")); Expect(m2.count(), EqualTo(2)); Expect(m2.valAt(2), EqualTo("c")); }
public void AssocExModifiesOnNewKey() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m1 = PersistentArrayMap.create(d); IPersistentMap m2 = m1.assocEx(3, "c"); Expect(m1.count(), EqualTo(2)); Expect(m1.containsKey(3), False); Expect(m2.count(), EqualTo(3)); Expect(m2.valAt(3), EqualTo("c")); }
protected void SetCustomAttributes(MethodBuilder mb) { GenInterface.SetCustomAttributes(mb, MethodMeta); if (Parms != null) { for (int i = 0; i < Parms.count(); i++) { IPersistentMap meta = GenInterface.ExtractAttributes(RT.meta(Parms.nth(i))); if (meta != null && meta.count() > 0) { ParameterBuilder pb = mb.DefineParameter(i + 1, ParameterAttributes.None, ((Symbol)Parms.nth(i)).Name); GenInterface.SetCustomAttributes(pb, meta); } } } }
public void WithoutOnExistingKeyRemovesKey() { Dictionary <int, string> d = new Dictionary <int, string>(); d[3] = "a"; d[5] = "b"; d[7] = "c"; IPersistentMap m1 = PersistentArrayMap.create(d); IPersistentMap m2 = m1.without(5); Expect(m1.count(), EqualTo(3)); Expect(m1.valAt(5), EqualTo("b")); Expect(m2.count(), EqualTo(2)); Expect(m2.containsKey(5), False); }
/// <summary> /// Notify all watchers. /// </summary> public void notifyWatches(object oldval, object newval) { IPersistentMap ws = _watches; if (ws.count() > 0) { for (ISeq s = ws.seq(); s != null; s = s.next()) { IMapEntry me = (IMapEntry)s.first(); IFn fn = (IFn)me.val(); if (fn != null) { fn.invoke(me.key(), this, oldval, newval); } } } }
public void ConsOnKeyValuePairReplacesExisting() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); IPersistentMap c = m.cons(new KeyValuePair <int, string>(2, "c")); Expect(m.count(), EqualTo(2)); Expect(m.valAt(1), EqualTo("a")); Expect(m.valAt(2), EqualTo("b")); Expect(c.count(), EqualTo(2)); Expect(c.valAt(1), EqualTo("a")); Expect(c.valAt(2), EqualTo("c")); }
public void ConsOnDictionaryEntryReplacesExisting() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); IPersistentMap c = m.cons(new DictionaryEntry(2, "c")); Expect(m.count()).To.Equal(2); Expect(m.valAt(1)).To.Equal("a"); Expect(m.valAt(2)).To.Equal("b"); Expect(c.count()).To.Equal(2); Expect(c.valAt(1)).To.Equal("a"); Expect(c.valAt(2)).To.Equal("c"); }
public void ConsOnKeyValuePairAddsNew() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); IPersistentMap c = m.cons(new KeyValuePair <int, string>(3, "c")); Expect(m.count()).To.Equal(2); Expect(m.valAt(1)).To.Equal("a"); Expect(m.valAt(2)).To.Equal("b"); Expect(c.count()).To.Equal(3); Expect(c.valAt(1)).To.Equal("a"); Expect(c.valAt(2)).To.Equal("b"); Expect(c.valAt(3)).To.Equal("c"); }
public void ConsOnDictionaryEntryAddsNew() { Dictionary <int, string> d = new Dictionary <int, string>(); d[1] = "a"; d[2] = "b"; IPersistentMap m = PersistentArrayMap.create(d); IPersistentMap c = m.cons(new DictionaryEntry(3, "c")); Expect(m.count(), EqualTo(2)); Expect(m.valAt(1), EqualTo("a")); Expect(m.valAt(2), EqualTo("b")); Expect(c.count(), EqualTo(3)); Expect(c.valAt(1), EqualTo("a")); Expect(c.valAt(2), EqualTo("b")); Expect(c.valAt(3), EqualTo("c")); }
public static bool mapEquals(IPersistentMap m1, Object obj) { if (m1 == obj) { return(true); } //if(!(obj instanceof Map)) // return false; //Map m = (Map) obj; IDictionary d = obj as IDictionary; if (d == null) { return(false); } // Java had the following. // This works on other APersistentMap implementations, but not on // arbitrary dictionaries. //if (d.Count != m1.Count || d.GetHashCode() != m1.GetHashCode()) // return false; if (d.Count != m1.count()) { return(false); } for (ISeq s = m1.seq(); s != null; s = s.next()) { IMapEntry me = (IMapEntry)s.first(); bool found = d.Contains(me.key()); if (!found || !Util.equals(me.val(), d[me.key()])) { return(false); } } return(true); }
private static void DefineMethod(TypeBuilder proxyTB, IPersistentVector sig) { Symbol mname = (Symbol)sig.nth(0); Type[] paramTypes = GenClass.CreateTypeArray((ISeq)sig.nth(1)); Type retType = (Type)sig.nth(2); ISeq pmetas = (ISeq)(sig.count() >= 4 ? sig.nth(3) : null); MethodBuilder mb = proxyTB.DefineMethod(mname.Name, MethodAttributes.Abstract | MethodAttributes.Public | MethodAttributes.Virtual, retType, paramTypes); SetCustomAttributes(mb, GenInterface.ExtractAttributes(RT.meta(mname))); int i = 1; for (ISeq s = pmetas; s != null; s = s.next(), i++) { IPersistentMap meta = GenInterface.ExtractAttributes((IPersistentMap)s.first()); if (meta != null && meta.count() > 0) { ParameterBuilder pb = mb.DefineParameter(i, ParameterAttributes.None, String.Format("p_{0}", i)); GenInterface.SetCustomAttributes(pb, meta); } } }
protected void EmitValue(object value, CljILGen ilg) { bool partial = true; if (value == null) { ilg.Emit(OpCodes.Ldnull); } else if (value is String) { ilg.Emit(OpCodes.Ldstr, (String)value); } else if (value is Boolean) { ilg.EmitBoolean((Boolean)value); ilg.Emit(OpCodes.Box, typeof(bool)); } else if (value is Int32) { ilg.EmitInt((int)value); ilg.Emit(OpCodes.Box, typeof(int)); } else if (value is Int64) { ilg.EmitLong((long)value); ilg.Emit(OpCodes.Box, typeof(long)); } else if (value is Double) { ilg.EmitDouble((double)value); ilg.Emit(OpCodes.Box, typeof(double)); } else if (value is Char) { ilg.EmitChar((char)value); ilg.Emit(OpCodes.Box, typeof(char)); } else if (value is Type) { Type t = (Type)value; if (t.IsValueType) { ilg.EmitType(t); } else { //ilg.EmitString(Compiler.DestubClassName(((Type)value).FullName)); ilg.EmitString(((Type)value).FullName); ilg.EmitCall(Compiler.Method_RT_classForName); } } else if (value is Symbol) { Symbol sym = (Symbol)value; if (sym.Namespace == null) { ilg.EmitNull(); } else { ilg.EmitString(sym.Namespace); } ilg.EmitString(sym.Name); ilg.EmitCall(Compiler.Method_Symbol_intern2); } else if (value is Keyword) { Keyword keyword = (Keyword)value; if (keyword.Namespace == null) { ilg.EmitNull(); } else { ilg.EmitString(keyword.Namespace); } ilg.EmitString(keyword.Name); ilg.EmitCall(Compiler.Method_RT_keyword); } else if (value is Var) { Var var = (Var)value; ilg.EmitString(var.Namespace.Name.ToString()); ilg.EmitString(var.Symbol.Name.ToString()); ilg.EmitCall(Compiler.Method_RT_var2); } else if (value is IType) { IPersistentVector fields = (IPersistentVector)Reflector.InvokeStaticMethod(value.GetType(), "getBasis", Type.EmptyTypes); for (ISeq s = RT.seq(fields); s != null; s = s.next()) { Symbol field = (Symbol)s.first(); Type k = Compiler.TagType(Compiler.TagOf(field)); object val = Reflector.GetInstanceFieldOrProperty(value, field.Name); EmitValue(val, ilg); if (k.IsPrimitive) { ilg.Emit(OpCodes.Castclass, k); } } ConstructorInfo cinfo = value.GetType().GetConstructors()[0]; ilg.EmitNew(cinfo); } else if (value is IRecord) { //MethodInfo[] minfos = value.GetType().GetMethods(BindingFlags.Static | BindingFlags.Public); EmitValue(PersistentArrayMap.create((IDictionary)value), ilg); MethodInfo createMI = value.GetType().GetMethod("create", BindingFlags.Static | BindingFlags.Public, null, CallingConventions.Standard, new Type[] { typeof(IPersistentMap) }, null); ilg.EmitCall(createMI); } else if (value is IPersistentMap) { IPersistentMap map = (IPersistentMap)value; List <object> entries = new List <object>(map.count() * 2); foreach (IMapEntry entry in map) { entries.Add(entry.key()); entries.Add(entry.val()); } EmitListAsObjectArray(entries, ilg); ilg.EmitCall(Compiler.Method_RT_map); } else if (value is IPersistentVector) { EmitListAsObjectArray(value, ilg); ilg.EmitCall(Compiler.Method_RT_vector); } else if (value is PersistentHashSet) { ISeq vs = RT.seq(value); if (vs == null) { ilg.EmitFieldGet(Compiler.Method_PersistentHashSet_EMPTY); } else { EmitListAsObjectArray(vs, ilg); ilg.EmitCall(Compiler.Method_PersistentHashSet_create); } } else if (value is ISeq || value is IPersistentList) { EmitListAsObjectArray(value, ilg); ilg.EmitCall(Compiler.Method_PersistentList_create); } else if (value is Regex) { ilg.EmitString(((Regex)value).ToString()); ilg.EmitNew(Compiler.Ctor_Regex_1); } else { string cs = null; try { cs = RT.printString(value); } catch (Exception) { throw new InvalidOperationException(String.Format("Can't embed object in code, maybe print-dup not defined: {0}", value)); } if (cs.Length == 0) { throw new InvalidOperationException(String.Format("Can't embed unreadable object in code: " + value)); } if (cs.StartsWith("#<")) { throw new InvalidOperationException(String.Format("Can't embed unreadable object in code: " + cs)); } ilg.EmitString(cs); ilg.EmitCall(Compiler.Method_RT_readString); partial = false; } if (partial) { if (value is IObj && RT.count(((IObj)value).meta()) > 0) { ilg.Emit(OpCodes.Castclass, typeof(IObj)); Object m = ((IObj)value).meta(); EmitValue(Compiler.ElideMeta(m), ilg); ilg.Emit(OpCodes.Castclass, typeof(IPersistentMap)); ilg.Emit(OpCodes.Callvirt, Compiler.Method_IObj_withMeta); } } }
private static Expression GenerateMapExpr(IPersistentMap m) { Expression[] args = new Expression[m.count() * 2]; int i = 0; for ( ISeq s = RT.seq(m); s != null; s = s.next(), i+=2) { IMapEntry me = (IMapEntry)s.first(); args[i] = MaybeBox(Generate(me.key())); args[i + 1] = MaybeBox(Generate(me.val())); } Expression argArray = Expression.NewArrayInit(typeof(object), args); Expression ret = Expression.Call(Method_RT_map,argArray); ret = OptionallyGenerateMetaInit(m,ret); return ret; }
public Expr Parse(ParserContext pcon, object form) { // (def x) or (def x initexpr) or (def x "docstring" initexpr) string docstring = null; if (RT.count(form) == 4 && (RT.third(form) is String)) { docstring = (String)RT.third(form); form = RT.list(RT.first(form), RT.second(form), RT.fourth(form)); } if (RT.count(form) > 3) { throw new ParseException("Too many arguments to def"); } if (RT.count(form) < 2) { throw new ParseException("Too few arguments to def"); } Symbol sym = RT.second(form) as Symbol; if (sym == null) { throw new ParseException("First argument to def must be a Symbol."); } //Console.WriteLine("Def {0}", sym.Name); Var v = Compiler.LookupVar(sym, true); if (v == null) { throw new ParseException("Can't refer to qualified var that doesn't exist"); } if (!v.Namespace.Equals(Compiler.CurrentNamespace)) { if (sym.Namespace == null) { v = Compiler.CurrentNamespace.intern(sym); Compiler.RegisterVar(v); } //throw new Exception(string.Format("Name conflict, can't def {0} because namespace: {1} refers to: {2}", // sym, Compiler.CurrentNamespace.Name, v)); else { throw new ParseException("Can't create defs outside of current namespace"); } } IPersistentMap mm = sym.meta(); bool isDynamic = RT.booleanCast(RT.get(mm, Compiler.DynamicKeyword)); if (isDynamic) { v.setDynamic(); } if (!isDynamic && sym.Name.StartsWith("*") && sym.Name.EndsWith("*") && sym.Name.Length > 2) { RT.errPrintWriter().WriteLine("Warning: {0} not declared dynamic and thus is not dynamically rebindable, " + "but its name suggests otherwise. Please either indicate ^:dynamic {0} or change the name. ({1}:{2}\n", sym, Compiler.SourcePathVar.get(), Compiler.LineVar.get()); } if (RT.booleanCast(RT.get(mm, Compiler.ArglistsKeyword))) { IPersistentMap vm = v.meta(); //vm = (IPersistentMap)RT.assoc(vm, Compiler.STATIC_KEY, true); // drop quote vm = (IPersistentMap)RT.assoc(vm, Compiler.ArglistsKeyword, RT.second(mm.valAt(Compiler.ArglistsKeyword))); v.setMeta(vm); } Object source_path = Compiler.SourcePathVar.get(); source_path = source_path ?? "NO_SOURCE_FILE"; mm = (IPersistentMap)RT.assoc(mm, RT.LineKey, Compiler.LineVar.get()) .assoc(RT.ColumnKey, Compiler.ColumnVar.get()) .assoc(RT.FileKey, source_path); //.assoc(RT.SOURCE_SPAN_KEY,Compiler.SOURCE_SPAN.deref()); if (docstring != null) { mm = (IPersistentMap)RT.assoc(mm, RT.DocKey, docstring); } // Following comment in JVM version //mm = mm.without(RT.DOC_KEY) // .without(Keyword.intern(null, "arglists")) // .without(RT.FILE_KEY) // .without(RT.LINE_KEY) // .without(RT.COLUMN_KEY) // .without(Keyword.intern(null, "ns")) // .without(Keyword.intern(null, "name")) // .without(Keyword.intern(null, "added")) // .without(Keyword.intern(null, "static")); mm = (IPersistentMap)Compiler.ElideMeta(mm); Expr meta = mm == null || mm.count() == 0 ? null : Compiler.Analyze(pcon.EvalOrExpr(), mm); Expr init = Compiler.Analyze(pcon.EvalOrExpr(), RT.third(form), v.Symbol.Name); bool initProvided = RT.count(form) == 3; return(new DefExpr( (string)Compiler.SourceVar.deref(), Compiler.LineVarDeref(), Compiler.ColumnVarDeref(), v, init, meta, initProvided, isDynamic)); }