-
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.
- Loading branch information
Showing
16 changed files
with
325 additions
and
38 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
language/src/main/java/com/github/arvyy/islisp/functions/ISLISPEval.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,58 @@ | ||
package com.github.arvyy.islisp.functions; | ||
|
||
import com.github.arvyy.islisp.ISLISPContext; | ||
import com.github.arvyy.islisp.nodes.ISLISPErrorSignalerNode; | ||
import com.github.arvyy.islisp.runtime.LispFunction; | ||
import com.oracle.truffle.api.TruffleLanguage; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.nodes.RootNode; | ||
import com.oracle.truffle.api.source.Source; | ||
|
||
/** | ||
* Implements `eval` function used for interop with other truffle languages. | ||
*/ | ||
public class ISLISPEval extends RootNode { | ||
|
||
@Child | ||
private ISLISPErrorSignalerNode errorSignalerNode; | ||
|
||
ISLISPEval(TruffleLanguage<?> lang) { | ||
super(lang); | ||
errorSignalerNode = new ISLISPErrorSignalerNode(this); | ||
} | ||
|
||
@Override | ||
public Object execute(VirtualFrame frame) { | ||
if (frame.getArguments().length != 3) { | ||
return errorSignalerNode.signalWrongArgumentCount( | ||
frame.getArguments().length - 1, | ||
2, | ||
2); | ||
} | ||
var lang = (String) frame.getArguments()[1]; | ||
var ctx = ISLISPContext.get(this); | ||
var env = ctx.getEnv(); | ||
/* | ||
var filePath = (String) frame.getArguments()[2]; | ||
var file = env.getPublicTruffleFile(filePath); | ||
*/ | ||
var script = (String) frame.getArguments()[2]; | ||
try { | ||
var source = Source.newBuilder(lang, script, "<eval>").build(); | ||
var target = env.parsePublic(source); | ||
return target.call(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Construct LispFunction using this root node. | ||
* | ||
* @param lang truffle language reference | ||
* @return lisp function | ||
*/ | ||
public static LispFunction makeLispFunction(TruffleLanguage<?> lang) { | ||
return new LispFunction(new ISLISPEval(lang).getCallTarget()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
language/src/main/java/com/github/arvyy/islisp/functions/ISLISPTruffleObjectFields.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,81 @@ | ||
package com.github.arvyy.islisp.functions; | ||
|
||
import com.github.arvyy.islisp.ISLISPContext; | ||
import com.github.arvyy.islisp.exceptions.ISLISPError; | ||
import com.github.arvyy.islisp.nodes.ISLISPErrorSignalerNode; | ||
import com.github.arvyy.islisp.runtime.LispFunction; | ||
import com.github.arvyy.islisp.runtime.LispVector; | ||
import com.oracle.truffle.api.TruffleLanguage; | ||
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.interop.InteropLibrary; | ||
import com.oracle.truffle.api.interop.InvalidArrayIndexException; | ||
import com.oracle.truffle.api.interop.UnsupportedMessageException; | ||
import com.oracle.truffle.api.library.CachedLibrary; | ||
import com.oracle.truffle.api.nodes.RootNode; | ||
|
||
/** | ||
* Implements `truffle-object-fields` function, which returns fields as a list of strings | ||
* in a given object. | ||
*/ | ||
public abstract class ISLISPTruffleObjectFields extends RootNode { | ||
|
||
@Child | ||
private ISLISPErrorSignalerNode errorSignalerNode; | ||
|
||
ISLISPTruffleObjectFields(TruffleLanguage<?> language) { | ||
super(language); | ||
errorSignalerNode = new ISLISPErrorSignalerNode(this); | ||
} | ||
|
||
@Override | ||
public final Object execute(VirtualFrame frame) { | ||
if (frame.getArguments().length != 2) { | ||
return errorSignalerNode.signalWrongArgumentCount(frame.getArguments().length - 1, 1, 1); | ||
} | ||
try { | ||
return executeGeneric(frame.getArguments()[1]); | ||
} catch (Exception e) { | ||
throw new ISLISPError(e.getMessage(), this); | ||
} | ||
} | ||
|
||
abstract Object executeGeneric(Object obj) throws UnsupportedMessageException, InvalidArrayIndexException; | ||
|
||
@Specialization(guards = { | ||
"interopLibrary.hasMembers(o)" | ||
}, limit = "3") | ||
Object doTruffleInterop( | ||
Object o, | ||
@CachedLibrary("o") InteropLibrary interopLibrary | ||
) throws UnsupportedMessageException, InvalidArrayIndexException { | ||
InteropLibrary uncached = InteropLibrary.getUncached(); | ||
var membersInterop = interopLibrary.getMembers(o); | ||
var copy = new Object[(int) uncached.getArraySize(membersInterop)]; | ||
for (int i = 0; i < copy.length; i++) { | ||
var fieldInterop = uncached.readArrayElement(membersInterop, i); | ||
var field = uncached.asString(fieldInterop); | ||
copy[i] = field; | ||
} | ||
return new LispVector(copy); | ||
} | ||
|
||
@Fallback | ||
Object fallback(Object o) { | ||
var ctx = ISLISPContext.get(this); | ||
var expectedClass = ctx.lookupClass("<truffle-object>"); | ||
return errorSignalerNode.signalWrongType(o, expectedClass); | ||
} | ||
|
||
/** | ||
* Construct LispFunction using this root node. | ||
* | ||
* @param lang truffle language reference | ||
* @return lisp function | ||
*/ | ||
public static LispFunction makeLispFunction(TruffleLanguage<?> lang) { | ||
return new LispFunction(ISLISPTruffleObjectFieldsNodeGen.create(lang).getCallTarget()); | ||
} | ||
|
||
} |
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
4 changes: 3 additions & 1 deletion
4
language/src/main/java/com/github/arvyy/islisp/runtime/LispArray.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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
package com.github.arvyy.islisp.runtime; | ||
|
||
import com.oracle.truffle.api.interop.TruffleObject; | ||
|
||
/** | ||
* Lisp array. | ||
* | ||
* @param data nested Object[] values | ||
* @param dimensions amount of nesting; 2+. | ||
*/ | ||
public record LispArray(Object[] data, int dimensions) { | ||
public record LispArray(Object[] data, int dimensions) implements TruffleObject { | ||
} |
4 changes: 3 additions & 1 deletion
4
language/src/main/java/com/github/arvyy/islisp/runtime/LispChar.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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package com.github.arvyy.islisp.runtime; | ||
|
||
import com.oracle.truffle.api.interop.TruffleObject; | ||
|
||
/** | ||
* Signifies LISP character type, carrying around codepoint. | ||
* | ||
* @param codepoint | ||
*/ | ||
public record LispChar(int codepoint) { | ||
public record LispChar(int codepoint) implements TruffleObject { | ||
} |
Oops, something went wrong.