summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-03-27 20:30:27 +0000
committerRich Hickey <richhickey@gmail.com>2006-03-27 20:30:27 +0000
commit60d5204f86972c94429009b7e97f554ba8c49f36 (patch)
tree59f86192175e41d5db61a3ba677ade1d517127a2 /src
parentf4f532eecf07fb55ba43a4fb5258b09a32a6bc3d (diff)
first-cut implementation
Diffstat (limited to 'src')
-rw-r--r--src/org/clojure/runtime/Namespace.java61
-rw-r--r--src/org/clojure/runtime/Symbol.java42
2 files changed, 103 insertions, 0 deletions
diff --git a/src/org/clojure/runtime/Namespace.java b/src/org/clojure/runtime/Namespace.java
new file mode 100644
index 00000000..d755cd89
--- /dev/null
+++ b/src/org/clojure/runtime/Namespace.java
@@ -0,0 +1,61 @@
+/**
+ * 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 Mar 27, 2006 1:29:39 PM */
+
+package org.clojure.runtime;
+
+import java.util.HashMap;
+
+public class Namespace{
+
+/**
+ * String->Namespace
+ */
+static final public HashMap table = new HashMap();
+
+/**
+ * String->Symbol
+ */
+final public HashMap symbols = new HashMap();
+final public String name;
+
+Namespace(String name)
+ {
+ this.name = name;
+ }
+
+static public Namespace find(String name)
+ {
+ return (Namespace) table.get(name);
+ }
+
+static public Namespace findOrCreate(String name)
+ {
+ synchronized(table)
+ {
+ Namespace ns = find(name);
+ if(ns == null)
+ table.put(name, ns = new Namespace(name));
+ return ns;
+ }
+ }
+
+public Symbol intern(String name)
+ {
+ synchronized(symbols)
+ {
+ Symbol sym = (Symbol) symbols.get(name);
+ if(sym == null)
+ symbols.put(name, sym = new Symbol(name, this));
+ return sym;
+ }
+ }
+}
diff --git a/src/org/clojure/runtime/Symbol.java b/src/org/clojure/runtime/Symbol.java
index c56c8b76..c1f96e0b 100644
--- a/src/org/clojure/runtime/Symbol.java
+++ b/src/org/clojure/runtime/Symbol.java
@@ -14,6 +14,48 @@ package org.clojure.runtime;
public class Symbol extends AFn{
+public final static Object UNBOUND = new Object();
+
+public final String name;
+public final Namespace namespace;
+public Object val = UNBOUND;
+public IFn fn;
+
+
+/**
+ * Used by Namespace.intern()
+ * @param name
+ * @param ns
+ */
+Symbol(String name, Namespace ns)
+ {
+ this.namespace = ns;
+ this.name = name;
+ }
+
+public Object getValue(ThreadLocalData tld) throws Exception
+ {
+ Cons binding = tld.getDynamicBinding(this);
+ if(binding != null)
+ return binding.first;
+ if(val == UNBOUND)
+ throw new Exception(name + " is unbound.");
+ return val;
+ }
+
+public Object setValue(ThreadLocalData tld, Object val)
+ {
+ Cons binding = tld.getDynamicBinding(this);
+ if(binding != null)
+ return binding.first = val;
+ //allow global set to create binding like this?
+ if(val instanceof IFn)
+ this.fn = (IFn) val;
+ else
+ this.fn = null; //todo, bind to throw stub?
+ return this.val = val;
+ }
+
/**
* Symbol implements IFn for attr access
* This single arg version is the getter