Search in sources :

Example 6 with SLBlockNode

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

the class SLLexicalScope method collectVars.

private Map<String, FrameSlot> collectVars(Node varsBlock, Node currentNode) {
    // Variables are slot-based.
    // To collect declared variables, traverse the block's AST and find slots associated
    // with SLWriteLocalVariableNode. The traversal stops when we hit the current node.
    Map<String, FrameSlot> slots = new LinkedHashMap<>(4);
    NodeUtil.forEachChild(varsBlock, new NodeVisitor() {

        @Override
        public boolean visit(Node node) {
            if (node == currentNode) {
                return false;
            }
            // Do not enter any nested blocks.
            if (!(node instanceof SLBlockNode)) {
                boolean all = NodeUtil.forEachChild(node, this);
                if (!all) {
                    return false;
                }
            }
            // Write to a variable is a declaration unless it exists already in a parent scope.
            if (node instanceof SLWriteLocalVariableNode) {
                SLWriteLocalVariableNode wn = (SLWriteLocalVariableNode) node;
                String name = Objects.toString(wn.getSlot().getIdentifier());
                if (!hasParentVar(name)) {
                    slots.put(name, wn.getSlot());
                }
            }
            return true;
        }
    });
    return slots;
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) LinkedHashMap(java.util.LinkedHashMap) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor)

Aggregations

SLBlockNode (com.oracle.truffle.sl.nodes.controlflow.SLBlockNode)6 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)5 SLStatementNode (com.oracle.truffle.sl.nodes.SLStatementNode)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 NodeVisitor (com.oracle.truffle.api.nodes.NodeVisitor)2 FrameSlot (com.oracle.truffle.api.frame.FrameSlot)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1