KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FCallNodeCompiler.java
3  *
4  * Created on January 3, 2007, 2:20 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.FCallNode;
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 FCallNodeCompiler implements NodeCompiler {
22     
23     /** Creates a new instance of FCallNodeCompiler */
24     public FCallNodeCompiler() {
25     }
26     
27     public void compile(Node node, Compiler JavaDoc context) {
28         context.lineNumber(node.getPosition());
29         
30         FCallNode fcallNode = (FCallNode)node;
31         
32         if (fcallNode.getIterNode() == null) {
33             // no block, go for simple version
34
if (fcallNode.getArgsNode() != null) {
35                 // args compiler processes args and results in an args array for invocation
36
NodeCompiler argsCompiler = NodeCompilerFactory.getArgumentsCompiler(fcallNode.getArgsNode());
37                 
38                 argsCompiler.compile(fcallNode.getArgsNode(), context);
39
40                 context.invokeDynamic(fcallNode.getName(), false, true, null);
41             } else {
42                 context.invokeDynamic(fcallNode.getName(), false, false, null);
43             }
44         } else {
45             // FIXME: Missing blockpasnode stuff here
46
final IterNode iterNode = (IterNode) fcallNode.getIterNode();
47
48             // create the closure class and instantiate it
49
final ClosureCallback closureBody = new ClosureCallback() {
50                 public void compile(Compiler JavaDoc context) {
51                     NodeCompilerFactory.getCompiler(iterNode.getBodyNode()).compile(iterNode.getBodyNode(), context);
52                 }
53             };
54             
55             final ClosureCallback closureArg = new ClosureCallback() {
56                 public void compile(Compiler JavaDoc context) {
57                     context.createNewClosure(iterNode.getScope(), Arity.procArityOf(iterNode.getVarNode()).getValue(), closureBody);
58                 }
59             };
60
61             if (fcallNode.getArgsNode() != null) {
62                 // args compiler processes args and results in an args array for invocation
63
NodeCompiler argsCompiler = NodeCompilerFactory.getArgumentsCompiler(fcallNode.getArgsNode());
64                 
65                 argsCompiler.compile(fcallNode.getArgsNode(), context);
66
67                 context.invokeDynamic(fcallNode.getName(), false, true, closureArg);
68             } else {
69                 context.invokeDynamic(fcallNode.getName(), false, false, closureArg);
70             }
71         }
72     }
73     
74 }
75
Popular Tags