-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add defclass :accessor and :boundp methods
- Loading branch information
Showing
7 changed files
with
142 additions
and
14 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
language/src/main/java/com/github/arvyy/islisp/functions/ISLISPClassSlotBoundp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.arvyy.islisp.functions; | ||
|
||
import com.github.arvyy.islisp.ISLISPContext; | ||
import com.github.arvyy.islisp.exceptions.ISLISPError; | ||
import com.github.arvyy.islisp.runtime.StandardClass; | ||
import com.github.arvyy.islisp.runtime.StandardClassObject; | ||
import com.github.arvyy.islisp.runtime.Symbol; | ||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.TruffleLanguage; | ||
import com.oracle.truffle.api.dsl.Cached; | ||
import com.oracle.truffle.api.dsl.Fallback; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.nodes.RootNode; | ||
import com.oracle.truffle.api.staticobject.StaticProperty; | ||
|
||
/** | ||
* Function instantiated for defclass slots with :boundp option. | ||
*/ | ||
public abstract class ISLISPClassSlotBoundp extends RootNode { | ||
|
||
private final Symbol slot; | ||
|
||
/** | ||
* Create slot boundp root node. | ||
* | ||
* @param slot slot's name | ||
* @param language language reference | ||
*/ | ||
public ISLISPClassSlotBoundp(Symbol slot, TruffleLanguage<?> language) { | ||
super(language); | ||
this.slot = slot; | ||
} | ||
|
||
@Override | ||
public final Object execute(VirtualFrame frame) { | ||
var obj = frame.getArguments()[1]; | ||
return executeGeneric(obj); | ||
} | ||
|
||
abstract Object executeGeneric(Object object); | ||
|
||
@Specialization(guards = "clsObject.clazz() == clazz", limit = "999") | ||
Object doSpecialized( | ||
StandardClassObject clsObject, | ||
@Cached("clsObject.clazz()") StandardClass clazz, | ||
@Cached("lookupProperty(clazz)") StaticProperty property | ||
) { | ||
var ctx = ISLISPContext.get(this); | ||
return property.getObject(clsObject.data()) == null | ||
? ctx.getNil() | ||
: ctx.getT(); | ||
} | ||
|
||
@Specialization | ||
Object doUnspecialized(StandardClassObject clsObject) { | ||
var property = lookupProperty(clsObject.clazz()); | ||
var ctx = ISLISPContext.get(this); | ||
return property.getObject(clsObject.data()) == null | ||
? ctx.getNil() | ||
: ctx.getT(); | ||
} | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
StaticProperty lookupProperty(StandardClass clazz) { | ||
for (var classSlot: clazz.slots()) { | ||
if (classSlot.name().equals(slot.identityReference())) { | ||
return classSlot.property(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Fallback | ||
Object doFallback(Object o) { | ||
throw new ISLISPError("Bad parameter", this); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
2 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters