1 9 10 package org.jruby; 11 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.IOException ; 15 import org.jruby.ast.Node; 16 import org.jruby.compiler.NodeCompilerFactory; 17 import org.jruby.compiler.NotCompilableException; 18 import org.jruby.compiler.impl.StandardASMCompiler; 19 20 24 public class JRubyC { 25 26 public static void main(String args[]) { 27 Ruby runtime = Ruby.getDefaultInstance(); 28 29 try { 30 if (args.length < 1 || args.length > 2) { 31 System.out.println("Usage: jrubyc <filename> [<dest>]"); 32 return; 33 } 34 String filename = args[0]; 35 File srcfile = new File (filename); 36 if (!srcfile.exists()) { 37 System.err.println("Error -- file not found: " + filename); 38 return; 39 } 40 File destfile = new File (System.getProperty("user.dir")); 41 if (args.length == 2) { 42 String dirname = args[1]; 43 destfile = new File (dirname); 44 if (!destfile.exists()) { 45 System.err.println("Error -- destination not found: " + dirname); 46 return; 47 } 48 if (!destfile.isDirectory()) { 49 System.err.println("Error -- not a directory: " + dirname); 50 } 51 } 52 53 int size = (int)srcfile.length(); 54 byte[] chars = new byte[size]; 55 new FileInputStream (srcfile).read(chars); 56 String content = new String (chars); 58 Node scriptNode = runtime.parse(content, filename, null); 59 60 StandardASMCompiler compiler = new StandardASMCompiler(filename.substring(0, filename.lastIndexOf(".")), filename); 62 NodeCompilerFactory.getCompiler(scriptNode).compile(scriptNode, compiler); 63 64 compiler.writeClass(destfile); 65 } catch (IOException ioe) { 66 System.err.println("Error -- IO exception during compile: " + ioe.getMessage()); 67 } catch (NotCompilableException nce) { 68 System.err.println("Error -- Not compileable: " + nce.getMessage()); 69 } 70 } 71 72 } 73 | Popular Tags |