Class: Rubber::C_Param

Inherits:
Object
  • Object
show all
Defined in:
lib/rubber/codegen/param.rb

Constant Summary collapse

RUBY_NATIVE_TYPES =
%w|T_NIL T_OBJECT T_CLASS T_MODULE T_FLOAT T_STRING T_REGEXP T_ARRAY T_FIXNUM T_HASH T_STRUCT T_BIGNUM T_FILE T_TRUE T_FALSE T_DATA T_SYMBOL|
RUBY_NATIVE_NAMES =
%w|Nil Object Class Module Float String Regexp Array Fixnum Hash Struct Bignum File True False Data Symbol|
NICE_CNAMES =
{'char*'=> 'String', 'long'=>'Integer', 'int'=>'Integer', 'uint'=>'Unsigned Integer', 'ulong'=>'Unsigned Integer', 'bool'=>'Boolean'}

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ C_Param

Returns a new instance of C_Param.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubber/codegen/param.rb', line 8

def initialize(str)
  r = StringScanner.new(str)
  @ctype = r.scan(/[a-z_A-Z0-9|]+\s[*]*/) || 'VALUE'
  @ctype.squeeze!(' ') if @ctype
  @ctype.strip! if @ctype
  if RUBY_NATIVE_TYPES.include?(@ctype)
    @rtype = @ctype
    @ctype = 'VALUE'
  elsif (types = @ctype.split(/\|/)).size > 1
    types.each { |i| i.strip! }
    if RUBY_NATIVE_TYPES.include?(types.first)
      @ctype = "VALUE"
      @rtype = types
    end
  end
  r.skip(/\s*/)
  @rest = (r.skip(/\*/) and true)
  @block = (r.skip(/\&/) and true)
  @name = r.scan(/[A-Za-z0-9_]+/)
  r.skip(/\s*/)
  if r.scan(/=(.*)/)
    @default= r[1].strip
  end
end

Instance Method Details

#auto_convert?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubber/codegen/param.rb', line 40

def auto_convert?
  ctype and ctype != "VALUE"
end

#check_type(io) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rubber/codegen/param.rb', line 50

def check_type(io)
  case @rtype
  when String
    io.puts "  Check_Type(#{cname}, #{@rtype});"
  when Array
    io.puts "  if (! (" + @rtype.collect { |type|  "(TYPE(#{cname}) == #{type})" }.join(' || ') + ") )"
    io.puts "    rb_raise(rb_eArgError, \"#{name} argument must be one of #{@rtype.collect {|i| RUBY_NATIVE_NAMES[RUBY_NATIVE_TYPES.index(i)]}.join(', ') }\");"
  end
end

#cnameObject

include RegisterChildren



33
34
35
36
37
38
39
# File 'lib/rubber/codegen/param.rb', line 33

def cname()
  if auto_convert?
    "__v_#{name}"
  else
    name
  end
end

#declare(io, fn) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/rubber/codegen/param.rb', line 59

def declare(io,fn)
  if auto_convert?
    io.puts "  VALUE #{cname} = #{init_value};" if fn.multi
    io.puts "  #{ctype} #{name}; #{ctype} __orig_#{name};"
  else
    io.puts "  VALUE #{cname} = #{init_value};" if fn.multi
  end
end

#init_valueObject



43
44
45
46
47
48
49
# File 'lib/rubber/codegen/param.rb', line 43

def init_value()
  if @block and not @default
    "rb_block_proc()"
  else
    "Qnil"
  end
end

#ruby_defObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rubber/codegen/param.rb', line 71

def ruby_def()
  if @rtype.kind_of?(String)
    type = RUBY_NATIVE_NAMES[RUBY_NATIVE_TYPES.index(rtype)] + ' '
  elsif @rtype.kind_of?(Array)
    types = @rtype.dup
    types.delete('T_NIL') # Don't mention nil option
    type = types.collect { |rtype| RUBY_NATIVE_NAMES[RUBY_NATIVE_TYPES.index(rtype)] }.join(' or ') + ' '
  elsif ctype and ctype != 'VALUE'
    if NICE_CNAMES.has_key?(ctype)
      type = NICE_CNAMES[ctype]
    else
      type = ctype
    end
    type += ' '
  else
    type = ''
  end
  "#{type}#{name}"
end

#to_strObject



67
68
69
# File 'lib/rubber/codegen/param.rb', line 67

def to_str()
  "#{ctype} #{name} #{default ? ' = ' + default : ''}"
end