Inheritance: AFn, IObj, Named, IComparable, ISerializable
Beispiel #1
0
 /// <summary>
 /// Construct a namespace with a given name.
 /// </summary>
 /// <param name="name">The name.</param>
 Namespace(Symbol name)
     : base(name.meta())
 {
     _name = name;
     _mappings.Set(RT.DEFAULT_IMPORTS);
     _aliases.Set(RT.map());
 }
        public Delegate GenerateTypedDelegate(Type delegateType, Symbol optName, IPersistentVector argList, ISeq body)
        {
            ScriptSource scriptSource = Engine.CreateScriptSourceFromString("<internal>");

            LambdaExpression ast = Generator.GenerateTypedDelegateExpression(GetLanguageContext(), delegateType, optName, argList, body);
            return ast.Compile();

            //ast = new GlobalLookupRewriter().RewriteLambda(ast);  -- doesn't work unless no args
            //ScriptCode code = new ScriptCode(ast, GetSourceUnit(scriptSource));
            //return code;
        }
Beispiel #3
0
        public static Keyword intern(Symbol sym)
        {
            Keyword k = null;
            WeakReference existingRef = _symKeyMap.Get(sym);
            if (existingRef == null)
            {
                if (sym.meta() != null)
                    sym = (Symbol)sym.withMeta(null);
                k = new Keyword(sym);

                WeakReference wr = new WeakReference(k);
                wr.Target = k;
                existingRef = _symKeyMap.PutIfAbsent(sym, wr);
            }
            if (existingRef == null)
                return k;
            Keyword existingk = (Keyword)existingRef.Target;
            if (existingk != null)
                return existingk;
            // entry died in the interim, do over
            // let's not get confused, remove it.  (else infinite loop).
            _symKeyMap.Remove(sym);
            return intern(sym);
        }
Beispiel #4
0
 // TODO: we have duplicate code below.
 public static Symbol resolveSymbol(Symbol sym)
 {
     //already qualified or classname?
     if (sym.Name.IndexOf('.') > 0)
         return sym;
     if (sym.Namespace != null)
     {
         Namespace ns = namespaceFor(sym);
         if (ns == null || ns.Name.Name == sym.Namespace)
             return sym;
         return Symbol.create(ns.Name.Name, sym.Name);
     }
     Object o = CurrentNamespace.GetMapping(sym);
     if (o == null)
         return Symbol.intern(CurrentNamespace.Name.Name, sym.Name);
     else if (o is Type)
         return Symbol.intern(null, Util.NameForType((Type)o));
     else if (o is Var)
     {
         Var v = (Var)o;
         return Symbol.create(v.Namespace.Name.Name, v.Symbol.Name);
     }
     return null;
 }
Beispiel #5
0
 internal static bool NamesStaticMember(Symbol sym)
 {
     return sym.Namespace != null && NamespaceFor(sym) == null;
 }
Beispiel #6
0
 /// <summary>
 /// Construct a var in a given namespace with a given name and root value.
 /// </summary>
 /// <param name="ns">The namespace.</param>
 /// <param name="sym">The var.</param>
 /// <param name="root">The root value.</param>
 Var(Namespace ns, Symbol sym, object root)
     : this(ns, sym)
 {
     _root = root;
     ++_rev;
 }
Beispiel #7
0
 public static object Resolve(Symbol symbol, bool allowPrivate)
 {
     return ResolveIn(CurrentNamespace, symbol, allowPrivate);
 }
            static object ReadRecord(object form, Symbol recordName, object opts, object pendingForms)
            {
                bool readeval = RT.booleanCast(RT.ReadEvalVar.deref());
                if (!readeval)
                    throw new InvalidOperationException("Record construction syntax can only be used when *read-eval* == true ");

                Type recordType = RT.classForNameE(recordName.ToString());

                IPersistentVector recordEntries;
                IPersistentMap vals;

                object ret = null;
                ConstructorInfo[] allCtors = recordType.GetConstructors();

                if ((recordEntries = form as IPersistentVector) != null)
                {
                    // shortForm
                    bool ctorFound = false;
                    foreach (ConstructorInfo cinfo in allCtors)
                        if (cinfo.GetParameters().Length == recordEntries.count())
                            ctorFound = true;

                    if (!ctorFound)
                        throw new ArgumentException(String.Format("Unexpected number of constructor arguments to {0}: got {1}", recordType.ToString(), recordEntries.count()));

                    ret = Reflector.InvokeConstructor(recordType, RT.toArray(recordEntries));
                }
                else if ((vals = form as IPersistentMap) != null)
                {
                    for (ISeq s = RT.keys(vals); s != null; s = s.next())
                    {
                        if (!(s.first() is Keyword))
                            throw new ArgumentException(String.Format("Unreadable defrecord form: key must be of type clojure.lang.Keyword, got {0}", s.first().ToString()));
                    }

                    ret = Reflector.InvokeStaticMethod(recordType, "create", new Object[] { vals });
                }
                else
                {
                    throw new ArgumentException("Unreadable constructor form starting with \"#" + recordName + "\"");
                }

                return ret;
            }
Beispiel #9
0
        private static object ResolveIn(Namespace n, Symbol symbol, bool allowPrivate)
        {
            // note: ns-qualified vars must already exist
            if (symbol.Namespace != null)
            {
                Namespace ns = NamespaceFor(n, symbol);
                if (ns == null)
                    throw new Exception("No such namespace: " + symbol.Namespace);

                Var v = ns.FindInternedVar(Symbol.create(symbol.Name));
                if (v == null)
                    throw new Exception("No such var: " + symbol);
                else if (v.Namespace != CurrentNamespace && !v.IsPublic && !allowPrivate)
                    throw new InvalidOperationException(string.Format("var: {0} is not public", symbol));
                return v;
            }
            else if (symbol.Name.IndexOf('.') > 0 || symbol.Name[0] == '[')
                return RT.classForName(symbol.Name);
            else if (symbol.Equals(NS))
                return RT.NS_VAR;
            else if (symbol.Equals(IN_NS))
                return RT.IN_NS_VAR;
            else
            {
                object o = n.GetMapping(symbol);
                if (o == null)
                {
                    if (RT.booleanCast(RT.ALLOW_UNRESOLVED_VARS.deref()))
                        return symbol;
                    else
                        throw new Exception(string.Format("Unable to resolve symbol: {0} in this context", symbol));
                }
                return o;
            }
        }
Beispiel #10
0
        /// <summary>
        /// Find the <see cref="Var">Var</see> mapped to a <see cref="Symbol">Symbol</see>.
        /// </summary>
        /// <param name="sym">The symbol to look up.</param>
        /// <returns>The mapped var.</returns>
        public Var FindInternedVar(Symbol sym)
        {
            Var v = Mappings.valAt(sym) as Var;

            return((v != null && v.Namespace == this) ? v : null);
        }
Beispiel #11
0
 public DeprecatedWrappingReader(Symbol sym, string macro)
 {
     _sym = sym;
     _macro = macro;
 }
Beispiel #12
0
 /// <summary>
 /// Add a <see cref="Symbol">Symbol</see> to <see cref="Var">Var</see> reference.
 /// </summary>
 /// <param name="sym"></param>
 /// <param name="var"></param>
 /// <returns></returns>
 public Var refer(Symbol sym, Var var)
 {
     return((Var)reference(sym, var));
 }
Beispiel #13
0
 /// <summary>
 /// Get the value mapped to a symbol.
 /// </summary>
 /// <param name="name">The symbol to look up.</param>
 /// <returns>The mapped value.</returns>
 public object GetMapping(Symbol name)
 {
     return(Mappings.valAt(name));
 }
Beispiel #14
0
        /// <summary>
        /// Map a symbol to a Type (import) using the type name for the symbol name.
        /// </summary>
        /// <param name="t">The type to associate with the symbol</param>
        /// <returns>The Type.</returns>
        /// <remarks>Named importClass instead of ImportType for core.clj compatibility.</remarks>
        public Type importClass(Type t)
        {
            string n = t.Name;

            return(importClass(Symbol.intern(n), t));
        }
Beispiel #15
0
 /// <summary>
 /// Map a symbol to a Type (import).
 /// </summary>
 /// <param name="sym">The symbol to associate with a Type.</param>
 /// <param name="t">The type to associate with the symbol.</param>
 /// <returns>The Type.</returns>
 /// <remarks>Named importClass instead of ImportType for core.clj compatibility.</remarks>
 public Type importClass(Symbol sym, Type t)
 {
     return((Type)reference(sym, t));
 }
Beispiel #16
0
 /// <summary>
 /// Find the namespace with a given name.
 /// </summary>
 /// <param name="name">The name of the namespace to find.</param>
 /// <returns>The namespace with the given name, or <value>null</value> if no such namespace exists.</returns>
 public static Namespace find(Symbol name)
 {
     return(_namespaces.Get(name));
 }
Beispiel #17
0
        internal static LocalBinding RegisterLocal(Symbol sym, Symbol tag, Expr init)
        {
            int num = GetAndIncLocalNum();

            LocalBinding b = new LocalBinding(num,sym, tag, init);

            IPersistentMap localsMap = (IPersistentMap)LOCAL_ENV.deref();
            LOCAL_ENV.set(RT.assoc(localsMap,b.Symbol, b));
            FnMethod method = (FnMethod)METHODS.deref();
            method.Locals = (IPersistentMap)RT.assoc(method.Locals,b, b);
            method.IndexLocals = (IPersistentMap)RT.assoc(method.IndexLocals, num, b);
            return b;
        }
Beispiel #18
0
 /// <summary>
 /// Find the <see cref="Namespace">Namespace</see> aliased by a <see cref="Symbol">Symbol</see>.
 /// </summary>
 /// <param name="alias">The symbol alias.</param>
 /// <returns>The aliased namespace</returns>
 public Namespace LookupAlias(Symbol alias)
 {
     return((Namespace)Aliases.valAt(alias));
 }
Beispiel #19
0
            static object ReadRecord(PushbackTextReader r, Symbol recordName)
            {
                Type recordType = RT.classForName(recordName.ToString());

                char endch;
                bool shortForm = true;

                int ch = r.Read();

                // flush whitespace
                //while (isWhitespace(ch))
                //    ch = r.Read();

                // A defrecord ctor can take two forms.  Check for map->R version first.
                if (ch == '{')
                {
                    endch = '}';
                    shortForm = false;
                }
                else if (ch == '[')
                    endch = ']';
                else
                    throw new ArgumentException(String.Format("Unreadable constructor form starting with \"#{0}{1}\"", recordName, (char)ch));

                object[] recordEntries = ReadDelimitedList(endch, r, true).ToArray();
                object ret = null;
                ConstructorInfo[] allCtors = recordType.GetConstructors();

                if (shortForm)
                {
                    bool ctorFound = false;
                    foreach ( ConstructorInfo cinfo in allCtors )
                        if ( cinfo.GetParameters().Length == recordEntries.Length )
                            ctorFound = true;

                    if ( ! ctorFound )
                        throw new ArgumentException(String.Format("Unexpected number of constructor arguments to {0}: got {1}", recordType.ToString(), recordEntries.Length));

                    ret = Reflector.InvokeConstructor(recordType,recordEntries);
                }
                else
                {
                    IPersistentMap vals = RT.map(recordEntries);
                    for (ISeq s = RT.keys(vals); s != null; s = s.next())
                    {
                        if (!(s.first() is Keyword))
                            throw new ArgumentException(String.Format("Unreadable defrecord form: key must be of type clojure.lang.Keyword, got {0}", s.first().ToString()));
                    }

                    ret = Reflector.InvokeStaticMethod(recordType, "create", new Object[] { vals });
                }

                return ret;
            }
Beispiel #20
0
        public static Var intern(Symbol nsName, Symbol sym)
        {
            Namespace ns = Namespace.findOrCreate(nsName);

            return(intern(ns, sym));
        }
Beispiel #21
0
 public static Var intern(Namespace ns, Symbol sym, object root)
 {
     return(intern(ns, sym, root, true));
 }
Beispiel #22
0
 public static Namespace namespaceFor(Symbol sym)
 {
     return namespaceFor(CurrentNamespace, sym);
 }
 static object ReadTagged(object o, Symbol tag, object opts, object pendingForms)
 {
     ILookup dataReaders = (ILookup)RT.DataReadersVar.deref();
     IFn dataReader = (IFn)RT.get(dataReaders, tag);
     if (dataReader == null)
     {
         dataReaders = (ILookup)RT.DefaultDataReadersVar.deref();
         dataReader = (IFn)RT.get(dataReaders, tag);
         if (dataReader == null)
         {
             IFn default_reader = (IFn)RT.DefaultDataReaderFnVar.deref();
             if (default_reader != null)
                 return default_reader.invoke(tag, o);
             else
                 throw new ArgumentException("No reader function for tag " + tag.ToString());
         }
     }
     return dataReader.invoke(o);
 }
Beispiel #24
0
 public static Namespace NamespaceFor(Symbol symbol)
 {
     return NamespaceFor(CurrentNamespace, symbol);
 }
Beispiel #25
0
        // core.clj compatibility
        public static object maybeResolveIn(Namespace n, Symbol symbol)
        {
            // note: ns-qualified vars must already exist
            if (symbol.Namespace != null)
            {
                Namespace ns = NamespaceFor(n, symbol);
                if (ns == null)
                    return null;

                Var v = ns.FindInternedVar(Symbol.create(symbol.Name));
                if (v == null)
                    return null;
                 return v;
            }
            else if (symbol.Name.IndexOf('.') > 0 || symbol.Name[0] == '[')
                return RT.classForName(symbol.Name);
            else if (symbol.Equals(NS))
                return RT.NS_VAR;
            else if (symbol.Equals(IN_NS))
                return RT.IN_NS_VAR;
            else
            {
                object o = n.GetMapping(symbol);
                return o;
            }
        }
Beispiel #26
0
 public static Var intern(Namespace ns, Symbol sym)
 {
     return(ns.intern(sym));
 }
Beispiel #27
0
 public static Namespace namespaceFor(Namespace inns, Symbol sym)
 {
     //note, presumes non-nil sym.ns
     // first check against currentNS' aliases...
     Symbol nsSym = Symbol.create(sym.Namespace);
     Namespace ns = inns.LookupAlias(nsSym);
     if (ns == null)
     {
         // ...otherwise check the Namespaces map.
         ns = Namespace.find(nsSym);
     }
     return ns;
 }
Beispiel #28
0
 public static Var find(Symbol nsQualifiedSym)
 {
     if (nsQualifiedSym.Namespace == null)
         throw new ArgumentException("Symbol must be namespace-qualified");
     Namespace ns = Namespace.find(Symbol.intern(nsQualifiedSym.Namespace));
     if (ns == null)
         throw new ArgumentException("No such namespace: " + nsQualifiedSym.Namespace);
     return ns.FindInternedVar(Symbol.intern(nsQualifiedSym.Name));
 }
Beispiel #29
0
 public static Namespace NamespaceFor(Namespace n, Symbol symbol)
 {
     // Note: presumes non-nil sym.ns
     // first check against CurrentNamespace's aliases
     Symbol nsSym = Symbol.create(symbol.Namespace);
     Namespace ns = n.LookupAlias(nsSym);
     if (ns == null)
         // otherwise, check the namespaces map
         ns = Namespace.find(nsSym);
     return ns;
 }
Beispiel #30
0
 public static Var intern(Namespace ns, Symbol sym, object root)
 {
     return intern(ns, sym, root, true);
 }
Beispiel #31
0
 public static object Resolve(Symbol symbol)
 {
     return ResolveIn(CurrentNamespace, symbol, false);
 }
Beispiel #32
0
 public static Var intern(Namespace ns, Symbol sym, object root, bool replaceRoot)
 {
     Var dvout = ns.intern(sym);
     if (!dvout.hasRoot() || replaceRoot)
         dvout.bindRoot(root);
     return dvout;
 }
Beispiel #33
0
        internal static Var LookupVar(Symbol sym, bool internNew)
        {
            Var var = null;

            // Note: ns-qualified vars in other namespaces must exist already
            if (sym.Namespace != null)
            {
                Namespace ns = Compiler.NamespaceFor(sym);
                if (ns == null)
                    return null;
                Symbol name = Symbol.create(sym.Name);
                if (internNew && ns == CurrentNamespace)
                    var = CurrentNamespace.intern(name);
                else
                    var = ns.FindInternedVar(name);
            }
            else if (sym.Equals(NS))
                var = RT.NS_VAR;
            else if (sym.Equals(IN_NS))
                var = RT.IN_NS_VAR;
            else
            {
                // is it mapped?
                Object o = CurrentNamespace.GetMapping(sym);
                if (o == null)
                {
                    // introduce a new var in the current ns
                    if (internNew)
                        var = CurrentNamespace.intern(Symbol.create(sym.Name));
                }
                else if (o is Var)
                    var = (Var)o;
                else
                    throw new Exception(string.Format("Expecting var, but {0} is mapped to {1}", sym, o));
            }
            if (var != null)
                RegisterVar(var);
            return var;
        }
Beispiel #34
0
 public static Var intern(Symbol nsName, Symbol sym)
 {
     Namespace ns = Namespace.findOrCreate(nsName);
     return intern(ns, sym);
 }
Beispiel #35
0
        internal static LocalBinding ReferenceLocal(Symbol symbol)
        {
            if (!LOCAL_ENV.IsBound)
                return null;

            LocalBinding b = (LocalBinding)RT.get(LOCAL_ENV.deref(), symbol);
            if (b != null)
            {
                FnMethod method = (FnMethod)METHODS.deref();
                CloseOver(b, method);
            }

            return b;
        }
Beispiel #36
0
 public static Var intern(Namespace ns, Symbol sym)
 {
     return ns.intern(sym);
 }
Beispiel #37
0
        private static Expr AnalyzeSymbol(Symbol symbol)
        {
            Symbol tag = TagOf(symbol);

            if (symbol.Namespace == null)
            {
                LocalBinding b = ReferenceLocal(symbol);
                if (b != null)
                    return new LocalBindingExpr(b, tag);
            }
            else
            {
                if (namespaceFor(symbol) == null)
                {
                    Symbol nsSym = Symbol.create(symbol.Namespace);
                    Type t = MaybeType(nsSym, false);
                    if (t != null)
                    {
                        FieldInfo finfo;
                        PropertyInfo pinfo;

                        if ((finfo = Reflector.GetField(t, symbol.Name, true)) != null)
                            return new StaticFieldExpr((int)LINE.deref(), t, symbol.Name,finfo);
                        else if ((pinfo = Reflector.GetProperty(t, symbol.Name, true)) != null)
                            return new StaticPropertyExpr((int)LINE.deref(), t, symbol.Name,pinfo);
                    }
                    throw new Exception(string.Format("Unable to find static field: {0} in {1}", symbol.Name, t));
                }
            }

            object o = Compiler.Resolve(symbol);
            if (o is Var)
            {
                Var v = (Var)o;
                if (IsMacro(v) != null)
                    throw new Exception("Can't take the value of a macro: " + v);
                RegisterVar(v);
                return new VarExpr(v, tag);
            }
            else if (o is Type)
                return new ConstantExpr(o);
            else if (o is Symbol)
                return new UnresolvedVarExpr((Symbol)o);

            throw new Exception(string.Format("Unable to resolve symbol: {0} in this context", symbol));
        }
Beispiel #38
0
 public void setTag(Symbol tag)
 {
     Tag = tag;
 }
Beispiel #39
0
            static object ReadTagged(PushbackTextReader r, Symbol tag)
            {
                object o = read(r, true, null, true);

                ILookup dataReaders = (ILookup)RT.DataReadersVar.deref();
                IFn dataReader = (IFn)RT.get(dataReaders, tag);
                if (dataReader == null)
                {
                    dataReaders = (ILookup)RT.DefaultDataReadersVar.deref();
                    dataReader = (IFn)RT.get(dataReaders, tag);
                    if (dataReader == null)
                    {
                        IFn default_reader = (IFn)RT.DefaultDataReaderFnVar.deref();
                        if (default_reader != null)
                            return default_reader.invoke(tag, o);
                        else
                            throw new ArgumentException("No reader function for tag " + tag.ToString());
                    }
                }
                return dataReader.invoke(o);
            }
Beispiel #40
0
        private static void CreateSuperCall(TypeBuilder proxyTB, Symbol p, MethodInfo mi)
        {
            Type[] paramTypes = CreateTypeArray(mi.GetParameters());

            MethodBuilder mb = proxyTB.DefineMethod(p.Name, MethodAttributes.Public, CallingConventions.HasThis, mi.ReturnType, paramTypes);
            ILGen gen = new ILGen(mb.GetILGenerator());
            gen.EmitLoadArg(0);                             // gen.Emit(OpCodes.Ldarg_0);
            for (int i = 0; i < paramTypes.Length; i++)
                gen.EmitLoadArg(i + 1);                     // gen.Emit(OpCodes.Ldarg, i + 1);
            gen.Emit(OpCodes.Call, mi);                     // not gen.EmitCall(mi); -- we need call versus callvirt
            gen.Emit(OpCodes.Ret);
        }
Beispiel #41
0
 public WrappingReader(Symbol sym)
 {
     _sym = sym;
 }
Beispiel #42
0
 public void setTag(Symbol tag)
 {
     Tag = tag;
 }