Class: AtCoderFriends::Generator::CxxBuiltin
- Inherits:
-
Object
- Object
- AtCoderFriends::Generator::CxxBuiltin
show all
- Includes:
- CxxBuiltinConstants
- Defined in:
- lib/at_coder_friends/generator/cxx_builtin.rb
Overview
generates C++ source code from problem description
Constant Summary
AtCoderFriends::Generator::CxxBuiltinConstants::ACF_HOME, AtCoderFriends::Generator::CxxBuiltinConstants::ADDR_FMTS, AtCoderFriends::Generator::CxxBuiltinConstants::BINARY_OUTPUT_FMT, AtCoderFriends::Generator::CxxBuiltinConstants::DEFAULT_OUTPUT, AtCoderFriends::Generator::CxxBuiltinConstants::DEFAULT_TMPL, AtCoderFriends::Generator::CxxBuiltinConstants::FMT_FMTS, AtCoderFriends::Generator::CxxBuiltinConstants::INTERACTIVE_TMPL, AtCoderFriends::Generator::CxxBuiltinConstants::SCANF_FMTS, AtCoderFriends::Generator::CxxBuiltinConstants::TMPL_DIR
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cfg = nil) ⇒ CxxBuiltin
Returns a new instance of CxxBuiltin.
52
53
54
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 52
def initialize(cfg = nil)
@cfg = cfg || {}
end
|
Instance Attribute Details
#cfg ⇒ Object
Returns the value of attribute cfg.
50
51
52
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 50
def cfg
@cfg
end
|
#pbm ⇒ Object
Returns the value of attribute pbm.
50
51
52
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 50
def pbm
@pbm
end
|
Instance Method Details
#default_template ⇒ Object
83
84
85
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 83
def default_template
cfg['default_template'] || DEFAULT_TMPL
end
|
#embed_lines(src, pat, lines) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 71
def embed_lines(src, pat, lines)
re = Regexp.escape(pat)
src.gsub(
/^(.*)#{re}(.*)$/,
lines.map { |s| '\1' + s + '\2' }.join("\n")
)
end
|
#gen_arr_size(szs) ⇒ Object
162
163
164
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 162
def gen_arr_size(szs)
szs.map { |sz| sz =~ /\D/ ? "#{sz.upcase}_MAX" : sz }
end
|
#gen_consts(constraints = pbm.constraints) ⇒ Object
91
92
93
94
95
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 91
def gen_consts(constraints = pbm.constraints)
constraints
.select { |c| c.type == :max }
.map { |c| "const int #{c.name.upcase}_MAX = #{c.value};" }
end
|
#gen_decl(inpdef) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 101
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(inpdefs = pbm.formats) ⇒ Object
97
98
99
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 97
def gen_decls(inpdefs = pbm.formats)
inpdefs.map { |inpdef| gen_decl(inpdef) }.flatten
end
|
#gen_harray_decl(inpdef) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 125
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
|
rubocop:disable Metrics/AbcSize
171
172
173
174
175
176
177
178
179
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 171
def gen_input(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
|
166
167
168
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 166
def gen_inputs(inpdefs = pbm.formats)
inpdefs.map { |inpdef| gen_input(inpdef) }.flatten
end
|
#gen_matrix_decl(inpdef) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 149
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_output(vs = pbm.options.binary_values) ⇒ Object
rubocop:enable Metrics/AbcSize
182
183
184
185
186
187
188
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 182
def gen_output(vs = pbm.options.binary_values)
if vs
format(BINARY_OUTPUT_FMT, *vs)
else
DEFAULT_OUTPUT
end
end
|
#gen_single_decl(inpdef) ⇒ Object
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 114
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
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 138
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(pbm) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 61
def generate(pbm)
@pbm = pbm
src = File.read(select_template)
src = embed_lines(src, '/*** URL ***/', [pbm.url])
src = embed_lines(src, '/*** CONSTS ***/', gen_consts)
src = embed_lines(src, '/*** DCLS ***/', gen_decls)
src = embed_lines(src, '/*** INPUTS ***/', gen_inputs)
embed_lines(src, '/*** OUTPUT ***/', gen_output.split("\n"))
end
|
#interactive_template ⇒ Object
87
88
89
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 87
def interactive_template
cfg['interactive_template'] || INTERACTIVE_TMPL
end
|
#process(pbm) ⇒ Object
56
57
58
59
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 56
def process(pbm)
src = generate(pbm)
pbm.add_src(:cxx, src)
end
|
#select_template(interactive = pbm.options.interactive) ⇒ Object
79
80
81
|
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 79
def select_template(interactive = pbm.options.interactive)
interactive ? interactive_template : default_template
end
|