Class: AtCoderFriends::CxxGenerator
- Inherits:
-
Object
- Object
- AtCoderFriends::CxxGenerator
- Defined in:
- lib/at_coder_friends/cxx_generator.rb
Overview
generates C++ source code from definition
Constant Summary collapse
- TEMPLATE =
<<~TEXT #include <cstdio> using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++) /*** CONSTS ***/ /*** DCLS ***/ void solve() { int ans = 0; printf("%d\\n", ans); } void input() { /*** READS ***/ } int main() { input(); solve(); return 0; } TEXT
- SCANF_FMTS =
[ 'scanf("%<fmt>s", %<addr>s);', 'REP(i, %<sz1>s) scanf("%<fmt>s", %<addr>s);', 'REP(i, %<sz1>s) REP(j, %<sz2>s) scanf("%<fmt>s", %<addr>s);' ].freeze
- FMT_FMTS =
{ number: '%d', string: '%s', char: '%s' }.freeze
- ADDR_FMTS =
{ single: { number: '&%<v>s', string: '%<v>s' }, harray: { number: '%<v>s + i', string: '%<v>s[i]', char: '%<v>s' }, varray: { number: '%<v>s + i', string: '%<v>s[i]' }, matrix: { number: '&%<v>s[i][j]', string: '%<v>s[i][j]', char: '%<v>s[i]' } }.freeze
Instance Method Summary collapse
- #gen_arr_size(szs) ⇒ Object
- #gen_consts(constraints) ⇒ Object
- #gen_decl(inpdef) ⇒ Object
- #gen_decls(defs) ⇒ Object
- #gen_harray_decl(inpdef) ⇒ Object
- #gen_matrix_decl(inpdef) ⇒ Object
-
#gen_read(inpdef) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #gen_reads(defs) ⇒ Object
- #gen_single_decl(inpdef) ⇒ Object
- #gen_varray_decl(inpdef) ⇒ Object
- #generate(defs, constraints) ⇒ Object
- #process(pbm) ⇒ Object
Instance Method Details
#gen_arr_size(szs) ⇒ Object
149 150 151 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 149 def gen_arr_size(szs) szs.map { |sz| sz =~ /\D/ ? "#{sz.upcase}_MAX" : sz } end |
#gen_consts(constraints) ⇒ Object
78 79 80 81 82 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 78 def gen_consts(constraints) constraints .select { |c| c.type == :max } .map { |c| "const int #{c.name.upcase}_MAX = #{c.value};" } end |
#gen_decl(inpdef) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 88 def gen_decl(inpdef) case inpdef.container when :single gen_single_decl(inpdef) when :harray gen_harray_decl(inpdef) when :varray gen_varray_decl(inpdef) when :matrix gen_matrix_decl(inpdef) end end |
#gen_decls(defs) ⇒ Object
84 85 86 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 84 def gen_decls(defs) defs.map { |inpdef| gen_decl(inpdef) }.flatten end |
#gen_harray_decl(inpdef) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 112 def gen_harray_decl(inpdef) v = inpdef.names[0] sz = gen_arr_size(inpdef.size)[0] case inpdef.item when :number "int #{v}[#{sz}];" when :string "char #{v}[#{sz}][#{v.upcase}_MAX + 1];" when :char "char #{v}[#{sz} + 1];" end end |
#gen_matrix_decl(inpdef) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 136 def gen_matrix_decl(inpdef) v = inpdef.names[0] sz1, sz2 = gen_arr_size(inpdef.size) case inpdef.item when :number "int #{v}[#{sz1}][#{sz2}];" when :string "char #{v}[#{sz1}][#{sz2}][#{v.upcase}_MAX + 1];" when :char "char #{v}[#{sz1}][#{sz2} + 1];" end end |
#gen_read(inpdef) ⇒ Object
rubocop:disable Metrics/AbcSize
158 159 160 161 162 163 164 165 166 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 158 def gen_read(inpdef) dim = inpdef.size.size - (inpdef.item == :char ? 1 : 0) scanf = SCANF_FMTS[dim] sz1, sz2 = inpdef.size fmt = FMT_FMTS[inpdef.item] * inpdef.names.size addr_fmt = ADDR_FMTS[inpdef.container][inpdef.item] addr = inpdef.names.map { |v| format(addr_fmt, v: v) }.join(', ') format(scanf, sz1: sz1, sz2: sz2, fmt: fmt, addr: addr) end |
#gen_reads(defs) ⇒ Object
153 154 155 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 153 def gen_reads(defs) defs.map { |inpdef| gen_read(inpdef) }.flatten end |
#gen_single_decl(inpdef) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 101 def gen_single_decl(inpdef) names = inpdef.names case inpdef.item when :number dcl = names.join(', ') "int #{dcl};" when :string names.map { |v| "char #{v}[#{v.upcase}_MAX + 1];" } end end |
#gen_varray_decl(inpdef) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 125 def gen_varray_decl(inpdef) names = inpdef.names sz = gen_arr_size(inpdef.size)[0] case inpdef.item when :number names.map { |v| "int #{v}[#{sz}];" } when :string names.map { |v| "char #{v}[#{sz}][#{v.upcase}_MAX + 1];" } end end |
#generate(defs, constraints) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 68 def generate(defs, constraints) consts = gen_consts(constraints) dcls = gen_decls(defs) reads = gen_reads(defs) TEMPLATE .sub('/*** CONSTS ***/', consts.join("\n")) .sub('/*** DCLS ***/', dcls.join("\n")) .sub('/*** READS ***/', reads.map { |s| ' ' + s }.join("\n")) end |
#process(pbm) ⇒ Object
63 64 65 66 |
# File 'lib/at_coder_friends/cxx_generator.rb', line 63 def process(pbm) src = generate(pbm.defs, pbm.constraints) pbm.add_src(:cxx, src) end |