Class: AtCoderFriends::Generator::RubyBuiltin
- Inherits:
-
Object
- Object
- AtCoderFriends::Generator::RubyBuiltin
- Defined in:
- lib/at_coder_friends/generator/ruby_builtin.rb
Overview
generates C++ source code from problem description
Constant Summary collapse
- ACF_HOME =
File.realpath(File.join(__dir__, '..', '..', '..'))
- TMPL_DIR =
File.join(ACF_HOME, 'templates')
- DEFAULT_TMPL =
File.join(TMPL_DIR, 'ruby_builtin_default.rb')
- INTERACTIVE_TMPL =
File.join(TMPL_DIR, 'ruby_builtin_interactive.rb')
Instance Attribute Summary collapse
-
#cfg ⇒ Object
readonly
Returns the value of attribute cfg.
-
#pbm ⇒ Object
readonly
Returns the value of attribute pbm.
Instance Method Summary collapse
- #default_template ⇒ Object
- #gen_decl(inpdef) ⇒ Object
- #gen_decls(inpdefs = pbm.formats) ⇒ Object
- #gen_expr(item, split) ⇒ Object
- #gen_harray_decl(inpdef) ⇒ Object
- #gen_matrix_decl(inpdef) ⇒ Object
- #gen_output(vs = pbm.options.binary_values) ⇒ Object
- #gen_single_decl(inpdef) ⇒ Object
- #gen_varray_1_decl(inpdef) ⇒ Object
- #gen_varray_n_decl(inpdef) ⇒ Object
- #generate(pbm) ⇒ Object
-
#initialize(cfg = nil) ⇒ RubyBuiltin
constructor
A new instance of RubyBuiltin.
- #interactive_template ⇒ Object
- #process(pbm) ⇒ Object
- #select_template(interactive = pbm.options.interactive) ⇒ Object
Constructor Details
#initialize(cfg = nil) ⇒ RubyBuiltin
14 15 16 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 14 def initialize(cfg = nil) @cfg = cfg || {} end |
Instance Attribute Details
#cfg ⇒ Object (readonly)
Returns the value of attribute cfg.
12 13 14 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 12 def cfg @cfg end |
#pbm ⇒ Object (readonly)
Returns the value of attribute pbm.
12 13 14 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 12 def pbm @pbm end |
Instance Method Details
#default_template ⇒ Object
36 37 38 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 36 def default_template cfg['default_template'] || DEFAULT_TMPL end |
#gen_decl(inpdef) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 48 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(inpdefs = pbm.formats) ⇒ Object
44 45 46 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 44 def gen_decls(inpdefs = pbm.formats) inpdefs.map { |inpdef| gen_decl(inpdef) }.flatten end |
#gen_expr(item, split) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 108 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
72 73 74 75 76 77 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 72 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
100 101 102 103 104 105 106 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 100 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_output(vs = pbm.options.binary_values) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 119 def gen_output(vs = pbm..binary_values) if vs "puts cond ? '#{vs[0]}' : '#{vs[1]}'" else 'puts ans' end end |
#gen_single_decl(inpdef) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 65 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
79 80 81 82 83 84 85 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 79 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
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 87 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(pbm) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 23 def generate(pbm) @pbm = pbm File .read(select_template) .gsub('### URL ###', pbm.url) .gsub('### DCLS ###', gen_decls.join("\n")) .gsub('### OUTPUT ###', gen_output) end |
#interactive_template ⇒ Object
40 41 42 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 40 def interactive_template cfg['interactive_template'] || INTERACTIVE_TMPL end |
#process(pbm) ⇒ Object
18 19 20 21 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 18 def process(pbm) src = generate(pbm) pbm.add_src(:rb, src) end |
#select_template(interactive = pbm.options.interactive) ⇒ Object
32 33 34 |
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 32 def select_template(interactive = pbm..interactive) interactive ? interactive_template : default_template end |