diff options
author | Rich Hickey <richhickey@gmail.com> | 2006-05-23 18:12:54 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2006-05-23 18:12:54 +0000 |
commit | 8800e169cf12df893fdd89c8e93edb36f8e5388e (patch) | |
tree | 0b7c32db9b7e4e2d3e7951a5b066220ac9d5ba22 /src | |
parent | 7b16ded965dfa76689d0235d4f1cf999a3f1c8a5 (diff) |
derived Keyword from Symbol, IFn
Diffstat (limited to 'src')
-rw-r--r-- | src/cli/runtime/AFn.cs | 35 | ||||
-rw-r--r-- | src/cli/runtime/AMap.cs | 8 | ||||
-rw-r--r-- | src/cli/runtime/Accessor.cs | 6 | ||||
-rw-r--r-- | src/cli/runtime/Keyword.cs | 41 | ||||
-rw-r--r-- | src/cli/runtime/Symbol.cs | 2 | ||||
-rw-r--r-- | src/org/clojure/runtime/AFn.java | 21 | ||||
-rw-r--r-- | src/org/clojure/runtime/AMap.java | 4 | ||||
-rw-r--r-- | src/org/clojure/runtime/Accessor.java | 8 | ||||
-rw-r--r-- | src/org/clojure/runtime/Keyword.java | 61 | ||||
-rw-r--r-- | src/org/clojure/runtime/Symbol.java | 8 |
10 files changed, 117 insertions, 77 deletions
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 diff --git a/src/org/clojure/runtime/AFn.java b/src/org/clojure/runtime/AFn.java index a4ace498..c483a466 100644 --- a/src/org/clojure/runtime/AFn.java +++ b/src/org/clojure/runtime/AFn.java @@ -51,38 +51,41 @@ public Object invoke(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, return throwArity(); } -public Object applyTo(ThreadLocalData tld, Cons arglist) throws Exception +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); + return ifn.invoke(tld); case 1: - return invoke(tld, arglist.first); + return ifn.invoke(tld, arglist.first); case 2: - return invoke(tld, arglist.first + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first ); case 3: - return invoke(tld, arglist.first + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first , (arglist = arglist.rest).first ); case 4: - return invoke(tld, arglist.first + 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 + 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 + return ifn.invoke(tld, arglist.first , (arglist = arglist.rest).first , (arglist = arglist.rest).first , (arglist = arglist.rest).first @@ -91,7 +94,7 @@ public Object applyTo(ThreadLocalData tld, Cons arglist) throws Exception } } -protected Object throwArity() +public static Object throwArity() { throw new IllegalArgumentException("Wrong number of args passed"); } diff --git a/src/org/clojure/runtime/AMap.java b/src/org/clojure/runtime/AMap.java index 5f8965c3..132d9ed8 100644 --- a/src/org/clojure/runtime/AMap.java +++ b/src/org/clojure/runtime/AMap.java @@ -19,7 +19,7 @@ public class AMap{ IdentityHashMap attrs; public static final int INITIAL_SIZE = 7; -public Object put(Indexer key, Object val) +public Object put(Keyword key, Object val) { if(attrs == null) attrs = new IdentityHashMap(INITIAL_SIZE); @@ -27,7 +27,7 @@ public Object put(Indexer key, Object val) return val; } -public Object get(Indexer key) +public Object get(Keyword key) { if(attrs == null) return null; diff --git a/src/org/clojure/runtime/Accessor.java b/src/org/clojure/runtime/Accessor.java index ca4a44d1..1471db18 100644 --- a/src/org/clojure/runtime/Accessor.java +++ b/src/org/clojure/runtime/Accessor.java @@ -40,10 +40,7 @@ public String toString() */ public Object invoke(ThreadLocalData tld, Object obj) throws Exception { - if(obj instanceof AMap) - return ((AMap)obj).get(this); - else if (obj == null) - return null; + return Reflector.invokeInstanceMember(name,obj); } @@ -58,8 +55,7 @@ public Object invoke(ThreadLocalData tld, Object obj) throws Exception */ public Object invoke(ThreadLocalData tld, Object obj, Object val) throws Exception { - if(obj instanceof AMap) - return ((AMap)obj).put(this,val); + return Reflector.invokeInstanceMember(name,obj,val); } diff --git a/src/org/clojure/runtime/Keyword.java b/src/org/clojure/runtime/Keyword.java index 7fa09415..ed1d4191 100644 --- a/src/org/clojure/runtime/Keyword.java +++ b/src/org/clojure/runtime/Keyword.java @@ -12,40 +12,25 @@ package org.clojure.runtime; -import java.util.HashMap; -public class Keyword extends Indexer{ +public class Keyword extends Symbol implements IFn{ -final public static HashMap table = new HashMap(); - -public final String name; - -public static Keyword intern(String name) - { - synchronized(table) - { - Keyword sym = (Keyword) table.get(name); - if(sym == null) - table.put(name, sym = new Keyword(name)); - return sym; - } - } /** - * Used by Namespace.intern() + * Used by intern() * * @param name */ Keyword(String name) { - this.name = name; + super(name); } -public String toString() - { - return ":" + name; - } + +public Object invoke(ThreadLocalData tld) throws Exception { + return AFn.throwArity(); +} /** * Indexer implements IFn for attr access @@ -56,11 +41,11 @@ public String toString() * @throws Exception */ public Object invoke(ThreadLocalData tld, Object obj) throws Exception - { + { if (obj == null) return null; - return ((AMap)obj).get(this); - } + return ((AMap)obj).get(this); + } /** * Indexer implements IFn for attr access @@ -75,4 +60,30 @@ public Object invoke(ThreadLocalData tld, Object obj, Object val) throws Excepti { 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); +} } diff --git a/src/org/clojure/runtime/Symbol.java b/src/org/clojure/runtime/Symbol.java index 8cb79e18..dee1f7fe 100644 --- a/src/org/clojure/runtime/Symbol.java +++ b/src/org/clojure/runtime/Symbol.java @@ -36,7 +36,13 @@ public static Symbol intern(String name) { Symbol sym = (Symbol) table.get(name); if(sym == null) - table.put(name, sym = new Symbol(name)); + { + if(name.charAt(0) == ':') + sym = new Keyword(name); + else + sym = new Symbol(name); + table.put(name, sym); + } return sym; } } |