How to create a minimal language? #3190
Answered
by
SchrodingerZhu
SchrodingerZhu
asked this question in
Q&A
-
So I tried to create a language with only expressions class Nothing {}
@TruffleLanguage.Registration(
id = Language.ID,
name = "testlang",
defaultMimeType = Language.MIME_TYPE,
characterMimeTypes = Language.MIME_TYPE)
public class Language extends TruffleLanguage<Nothing>{
public static final String ID = "testlang";
public static final String MIME_TYPE = "application/x-testlang";
@Override
protected Nothing createContext(Env env) {
return new Nothing();
}
@Override
protected CallTarget parse(ParsingRequest request) {
Source source = request.getSource();
var code = source.getCharacters().toString();
var root = new Parser(code).parse();
return Truffle.getRuntime().createCallTarget(root);
}
} and public class Main {
private static String TEST_LANG = "testlang";
public static void main(String[] args) {
Source src;
try {
src = Source.newBuilder(TEST_LANG, new InputStreamReader(System.in), "<stdin>")
.build();
} catch (IOException ex) {
System.err.println(ex.getMessage());
return;
}
try (Context context = Context.newBuilder(TEST_LANG)
.in(System.in)
.out(System.out)
.build()) {
Value result = context.eval(src);
if (result != null) {
System.out.println(result);
}
} catch (PolyglotException ex) {
System.err.println(ex.getMessage());
}
}
} The graalvm is invoked with
However, with the above commands, I get a nullpointerexception caused by // package com.oracle.truffle.polyglot;
public Object getPolyglotEngine(Object polyglotLanguageInstance) {
return ((PolyglotLanguageInstance) polyglotLanguageInstance).language.engine;
} Actually, I cannot find more details why this is null. |
Beta Was this translation helpful? Give feedback.
Answered by
SchrodingerZhu
Feb 7, 2021
Replies: 2 comments
-
The parser returns correctly with a public class Toplevel extends RootNode {
Expression expr;
public Toplevel(TruffleLanguage<?> language, Expression mainExpr) {
super(language);
expr = mainExpr;
}
@Override
public Object execute(VirtualFrame frame) {
var result = expr.executeGeneric(frame);
System.out.println(result);
return 0;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Ha, I see the reason, I forget to pass the language instance to |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
thomaswue
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ha, I see the reason, I forget to pass the language instance to
Toplevel