Class: Argument

Inherits:
Object
  • Object
show all
Defined in:
ext/numo/gsl/gen/func_parser.rb

Constant Summary collapse

NUM2DATA =
{
 "double"=>"%2 = NUM2DBL(%1)",
 "int"=>"%2 = NUM2INT(%1)",
 "unsigned int"=>"%2 = NUM2UINT(%1)",
 "size_t"=>"%2 = NUM2SIZET(%1)",
 "gsl_mode_t"=>"%2 = NUM2INT(%1)",
 "gsl_sf_legendre_t"=>"%2 = NUM2INT(%1)",
 "gsl_sf_mathieu_workspace *"=>'
if (rb_obj_is_kind_of(%1,cWorkspace)!=Qtrue) {
    rb_raise(rb_eTypeError,"last argument must be "
             "Numo::GSL::Sf::MathieuWorkspace class");
}
TypedData_Get_Struct(%1, gsl_sf_mathieu_workspace, &mathieuws_type, %2)'
}
DATA2NUM =
{
 "double"=>"DBL2NUM(%1)",
 "int"=>"INT2NUM(%1)",
 "unsigned int"=>"UINT2NUM(%1)",
 "size_t"=>"SIZET2NUM(%1)",
 "gsl_sf_result *"=>["DBL2NUM(%1.val)","DBL2NUM(%1.err)"]
}
TYPEMAP =
{
 "double"=>"cDF",
 "gsl_complex"=>"cDC",
 "unsigned int"=>"cUI",
 "int"=>"cI",
 "size_t"=>"cSZ",
}
NACLASSMAP =
{
 "double"=>"Numo::DFloat",
 "gsl_complex"=>"Numo::DComplex",
 "unsigned int"=>"Numo::UInt",
 "int"=>"Numo::Int",
 "size_t"=>"Numo::UInt64",
 "gsl_sf_result *"=>["Numo::DFloat"]*2,
 "gsl_sf_result_e10 *"=>["Numo::DFloat"]*2+["Numo::Int"],
 "gsl_sf_mathieu_workspace *"=>"Numo::GSL::Sf::MathieuWorkspace"
}
RBCLASSMAP =
{
 "double"=>"Float",
 "gsl_complex"=>"Complex",
 "unsigned int"=>"Integer",
 "int"=>"Integer",
 "size_t"=>"Integer",
 "gsl_mode_t"=>"Integer",
 "gsl_sf_result *"=>["Float"]*2
}
CTYPEMAP =
{
 "double"=>"double",
 #"unsigned int"=>"uint32_t",
 #"int"=>"int32_t",
 "unsigned int"=>"unsigned int",
 "int"=>"int",
 "size_t"=>"uint64_t",
 "gsl_complex"=>"gsl_complex",
}
RETNAMEMAP =
{
 "double"=>"Numo::DFloat",
 "gsl_complex"=>"Numo::DComplex",
 #"unsigned int"=>"Numo::UInt32",
 #"int"=>"Numo::Int32",
 "unsigned int"=>"Numo::UInt",
 "int"=>"Numo::Int",
 "size_t"=>"Numo::UInt64",
 "gsl_sf_result *"=>["Numo::DFloat"]*2,
 "gsl_sf_result_e10 *"=>["Numo::DFloat"]*2+["Numo::Int"]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(func, idx, type, name, prop) ⇒ Argument



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/numo/gsl/gen/func_parser.rb', line 93

def initialize(func,idx,type,name,prop)
  @func = func
  @idx  = idx
  @type = type
  @name = name
  @prop = prop
  @pass = @prop[:pass]

  if /(.+)\*$/ =~ @type
    @type2 = $1.strip
  end

  if @prop[:output]
    @n_out =
      case @type
      when "gsl_sf_result_e10 *";  3
      when "gsl_sf_result *";      2
      else                         1
      end
  end

  @ctype = CTYPEMAP[@type] || CTYPEMAP[@type2]
end

Instance Attribute Details

#n_outObject (readonly)

Returns the value of attribute n_out.



118
119
120
# File 'ext/numo/gsl/gen/func_parser.rb', line 118

def n_out
  @n_out
end

#nameObject (readonly)

Returns the value of attribute name.



117
118
119
# File 'ext/numo/gsl/gen/func_parser.rb', line 117

def name
  @name
end

#passObject (readonly)

Returns the value of attribute pass.



118
119
120
# File 'ext/numo/gsl/gen/func_parser.rb', line 118

def pass
  @pass
end

#typeObject (readonly)

Returns the value of attribute type.



117
118
119
# File 'ext/numo/gsl/gen/func_parser.rb', line 117

def type
  @type
end

Instance Method Details

#ain_defObject



140
141
142
# File 'ext/numo/gsl/gen/func_parser.rb', line 140

def ain_def
  "{#{na_type},0}"
end

#aout_defObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'ext/numo/gsl/gen/func_parser.rb', line 144

def aout_def
  case @type
  when "gsl_sf_result_e10 *"
    return "{cDF,0},{cDF,0},{cI,0}"
  when "gsl_sf_result *"
    return "{cDF,0},{cDF,0}"
  end
  case @pass
  when :array
    "{#{na_type},1,shape}"
  when :pointer,:return
    "{#{na_type},0}"
  else
    raise "no aout_def for #{self.inspect}"
  end
end

#c_argObject



128
129
130
# File 'ext/numo/gsl/gen/func_parser.rb', line 128

def c_arg
  (@pass == :pointer) ? "&"+c_var : c_var
end

#c_varObject



124
125
126
# File 'ext/numo/gsl/gen/func_parser.rb', line 124

def c_var
  "c#{@idx}"
end

#cntObject



270
271
272
# File 'ext/numo/gsl/gen/func_parser.rb', line 270

def cnt
  @func.counter.inc
end

#def_valueObject



200
201
202
# File 'ext/numo/gsl/gen/func_parser.rb', line 200

def def_value
  "VALUE #{v_var};"
end

#def_varObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'ext/numo/gsl/gen/func_parser.rb', line 184

def def_var
  if @prop[:param]
    "#{@type} #{c_var};"
  elsif @prop[:input]
    "#{@type} #{c_var};"
  elsif @pass == :array
    "#{@type} *#{c_var};"
  elsif @pass == :pointer
    "#{@type2} #{c_var};"
  elsif @pass == :return
    "#{@type} #{c_var};"
  else
    raise "no variable definition"
  end
end

#descriptionObject



3
4
5
6
7
8
9
10
# File 'ext/numo/gsl/gen/func_parser.rb', line 3

def description
  @func.param_desc[@name] ||
    if @prop[:param]
      "@param [#{rb_class}] #{@name}"
    elsif @prop[:narray]
      "@param [#{na_class}] #{@name}"
    end
end

#get_data(lp) ⇒ Object



245
246
247
# File 'ext/numo/gsl/gen/func_parser.rb', line 245

def get_data(lp)
  "#{c_var} = *(#{@ctype}*)GET_PTR(#{lp},#{cnt}); //#{@name}"
end

#get_param(opt) ⇒ Object



241
242
243
# File 'ext/numo/gsl/gen/func_parser.rb', line 241

def get_param(opt)
  "#{c_var} = *(#{@type}*)(#{opt}); //#{@name}"
end

#get_ptr(lp) ⇒ Object



249
250
251
252
253
# File 'ext/numo/gsl/gen/func_parser.rb', line 249

def get_ptr(lp)
  if @pass == :array
    "#{c_var} = (#{@ctype}*)GET_PTR(#{lp},#{cnt}); //#{@name}"
  end
end

#get_valueObject



208
209
210
211
212
213
214
215
# File 'ext/numo/gsl/gen/func_parser.rb', line 208

def get_value
  case a = DATA2NUM[@type]
  when String
    a.gsub(/%1/,c_var)
  when Array
    a.map{|s| s.gsub(/%1/,c_var)}
  end
end

#na_classObject



165
166
167
# File 'ext/numo/gsl/gen/func_parser.rb', line 165

def na_class
  NACLASSMAP[@type] || NACLASSMAP[@type2]
end

#na_typeObject



161
162
163
# File 'ext/numo/gsl/gen/func_parser.rb', line 161

def na_type
  TYPEMAP[@type] || TYPEMAP[@type2]
end

#rb_classObject



169
170
171
# File 'ext/numo/gsl/gen/func_parser.rb', line 169

def rb_class
  RBCLASSMAP[@type] || RBCLASSMAP[@type2]
end

#ret_valObject



173
174
175
176
177
178
179
180
181
182
# File 'ext/numo/gsl/gen/func_parser.rb', line 173

def ret_val
  case @type
  when "gsl_sf_result *"
    [@name+".val",@name+".err"]
  when "gsl_sf_result_e10 *"
    [@name+".val",@name+".err",@name+".e10"]
  else
    @name
  end
end

#set_data(lp) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'ext/numo/gsl/gen/func_parser.rb', line 255

def set_data(lp)
  if @pass != :array
    if "gsl_sf_result_e10 *" == @type
      "*(double*)GET_PTR(#{lp},#{cnt}) = #{c_var}.val; "+
      "*(double*)GET_PTR(#{lp},#{cnt}) = #{c_var}.err; "+
      "*(int*)GET_PTR(#{lp},#{cnt}) = #{c_var}.e10; //#{@name}"
    elsif "gsl_sf_result *" == @type
      "*(double*)GET_PTR(#{lp},#{cnt}) = #{c_var}.val; "+
      "*(double*)GET_PTR(#{lp},#{cnt}) = #{c_var}.err; //#{@name}"
    else
      "*(#{@ctype}*)GET_PTR(#{lp},#{cnt}) = #{c_var}; //#{@name}"
    end
  end
end

#set_param(opt) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/numo/gsl/gen/func_parser.rb', line 223

def set_param(opt)
  n = @func.n_input
  if @type == "gsl_mode_t" && n == @idx+1
    return "if (argc==\#{@idx}) {\n      \#{c_var} = GSL_MODE_DEFAULT;\n  } else if (argc==\#{@idx+1}) {\n      \#{set_value}\n  } else {\n      rb_raise(rb_eArgError,\"invalid number of argument: %d for \#{n-1} or \#{n}\",argc);\n  }\n  \#{opt} = &\#{c_var}; //\#{@name}\n"
  else
    return "#{set_value} #{opt} = &#{c_var}; //#{@name}"
  end
end

#set_valueObject



204
205
206
# File 'ext/numo/gsl/gen/func_parser.rb', line 204

def set_value
  NUM2DATA[@type].gsub(/%1/,v_var).sub(/%2/,c_var)+";"
end

#store_to_array(va) ⇒ Object



217
218
219
220
221
# File 'ext/numo/gsl/gen/func_parser.rb', line 217

def store_to_array(va)
  a = get_value
  a = [a] unless Array === a
  a.map{|x|"rb_ary_push(#{va},#{x});"}.join("\n"+" "*8)
end

#v_varObject



132
133
134
135
136
137
138
# File 'ext/numo/gsl/gen/func_parser.rb', line 132

def v_var
  if @func.n_arg < 0
    "v[#{@idx}]"
  else
    "v#{@idx}"
  end
end