KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 public class WhileNodeCompiler implements NodeCompiler {
20     
21     /** Creates a new instance of WhileNodeCompiler */
22     public WhileNodeCompiler() {
23     }
24     
25     public void compile(Node node, Compiler JavaDoc context) {
26         context.lineNumber(node.getPosition());
27         
28         final WhileNode whileNode = (WhileNode)node;
29         
30         BranchCallback condition = new BranchCallback() {
31             public void branch(Compiler JavaDoc context) {
32                 NodeCompilerFactory.getCompiler(whileNode.getConditionNode()).compile(whileNode.getConditionNode(), context);
33             }
34         };
35         
36         BranchCallback body = new BranchCallback() {
37             public void branch(Compiler JavaDoc context) {
38                 // this could probably be more efficient, and just avoid popping values for each loop
39
// when no values are being generated
40
if (whileNode.getBodyNode() == null) {
41                     context.loadNil();
42                     return;
43                 }
44                 NodeCompilerFactory.getCompiler(whileNode.getBodyNode()).compile(whileNode.getBodyNode(), context);
45             }
46         };
47         
48         context.performBooleanLoop(condition, body, whileNode.evaluateAtStart());
49     }
50     
51 }
52
Popular Tags