Skip to content

Commit 0d7aed7

Browse files
committed
add support for constant vectors and sets
1 parent 662b384 commit 0d7aed7

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/jvm/clojure/lang/Compiler.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,15 +2575,31 @@ public Class getJavaClass() throws Exception{
25752575

25762576
static public Expr parse(C context, IPersistentSet form) throws Exception{
25772577
IPersistentVector keys = PersistentVector.EMPTY;
2578+
boolean constant = true;
2579+
25782580
for(ISeq s = RT.seq(form); s != null; s = s.next())
25792581
{
25802582
Object e = s.first();
2581-
keys = (IPersistentVector) keys.cons(analyze(context == C.EVAL ? context : C.EXPRESSION, e));
2583+
Expr expr = analyze(context == C.EVAL ? context : C.EXPRESSION, e);
2584+
keys = (IPersistentVector) keys.cons(expr);
2585+
if(!(expr instanceof LiteralExpr))
2586+
constant = false;
25822587
}
25832588
Expr ret = new SetExpr(keys);
25842589
if(form instanceof IObj && ((IObj) form).meta() != null)
25852590
return new MetaExpr(ret, MapExpr
25862591
.parse(context == C.EVAL ? context : C.EXPRESSION, ((IObj) form).meta()));
2592+
else if(constant)
2593+
{
2594+
IPersistentSet set = PersistentHashSet.EMPTY;
2595+
for(int i=0;i<keys.count();i++)
2596+
{
2597+
LiteralExpr ve = (LiteralExpr)keys.nth(i);
2598+
set = (IPersistentSet)set.cons(ve.val());
2599+
}
2600+
// System.err.println("Constant: " + set);
2601+
return new ConstantExpr(set);
2602+
}
25872603
else
25882604
return ret;
25892605
}
@@ -2621,13 +2637,31 @@ public Class getJavaClass() throws Exception{
26212637
}
26222638

26232639
static public Expr parse(C context, IPersistentVector form) throws Exception{
2640+
boolean constant = true;
2641+
26242642
IPersistentVector args = PersistentVector.EMPTY;
26252643
for(int i = 0; i < form.count(); i++)
2626-
args = (IPersistentVector) args.cons(analyze(context == C.EVAL ? context : C.EXPRESSION, form.nth(i)));
2644+
{
2645+
Expr v = analyze(context == C.EVAL ? context : C.EXPRESSION, form.nth(i));
2646+
args = (IPersistentVector) args.cons(v);
2647+
if(!(v instanceof LiteralExpr))
2648+
constant = false;
2649+
}
26272650
Expr ret = new VectorExpr(args);
26282651
if(form instanceof IObj && ((IObj) form).meta() != null)
26292652
return new MetaExpr(ret, MapExpr
26302653
.parse(context == C.EVAL ? context : C.EXPRESSION, ((IObj) form).meta()));
2654+
else if (constant)
2655+
{
2656+
PersistentVector rv = PersistentVector.EMPTY;
2657+
for(int i =0;i<args.count();i++)
2658+
{
2659+
LiteralExpr ve = (LiteralExpr)args.nth(i);
2660+
rv = rv.cons(ve.val());
2661+
}
2662+
// System.err.println("Constant: " + rv);
2663+
return new ConstantExpr(rv);
2664+
}
26312665
else
26322666
return ret;
26332667
}

0 commit comments

Comments
 (0)