Class: MethodDefinition

Inherits:
Object
  • Object
show all
Defined in:
ext/kmat/auto_collect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_arg, comment) ⇒ MethodDefinition

Returns a new instance of MethodDefinition.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'ext/kmat/auto_collect.rb', line 3

def initialize(name_arg, comment)
	comment = %r|//\s+(.*)|.match(comment)[1]
	@als = /\Aalias (.+)/.match(comment)&.[](1)&.split(/[,\s]\s*/)
	@meth = /\Akmm_/.match(name_arg)
	return unless @meth
	m = %r|\A(kmm_([^\(_]+)_([^\(]+))\((.+)\)\Z|.match(name_arg)
	raise "unknown name_arg pattern `#{name_arg}' found" unless m
	@funcname, @type, @name, arg =  m[1], type_trans(m[2]), m[3], m[4]
	if m = %r|(.+)_p\Z|.match(@name)
		@name = m[1]+'?'
	elsif m = %r|(.+)_dest\Z|.match(@name)
		@name = m[1]
		@dup_esc = true
	elsif m = %r|(.+)_destl\Z|.match(@name)
		@name = m[1]+'!'
	end
	if m = %r|(.+)_m2\Z|.match(@name)
		@name = m[1]
		@argc = -2
	elsif m = %r|\Aint argc|.match(arg)
		@argc = -1
	else
		@argc = arg.split(/,/).size-1
	end
end

Instance Attribute Details

#methObject (readonly)

Returns the value of attribute meth.



28
29
30
# File 'ext/kmat/auto_collect.rb', line 28

def meth
  @meth
end

Instance Method Details

#to_s(join = "\n\t") ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/kmat/auto_collect.rb', line 49

def to_s(join="\n\t")
	ret = []
	if @type.kind_of?(Array)
		ret << %Q|rb_define_module_function(#{@type[0]}, "#{@name}", #{@funcname}, #{@argc});|
	elsif %r|\A\_|.match(@name)
		ret << %Q|rb_define_private_method(#{@type}, "#{@name}", #{@funcname}, #{@argc});|
	elsif @dup_esc
		ret << %Q|rb_define_method(#{@type}, "#{@name}!", #{@funcname}, #{@argc});|
		ret << %Q|rb_funcall(km_cMat, id__define_dup_escaped_method, 1, rb_str_new_cstr("#{@name}"));|
	else
		ret << %Q|rb_define_method(#{@type}, "#{@name}", #{@funcname}, #{@argc});|
	end
	@als&.each do |a|
		if @type.kind_of?(Array)
			ret << %Q|rb_define_alias(#{@type[0]}, "#{a}", "#{@name}");|
			ret << %Q|rb_define_alias(#{@type[1]}, "#{a}", "#{@name}");|
		else
			ret << %Q|rb_define_alias(#{@type}, "#{a}", "#{@name}");|
			ret << %Q|rb_define_alias(#{@type}, "#{a}!", "#{@name}!");| if @dup_esc
		end
	end
	ret.join(join)
end

#type_trans(t) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/kmat/auto_collect.rb', line 30

def type_trans(t)
	case t
	when 'obj'
		'rb_cObject'
	when 'mat'
		'km_cMat'
	when 'Mat'
		'km_sMat'
	when 'ary'
		'rb_cArray'
	when 'MATH'
		['rb_mMath', 'rb_sMath']
	when 'float'
		'rb_cFloat'
	else
		raise "unknown type `#{t}' found"
	end
end