Class: Amrita2::Core::CodeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/amrita2/template.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: CGModule

Instance Method Summary collapse

Constructor Details

#initializeCodeGenerator

Returns a new instance of CodeGenerator.



514
515
516
517
518
519
520
521
# File 'lib/amrita2/template.rb', line 514

def initialize
  @iehack = true
  @lines = []
  @indent = 0
  init_strbuf
  @module_stack = [CGModule.new('')]
  @current_stream = "__stream__"
end

Instance Method Details

#case_(obj, &block) ⇒ Object



583
584
585
586
587
# File 'lib/amrita2/template.rb', line 583

def case_(obj, &block)
  code("case #{obj}")
  yield
  code("end")
end

#code(line) ⇒ Object



527
528
529
530
# File 'lib/amrita2/template.rb', line 527

def code(line)
  flush
  @lines << [@indent, line]
end

#comment(comments) ⇒ Object



532
533
534
535
536
# File 'lib/amrita2/template.rb', line 532

def comment(comments)
  comments.each do |c|
    @lines << [@indent, "# #{c}"]
  end
end

#def_method(name, *args) ⇒ Object



626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/amrita2/template.rb', line 626

def def_method(name, *args)
  if args.size > 0
    code("def #{name}(#{args.join(',')})")
  else
    code("def #{name}")
  end
  level_up do
    with_stream do
      yield
      flush
    end
  end
  code("end")
end

#define_class(name, &block) ⇒ Object



662
663
664
# File 'lib/amrita2/template.rb', line 662

def define_class(name, &block)
  define_module_or_class("class", name, &block)
end

#define_constant(const_def) ⇒ Object



677
678
679
# File 'lib/amrita2/template.rb', line 677

def define_constant(const_def)
  @module_stack.first.define_constant(const_def)
end

#define_module_or_class(typ, name, &block) ⇒ Object



666
667
668
669
670
671
672
673
674
675
# File 'lib/amrita2/template.rb', line 666

def define_module_or_class(typ, name, &block)
  @module_stack.unshift CGModule.new(name)
  code("#{typ} #{name}")
  level_up do
    yield
    m = @module_stack.shift
    m.end_of_module(self)
  end
  code("end")
end

#else_(&block) ⇒ Object



596
597
598
599
600
601
# File 'lib/amrita2/template.rb', line 596

def else_(&block)
  code("else")
  level_up do
    yield
  end
end

#eval(src) ⇒ Object



681
682
683
# File 'lib/amrita2/template.rb', line 681

def eval(src)
  code "eval(#{src}, __binding__)"
end

#eval_and_print(src) ⇒ Object



685
686
687
# File 'lib/amrita2/template.rb', line 685

def eval_and_print(src)
  put_string_expression "eval(#{src}, __binding__)"
end

#flushObject



555
556
557
558
559
560
# File 'lib/amrita2/template.rb', line 555

def flush
  if @strbuf.size > 0
    @lines << [@indent, "#{@current_stream}.concat(#{@strbuf.inspect})"]
    init_strbuf
  end
end

#if_(cond, &block) ⇒ Object



575
576
577
578
579
580
581
# File 'lib/amrita2/template.rb', line 575

def if_(cond, &block)
  code("if #{cond}")
  level_up do
    yield
  end
  code("end")
end

#init_strbufObject



523
524
525
# File 'lib/amrita2/template.rb', line 523

def init_strbuf
  @strbuf = ""
end

#level_upObject



568
569
570
571
572
573
# File 'lib/amrita2/template.rb', line 568

def level_up
  @indent += 1
  yield
  flush
  @indent -= 1
end

#put_constant(c) ⇒ Object



551
552
553
# File 'lib/amrita2/template.rb', line 551

def put_constant(c)
  @strbuf.concat c.to_s
end

#put_expression(exp) ⇒ Object



654
655
656
# File 'lib/amrita2/template.rb', line 654

def put_expression(exp)
  code("#{@current_stream}.concat((#{exp}).to_s)")
end

#put_hpricot_node(node) ⇒ Object



562
563
564
565
566
# File 'lib/amrita2/template.rb', line 562

def put_hpricot_node(node)
  s = ""
  node.output(s)
  put_constant s
end

#put_static_element(e) ⇒ Object



641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/amrita2/template.rb', line 641

def put_static_element(e)
  attr = e.attributes.collect do |name, value|
    "#{name} = #{value.inspect}"
  end
  if attr.size > 0
    put_constant("<#{e.name} #{attr.join(' ')}>")
  else
    put_constant("<#{e.name}>")
  end
  yield
  put_constant("</#{e.name}>")
end

#put_stream(name) ⇒ Object



622
623
624
# File 'lib/amrita2/template.rb', line 622

def put_stream(name)
  code %[#@current_stream.concat(Thread::current[:amrita_stream][#{name.inspect}])]
end

#put_string_expression(exp) ⇒ Object



658
659
660
# File 'lib/amrita2/template.rb', line 658

def put_string_expression(exp)
  code("#{@current_stream}.concat(#{exp})")
end

#resultObject



538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/amrita2/template.rb', line 538

def result
  flush
  m = @module_stack.shift
  if m
    m.constants.each do |name, defs|
      code("#{name} = #{defs}")
    end
  end
  @lines.collect do |indent, l|
    "  " * indent + l
  end.join("\n")
end

#when_(obj, &block) ⇒ Object



589
590
591
592
593
594
# File 'lib/amrita2/template.rb', line 589

def when_(obj, &block)
  code("when #{obj}")
  level_up do
    yield
  end
end

#with_stream(new_stream_name = nil) ⇒ Object



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/amrita2/template.rb', line 603

def with_stream(new_stream_name=nil)
  if new_stream_name
    old_stream = @current_stream
    code("before_#{new_stream_name} = #@current_stream")
    @current_stream = new_stream_name.to_s
  end
  code("#@current_stream = '' ")
  yield
  flush
ensure
  if new_stream_name
    @current_stream = old_stream
    code %[Thread::current[:amrita_stream] ||= {} ]
    code %[Thread::current[:amrita_stream][#{new_stream_name.inspect}] = #{new_stream_name}]
    code("#@current_stream = before_#{new_stream_name}")
  end
  code("#@current_stream")
end