Package org.jruby.ast

Examples of org.jruby.ast.Node


        ArrayNode arrayNode = (ArrayNode) node;

        ArrayCallback callback = new ArrayCallback() {

                    public void nextValue(BodyCompiler context, Object sourceArray, int index) {
                        Node node = (Node) ((Object[]) sourceArray)[index];
                        compile(node, context);
                    }
                };

        context.createNewArray(arrayNode.childNodes().toArray(), callback, arrayNode.isLightweight());
View Full Code Here


    public void compileBlock(Node node, BodyCompiler context) {
        BlockNode blockNode = (BlockNode) node;

        for (Iterator<Node> iter = blockNode.childNodes().iterator(); iter.hasNext();) {
            Node n = iter.next();

            compile(n, context);

            if (iter.hasNext()) {
                // clear result from previous line
View Full Code Here

            hasCase = true;
        }

        context.pollThreadEvents();
       
        Node firstWhenNode = caseNode.getFirstWhenNode();

        // NOTE: Currently this optimization is limited to the following situations:
        // * No multi-valued whens
        // * All whens must be an int-ranged literal fixnum
        // It also still emits the code for the "safe" when logic, which is rather
View Full Code Here

    }

    @SuppressWarnings("unchecked")
    public void compileFixnumCase(final Node node, BodyCompiler context) {
        List<WhenNode> cases = new ArrayList<WhenNode>();
        Node elseBody = null;

        // first get all when nodes
        for (Node tmpNode = node; tmpNode != null; tmpNode = ((WhenNode)tmpNode).getNextCase()) {
            WhenNode whenNode = (WhenNode)tmpNode;

            cases.add(whenNode);

            if (whenNode.getNextCase() != null) {
                // if there's another node coming, make sure it will be a when...
                if (whenNode.getNextCase() instanceof WhenNode) {
                    // normal when, continue
                    continue;
                } else {
                    // ...or handle it as an else and we're done
                    elseBody = whenNode.getNextCase();
                    break;
                }
            }
        }
        // sort them based on literal value
        Collections.sort(cases, new Comparator() {
            public int compare(Object o1, Object o2) {
                WhenNode w1 = (WhenNode)o1;
                WhenNode w2 = (WhenNode)o2;
                Integer value1 = (int)((FixnumNode)((ArrayNode)w1.getExpressionNodes()).get(0)).getValue();
                Integer value2 = (int)((FixnumNode)((ArrayNode)w2.getExpressionNodes()).get(0)).getValue();
                return value1.compareTo(value2);
            }
        });
        final int[] intCases = new int[cases.size()];
        final Node[] bodies = new Node[intCases.length];

        // prepare arrays of int cases and code bodies
        for (int i = 0 ; i < intCases.length; i++) {
            intCases[i] = (int)((FixnumNode)((ArrayNode)cases.get(i).getExpressionNodes()).get(0)).getValue();
            bodies[i] = cases.get(i).getBodyNode();
        }
       
        final ArrayCallback callback = new ArrayCallback() {
            public void nextValue(BodyCompiler context, Object array, int index) {
                Node[] bodies = (Node[])array;
                if (bodies[index] != null) {
                    compile(bodies[index], context);
                } else {
                    context.loadNil();
                }
            }
        };

        final Node finalElse = elseBody;
        final CompilerCallback elseCallback = new CompilerCallback() {
            public void call(BodyCompiler context) {
                if (finalElse == null) {
                    context.loadNil();
                } else {
View Full Code Here

            // done with conditions, continue to next when in the chain
            compileWhen(whenNode.getNextCase(), context, hasCase);
            return;
        }

        Node tag = expressionsNode.get(conditionIndex);

        context.setLinePosition(tag.getPosition());

        // reduce the when cases to a true or false ruby value for the branch below
        if (tag instanceof WhenNode) {
            // prepare to handle the when logic
            if (hasCase) {
View Full Code Here

    }

    public void compileClass(Node node, BodyCompiler context) {
        final ClassNode classNode = (ClassNode) node;

        final Node superNode = classNode.getSuperNode();

        final Node cpathNode = classNode.getCPath();

        CompilerCallback superCallback = new CompilerCallback() {

                    public void call(BodyCompiler context) {
                        compile(superNode, context);
                    }
                };
        if (superNode == null) {
            superCallback = null;
        }

        CompilerCallback bodyCallback = new CompilerCallback() {

                    public void call(BodyCompiler context) {
                        boolean oldIsAtRoot = isAtRoot;
                        isAtRoot = false;
                        if (classNode.getBodyNode() != null) {
                            compile(classNode.getBodyNode(), context);
                        } else {
                            context.loadNil();
                        }
                        isAtRoot = oldIsAtRoot;
                    }
                };

        CompilerCallback pathCallback = new CompilerCallback() {

                    public void call(BodyCompiler context) {
                        if (cpathNode instanceof Colon2Node) {
                            Node leftNode = ((Colon2Node) cpathNode).getLeftNode();
                            if (leftNode != null) {
                                compile(leftNode, context);
                            } else {
                                context.loadNil();
                            }
View Full Code Here

    }

    public void compileConstDecl(Node node, BodyCompiler context) {
        // TODO: callback for value would be more efficient, but unlikely to be a big cost (constants are rarely assigned)
        ConstDeclNode constDeclNode = (ConstDeclNode) node;
        Node constNode = constDeclNode.getConstNode();

        if (constNode == null) {
            compile(constDeclNode.getValueNode(), context);

            context.assignConstantInCurrent(constDeclNode.getName());
View Full Code Here

    }

    public void compileConstDeclAssignment(Node node, BodyCompiler context) {
        // TODO: callback for value would be more efficient, but unlikely to be a big cost (constants are rarely assigned)
        ConstDeclNode constDeclNode = (ConstDeclNode) node;
        Node constNode = constDeclNode.getConstNode();

        if (constNode == null) {
            context.assignConstantInCurrent(constDeclNode.getName());
        } else if (constNode.nodeId == NodeType.COLON2NODE) {
            compile(((Colon2Node) constNode).getLeftNode(), context);
View Full Code Here

        context.retrieveConstant(constNode.getName());
    }

    public void compileColon2(Node node, BodyCompiler context) {
        final Colon2Node iVisited = (Colon2Node) node;
        Node leftNode = iVisited.getLeftNode();
        final String name = iVisited.getName();

        if (leftNode == null) {
            context.loadObject();
            context.retrieveConstantFromModule(name);
View Full Code Here

        if (node == null) {
            context.pushString(type);
        } else if (node instanceof ArrayNode) {
            Object endToken = context.getNewEnding();
            for (int i = 0; i < ((ArrayNode) node).size(); i++) {
                Node iterNode = ((ArrayNode) node).get(i);
                compileGetDefinition(iterNode, context);
                context.ifNull(endToken);
            }
            context.pushString(type);
            Object realToken = context.getNewEnding();
View Full Code Here

TOP

Related Classes of org.jruby.ast.Node

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.