KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > compiler > CallNodeCompiler


1 /*
2  * CallNodeCompiler.java
3  *
4  * Created on January 3, 2007, 4:11 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.jruby.compiler;
11
12 import org.jruby.ast.CallNode;
13 import org.jruby.ast.IterNode;
14 import org.jruby.ast.Node;
15 import org.jruby.runtime.Arity;
16
17 /**
18  *
19  * @author headius
20  */

21 public class CallNodeCompiler implements NodeCompiler {
22     
23     /** Creates a new instance of CallNodeCompiler */
24     public CallNodeCompiler() {
25     }
26     
27     public void compile(Node node, Compiler JavaDoc context) {
28         context.lineNumber(node.getPosition());
29         
30         CallNode callNode = (CallNode)node;
31         
32         if (callNode.getIterNode() == null) {
33             // no block, go for simple version
34

35             // handle receiver
36
NodeCompilerFactory.getCompiler(callNode.getReceiverNode()).compile(callNode.getReceiverNode(), context);
37             
38             if (callNode.getArgsNode() != null) {
39                 // args compiler processes args and results in an args array for invocation
40
NodeCompiler argsCompiler = NodeCompilerFactory.getArgumentsCompiler(callNode.getArgsNode());
41                 
42                 argsCompiler.compile(callNode.getArgsNode(), context);
43
44                 context.invokeDynamic(callNode.getName(), true, true, null);
45             } else {
46                 context.invokeDynamic(callNode.getName(), true, false, null);
47             }
48         } else {
49             // FIXME: Missing blockpassnode handling
50
final IterNode iterNode = (IterNode) callNode.getIterNode();
51
52             // create the closure class and instantiate it
53
final ClosureCallback closureBody = new ClosureCallback() {
54                 public void compile(Compiler JavaDoc context) {
55                     NodeCompilerFactory.getCompiler(iterNode.getBodyNode()).compile(iterNode.getBodyNode(), context);
56                 }
57             };
58             
59             final ClosureCallback closureArg = new ClosureCallback() {
60                 public void compile(Compiler JavaDoc context) {
61                     context.createNewClosure(iterNode.getScope(), Arity.procArityOf(iterNode.getVarNode()).getValue(), closureBody);
62                 }
63             };
64             
65             // handle receiver
66
NodeCompilerFactory.getCompiler(callNode.getReceiverNode()).compile(callNode.getReceiverNode(), context);
67             
68             if (callNode.getArgsNode() != null) {
69                 // args compiler processes args and results in an args array for invocation
70
NodeCompiler argsCompiler = NodeCompilerFactory.getArgumentsCompiler(callNode.getArgsNode());
71                 
72                 argsCompiler.compile(callNode.getArgsNode(), context);
73
74                 context.invokeDynamic(callNode.getName(), true, true, closureArg);
75             } else {
76                 context.invokeDynamic(callNode.getName(), true, false, closureArg);
77             }
78         }
79     }
80     
81 }
82
Popular Tags