Class: AtCoderFriends::RubyGenerator
- Inherits:
-
Object
- Object
- AtCoderFriends::RubyGenerator
- Defined in:
- lib/at_coder_friends/ruby_generator.rb
Overview
generates C++ source code from definition
Constant Summary collapse
- TEMPLATE =
"### DCLS ###\n\nputs ans\n"
Instance Method Summary collapse
- #gen_decl(inpdef) ⇒ Object
- #gen_decls(defs) ⇒ Object
- #gen_expr(item, split) ⇒ Object
- #gen_harray_decl(inpdef) ⇒ Object
- #gen_matrix_decl(inpdef) ⇒ Object
- #gen_single_decl(inpdef) ⇒ Object
- #gen_varray_1_decl(inpdef) ⇒ Object
- #gen_varray_n_decl(inpdef) ⇒ Object
- #generate(defs) ⇒ Object
- #process(pbm) ⇒ Object
Instance Method Details
#gen_decl(inpdef) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 26 def gen_decl(inpdef) case inpdef.container when :single gen_single_decl(inpdef) when :harray gen_harray_decl(inpdef) when :varray if inpdef.names.size == 1 gen_varray_1_decl(inpdef) else gen_varray_n_decl(inpdef) end when :matrix gen_matrix_decl(inpdef) end end |
#gen_decls(defs) ⇒ Object
22 23 24 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 22 def gen_decls(defs) defs.map { |inpdef| gen_decl(inpdef) }.flatten end |
#gen_expr(item, split) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 86 def gen_expr(item, split) case item when :number split ? 'gets.split.map(&:to_i)' : 'gets.to_i' when :string split ? 'gets.chomp.split' : 'gets.chomp' when :char 'gets.chomp' end end |
#gen_harray_decl(inpdef) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 50 def gen_harray_decl(inpdef) v = inpdef.names[0] dcl = "#{v}s" expr = gen_expr(inpdef.item, true) "#{dcl} = #{expr}" end |
#gen_matrix_decl(inpdef) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 78 def gen_matrix_decl(inpdef) v = inpdef.names[0] sz = inpdef.size[0] decl = "#{v}ss" expr = gen_expr(inpdef.item, true) "#{decl} = Array.new(#{sz}) { #{expr} }" end |
#gen_single_decl(inpdef) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 43 def gen_single_decl(inpdef) names = inpdef.names dcl = names.join(', ') expr = gen_expr(inpdef.item, names.size > 1) "#{dcl} = #{expr}" end |
#gen_varray_1_decl(inpdef) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 57 def gen_varray_1_decl(inpdef) v = inpdef.names[0] sz = inpdef.size[0] dcl = "#{v}s" expr = gen_expr(inpdef.item, false) "#{dcl} = Array.new(#{sz}) { #{expr} }" end |
#gen_varray_n_decl(inpdef) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 65 def gen_varray_n_decl(inpdef) names = inpdef.names sz = inpdef.size[0] dcl = names.map { |v| "#{v}s[i]" }.join(', ') expr = gen_expr(inpdef.item, true) ret = [] ret += names.map { |v| "#{v}s = Array.new(#{sz})" } ret << "#{sz}.times do |i|" ret << " #{dcl} = #{expr}" ret << 'end' ret end |
#generate(defs) ⇒ Object
17 18 19 20 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 17 def generate(defs) dcls = gen_decls(defs).join("\n") TEMPLATE.sub('### DCLS ###', dcls) end |
#process(pbm) ⇒ Object
12 13 14 15 |
# File 'lib/at_coder_friends/ruby_generator.rb', line 12 def process(pbm) src = generate(pbm.defs) pbm.add_src(:rb, src) end |