Skip to content

Commit

Permalink
[java-truffle] step 8
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcgill authored and kanaka committed May 31, 2021
1 parent 0cde9e9 commit 146ecc8
Show file tree
Hide file tree
Showing 3 changed files with 794 additions and 0 deletions.
88 changes: 88 additions & 0 deletions impls/java-truffle/src/main/java/truffle/mal/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Core {
NS.put("cons", ConsBuiltinFactory.getInstance());
NS.put("concat", ConcatBuiltinFactory.getInstance());
NS.put("vec", VecBuiltinFactory.getInstance());

NS.put("nth", NthBuiltinFactory.getInstance());
NS.put("first", FirstBuiltinFactory.getInstance());
NS.put("rest", RestBuiltinFactory.getInstance());
}

static MalEnv newGlobalEnv(Class<? extends TruffleLanguage<?>> languageClass, TruffleLanguage<?> language) {
Expand Down Expand Up @@ -538,6 +542,90 @@ protected MalVector vec(MalList l) {
}
}

@NodeChild(value="list", type=ReadArgNode.class)
@NodeChild(value="n", type=ReadArgNode.class)
@GenerateNodeFactory
abstract class NthBuiltin extends BuiltinNode {

protected NthBuiltin() { super("nth"); }

@Specialization
@TruffleBoundary
protected Object nth(MalVector vec, long n) {
if (n >= vec.size()) {
throwInvalidArgument();
}
return vec.get((int)n);
}

private void throwInvalidArgument() {
throw new MalException("Out of bounds");
}

@Specialization
protected Object nth(MalList list, long n) {
if (n >= list.length) {
throwInvalidArgument();
}
while (--n >= 0) {
list = list.tail;
}
return list.head;
}
}

@GenerateNodeFactory
@NodeChild(value="arg", type=ReadArgNode.class)
abstract class FirstBuiltin extends BuiltinNode {
protected FirstBuiltin() { super("first"); }

@Specialization
protected MalNil first(MalNil nil) {
return MalNil.NIL;
}

@Specialization
protected Object first(MalVector vec) {
if (vec.size() == 0)
return MalNil.NIL;
return vec.get(0);
}

@Specialization
protected Object first(MalList list) {
if (list.head == null) {
return MalNil.NIL;
}
return list.head;
}
}

@NodeChild(value="arg", type=ReadArgNode.class)
@GenerateNodeFactory
abstract class RestBuiltin extends BuiltinNode {

protected RestBuiltin() { super("rest"); }

@Specialization
protected MalList rest(MalNil nil) {
return MalList.EMPTY;
}

@Specialization
@TruffleBoundary
protected MalList rest(MalVector vec) {
return rest(vec.toList());
}

@Specialization
protected MalList rest(MalList list) {
if (list.head == null) {
return list;
}
return list.tail;
}
}

/************* COMPARISONS *************/

@NodeChild(value="lhs", type=ReadArgNode.class)
Expand Down
9 changes: 9 additions & 0 deletions impls/java-truffle/src/main/java/truffle/mal/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,20 @@ class MalFunction extends MalValue implements TruffleObject {
final RootCallTarget callTarget;
final MalEnv closedOverEnv;
final int numArgs;
final boolean isMacro;

MalFunction(RootCallTarget callTarget, MalEnv closedOverEnv, int numArgs) {
this.callTarget = callTarget;
this.closedOverEnv = closedOverEnv;
this.numArgs = numArgs;
this.isMacro = false;
}

MalFunction(MalFunction f, boolean isMacro) {
this.callTarget = f.callTarget;
this.closedOverEnv = f.closedOverEnv;
this.numArgs = f.numArgs;
this.isMacro = isMacro;
}

@ExportMessage
Expand Down
Loading

0 comments on commit 146ecc8

Please sign in to comment.