KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * IfNodeCompiler.java
3  *
4  * Created on January 3, 2007, 4:42 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.IfNode;
13 import org.jruby.ast.Node;
14
15 /**
16  *
17  * @author headius
18  */

19 public class IfNodeCompiler implements NodeCompiler {
20     
21     /** Creates a new instance of IfNodeCompiler */
22     public IfNodeCompiler() {
23     }
24     
25     public void compile(Node node, Compiler JavaDoc context) {
26         context.lineNumber(node.getPosition());
27         
28         final IfNode ifNode = (IfNode)node;
29         
30         NodeCompilerFactory.getCompiler(ifNode.getCondition()).compile(ifNode.getCondition(), context);
31         
32         BranchCallback trueCallback = new BranchCallback() {
33             public void branch(Compiler JavaDoc context) {
34                 NodeCompilerFactory.getCompiler(ifNode.getThenBody()).compile(ifNode.getThenBody(), context);
35             }
36         };
37         
38         BranchCallback falseCallback = new BranchCallback() {
39             public void branch(Compiler JavaDoc context) {
40                 if (ifNode.getElseBody() != null) {
41                     NodeCompilerFactory.getCompiler(ifNode.getElseBody()).compile(ifNode.getElseBody(), context);
42                 } else {
43                     context.loadNil();
44                 }
45             }
46         };
47         
48         context.performBooleanBranch(trueCallback, falseCallback);
49     }
50     
51 }
52
Popular Tags