From 8800e169cf12df893fdd89c8e93edb36f8e5388e Mon Sep 17 00:00:00 2001 From: Rich Hickey Date: Tue, 23 May 2006 18:12:54 +0000 Subject: derived Keyword from Symbol, IFn --- src/cli/runtime/AFn.cs | 35 ++++++++++++++++++++--------------- src/cli/runtime/AMap.cs | 8 ++++---- src/cli/runtime/Accessor.cs | 6 ++---- src/cli/runtime/Keyword.cs | 41 +++++++++++++++++++++++++++++++---------- src/cli/runtime/Symbol.cs | 2 +- 5 files changed, 58 insertions(+), 34 deletions(-) (limited to 'src/cli') diff --git a/src/cli/runtime/AFn.cs b/src/cli/runtime/AFn.cs index 9b1cb05b..193f4387 100644 --- a/src/cli/runtime/AFn.cs +++ b/src/cli/runtime/AFn.cs @@ -56,37 +56,42 @@ virtual public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Obje } virtual public Object applyTo(ThreadLocalData tld, Cons arglist) /*throws Exception*/ +{ +return applyToHelper(this,tld,arglist); +} + +static public Object applyToHelper(IFn ifn,ThreadLocalData tld, Cons arglist) /*throws Exception*/ { switch(RT.boundedLength(arglist, 5)) { - case 0: - return invoke(tld); - case 1: - return invoke(tld, arglist.first); - case 2: - return invoke(tld, arglist.first + case 0: + return ifn.invoke(tld); + case 1: + return ifn.invoke(tld, arglist.first); + case 2: + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first ); - case 3: - return invoke(tld, arglist.first + case 3: + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first , (arglist = arglist.rest).first ); - case 4: - return invoke(tld, arglist.first + case 4: + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first , (arglist = arglist.rest).first , (arglist = arglist.rest).first ); - case 5: - return invoke(tld, arglist.first + case 5: + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first , (arglist = arglist.rest).first , (arglist = arglist.rest).first , (arglist = arglist.rest).first ); - default: - return invoke(tld, arglist.first + default: + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first , (arglist = arglist.rest).first , (arglist = arglist.rest).first @@ -95,7 +100,7 @@ virtual public Object applyTo(ThreadLocalData tld, Cons arglist) /*throws Except } } -protected Object throwArity() +static public Object throwArity() { throw new Exception("Wrong number of args passed"); } diff --git a/src/cli/runtime/AMap.cs b/src/cli/runtime/AMap.cs index 9db0a525..a34e1560 100644 --- a/src/cli/runtime/AMap.cs +++ b/src/cli/runtime/AMap.cs @@ -22,15 +22,15 @@ public class AMap HybridDictionary attrs; public static int INITIAL_SIZE = 7; -public Object put(Indexer key, Object val) +public Object put(Keyword key, Object val) { if(attrs == null) attrs = new HybridDictionary(INITIAL_SIZE); attrs[key] = val; return val; - } - -public Object get(Indexer key) + } + +public Object get(Keyword key) { if(attrs == null) return null; diff --git a/src/cli/runtime/Accessor.cs b/src/cli/runtime/Accessor.cs index c15f91a0..cf341d95 100644 --- a/src/cli/runtime/Accessor.cs +++ b/src/cli/runtime/Accessor.cs @@ -14,8 +14,6 @@ namespace org.clojure.runtime { public class Accessor :Indexer { - public String name; public Namespace ns; internal Accessor(String name, Namespace ns) { this.ns = ns; this.name = name; } public String toString() { if(ns == null) return "#:." + name; return ns.name + ":." + name; } /** * Indexer implements IFn for attr access * This single arg version is the getter * @param tld * @param obj - must be AMap * @return the value of the attr or nil if not found * @throws Exception */ override public Object invoke(ThreadLocalData tld, Object obj) //throws Exception { if(obj is AMap) return ((AMap)obj).get(this); - else if (obj == null) - return null; - return Reflector.invokeInstanceMember(name, obj); } /** * Indexer implements IFn for attr access * This two arg version is the setter * @param tld * @param obj - must be AMap * @param val * @return val * @throws Exception */ override public Object invoke(ThreadLocalData tld, Object obj, Object val) //throws Exception { if(obj is AMap) return ((AMap)obj).put(this,val); return Reflector.invokeInstanceMember(name,obj,val); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3,arg4); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3,arg4,arg5); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Cons args) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3,arg4,arg5,args); } } + public String name; public Namespace ns; internal Accessor(String name, Namespace ns) { this.ns = ns; this.name = name; } public String toString() { if(ns == null) return "#:." + name; return ns.name + ":." + name; } /** * Indexer implements IFn for attr access * This single arg version is the getter * @param tld * @param obj - must be AMap * @return the value of the attr or nil if not found * @throws Exception */ override public Object invoke(ThreadLocalData tld, Object obj) //throws Exception { + return Reflector.invokeInstanceMember(name, obj); } /** * Indexer implements IFn for attr access * This two arg version is the setter * @param tld * @param obj - must be AMap * @param val * @return val * @throws Exception */ override public Object invoke(ThreadLocalData tld, Object obj, Object val) //throws Exception { return Reflector.invokeInstanceMember(name,obj,val); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3,arg4); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3,arg4,arg5); } override public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Cons args) //throws Exception { return Reflector.invokeInstanceMember(name,arg1,arg2,arg3,arg4,arg5,args); } } } diff --git a/src/cli/runtime/Keyword.cs b/src/cli/runtime/Keyword.cs index 98f3773c..2250d097 100644 --- a/src/cli/runtime/Keyword.cs +++ b/src/cli/runtime/Keyword.cs @@ -11,24 +11,45 @@ /* rich Mar 29, 2006 10:39:05 AM */ using System; -using System.Collections.Specialized; + namespace org.clojure.runtime { -public class Keyword : Indexer - { -static public HybridDictionary table = new HybridDictionary(); +public class Keyword : Symbol, IFn{ -public String name; override public String ToString() - { - return ":" + name; - } - public static Keyword intern(String name) { lock(table) { Keyword sym = (Keyword) table[name]; if(sym == null) table.Add(name, sym = new Keyword(name)); return sym; } } /** * Used by Namespace.intern() * * @param name */ Keyword(String name) { this.name = name; } /** * Indexer implements IFn for attr access * This single arg version is the getter * @param tld * @param obj - must be AMap * @return the value of the attr or nil if not found */ override public Object invoke(ThreadLocalData tld, Object obj) /*throws Exception*/ { +internal Keyword(String name):base(name) { } public Object invoke(ThreadLocalData tld) /*throws Exception*/ { + return AFn.throwArity(); +} /** * Indexer implements IFn for attr access * This single arg version is the getter * @param tld * @param obj - must be AMap * @return the value of the attr or nil if not found */ public Object invoke(ThreadLocalData tld, Object obj) /*throws Exception*/ { if (obj == null) return null; - return ((AMap)obj).get(this); } /** * Indexer implements IFn for attr access * This two arg version is the setter * @param tld * @param obj - must be AMap * @param val * @return val */ override public Object invoke(ThreadLocalData tld, Object obj, Object val) /*throws Exception*/ { return ((AMap)obj).put(this,val); } + return ((AMap)obj).get(this); } /** * Indexer implements IFn for attr access * This two arg version is the setter * @param tld * @param obj - must be AMap * @param val * @return val */ public Object invoke(ThreadLocalData tld, Object obj, Object val) /*throws Exception*/ { return ((AMap)obj).put(this,val); } +public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3) /*throws Exception*/ + { + return AFn.throwArity(); + } + +public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4) /*throws Exception*/ + { + return AFn.throwArity(); + } + +public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) + /*throws Exception*/ + { + return AFn.throwArity(); + } + +public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Cons args) + /*throws Exception*/ + { + return AFn.throwArity(); + } + +public Object applyTo(ThreadLocalData tld, Cons arglist) /*throws Exception*/ { + return AFn.applyToHelper(this, tld, arglist); +} } } \ No newline at end of file diff --git a/src/cli/runtime/Symbol.cs b/src/cli/runtime/Symbol.cs index 532b5f39..4f532d14 100644 --- a/src/cli/runtime/Symbol.cs +++ b/src/cli/runtime/Symbol.cs @@ -30,7 +30,7 @@ public String toString() return name; } -public static Symbol intern(String name) { lock(table) { Symbol sym = (Symbol) table[name]; if(sym == null) table.Add(name, sym = new Symbol(name)); return sym; } } +public static Symbol intern(String name) { lock(table) { Symbol sym = (Symbol) table[name]; if(sym == null) { if(name[0] == ':') sym = new Keyword(name); else sym = new Symbol(name); table.Add(name, sym); } return sym; } } /** * Used by Namespace.intern() * @param name -- cgit v1.2.3-70-g09d2