Skip to content

Commit 59fede9

Browse files
committed
added identical?
1 parent 3c2c1e9 commit 59fede9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/jvm/clojure/lang/Compiler.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class Compiler implements Opcodes{
5252
static final Symbol MONITOR_ENTER = Symbol.create("monitor-enter");
5353
static final Symbol MONITOR_EXIT = Symbol.create("monitor-exit");
5454
static final Symbol INSTANCE = Symbol.create("instance?");
55+
static final Symbol IDENTICAL = Symbol.create("identical?");
5556

5657
static final Symbol THISFN = Symbol.create("thisfn");
5758
static final Symbol CLASS = Symbol.create("class");
@@ -88,6 +89,7 @@ public class Compiler implements Opcodes{
8889
MONITOR_ENTER, new MonitorEnterExpr.Parser(),
8990
MONITOR_EXIT, new MonitorExitExpr.Parser(),
9091
INSTANCE, new InstanceExpr.Parser(),
92+
IDENTICAL, new IdenticalExpr.Parser(),
9193
THISFN, null,
9294
CLASS, new ClassExpr.Parser(),
9395
NEW, new NewExpr.Parser(),
@@ -1555,6 +1557,48 @@ public Expr parse(C context, Object frm) throws Exception{
15551557

15561558
}
15571559

1560+
static class IdenticalExpr extends UntypedExpr{
1561+
final Expr expr1;
1562+
final Expr expr2;
1563+
1564+
1565+
public IdenticalExpr(Expr expr1, Expr expr2){
1566+
this.expr1 = expr1;
1567+
this.expr2 = expr2;
1568+
}
1569+
1570+
public Object eval() throws Exception{
1571+
return expr1.eval() == expr2.eval() ?
1572+
RT.T : null;
1573+
}
1574+
1575+
public void emit(C context, FnExpr fn, GeneratorAdapter gen){
1576+
if(context != C.STATEMENT)
1577+
{
1578+
Label not = gen.newLabel();
1579+
Label end = gen.newLabel();
1580+
expr1.emit(C.EXPRESSION, fn, gen);
1581+
expr2.emit(C.EXPRESSION, fn, gen);
1582+
gen.visitJumpInsn(IF_ACMPNE, not);
1583+
gen.getStatic(RT_TYPE, "T", KEYWORD_TYPE);
1584+
gen.goTo(end);
1585+
gen.mark(not);
1586+
NIL_EXPR.emit(C.EXPRESSION, fn, gen);
1587+
gen.mark(end);
1588+
}
1589+
}
1590+
1591+
static class Parser implements IParser{
1592+
public Expr parse(C context, Object frm) throws Exception{
1593+
ISeq form = (ISeq) frm;
1594+
if(form.count() != 3)
1595+
throw new Exception("wrong number of arguments, expecting: (identical? x y)");
1596+
1597+
return new IdenticalExpr(analyze(C.EXPRESSION, RT.second(form)), analyze(C.EXPRESSION, RT.third(form)));
1598+
}
1599+
}
1600+
}
1601+
15581602
static class InstanceExpr extends UntypedExpr{
15591603
final Expr expr;
15601604
final String className;

0 commit comments

Comments
 (0)