summaryrefslogtreecommitdiff
path: root/src/jvm/clojure
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-01-25 19:03:49 +0000
committerRich Hickey <richhickey@gmail.com>2008-01-25 19:03:49 +0000
commit8da00aca0e47b20cd7c50e29d42ed6a58e9a7a62 (patch)
tree441c20967cbd1965bc6cd188e43be26c30f0981c /src/jvm/clojure
parent755aee1231b268a3fafb1f385af83156938f940c (diff)
interim checkin, do not use
Diffstat (limited to 'src/jvm/clojure')
-rw-r--r--src/jvm/clojure/lang/Compiler.java356
-rw-r--r--src/jvm/clojure/lang/Namespace.java2
-rw-r--r--src/jvm/clojure/lang/RT.java54
3 files changed, 215 insertions, 197 deletions
diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java
index a1366585..7855a281 100644
--- a/src/jvm/clojure/lang/Compiler.java
+++ b/src/jvm/clojure/lang/Compiler.java
@@ -48,18 +48,17 @@ static final Symbol QUOTE = Symbol.create("quote");
static final Symbol THE_VAR = Symbol.create("the-var");
static final Symbol DOT = Symbol.create(".");
static final Symbol ASSIGN = Symbol.create("set!");
-static final Symbol TRY_FINALLY = Symbol.create("try-finally");
+//static final Symbol TRY_FINALLY = Symbol.create("try-finally");
static final Symbol TRY = Symbol.create("try");
static final Symbol CATCH = Symbol.create("catch");
static final Symbol FINALLY = Symbol.create("finally");
static final Symbol THROW = Symbol.create("throw");
static final Symbol MONITOR_ENTER = Symbol.create("monitor-enter");
static final Symbol MONITOR_EXIT = Symbol.create("monitor-exit");
-static final Symbol INSTANCE = Symbol.create("instance?");
-static final Symbol IDENTICAL = Symbol.create("identical?");
+//static final Symbol INSTANCE = Symbol.create("instance?");
static final Symbol THISFN = Symbol.create("thisfn");
-static final Symbol CLASS = Symbol.create("class");
+//static final Symbol CLASS = Symbol.create("class");
static final Symbol NEW = Symbol.create("new");
//static final Symbol UNQUOTE = Symbol.create("unquote");
//static final Symbol UNQUOTE_SPLICING = Symbol.create("unquote-splicing");
@@ -88,17 +87,17 @@ static IPersistentMap specials = RT.map(
THE_VAR, new TheVarExpr.Parser(),
DOT, new HostExpr.Parser(),
ASSIGN, new AssignExpr.Parser(),
- TRY_FINALLY, new TryFinallyExpr.Parser(),
+// TRY_FINALLY, new TryFinallyExpr.Parser(),
TRY, new TryExpr.Parser(),
THROW, new ThrowExpr.Parser(),
MONITOR_ENTER, new MonitorEnterExpr.Parser(),
MONITOR_EXIT, new MonitorExitExpr.Parser(),
- INSTANCE, new InstanceExpr.Parser(),
- IDENTICAL, new IdenticalExpr.Parser(),
+// INSTANCE, new InstanceExpr.Parser(),
+// IDENTICAL, new IdenticalExpr.Parser(),
THISFN, null,
CATCH, null,
FINALLY, null,
- CLASS, new ClassExpr.Parser(),
+// CLASS, new ClassExpr.Parser(),
NEW, new NewExpr.Parser(),
// UNQUOTE, null,
// UNQUOTE_SPLICING, null,
@@ -1355,7 +1354,7 @@ static class MonitorEnterExpr extends UntypedExpr{
static class Parser implements IParser{
public Expr parse(C context, Object form) throws Exception{
- return analyze(C.EXPRESSION, RT.second(form));
+ return new MonitorEnterExpr(analyze(C.EXPRESSION, RT.second(form)));
}
}
}
@@ -1382,7 +1381,7 @@ static class MonitorExitExpr extends UntypedExpr{
static class Parser implements IParser{
public Expr parse(C context, Object form) throws Exception{
- return analyze(C.EXPRESSION, RT.second(form));
+ return new MonitorExitExpr(analyze(C.EXPRESSION, RT.second(form)));
}
}
@@ -1548,62 +1547,62 @@ static class TryExpr implements Expr{
}
}
-static class TryFinallyExpr implements Expr{
- final Expr tryExpr;
- final Expr finallyExpr;
-
-
- public TryFinallyExpr(Expr tryExpr, Expr finallyExpr){
- this.tryExpr = tryExpr;
- this.finallyExpr = finallyExpr;
- }
-
- public Object eval() throws Exception{
- throw new UnsupportedOperationException("Can't eval try");
- }
-
- public void emit(C context, FnExpr fn, GeneratorAdapter gen){
- Label startTry = gen.newLabel();
- Label endTry = gen.newLabel();
- Label end = gen.newLabel();
- Label finallyLabel = gen.newLabel();
- gen.visitTryCatchBlock(startTry, endTry, finallyLabel, null);
- gen.mark(startTry);
- tryExpr.emit(context, fn, gen);
- gen.mark(endTry);
- finallyExpr.emit(C.STATEMENT, fn, gen);
- gen.goTo(end);
- gen.mark(finallyLabel);
- //exception should be on stack
- finallyExpr.emit(C.STATEMENT, fn, gen);
- gen.throwException();
- gen.mark(end);
- }
-
- public boolean hasJavaClass() throws Exception{
- return tryExpr.hasJavaClass();
- }
-
- public Class getJavaClass() throws Exception{
- return tryExpr.getJavaClass();
- }
-
- static class Parser implements IParser{
- public Expr parse(C context, Object frm) throws Exception{
- ISeq form = (ISeq) frm;
- //(try-finally try-expr finally-expr)
- if(form.count() != 3)
- throw new IllegalArgumentException(
- "Wrong number of arguments, expecting: (try-finally try-expr finally-expr) ");
-
- if(context == C.EVAL || context == C.EXPRESSION)
- return analyze(context, RT.list(RT.list(FN, PersistentVector.EMPTY, form)));
-
- return new TryFinallyExpr(analyze(context, RT.second(form)),
- analyze(C.STATEMENT, RT.third(form)));
- }
- }
-}
+//static class TryFinallyExpr implements Expr{
+// final Expr tryExpr;
+// final Expr finallyExpr;
+//
+//
+// public TryFinallyExpr(Expr tryExpr, Expr finallyExpr){
+// this.tryExpr = tryExpr;
+// this.finallyExpr = finallyExpr;
+// }
+//
+// public Object eval() throws Exception{
+// throw new UnsupportedOperationException("Can't eval try");
+// }
+//
+// public void emit(C context, FnExpr fn, GeneratorAdapter gen){
+// Label startTry = gen.newLabel();
+// Label endTry = gen.newLabel();
+// Label end = gen.newLabel();
+// Label finallyLabel = gen.newLabel();
+// gen.visitTryCatchBlock(startTry, endTry, finallyLabel, null);
+// gen.mark(startTry);
+// tryExpr.emit(context, fn, gen);
+// gen.mark(endTry);
+// finallyExpr.emit(C.STATEMENT, fn, gen);
+// gen.goTo(end);
+// gen.mark(finallyLabel);
+// //exception should be on stack
+// finallyExpr.emit(C.STATEMENT, fn, gen);
+// gen.throwException();
+// gen.mark(end);
+// }
+//
+// public boolean hasJavaClass() throws Exception{
+// return tryExpr.hasJavaClass();
+// }
+//
+// public Class getJavaClass() throws Exception{
+// return tryExpr.getJavaClass();
+// }
+//
+// static class Parser implements IParser{
+// public Expr parse(C context, Object frm) throws Exception{
+// ISeq form = (ISeq) frm;
+// //(try-finally try-expr finally-expr)
+// if(form.count() != 3)
+// throw new IllegalArgumentException(
+// "Wrong number of arguments, expecting: (try-finally try-expr finally-expr) ");
+//
+// if(context == C.EVAL || context == C.EXPRESSION)
+// return analyze(context, RT.list(RT.list(FN, PersistentVector.EMPTY, form)));
+//
+// return new TryFinallyExpr(analyze(context, RT.second(form)),
+// analyze(C.STATEMENT, RT.third(form)));
+// }
+// }
+//}
static class ThrowExpr extends UntypedExpr{
final Expr excExpr;
@@ -1791,112 +1790,112 @@ static class NewExpr implements Expr{
}
-static class IdenticalExpr implements Expr{
- final Expr expr1;
- final Expr expr2;
-
-
- public IdenticalExpr(Expr expr1, Expr expr2){
- this.expr1 = expr1;
- this.expr2 = expr2;
- }
-
- public boolean hasJavaClass(){
- return true;
- }
-
- public Class getJavaClass(){
- return Boolean.class;
- }
-
- public Object eval() throws Exception{
- return expr1.eval() == expr2.eval() ?
- RT.T : RT.F;
- }
-
- public void emit(C context, FnExpr fn, GeneratorAdapter gen){
- if(context != C.STATEMENT)
- {
- Label not = gen.newLabel();
- Label end = gen.newLabel();
- expr1.emit(C.EXPRESSION, fn, gen);
- expr2.emit(C.EXPRESSION, fn, gen);
- gen.visitJumpInsn(IF_ACMPNE, not);
- gen.getStatic(BOOLEAN_OBJECT_TYPE, "TRUE", BOOLEAN_OBJECT_TYPE);
-// gen.getStatic(RT_TYPE, "T", KEYWORD_TYPE);
- gen.goTo(end);
- gen.mark(not);
- gen.getStatic(BOOLEAN_OBJECT_TYPE, "FALSE", BOOLEAN_OBJECT_TYPE);
-// NIL_EXPR.emit(C.EXPRESSION, fn, gen);
- gen.mark(end);
- }
- }
-
- static class Parser implements IParser{
- public Expr parse(C context, Object frm) throws Exception{
- ISeq form = (ISeq) frm;
- if(form.count() != 3)
- throw new Exception("wrong number of arguments, expecting: (identical? x y)");
-
- return new IdenticalExpr(analyze(C.EXPRESSION, RT.second(form)), analyze(C.EXPRESSION, RT.third(form)));
- }
- }
-}
-
-static class InstanceExpr implements Expr{
- final Expr expr;
- final Class c;
-
-
- public InstanceExpr(Expr expr, Class c){
- this.expr = expr;
- this.c = c;
- }
-
- public Object eval() throws Exception{
- return c.isInstance(expr.eval()) ?
- RT.T : RT.F;
- }
-
- public boolean hasJavaClass(){
- return true;
- }
-
- public Class getJavaClass(){
- return Boolean.class;
- }
-
- public void emit(C context, FnExpr fn, GeneratorAdapter gen){
- if(context != C.STATEMENT)
- {
- Label not = gen.newLabel();
- Label end = gen.newLabel();
- expr.emit(C.EXPRESSION, fn, gen);
- gen.instanceOf(Type.getType(c));
- gen.ifZCmp(GeneratorAdapter.EQ, not);
- gen.getStatic(BOOLEAN_OBJECT_TYPE, "TRUE", BOOLEAN_OBJECT_TYPE);
-// gen.getStatic(RT_TYPE, "T", KEYWORD_TYPE);
- gen.goTo(end);
- gen.mark(not);
- gen.getStatic(BOOLEAN_OBJECT_TYPE, "FALSE", BOOLEAN_OBJECT_TYPE);
-// NIL_EXPR.emit(C.EXPRESSION, fn, gen);
- gen.mark(end);
- }
- }
-
- static class Parser implements IParser{
- public Expr parse(C context, Object frm) throws Exception{
- ISeq form = (ISeq) frm;
- //(instance? x Classname)
- if(form.count() != 3)
- throw new Exception("wrong number of arguments, expecting: (instance? x Classname)");
- Class c = HostExpr.maybeClass(RT.third(form), true);
- if(c == null)
- throw new IllegalArgumentException("Unable to resolve classname: " + RT.third(form));
- return new InstanceExpr(analyze(C.EXPRESSION, RT.second(form)), c);
- }
- }
-}
+//static class IdenticalExpr implements Expr{
+// final Expr expr1;
+// final Expr expr2;
+//
+//
+// public IdenticalExpr(Expr expr1, Expr expr2){
+// this.expr1 = expr1;
+// this.expr2 = expr2;
+// }
+//
+// public boolean hasJavaClass(){
+// return true;
+// }
+//
+// public Class getJavaClass(){
+// return Boolean.class;
+// }
+//
+// public Object eval() throws Exception{
+// return expr1.eval() == expr2.eval() ?
+// RT.T : RT.F;
+// }
+//
+// public void emit(C context, FnExpr fn, GeneratorAdapter gen){
+// if(context != C.STATEMENT)
+// {
+// Label not = gen.newLabel();
+// Label end = gen.newLabel();
+// expr1.emit(C.EXPRESSION, fn, gen);
+// expr2.emit(C.EXPRESSION, fn, gen);
+// gen.visitJumpInsn(IF_ACMPNE, not);
+// gen.getStatic(BOOLEAN_OBJECT_TYPE, "TRUE", BOOLEAN_OBJECT_TYPE);
+//// gen.getStatic(RT_TYPE, "T", KEYWORD_TYPE);
+// gen.goTo(end);
+// gen.mark(not);
+// gen.getStatic(BOOLEAN_OBJECT_TYPE, "FALSE", BOOLEAN_OBJECT_TYPE);
+//// NIL_EXPR.emit(C.EXPRESSION, fn, gen);
+// gen.mark(end);
+// }
+// }
+//
+// static class Parser implements IParser{
+// public Expr parse(C context, Object frm) throws Exception{
+// ISeq form = (ISeq) frm;
+// if(form.count() != 3)
+// throw new Exception("wrong number of arguments, expecting: (identical? x y)");
+//
+// return new IdenticalExpr(analyze(C.EXPRESSION, RT.second(form)), analyze(C.EXPRESSION, RT.third(form)));
+// }
+// }
+//}
+
+//static class InstanceExpr implements Expr{
+// final Expr expr;
+// final Class c;
+//
+//
+// public InstanceExpr(Expr expr, Class c){
+// this.expr = expr;
+// this.c = c;
+// }
+//
+// public Object eval() throws Exception{
+// return c.isInstance(expr.eval()) ?
+// RT.T : RT.F;
+// }
+//
+// public boolean hasJavaClass(){
+// return true;
+// }
+//
+// public Class getJavaClass(){
+// return Boolean.class;
+// }
+//
+// public void emit(C context, FnExpr fn, GeneratorAdapter gen){
+// if(context != C.STATEMENT)
+// {
+// Label not = gen.newLabel();
+// Label end = gen.newLabel();
+// expr.emit(C.EXPRESSION, fn, gen);
+// gen.instanceOf(Type.getType(c));
+// gen.ifZCmp(GeneratorAdapter.EQ, not);
+// gen.getStatic(BOOLEAN_OBJECT_TYPE, "TRUE", BOOLEAN_OBJECT_TYPE);
+//// gen.getStatic(RT_TYPE, "T", KEYWORD_TYPE);
+// gen.goTo(end);
+// gen.mark(not);
+// gen.getStatic(BOOLEAN_OBJECT_TYPE, "FALSE", BOOLEAN_OBJECT_TYPE);
+//// NIL_EXPR.emit(C.EXPRESSION, fn, gen);
+// gen.mark(end);
+// }
+// }
+//
+// static class Parser implements IParser{
+// public Expr parse(C context, Object frm) throws Exception{
+// ISeq form = (ISeq) frm;
+// //(instance? x Classname)
+// if(form.count() != 3)
+// throw new Exception("wrong number of arguments, expecting: (instance? x Classname)");
+// Class c = HostExpr.maybeClass(RT.third(form), true);
+// if(c == null)
+// throw new IllegalArgumentException("Unable to resolve classname: " + RT.third(form));
+// return new InstanceExpr(analyze(C.EXPRESSION, RT.second(form)), c);
+// }
+// }
+//}
static class MetaExpr implements Expr{
final Expr expr;
@@ -3152,7 +3151,7 @@ private static Expr analyzeSymbol(Symbol sym) throws Exception{
return new VarExpr(v, tag);
}
else if(o instanceof Class)
- return new ClassExpr((Class) o);
+ return new QuoteExpr(o);
throw new Exception("Unable to resolve symbol: " + sym + " in this context");
@@ -3169,9 +3168,13 @@ static Object resolve(Symbol sym) throws Exception{
if(v == null)
throw new Exception("No such var: " + sym);
else if(v.ns != currentNS() && !v.isExported())
- throw new Exception("var: " + sym + " is not exported");
+ throw new IllegalAccessError("var: " + sym + " is not exported");
return v;
}
+ else if(sym.name.indexOf('.') > 0 || sym.name.charAt(0) == '[')
+ {
+ return Class.forName(sym.name);
+ }
else
{
Object o = currentNS().getMapping(sym);
@@ -3182,7 +3185,7 @@ static Object resolve(Symbol sym) throws Exception{
}
static Var lookupVar(Symbol sym, boolean internNew) throws Exception{
- Var var;
+ Var var = null;
//note - ns-qualified vars must already exist
if(sym.ns != null)
@@ -3199,7 +3202,8 @@ static Var lookupVar(Symbol sym, boolean internNew) throws Exception{
if(o == null)
{
//introduce a new var in the current ns
- var = currentNS().intern(Symbol.create(sym.name));
+ if(internNew)
+ var = currentNS().intern(Symbol.create(sym.name));
}
else if(o instanceof Var)
{
diff --git a/src/jvm/clojure/lang/Namespace.java b/src/jvm/clojure/lang/Namespace.java
index fb59fba9..8893b9e9 100644
--- a/src/jvm/clojure/lang/Namespace.java
+++ b/src/jvm/clojure/lang/Namespace.java
@@ -30,7 +30,7 @@ public IPersistentMap getMappings(){
return mappings.get();
}
-Var intern(Symbol sym){
+public Var intern(Symbol sym){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java
index 6312eb57..aa09bd9e 100644
--- a/src/jvm/clojure/lang/RT.java
+++ b/src/jvm/clojure/lang/RT.java
@@ -113,7 +113,8 @@ final static Symbol LOAD_FILE = Symbol.create("load-file");
final static Symbol IN_NAMESPACE = Symbol.create("in-namespace");
final static Symbol EXPORTS = Symbol.create("*exports*");
final static Var EXPORTS_VAR = Var.intern(CLOJURE_NS, EXPORTS, PersistentHashMap.EMPTY);
-final static Symbol EQL_REF = Symbol.create("eql-ref?");
+//final static Symbol EQL_REF = Symbol.create("eql-ref?");
+static final Symbol IDENTICAL = Symbol.create("identical?");
//symbol
final static Var CURRENT_NS = Var.intern(CLOJURE_NS, Symbol.create("*current-namespace*"),
@@ -143,24 +144,24 @@ final static IFn inNamespace = new AFn(){
}
};
//simple-symbol->var
-final static Var REFERS =
- Var.intern(CLOJURE_NS, Symbol.create("*refers*"),
- map(
- IN_NAMESPACE, Var.intern(CLOJURE_NS, IN_NAMESPACE, inNamespace),
- LOAD_FILE, Var.intern(CLOJURE_NS, LOAD_FILE,
- new AFn(){
- public Object invoke(Object arg1) throws Exception{
- return Compiler.loadFile((String) arg1);
- }
- }),
- EQL_REF, Var.intern(CLOJURE_NS, EQL_REF,
- new AFn(){
- public Object invoke(Object arg1, Object arg2)
- throws Exception{
- return arg1 == arg2 ? RT.T : RT.F;
- }
- })
- ));
+//final static Var REFERS =
+// Var.intern(CLOJURE_NS, Symbol.create("*refers*"),
+// map(
+// IN_NAMESPACE, Var.intern(CLOJURE_NS, IN_NAMESPACE, inNamespace),
+// LOAD_FILE, Var.intern(CLOJURE_NS, LOAD_FILE,
+// new AFn(){
+// public Object invoke(Object arg1) throws Exception{
+// return Compiler.loadFile((String) arg1);
+// }
+// }),
+// IDENTICAL, Var.intern(CLOJURE_NS, IDENTICAL,
+// new AFn(){
+// public Object invoke(Object arg1, Object arg2)
+// throws Exception{
+// return arg1 == arg2 ? RT.T : RT.F;
+// }
+// })
+// ));
//static Var NS_IMPORTS = Var.intern(CLOJURE_NS,Symbol.create("*ns-imports*"), IMPORTS);
//static Var NS_REFERS = Var.intern(CLOJURE_NS,Symbol.create("*ns-refers*"), REFERS);
@@ -170,7 +171,20 @@ static AtomicInteger id = new AtomicInteger(1);
static
{
- OUT.setTag(Symbol.create("java.io.OutputStreamWriter"));
+ Var.intern(CLOJURE_NS, IN_NAMESPACE, inNamespace);
+ Var.intern(CLOJURE_NS, LOAD_FILE,
+ new AFn(){
+ public Object invoke(Object arg1) throws Exception{
+ return Compiler.loadFile((String) arg1);
+ }
+ });
+ Var.intern(CLOJURE_NS, IDENTICAL,
+ new AFn(){
+ public Object invoke(Object arg1, Object arg2)
+ throws Exception{
+ return arg1 == arg2 ? RT.T : RT.F;
+ }
+ });
try
{
InputStream ins = RT.class.getResourceAsStream("/boot.clj");