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

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

Constructor Details

#initialize(cls, options) ⇒ MethodBuilder

Returns a new instance of MethodBuilder.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/duby/jvm/source_generator/builder.rb', line 269

def initialize(cls, options)
  @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



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

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.



267
268
269
# File 'lib/duby/jvm/source_generator/builder.rb', line 267

def name
  @name
end

#outObject

Returns the value of attribute out.



267
268
269
# File 'lib/duby/jvm/source_generator/builder.rb', line 267

def out
  @out
end

#typeObject

Returns the value of attribute type.



267
268
269
# File 'lib/duby/jvm/source_generator/builder.rb', line 267

def type
  @type
end

Instance Method Details

#declare_local(type, name) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/duby/jvm/source_generator/builder.rb', line 318

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



341
342
343
344
# File 'lib/duby/jvm/source_generator/builder.rb', line 341

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

#ldc_double(value) ⇒ Object



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

def ldc_double(value)
  print value
end

#ldc_float(value) ⇒ Object



350
351
352
# File 'lib/duby/jvm/source_generator/builder.rb', line 350

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

#local?(name) ⇒ Boolean

Returns:

  • (Boolean)


332
333
334
# File 'lib/duby/jvm/source_generator/builder.rb', line 332

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

#push_int(value) ⇒ Object



346
347
348
# File 'lib/duby/jvm/source_generator/builder.rb', line 346

def push_int(value)
  print value
end

#startObject



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

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



313
314
315
316
# File 'lib/duby/jvm/source_generator/builder.rb', line 313

def stop
  dedent
  puts "}"
end

#tmp(type) ⇒ Object



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

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