Class: Delorean::BlockExpression

Inherits:
SNode
  • Object
show all
Defined in:
lib/delorean/nodes.rb

Instance Method Summary collapse

Instance Method Details

#check(context) ⇒ Object



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/delorean/nodes.rb', line 485

def check(context, *)
  if respond_to?(:al)
    al.text_value.empty? ? [] : al.check(context)
  end

  b_args.elements.each do |element|
    element.check(context)
    element.force_def(context)
  end

  formulas = expressions.elements.select do |elem|
    elem.is_a? BlockFormula
  end

  result_formula = formulas.find do |formula|
    formula.i.text_value == 'result'
  end

  unless result_formula
    raise Delorean::ParseError.new(
      'result formula is required in blocks',
      context.module_name,
      context.line_no
    )
  end

  expressions.elements.each do |element|
    element.check(context)
  end

  b_args.elements.each do |element|
    element.force_undef(context)
  end
end

#rewrite(context, vcode) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/delorean/nodes.rb', line 520

def rewrite(context, vcode)
  if !respond_to?(:al) || al.text_value.empty?
    args_str = ''
    arg_count = 0
  else
    args_str = al.rewrite(context)
    arg_count = al.arg_count
  end
  if vcode.is_a?(ClassText)
    # FIXME: Do we really need this check here?
    # ruby class call
    class_name = vcode.text
    context.parse_check_call_fn(i.text_value, arg_count, class_name)
  end

  b_args.elements.each do |element|
    element.force_def(context)
  end

  block_args_str = if b_args.elements.any?
                     block_args = b_args.elements.map do |v|
                       v.rewrite(context)
                     end

                     "|#{block_args.join(', ')}|"
                   else
                     ''
                   end

  expr_arr = expressions.elements.map do |v|
    v.rewrite(context)
  end + ["result#{POST}"]

  expression_str = expr_arr.join('; ')

  b_args.elements.each do |element|
    element.force_undef(context)
  end

  block = "{ #{block_args_str} #{expression_str} }"

  "_instance_call(#{vcode}, '#{i.text_value}', [#{args_str}], _e) #{block}"
end