Class: AtCoderFriends::Generator::RubyBuiltin

Inherits:
Base
  • Object
show all
Defined in:
lib/at_coder_friends/generator/ruby_builtin.rb

Overview

generates Ruby source 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.rb.erb')
ATTRS =
Attributes.new(:rb, DEFAULT_TMPL)

Instance Attribute Summary

Attributes inherited from Base

#cfg, #pbm

Instance Method Summary collapse

Methods inherited from Base

#embed_lines, #generate, #initialize, #process, #select_template

Constructor Details

This class inherits a constructor from AtCoderFriends::Generator::Base

Instance Method Details

#attrsObject



12
13
14
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 12

def attrs
  ATTRS
end

#gen_cmb_decl(inpdef) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 106

def gen_cmb_decl(inpdef)
  mx = inpdef.container == :varray_matrix ? -1 : 0
  vs = inpdef.names.map { |v| "#{v}s" }
  vs[mx] += 's'
  sz = inpdef.size[0]
  dcls = vs.map { |v| "#{v}[i]" }
  dcls[mx] = "*#{dcls[mx]}" unless inpdef.item == :char
  dcl = dcls.join(', ')
  expr = gen_cmb_expr(inpdef)
  ret = []
  ret += vs.map { |v| "#{v} = Array.new(#{sz})" }
  ret << "#{sz}.times do |i|"
  ret << "  #{dcl} = #{expr}"
  ret << 'end'
  ret
end

#gen_cmb_expr(inpdef) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 167

def gen_cmb_expr(inpdef)
  read = gen_read(inpdef.delim)
  case inpdef.item
  when :number
    "#{read}.split.map(&:to_i)"
  when :decimal
    "#{read}.split.map(&:to_f)"
  when :string, :char
    "#{read}.chomp.split"
  end
end

#gen_consts(constants = pbm.constants) ⇒ Object



21
22
23
24
25
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 21

def gen_consts(constants = pbm.constants)
  constants
    .select { |c| c.type == :mod }
    .map { |c| gen_mod(c) }
end

#gen_decl(inpdef) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 36

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)
  when :varray_matrix, :matrix_varray
    gen_cmb_decl(inpdef)
  when :vmatrix
    gen_vmatrix_decl(inpdef)
  when :hmatrix
    gen_hmatrix_decl(inpdef)
  end
end

#gen_decls(inpdefs = pbm.formats) ⇒ Object



32
33
34
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 32

def gen_decls(inpdefs = pbm.formats)
  inpdefs.map { |inpdef| gen_decl(inpdef) }.flatten
end

#gen_expr(inpdef, split) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 153

def gen_expr(inpdef, split)
  read = gen_read(inpdef.delim)
  case inpdef.item
  when :number
    split ? "#{read}.split.map(&:to_i)" : "#{read}.to_i"
  when :decimal
    split ? "#{read}.split.map(&:to_f)" : "#{read}.to_f"
  when :string
    split ? "#{read}.chomp.split" : "#{read}.chomp"
  when :char
    'gets.chomp'
  end
end

#gen_harray_decl(inpdef) ⇒ Object



62
63
64
65
66
67
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 62

def gen_harray_decl(inpdef)
  v = inpdef.names[0]
  dcl = "#{v}s"
  expr = gen_expr(inpdef, true)
  "#{dcl} = #{expr}"
end

#gen_hmatrix_decl(inpdef) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 140

def gen_hmatrix_decl(inpdef)
  names = inpdef.names
  sz = inpdef.size[0]
  dcl = names.map { |v| "#{v}ss[i]" }.join(', ')
  expr = gen_expr(inpdef, true)
  ret = []
  ret += names.map { |v| "#{v}ss = Array.new(#{sz})" }
  ret << "#{sz}.times do |i|"
  ret << "  #{dcl} = #{expr}.each_slice(#{names.size}).to_a.transpose"
  ret << 'end'
  ret
end

#gen_matrix_decl(inpdef) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 98

def gen_matrix_decl(inpdef)
  v = inpdef.names[0]
  sz = inpdef.size[0]
  decl = "#{v}ss"
  expr = gen_expr(inpdef, true)
  "#{decl} = Array.new(#{sz}) { #{expr} }"
end

#gen_mod(c) ⇒ Object



27
28
29
30
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 27

def gen_mod(c)
  v = c.value.gsub('^', '**').gsub(',', '_')
  "MOD = #{v}"
end

#gen_read(delim) ⇒ Object



179
180
181
182
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 179

def gen_read(delim)
  sub = delim.chars.map { |d| ".gsub('#{d}', ' ')" }.join
  "gets#{sub}"
end

#gen_single_decl(inpdef) ⇒ Object



55
56
57
58
59
60
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 55

def gen_single_decl(inpdef)
  names = inpdef.names
  dcl = names.join(', ')
  expr = gen_expr(inpdef, names.size > 1)
  "#{dcl} = #{expr}"
end

#gen_varray_1_decl(inpdef) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 77

def gen_varray_1_decl(inpdef)
  v = inpdef.names[0]
  sz = inpdef.size[0]
  dcl = "#{v}s"
  expr = gen_expr(inpdef, false)
  "#{dcl} = Array.new(#{sz}) { #{expr} }"
end

#gen_varray_decl(inpdef) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 69

def gen_varray_decl(inpdef)
  if inpdef.names.size == 1
    gen_varray_1_decl(inpdef)
  else
    gen_varray_n_decl(inpdef)
  end
end

#gen_varray_n_decl(inpdef) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 85

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, true)
  ret = []
  ret += names.map { |v| "#{v}s = Array.new(#{sz})" }
  ret << "#{sz}.times do |i|"
  ret << "  #{dcl} = #{expr}"
  ret << 'end'
  ret
end

#gen_vmatrix_decl(inpdef) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 123

def gen_vmatrix_decl(inpdef)
  names = inpdef.names
  sz1, sz2 = inpdef.size
  dcl = names.map { |v| "#{v}ss[i][j]" }.join(', ')
  expr = gen_expr(inpdef, true)
  ret = []
  ret += names.map do |v|
    "#{v}ss = Array.new(#{sz1}) { Array.new(#{sz2}) }"
  end
  ret << "#{sz1}.times do |i|"
  ret << "  #{sz2}.times do |j|"
  ret << "    #{dcl} = #{expr}"
  ret << '  end'
  ret << 'end'
  ret
end

#render(src) ⇒ Object



16
17
18
19
# File 'lib/at_coder_friends/generator/ruby_builtin.rb', line 16

def render(src)
  src = embed_lines(src, '### CONSTS ###', gen_consts)
  embed_lines(src, '### DCLS ###', gen_decls)
end