Class: Java::OrgJrubyAst::DefnNode

Inherits:
Object
  • Object
show all
Defined in:
lib/duby/old/compiler_old.rb,
lib/duby/old/mapper.rb

Instance Method Summary collapse

Instance Method Details

#compile(builder) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/duby/old/compiler_old.rb', line 366

def compile(builder)
  first_real_node = body_node
  first_real_node = body_node.child_nodes[0] if BlockNode === body_node
  while NewlineNode === first_real_node
    first_real_node = first_real_node.next_node
  end
  
  # determine signature from declaration line
  signature = first_real_node.signature(builder) if HashNode === first_real_node
  
  signature ||= [Void]
  
  log "Compiling instance method for #{name} as #{signature.join(',')}"
  builder.method(mapped_name(builder), *signature) do |method|
    # Run through any type declarations first
    first_real_node.declare_types(method) if HashNode === first_real_node

    # declare args that may not have been declared already
    args_node.compile(method)
    
    body_node.compile(method) if body_node
    
    # Expectation is that last element leaves the right type on stack
    if signature[0].primitive?
      case signature[0]
      when Void
        method.returnvoid
      when Jboolean, Jbyte
        method.breturn
      when Jchar
        method.creturn
      when Jshort
        method.sreturn
      when Jint
        method.ireturn
      when Jlong
        method.lreturn
      when Jfloat
        method.freturn
      when Jdouble
        method.dreturn
      else
        raise CompileError.new(position, "Unknown return type: #{signature[0]}")
      end
    else
      method.areturn
    end
  end
end

#mapped_name(builder) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/duby/old/mapper.rb', line 34

def mapped_name(builder)
  case name
  when "initialize"
    "<init>"
  else
    name
  end
end