Class: Duby::JavaSource::MethodBuilder

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/duby/jvm/source_generator/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#annotate, #block, #dedent, #indent, #init_value, #print, #puts

Constructor Details

#initialize(cls, options) ⇒ MethodBuilder

Returns a new instance of MethodBuilder.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/duby/jvm/source_generator/builder.rb', line 291

def initialize(cls, options)
  @class = cls
  @compiler = cls.compiler
  @out = Output.new
  @name = options[:name]
  @type = options[:return]
  @typename = @type && @type.to_source
  @locals = {}
  @args = options[:args].map do |arg|
    unless arg.kind_of? Array
      arg = [arg.inferred_type, arg.name]
    end
    @locals[arg[1]] = arg[0]
    arg
  end
  @static = options[:static] && ' static'
  @abstract = options[:abstract] && ' abstract'
  @exceptions = options[:exceptions] || []
  @temps = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



381
382
383
384
385
386
387
388
# File 'lib/duby/jvm/source_generator/builder.rb', line 381

def method_missing(name, *args)
  if name.to_s =~ /.const_(m)?(\d)/
    print '-' if $1
    print $2
  else
    super
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



289
290
291
# File 'lib/duby/jvm/source_generator/builder.rb', line 289

def name
  @name
end

#outObject

Returns the value of attribute out.



289
290
291
# File 'lib/duby/jvm/source_generator/builder.rb', line 289

def out
  @out
end

#typeObject

Returns the value of attribute type.



289
290
291
# File 'lib/duby/jvm/source_generator/builder.rb', line 289

def type
  @type
end

Instance Method Details

#declare_local(type, name) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/duby/jvm/source_generator/builder.rb', line 341

def declare_local(type, name)
  unless @locals[name]
    print "#{type.to_source} #{name} = "
    if block_given?
      yield self
    else
      print init_value(type)
    end
    puts ';'
    @locals[name] = type
  end
  name
end

#labelObject



364
365
366
367
# File 'lib/duby/jvm/source_generator/builder.rb', line 364

def label
  @temps += 1
  "label#{@temps}"
end

#ldc_double(value) ⇒ Object



377
378
379
# File 'lib/duby/jvm/source_generator/builder.rb', line 377

def ldc_double(value)
  print value
end

#ldc_float(value) ⇒ Object



373
374
375
# File 'lib/duby/jvm/source_generator/builder.rb', line 373

def ldc_float(value)
  print "(float)#{value}"
end

#local?(name) ⇒ Boolean

Returns:

  • (Boolean)


355
356
357
# File 'lib/duby/jvm/source_generator/builder.rb', line 355

def local?(name)
  !!@locals[name]
end

#push_int(value) ⇒ Object



369
370
371
# File 'lib/duby/jvm/source_generator/builder.rb', line 369

def push_int(value)
  print value
end

#startObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/duby/jvm/source_generator/builder.rb', line 312

def start
  print "public#{@static}#{@abstract} #{@typename} #{@name}("
  @args.each_with_index do |(type, name), i|
    print ', ' unless i == 0
    print "#{type.to_source} #{name}"
  end
  print ')'
  unless @exceptions.empty?
    print ' throws '
    @exceptions.each_with_index do |exception, i|
      print ', ' unless i == 0
      print exception.name
    end
  end
  if @abstract
    puts ";"
    def self.puts(*args); end
    def self.print(*args); end
  else
    puts " {"
  end
  indent
end

#stopObject



336
337
338
339
# File 'lib/duby/jvm/source_generator/builder.rb', line 336

def stop
  dedent
  puts "}"
end

#tmp(type, &block) ⇒ Object



359
360
361
362
# File 'lib/duby/jvm/source_generator/builder.rb', line 359

def tmp(type, &block)
  @temps += 1
  declare_local(type, "temp$#{@temps}", &block)
end