Search in sources :

Example 1 with SLEvalRootNode

use of com.oracle.truffle.sl.nodes.SLEvalRootNode in project graal by oracle.

the class SLLanguage method parse.

@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
    Source source = request.getSource();
    Map<String, RootCallTarget> functions;
    /*
         * Parse the provided source. At this point, we do not have a SLContext yet. Registration of
         * the functions with the SLContext happens lazily in SLEvalRootNode.
         */
    if (request.getArgumentNames().isEmpty()) {
        functions = Parser.parseSL(this, source);
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("function main(");
        String sep = "";
        for (String argumentName : request.getArgumentNames()) {
            sb.append(sep);
            sb.append(argumentName);
            sep = ",";
        }
        sb.append(") { return ");
        sb.append(request.getSource().getCharacters());
        sb.append(";}");
        Source decoratedSource = Source.newBuilder(sb.toString()).mimeType(request.getSource().getMimeType()).name(request.getSource().getName()).build();
        functions = Parser.parseSL(this, decoratedSource);
    }
    RootCallTarget main = functions.get("main");
    RootNode evalMain;
    if (main != null) {
        /*
             * We have a main function, so "evaluating" the parsed source means invoking that main
             * function. However, we need to lazily register functions into the SLContext first, so
             * we cannot use the original SLRootNode for the main function. Instead, we create a new
             * SLEvalRootNode that does everything we need.
             */
        evalMain = new SLEvalRootNode(this, main, functions);
    } else {
        /*
             * Even without a main function, "evaluating" the parsed source needs to register the
             * functions into the SLContext.
             */
        evalMain = new SLEvalRootNode(this, null, functions);
    }
    return Truffle.getRuntime().createCallTarget(evalMain);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Source(com.oracle.truffle.api.source.Source)

Example 2 with SLEvalRootNode

use of com.oracle.truffle.sl.nodes.SLEvalRootNode in project graal by oracle.

the class SLLexicalScope method createScope.

// The parameter node should not be assigned
@SuppressWarnings("all")
public static SLLexicalScope createScope(Node node) {
    SLBlockNode block = getParentBlock(node);
    if (block == null) {
        // We're in the root.
        block = findChildrenBlock(node);
        if (block == null) {
            // Corrupted SL AST, no block was found
            assert node.getRootNode() instanceof SLEvalRootNode : "Corrupted SL AST under " + node;
            return new SLLexicalScope(null, null, (SLBlockNode) null);
        }
        // node is above the block
        node = null;
    }
    // Test if there is a parent block. If not, we're in the root scope.
    SLBlockNode parentBlock = getParentBlock(block);
    if (parentBlock == null) {
        return new SLLexicalScope(node, block, block.getRootNode());
    } else {
        return new SLLexicalScope(node, block, parentBlock);
    }
}
Also used : SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode)

Aggregations

SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)2 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 Source (com.oracle.truffle.api.source.Source)1 SLBlockNode (com.oracle.truffle.sl.nodes.controlflow.SLBlockNode)1