summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-04-19 16:52:33 +0000
committerRich Hickey <richhickey@gmail.com>2008-04-19 16:52:33 +0000
commit9e24fabf1232f129e5aac45802cce6dfd5e300ce (patch)
tree1607fa499afe76e4d0e38d8cef5e778ff6e8ace9
parent7cd3b285328e7e7e71b23080303d66640e0f21e8 (diff)
refactor to remove static init cycle between RT and Compiler, in order to run RT.init in static init
-rw-r--r--src/boot.clj2
-rw-r--r--src/jvm/clojure/lang/AMapEntry.java10
-rw-r--r--src/jvm/clojure/lang/APersistentMap.java6
-rw-r--r--src/jvm/clojure/lang/APersistentSet.java6
-rw-r--r--src/jvm/clojure/lang/APersistentVector.java8
-rw-r--r--src/jvm/clojure/lang/ASeq.java20
-rw-r--r--src/jvm/clojure/lang/Compiler.java10
-rw-r--r--src/jvm/clojure/lang/PersistentHashMap.java16
-rw-r--r--src/jvm/clojure/lang/PersistentQueue.java8
-rw-r--r--src/jvm/clojure/lang/RT.java83
-rw-r--r--src/jvm/clojure/lang/Repl.java4
-rw-r--r--src/jvm/clojure/lang/Script.java5
-rw-r--r--src/jvm/clojure/lang/Symbol.java4
-rw-r--r--src/jvm/clojure/lang/Util.java39
14 files changed, 123 insertions, 98 deletions
diff --git a/src/boot.clj b/src/boot.clj
index cbc10525..bee1bc0f 100644
--- a/src/boot.clj
+++ b/src/boot.clj
@@ -300,7 +300,7 @@
structures define equals() (and thus =) as a value, not an identity,
comparison."
{:tag Boolean}
- [x y] (. clojure.lang.RT (equal x y)))
+ [x y] (. clojure.lang.Util (equal x y)))
(defn not=
"Same as (not (= obj1 obj2))"
diff --git a/src/jvm/clojure/lang/AMapEntry.java b/src/jvm/clojure/lang/AMapEntry.java
index 24313bf2..395fb98a 100644
--- a/src/jvm/clojure/lang/AMapEntry.java
+++ b/src/jvm/clojure/lang/AMapEntry.java
@@ -22,14 +22,14 @@ public boolean equals(Object obj){
public int hashCode(){
//must match logic in APersistentVector
- return RT.hashCombine(RT.hashCombine(0,RT.hash(key())), RT.hash(val()));
+ return Util.hashCombine(Util.hashCombine(0, Util.hash(key())), Util.hash(val()));
}
public String toString(){
StringWriter sw = new StringWriter();
try
{
- RT.print(this,sw);
+ RT.print(this, sw);
}
catch(Exception e)
{
@@ -46,7 +46,7 @@ public int length(){
public Object nth(int i){
if(i == 0)
return key();
- else if (i == 1)
+ else if(i == 1)
return val();
else
throw new IndexOutOfBoundsException();
@@ -81,7 +81,7 @@ public IMapEntry entryAt(Object key){
}
public Associative assoc(Object key, Object val){
- return asVector().assoc(key,val);
+ return asVector().assoc(key, val);
}
public Object valAt(Object key){
@@ -89,7 +89,7 @@ public Object valAt(Object key){
}
public Object valAt(Object key, Object notFound){
- return asVector().valAt(key,notFound);
+ return asVector().valAt(key, notFound);
}
public Object peek(){
diff --git a/src/jvm/clojure/lang/APersistentMap.java b/src/jvm/clojure/lang/APersistentMap.java
index d131cbce..664d27ca 100644
--- a/src/jvm/clojure/lang/APersistentMap.java
+++ b/src/jvm/clojure/lang/APersistentMap.java
@@ -59,7 +59,7 @@ public boolean equals(Object obj){
IMapEntry e = (IMapEntry) s.first();
IMapEntry me = m.entryAt(e.key());
- if(me == null || !RT.equal(e.val(), me.val()))
+ if(me == null || !Util.equal(e.val(), me.val()))
return false;
}
@@ -73,7 +73,7 @@ public int hashCode(){
for(ISeq s = seq(); s != null; s = s.rest())
{
IMapEntry e = (IMapEntry) s.first();
- hash ^= RT.hashCombine(RT.hash(e.key()), RT.hash(e.val()));
+ hash ^= Util.hashCombine(Util.hash(e.key()), Util.hash(e.val()));
}
this._hash = hash;
}
@@ -215,7 +215,7 @@ public boolean contains(Object o){
{
IMapEntry e = (IMapEntry) o;
IMapEntry v = entryAt(e.key());
- return (v != null && RT.equal(v.val(), e.val()));
+ return (v != null && Util.equal(v.val(), e.val()));
}
return false;
}
diff --git a/src/jvm/clojure/lang/APersistentSet.java b/src/jvm/clojure/lang/APersistentSet.java
index b55631bc..a3fffa70 100644
--- a/src/jvm/clojure/lang/APersistentSet.java
+++ b/src/jvm/clojure/lang/APersistentSet.java
@@ -37,7 +37,7 @@ public ISeq seq(){
}
public Object invoke(Object arg1) throws Exception{
- return contains(arg1)?arg1:null;
+ return contains(arg1) ? arg1 : null;
}
public boolean equals(Object obj){
@@ -63,8 +63,8 @@ public int hashCode(){
int hash = count();
for(ISeq s = seq(); s != null; s = s.rest())
{
- Object e = s.first();
- hash = RT.hashCombine(hash,RT.hash(e));
+ Object e = s.first();
+ hash = Util.hashCombine(hash, Util.hash(e));
}
this._hash = hash;
}
diff --git a/src/jvm/clojure/lang/APersistentVector.java b/src/jvm/clojure/lang/APersistentVector.java
index 28bc856e..242e11e6 100644
--- a/src/jvm/clojure/lang/APersistentVector.java
+++ b/src/jvm/clojure/lang/APersistentVector.java
@@ -46,7 +46,7 @@ static boolean doEquals(IPersistentVector v, Object obj){
return false;
for(int i = 0; i < v.count(); i++)
{
- if(!RT.equal(v.nth(i), ma.nth(i)))
+ if(!Util.equal(v.nth(i), ma.nth(i)))
return false;
}
}
@@ -57,7 +57,7 @@ static boolean doEquals(IPersistentVector v, Object obj){
ISeq ms = ((IPersistentCollection) obj).seq();
for(int i = 0; i < v.count(); i++, ms = ms.rest())
{
- if(ms == null || !RT.equal(v.nth(i), ms.first()))
+ if(ms == null || !Util.equal(v.nth(i), ms.first()))
return false;
}
if(ms != null)
@@ -78,7 +78,7 @@ public int hashCode(){
int hash = 0;
for(int i = 0; i < count(); i++)
{
- hash = RT.hashCombine(hash, RT.hash(nth(i)));
+ hash = Util.hashCombine(hash, Util.hash(nth(i)));
}
this._hash = hash;
}
@@ -220,7 +220,7 @@ public boolean isEmpty(){
public boolean contains(Object o){
for(ISeq s = seq(); s != null; s = s.rest())
{
- if(RT.equal(s.first(), o))
+ if(Util.equal(s.first(), o))
return true;
}
return false;
diff --git a/src/jvm/clojure/lang/ASeq.java b/src/jvm/clojure/lang/ASeq.java
index ac821f5e..2dca27a5 100644
--- a/src/jvm/clojure/lang/ASeq.java
+++ b/src/jvm/clojure/lang/ASeq.java
@@ -32,7 +32,7 @@ public boolean equals(Object obj){
ISeq ms = ((IPersistentCollection) obj).seq();
for(ISeq s = seq(); s != null; s = s.rest(), ms = ms.rest())
{
- if(ms == null || !RT.equal(s.first(), ms.first()))
+ if(ms == null || !Util.equal(s.first(), ms.first()))
return false;
}
if(ms != null)
@@ -47,7 +47,7 @@ public int hashCode(){
int hash = 0;
for(ISeq s = seq(); s != null; s = s.rest())
{
- hash = RT.hashCombine(hash, RT.hash(s.first()));
+ hash = Util.hashCombine(hash, Util.hash(s.first()));
}
this._hash = hash;
}
@@ -56,15 +56,15 @@ public int hashCode(){
public Object reduce(IFn f) throws Exception{
Object ret = first();
- for(ISeq s = rest();s != null;s=s.rest())
- ret = f.invoke(ret,s.first());
+ for(ISeq s = rest(); s != null; s = s.rest())
+ ret = f.invoke(ret, s.first());
return ret;
}
public Object reduce(IFn f, Object start) throws Exception{
- Object ret = f.invoke(start,first());
- for(ISeq s = rest();s != null;s=s.rest())
- ret = f.invoke(ret,s.first());
+ Object ret = f.invoke(start, first());
+ for(ISeq s = rest(); s != null; s = s.rest())
+ ret = f.invoke(ret, s.first());
return ret;
}
@@ -77,8 +77,8 @@ public Object reduce(IFn f, Object start) throws Exception{
//}
public int count(){
- int i=1;
- for(ISeq s = rest();s!=null;s = s.rest(),i++)
+ int i = 1;
+ for(ISeq s = rest(); s != null; s = s.rest(), i++)
;
return i;
}
@@ -157,7 +157,7 @@ public boolean isEmpty(){
public boolean contains(Object o){
for(ISeq s = seq(); s != null; s = s.rest())
{
- if(RT.equal(s.first(), o))
+ if(Util.equal(s.first(), o))
return true;
}
return false;
diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java
index d07410ab..81bbc233 100644
--- a/src/jvm/clojure/lang/Compiler.java
+++ b/src/jvm/clojure/lang/Compiler.java
@@ -74,7 +74,7 @@ static final Symbol ISEQ = Symbol.create("clojure.lang.ISeq");
//static final Symbol IFN = Symbol.create("clojure.lang", "IFn");
-static final public IPersistentMap specials = RT.map(
+static final public IPersistentMap specials = PersistentHashMap.create(
DEF, new DefExpr.Parser(),
LOOP, new LetExpr.Parser(),
RECUR, new RecurExpr.Parser(),
@@ -783,7 +783,7 @@ static class InstanceFieldExpr extends FieldExpr implements AssignableExpr{
public InstanceFieldExpr(int line, Expr target, String fieldName) throws Exception{
this.target = target;
this.targetClass = target.hasJavaClass() ? target.getJavaClass() : null;
- this.field = targetClass != null ? Reflector.getField(targetClass,fieldName,false) : null;
+ this.field = targetClass != null ? Reflector.getField(targetClass, fieldName, false) : null;
this.fieldName = fieldName;
this.line = line;
if(field == null && RT.booleanCast(RT.WARN_ON_REFLECTION.get()))
@@ -1552,7 +1552,7 @@ static class TryExpr implements Expr{
{
Object f = fs.first();
Object op = (f instanceof ISeq) ? ((ISeq) f).first() : null;
- if(!RT.equal(op, CATCH) && !RT.equal(op, FINALLY))
+ if(!Util.equal(op, CATCH) && !Util.equal(op, FINALLY))
{
if(caught)
throw new Exception("Only catch or finally clause can follow catch in try expression");
@@ -1560,7 +1560,7 @@ static class TryExpr implements Expr{
}
else
{
- if(RT.equal(op, CATCH))
+ if(Util.equal(op, CATCH))
{
Class c = HostExpr.maybeClass(RT.second(f), false);
if(c == null)
@@ -3031,7 +3031,7 @@ static class BodyExpr implements Expr{
static class Parser implements IParser{
public Expr parse(C context, Object frms) throws Exception{
ISeq forms = (ISeq) frms;
- if(RT.equal(RT.first(forms), DO))
+ if(Util.equal(RT.first(forms), DO))
forms = RT.rest(forms);
PersistentVector exprs = PersistentVector.EMPTY;
for(; forms != null; forms = forms.rest())
diff --git a/src/jvm/clojure/lang/PersistentHashMap.java b/src/jvm/clojure/lang/PersistentHashMap.java
index d7a94bdb..adf3beeb 100644
--- a/src/jvm/clojure/lang/PersistentHashMap.java
+++ b/src/jvm/clojure/lang/PersistentHashMap.java
@@ -97,12 +97,12 @@ public boolean containsKey(Object key){
}
public IMapEntry entryAt(Object key){
- return root.find(RT.hash(key), key);
+ return root.find(Util.hash(key), key);
}
public IPersistentMap assoc(Object key, Object val){
Box addedLeaf = new Box(null);
- INode newroot = root.assoc(0, RT.hash(key), key, val, addedLeaf);
+ INode newroot = root.assoc(0, Util.hash(key), key, val, addedLeaf);
if(newroot == root)
return this;
return new PersistentHashMap(meta(), addedLeaf.val == null ? count : count + 1, newroot);
@@ -126,7 +126,7 @@ public IPersistentMap assocEx(Object key, Object val) throws Exception{
}
public IPersistentMap without(Object key){
- INode newroot = root.without(RT.hash(key), key);
+ INode newroot = root.without(Util.hash(key), key);
if(newroot == root)
return this;
if(newroot == null)
@@ -498,9 +498,9 @@ final static class LeafNode extends AMapEntry implements INode{
public INode assoc(int shift, int hash, Object key, Object val, Box addedLeaf){
if(hash == this.hash)
{
- if(RT.equal(key, this.key))
+ if(Util.equal(key, this.key))
{
- if(RT.equal(val, this.val))
+ if(Util.equal(val, this.val))
return this;
//note - do not set addedLeaf, since we are replacing
return new LeafNode(hash, key, val);
@@ -514,13 +514,13 @@ final static class LeafNode extends AMapEntry implements INode{
}
public INode without(int hash, Object key){
- if(hash == this.hash && RT.equal(key, this.key))
+ if(hash == this.hash && Util.equal(key, this.key))
return null;
return this;
}
public LeafNode find(int hash, Object key){
- if(hash == this.hash && RT.equal(key, this.key))
+ if(hash == this.hash && Util.equal(key, this.key))
return this;
return null;
}
@@ -570,7 +570,7 @@ final static class HashCollisionNode implements INode{
int idx = findIndex(hash, key);
if(idx != -1)
{
- if(RT.equal(leaves[idx].val, val))
+ if(Util.equal(leaves[idx].val, val))
return this;
LeafNode[] newLeaves = leaves.clone();
//note - do not set addedLeaf, since we are replacing
diff --git a/src/jvm/clojure/lang/PersistentQueue.java b/src/jvm/clojure/lang/PersistentQueue.java
index 4d21fff3..054b21af 100644
--- a/src/jvm/clojure/lang/PersistentQueue.java
+++ b/src/jvm/clojure/lang/PersistentQueue.java
@@ -10,8 +10,6 @@
package clojure.lang;
-import java.util.Queue;
-import java.util.LinkedList;
import java.util.Collection;
import java.util.Iterator;
//import java.util.concurrent.ConcurrentLinkedQueue;
@@ -46,7 +44,7 @@ public boolean equals(Object obj){
ISeq ms = ((IPersistentCollection) obj).seq();
for(ISeq s = seq(); s != null; s = s.rest(), ms = ms.rest())
{
- if(ms == null || !RT.equal(s.first(), ms.first()))
+ if(ms == null || !Util.equal(s.first(), ms.first()))
return false;
}
return ms.rest() == null;
@@ -59,7 +57,7 @@ public int hashCode(){
int hash = 0;
for(ISeq s = seq(); s != null; s = s.rest())
{
- hash = RT.hashCombine(hash, RT.hash(s.first()));
+ hash = Util.hashCombine(hash, Util.hash(s.first()));
}
this._hash = hash;
}
@@ -212,7 +210,7 @@ public boolean isEmpty(){
public boolean contains(Object o){
for(ISeq s = seq(); s != null; s = s.rest())
{
- if(RT.equal(s.first(), o))
+ if(Util.equal(s.first(), o))
return true;
}
return false;
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index 9818914f..e4ced035 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -238,6 +238,15 @@ static
// {
// throw new IllegalStateException("Error loading boot.clj", e);
// }
+
+ try
+ {
+ doInit();
+ }
+ catch(Exception e)
+ {
+ throw new RuntimeException(e);
+ }
}
//static
// {
@@ -251,13 +260,17 @@ static public Var var(String ns, String name){
return Var.intern(Namespace.findOrCreate(Symbol.intern(null, ns)), Symbol.intern(null, name));
}
-public static void loadResourceScript(String name) throws Exception{
- InputStream ins = RT.class.getResourceAsStream("/" + name);
- Compiler.load(new InputStreamReader(ins),name,name);
- ins.close();
+public static void loadResourceScript(String name) throws Exception{
+ InputStream ins = RT.class.getResourceAsStream("/" + name);
+ Compiler.load(new InputStreamReader(ins), name, name);
+ ins.close();
}
static public void init() throws Exception{
+ System.err.println("No need to call RT.init() anymore");
+}
+
+static void doInit() throws Exception{
loadResourceScript("boot.clj");
loadResourceScript("proxy.clj");
loadResourceScript("zip.clj");
@@ -269,18 +282,6 @@ static public int nextID(){
return id.getAndIncrement();
}
-static public boolean equal(Object k1, Object k2){
- if(k1 == k2)
- return true;
- if(k1 != null)
- {
- if(k1 instanceof Number)
- return Numbers.equiv(k1, k2);
- return k1.equals(k2);
- }
- return false;
-}
-
//static public Object eq(Object arg1, Object arg2){
// return (arg1 == arg2) ? Boolean.TRUE : null;
//}
@@ -316,18 +317,6 @@ static public boolean equal(Object k1, Object k2){
////////////// Collections support /////////////////////////////////
-static public int hash(Object o){
- if(o == null)
- return 0;
- return o.hashCode();
-}
-
-static public int hashCombine(int seed, int hash){
- //a la boost
- seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
- return seed;
-}
-
static public ISeq seq(Object coll){
if(coll == null)
return null;
@@ -598,7 +587,7 @@ static boolean hasTag(Object o, Object tag){
if(!(o instanceof IObj))
return false;
IPersistentMap meta = ((IObj) o).meta();
- return RT.equal(tag, RT.get(meta, TAG_KEY));
+ return Util.equal(tag, RT.get(meta, TAG_KEY));
}
/**
@@ -907,11 +896,11 @@ static public void print(Object x, Writer w) throws Exception{
w.write("\\\\");
break;
case '\f':
- w.write("\\f");
- break;
- case '\b':
- w.write("\\b");
- break;
+ w.write("\\f");
+ break;
+ case '\b':
+ w.write("\\b");
+ break;
default:
w.write(c);
}
@@ -991,12 +980,12 @@ static public void print(Object x, Writer w) throws Exception{
case ' ':
w.write("space");
break;
- case '\b':
- w.write("backspace");
- break;
- case '\f':
- w.write("formfeed");
- break;
+ case '\b':
+ w.write("backspace");
+ break;
+ case '\f':
+ w.write("formfeed");
+ break;
default:
w.write(c);
}
@@ -1050,12 +1039,12 @@ static public void formatStandard(Writer w, Object obj) throws IOException{
case ' ':
w.write("space");
break;
- case '\b':
- w.write("backspace");
- break;
- case '\f':
- w.write("formfeed");
- break;
+ case '\b':
+ w.write("backspace");
+ break;
+ case '\f':
+ w.write("formfeed");
+ break;
default:
w.write(c);
}
@@ -1068,7 +1057,7 @@ static public Object format(Object o, String s, Object... args) throws Exception
Writer w;
if(o == null)
w = new StringWriter();
- else if(equal(o, T))
+ else if(Util.equal(o, T))
w = (Writer) OUT.get();
else
w = (Writer) o;
diff --git a/src/jvm/clojure/lang/Repl.java b/src/jvm/clojure/lang/Repl.java
index 37d1b654..4c15be13 100644
--- a/src/jvm/clojure/lang/Repl.java
+++ b/src/jvm/clojure/lang/Repl.java
@@ -25,8 +25,8 @@ static final Var ns = RT.var("clojure", "*ns*");
static final Var warn_on_reflection = RT.var("clojure", "*warn-on-reflection*");
public static void main(String[] args) throws Exception{
- //must call this once before using Clojure
- RT.init();
+
+// RT.init();
try
{
diff --git a/src/jvm/clojure/lang/Script.java b/src/jvm/clojure/lang/Script.java
index 2699d6a3..32d16f77 100644
--- a/src/jvm/clojure/lang/Script.java
+++ b/src/jvm/clojure/lang/Script.java
@@ -20,8 +20,7 @@ import java.util.Arrays;
public class Script{
public static void main(String[] args) throws Exception{
- RT.init();
-
+// RT.init();
for(String file : RT.processCommandLine(args))
try
@@ -43,7 +42,7 @@ public static void main(String[] args) throws Exception{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
- System.exit(0);
+ System.exit(0);
}
}
diff --git a/src/jvm/clojure/lang/Symbol.java b/src/jvm/clojure/lang/Symbol.java
index 8f3be2c6..248b3aa7 100644
--- a/src/jvm/clojure/lang/Symbol.java
+++ b/src/jvm/clojure/lang/Symbol.java
@@ -59,7 +59,7 @@ static public Symbol create(String ns_interned, String name_interned){
private Symbol(String ns_interned, String name_interned){
this.name = name_interned;
this.ns = ns_interned;
- this.hash = RT.hashCombine(name.hashCode(), RT.hash(ns));
+ this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns));
}
public boolean equals(Object o){
@@ -86,7 +86,7 @@ private Symbol(IPersistentMap meta, String ns, String name){
super(meta);
this.name = name;
this.ns = ns;
- this.hash = RT.hashCombine(name.hashCode(), RT.hash(ns));
+ this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns));
}
public int compareTo(Object o){
diff --git a/src/jvm/clojure/lang/Util.java b/src/jvm/clojure/lang/Util.java
new file mode 100644
index 00000000..a4488e5b
--- /dev/null
+++ b/src/jvm/clojure/lang/Util.java
@@ -0,0 +1,39 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ **/
+
+/* rich Apr 19, 2008 */
+
+package clojure.lang;
+
+public class Util{
+static public boolean equal(Object k1, Object k2){
+ if(k1 == k2)
+ return true;
+ if(k1 != null)
+ {
+ if(k1 instanceof Number)
+ return Numbers.equiv(k1, k2);
+ return k1.equals(k2);
+ }
+ return false;
+}
+
+static public int hash(Object o){
+ if(o == null)
+ return 0;
+ return o.hashCode();
+}
+
+static public int hashCombine(int seed, int hash){
+ //a la boost
+ seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
+ return seed;
+}
+}