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.



399
400
401
402
403
404
405
406
# File 'lib/amrita2/template.rb', line 399

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

Instance Method Details

#case_(obj, &block) ⇒ Object



468
469
470
471
472
# File 'lib/amrita2/template.rb', line 468

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

#code(line) ⇒ Object



412
413
414
415
# File 'lib/amrita2/template.rb', line 412

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

#comment(comments) ⇒ Object



417
418
419
420
421
# File 'lib/amrita2/template.rb', line 417

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

#def_method(name, *args) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/amrita2/template.rb', line 511

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



547
548
549
# File 'lib/amrita2/template.rb', line 547

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

#define_constant(const_def) ⇒ Object



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

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

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



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

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



481
482
483
484
485
486
# File 'lib/amrita2/template.rb', line 481

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

#eval(src) ⇒ Object



566
567
568
# File 'lib/amrita2/template.rb', line 566

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

#eval_and_print(src) ⇒ Object



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

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

#flushObject



440
441
442
443
444
445
# File 'lib/amrita2/template.rb', line 440

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

#if_(cond, &block) ⇒ Object



460
461
462
463
464
465
466
# File 'lib/amrita2/template.rb', line 460

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

#init_strbufObject



408
409
410
# File 'lib/amrita2/template.rb', line 408

def init_strbuf
  @strbuf = ""
end

#level_upObject



453
454
455
456
457
458
# File 'lib/amrita2/template.rb', line 453

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

#put_constant(c) ⇒ Object



436
437
438
# File 'lib/amrita2/template.rb', line 436

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

#put_expression(exp) ⇒ Object



539
540
541
# File 'lib/amrita2/template.rb', line 539

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

#put_hpricot_node(node) ⇒ Object



447
448
449
450
451
# File 'lib/amrita2/template.rb', line 447

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

#put_static_element(e) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/amrita2/template.rb', line 526

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



507
508
509
# File 'lib/amrita2/template.rb', line 507

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

#put_string_expression(exp) ⇒ Object



543
544
545
# File 'lib/amrita2/template.rb', line 543

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

#resultObject



423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/amrita2/template.rb', line 423

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



474
475
476
477
478
479
# File 'lib/amrita2/template.rb', line 474

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

#with_stream(new_stream_name = nil) ⇒ Object



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/amrita2/template.rb', line 488

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