Class: DubyImpl
- Inherits:
-
Object
- Object
- DubyImpl
- Defined in:
- lib/duby.rb
Instance Method Summary collapse
- #compile(*args) ⇒ Object
- #compile_ast(ast, &block) ⇒ Object
- #expand_files(files) ⇒ Object
- #parse(*args) ⇒ Object
- #print_help ⇒ Object
- #process_flags!(args) ⇒ Object
- #run(*args) ⇒ Object
Instance Method Details
#compile(*args) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/duby.rb', line 144 def compile(*args) process_flags!(args) (args).each do |duby_file| if duby_file == '-e' @filename = '-e' next elsif @filename == '-e' ast = parse('-e', duby_file) else ast = parse(duby_file) end exit 1 if @error compile_ast(ast) do |filename, builder| filename = "#{@state.destination}#{filename}" FileUtils.mkdir_p(File.dirname(filename)) bytes = builder.generate File.open(filename, 'w') {|f| f.write(bytes)} end @filename = nil end end |
#compile_ast(ast, &block) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/duby.rb', line 200 def compile_ast(ast, &block) typer = Duby::Typer::JVM.new(@filename, @transformer) typer.infer(ast) begin typer.resolve(false) ensure puts ast.inspect if @state.verbose failed = !typer.errors.empty? if failed puts "Inference Error:" typer.errors.each do |ex| Duby.print_error(ex., ex.node.position) puts ex.backtrace if @state.verbose end exit 1 end end compiler = @compiler_class.new(@filename) ast.compile(compiler, false) compiler.generate(&block) end |
#expand_files(files) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/duby.rb', line 280 def (files) = [] files.each do |filename| if File.directory?(filename) Dir[File.join(filename, '*')].each do |child| if File.directory?(child) files << child elsif child =~ /\.duby$/ << child end end else << filename end end end |
#parse(*args) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/duby.rb', line 168 def parse(*args) process_flags!(args) @filename = args.shift if @filename if @filename == '-e' @filename = 'DashE' src = args[0] else src = File.read(@filename) end else print_help exit(1) end Duby::AST.type_factory = Duby::JVM::Types::TypeFactory.new(@filename) begin ast = Duby::AST.parse_ruby(src, @filename) rescue org.jrubyparser.lexer.SyntaxException => ex Duby.print_error(ex., ex.position) raise ex if @state.verbose end @transformer = Duby::Transform::Transformer.new(@state) ast = @transformer.transform(ast, nil) @transformer.errors.each do |ex| Duby.print_error(ex., ex.position) raise ex.cause || ex if @state.verbose end @error = @transformer.errors.size > 0 ast end |
#print_help ⇒ Object
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/duby.rb', line 269 def print_help $stdout.print "#{$0} [flags] <files or \"-e SCRIPT\"> -V, --verbose\t\tVerbose logging -j, --java\t\tOutput .java source (jrubyc only) -d, --dir DIR\t\tUse DIR as the base dir for compilation, packages -p, --plugin PLUGIN\tLoad and use plugin during compilation -c, --classpath PATH\tAdd PATH to the Java classpath for compilation -h, --help\t\tPrint this help message -e\t\t\tCompile or run the script following -e (naming it \"DashE\")" end |
#process_flags!(args) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/duby.rb', line 224 def process_flags!(args) @state ||= Duby::CompilationState.new while args.length > 0 && args[0] =~ /^-/ case args[0] when '--verbose', '-V' Duby::Typer.verbose = true Duby::AST.verbose = true Duby::Compiler::JVM.verbose = true @state.verbose = true args.shift when '--java', '-j' require 'duby/jvm/source_compiler' @compiler_class = Duby::Compiler::JavaSource args.shift when '--dest', '-d' args.shift @state.destination = File.join(File.(args.shift), '') when '--cd' args.shift Dir.chdir(args.shift) when '--plugin', '-p' args.shift plugin = args.shift require "duby/plugin/#{plugin}" when '-I' args.shift $: << args.shift when '--classpath', '-c' args.shift Duby::Env.decode_paths(args.shift, $CLASSPATH) when '--help', '-h' print_help exit(0) when '-e' break else puts "unrecognized flag: " + args[0] print_help exit(1) end end @state.destination ||= File.join(File.('.'), '') @compiler_class ||= Duby::Compiler::JVM end |
#run(*args) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/duby.rb', line 111 def run(*args) ast = parse(*args) main = nil class_map = {} # generate all bytes for all classes compile_ast(ast) do |outfile, builder| bytes = builder.generate name = builder.class_name.gsub(/\//, '.') class_map[name] = bytes end # load all classes dcl = DubyClassLoader.new(java.lang.ClassLoader.system_class_loader, class_map) class_map.each do |name,| cls = dcl.load_class(name) # TODO: using first main; find correct one main ||= cls.get_method("main", java::lang::String[].java_class) #rescue nil end # run the main method we found if main begin main.invoke(nil, [args.to_java(:string)].to_java) rescue java.lang.Exception => e e = e.cause if e.cause raise e end else puts "No main found" end end |