LList parameters = (LList) ((LList) tokens).getFirst();
LList body = (LList) ((LList) tokens).getRest();
return new LLambda(parameters, body, environment);
}
});
// (if condition ifExpression elseExpression)
root.put(LSymbol.get("if"), new LOperation("if") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LObject condition = ((LList) tokens).get(0);
LObject result = condition.run(environment, tokens);
if (result.equals(LBoolean.TRUE)) {
LObject ifExpression = ((LList) tokens).get(1);
return ifExpression.run(environment, tokens);
}
else {
LObject elseExpression = ((LList) tokens).get(2);
return elseExpression.run(environment, tokens);
}
}
});
// (for (define i 0) (< i 5) (set! i (+ i 1)) (print i))
root.put(LSymbol.get("for"), new LOperation("for") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LList define = (LList) ((LList) tokens).get(0);
LList condition = (LList) ((LList) tokens).get(1);
LList increment = (LList) ((LList) tokens).get(2);
LList block = (LList) ((LList) tokens).get(3);
Environment innerEnvironment = new Environment(environment);
define.evaluate(innerEnvironment, tokens);
LObject result = LNull.NULL;
while(condition.evaluate(innerEnvironment, tokens) == LBoolean.TRUE) {
result = block.evaluate(innerEnvironment, tokens);
increment.evaluate(innerEnvironment, tokens);
}
return result;
}
});
// (while (true) (print i))
root.put(LSymbol.get("while"), new LOperation("while") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LList condition = (LList) ((LList) tokens).get(0);
LList block = (LList) ((LList) tokens).get(1);
LObject result = LNull.NULL;
while(condition.evaluate(environment, tokens) == LBoolean.TRUE) {
result = block.evaluate(environment, tokens);
}
return result;
}
});
// (do (true) (print i))
root.put(LSymbol.get("do"), new LOperation("do") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LList condition = (LList) ((LList) tokens).get(0);
LList block = (LList) ((LList) tokens).get(1);
LObject result = LNull.NULL;
do {
result = block.evaluate(environment, tokens);
}while(condition.evaluate(environment, tokens) == LBoolean.TRUE);
return result;
}
});
/**
* Java functions
*/
// (import "name")
root.put(LSymbol.get("import"), new LOperation("import") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LList list = (LList) tokens;
for (LObject object: list) {
String tmp = object.toString();
System.out.println("import " + tmp);
if(tmp.endsWith("*")) {
System.out.println("package");
LJava.importPackage(tmp.substring(0, tmp.length()-2));
}
else {
System.out.println("class");
LJava.importClass(tmp);
}
}
return LBoolean.TRUE;
}
});
// (interface Interface.class object)
root.put(LSymbol.get("interface"), new LOperation("interface") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LList list = (LList) tokens;
LObject c = list.get(0).run(environment, tokens);
LLambda lambda = (LLambda) list.get(1).run(environment, tokens);
LJClass cl = (LJClass) c.run(environment, tokens);
return LJava.createInterface((Class<?>) cl.getJObject(), lambda, environment);
}
});
// (class Class.class object)
root.put(LSymbol.get("class"), new LOperation("class") {
@Override
public LObject evaluate(Environment environment, LObject tokens) {
LList list = (LList) tokens;
LObject c = list.get(0).run(environment, tokens);
LLambda lambda = (LLambda) list.get(1).run(environment, tokens);
LJClass cl = (LJClass) c.run(environment, tokens);
return LJava.createClass((Class<?>) cl.getJObject(), lambda, environment);
}