Module: AtCoderFriends::Generator::CxxBuiltinDeclGen

Included in:
CxxBuiltin
Defined in:
lib/at_coder_friends/generator/cxx_builtin.rb

Overview

generates C++ variable declarations

Constant Summary collapse

TYPE_TBL =
{
  number: 'int',
  decimal: 'double',
  string: 'char',
  char: 'char'
}.tap { |h| h.default = 'int' }

Instance Method Summary collapse

Instance Method Details

#gen_arr_size(szs) ⇒ Object



100
101
102
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 100

def gen_arr_size(szs)
  szs.map { |sz| sz.gsub(/([a-z][a-z0-9_]*)/i, '\1_MAX').upcase }
end

#gen_decl(inpdef) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 32

def gen_decl(inpdef)
  if inpdef.components
    inpdef.components.map { |cmp| gen_decl(cmp) }
  else
    case inpdef.container
    when :single
      gen_single_decl(inpdef)
    when :harray
      gen_harray_decl(inpdef)
    when :varray
      gen_varray_decl(inpdef)
    when :matrix, :vmatrix, :hmatrix
      gen_matrix_decl(inpdef)
    end
  end
end

#gen_harray_decl(inpdef) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 65

def gen_harray_decl(inpdef)
  type = TYPE_TBL[inpdef.item]
  v = inpdef.names[0]
  sz = gen_arr_size(inpdef.size)[0]
  case inpdef.item
  when :number, :decimal
    "#{type} #{v}[#{sz}];"
  when :string
    "#{type} #{v}[#{sz}][#{v.upcase}_MAX + 1];"
  when :char
    "#{type} #{v}[#{sz} + 1];"
  end
end

#gen_matrix_decl(inpdef) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 89

def gen_matrix_decl(inpdef)
  sz1, sz2 = gen_arr_size(inpdef.size)
  inpdef.vars.map do |v, item|
    type = TYPE_TBL[item]
    dcl = "#{v}[#{sz1}]"
    dcl += item == :char ? "[#{sz2} + 1]" : "[#{sz2}]"
    dcl += "[#{v.upcase}_MAX + 1]" if item == :string
    "#{type} #{dcl};"
  end
end

#gen_single_decl(inpdef) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 49

def gen_single_decl(inpdef)
  names, cols = inpdef.vars.transpose
  if cols.uniq.size == 1 && cols[0] != :string
    type = TYPE_TBL[cols[0]]
    dcl = names.join(', ')
    "#{type} #{dcl};"
  else
    inpdef.vars.map do |v, item|
      type = TYPE_TBL[item]
      dcl = v
      dcl += "[#{v.upcase}_MAX + 1]" if item == :string
      "#{type} #{dcl};"
    end
  end
end

#gen_varray_decl(inpdef) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/at_coder_friends/generator/cxx_builtin.rb', line 79

def gen_varray_decl(inpdef)
  sz = gen_arr_size(inpdef.size)[0]
  inpdef.vars.map do |v, item|
    type = TYPE_TBL[item]
    dcl = "#{v}[#{sz}]"
    dcl += "[#{v.upcase}_MAX + 1]" if item == :string
    "#{type} #{dcl};"
  end
end