Class: Rubber::C_Function
- Inherits:
-
Object
- Object
- Rubber::C_Function
- Defined in:
- lib/rubber/codegen/function.rb
Constant Summary collapse
- CAST_SAFER =
/\<\{([a-z0-9A-Z* ]+)(>[a-z0-9A-Z* ]+)?:([^{};:]+)\}\>/
- CAST =
/\<([^:;{}]+):([a-z0-9A-Z* ]+)\>/
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#multi ⇒ Object
readonly
Returns the value of attribute multi.
-
#rest ⇒ Object
readonly
Returns the value of attribute rest.
-
#singleton ⇒ Object
readonly
Returns the value of attribute singleton.
-
#source_file ⇒ Object
Returns the value of attribute source_file.
-
#source_line ⇒ Object
Returns the value of attribute source_line.
Instance Method Summary collapse
- #alloc_func? ⇒ Boolean
- #arity ⇒ Object
- #check ⇒ Object
-
#cname ⇒ Object
include RegisterChildren.
- #code(io) ⇒ Object
- #declare(io) ⇒ Object
- #default ⇒ Object
- #doc_rd(io) ⇒ Object
- #fullname ⇒ Object
- #guess(name) ⇒ Object
- #prototype(io) ⇒ Object
- #register(io, already_defined = false) ⇒ Object
- #rname ⇒ Object
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
6 7 8 |
# File 'lib/rubber/codegen/function.rb', line 6 def block @block end |
#multi ⇒ Object (readonly)
Returns the value of attribute multi.
6 7 8 |
# File 'lib/rubber/codegen/function.rb', line 6 def multi @multi end |
#rest ⇒ Object (readonly)
Returns the value of attribute rest.
6 7 8 |
# File 'lib/rubber/codegen/function.rb', line 6 def rest @rest end |
#singleton ⇒ Object (readonly)
Returns the value of attribute singleton.
6 7 8 |
# File 'lib/rubber/codegen/function.rb', line 6 def singleton @singleton end |
#source_file ⇒ Object
Returns the value of attribute source_file.
5 6 7 |
# File 'lib/rubber/codegen/function.rb', line 5 def source_file @source_file end |
#source_line ⇒ Object
Returns the value of attribute source_line.
5 6 7 |
# File 'lib/rubber/codegen/function.rb', line 5 def source_line @source_line end |
Instance Method Details
#alloc_func? ⇒ Boolean
259 260 261 |
# File 'lib/rubber/codegen/function.rb', line 259 def alloc_func? name.split(/\./,2).last == '__alloc__' end |
#arity ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/rubber/codegen/function.rb', line 252 def arity() check() return -1 if @multi num = args.size num -= 1 if @block num end |
#check ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubber/codegen/function.rb', line 7 def check() return if @checked @block = @rest = @multi = false @singleton = true if name.include?('.') @arghash = {} @min_args = 0 @opt_args = 0 args.each { |arg| @arghash[arg.name] = arg raise "Too many block parameters for #{parent.name}.#{name.gsub(/^self./,'')}" if @block and arg.block raise "Too many rest parameters for #{parent.name}.#{name}" if @rest and arg.rest @multi = true if arg.default or arg.rest if @multi @opt_args += 1 unless arg.block or arg.rest else @min_args += 1 unless arg.block end @block = arg if arg.block @rest = arg if arg.rest } @scan_string = "#{@min_args}#{@opt_args}#{@rest ? "*" : ""}#{@block ? "&" : ""}" @vars ||= {} @checked = true end |
#cname ⇒ Object
include RegisterChildren
35 36 37 |
# File 'lib/rubber/codegen/function.rb', line 35 def cname() @cname ||= parent.name + ( @singleton ? '_CLASS' :'') + '_' + rname.gsub(/[?]/, '_query'). gsub(/[!]/, '_pling'). gsub(/[=]/, '_equals'). gsub(/[\[\]]/, '_brace') end |
#code(io) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rubber/codegen/function.rb', line 92 def code(io) require 'stringio' check() prototype(io) io.puts "" io.puts "{" oio = io io = StringIO.new() args.each { |arg| arg.declare(io,self) unless not @multi and not arg.auto_convert? and not arg.block } parent.pre_func(io, self) if parent.respond_to?(:pre_func) if @multi io.puts "" io.puts " /* Scan arguments */" io.puts(" rb_scan_args(__p_argc, __p_argv, #{@scan_string.inspect},#{args.collect {|i| "&"+i.cname }.join(', ') });") io.puts "" io.puts " /* Set defaults */" args.each_index { |i| arg = args[i] if arg.auto_convert? io.write(" if (__p_argc > #{i})\n ") if arg.default io.puts(" __orig_#{arg.name} = #{arg.name} = #{Rubber::explicit_cast(arg.cname, 'VALUE', arg.ctype)};") io.puts(" else\n #{arg.name} = #{arg.default};") if arg.default else io.puts(" if (__p_argc <= #{i})\n #{arg.name} = #{arg.default};") if arg.default and not arg.block io.puts(" else") if arg.default and arg.rtype arg.check_type(io) if arg.rtype end io.puts "" if arg.default or arg.auto_convert? } #io.puts(" switch (argc) {") #for i in @min_args .. @min_args + @opt_args # io.puts " case #{i}:" #end #io.puts(" }") else args.each { |arg| if arg.auto_convert? io.puts(" __orig_#{arg.name} = #{arg.name} = #{Rubber::explicit_cast(arg.cname, 'VALUE', arg.ctype)};") elsif arg.block io.puts(" VALUE #{arg.name} OPTIONAL_ATTR = #{arg.init_value()};") else arg.check_type(io) if arg.rtype end } end io.puts "" io.puts "#line #{source_line} #{source_file.inspect}" if source_line setupvars = io.string io = oio returned = false oio =io io = StringIO.new() sc = StringScanner.new(text.strip) io.write " " # Initial indent until sc.empty? if txt = sc.scan(CAST_SAFER) from_type, to_type, cast = sc[1], sc[2], sc[3] arg = @arghash[cast] to_type = to_type[1..-1] unless to_type.nil? or to_type.empty? to_type = (!(to_type.nil? or to_type.empty?) && to_type || (arg && arg.ctype || guess(cast))) io.write(Rubber.explicit_cast(cast, from_type, to_type)) elsif txt = sc.scan(CAST) warn("<TYPE:VALUE> is deprecated - please use <{FROM_TYPE>TO_TYPE:VALUE}> instead.") name, cast = sc[1], sc[2] arg = @arghash[name] io.write(Rubber::explicit_cast(name, arg && arg.ctype || guess(name), cast)) elsif txt = sc.scan(/['"]/) #' Skip quoted string txt += sc.scan_until(/(^|[^\\])#{txt}/) # Skip until unescaped quote io.write(txt) elsif c = sc.scan(/;/) io.write ";\n " elsif c = sc.scan(/return\s+/) val = sc.scan_until(/;/) val.strip! if val if val and val.size > 1 val.chop! # Trim last char io.write "do { __p_retval = " # Scan returned bit for casts retval = "" mini_scanner = StringScanner.new(val) until mini_scanner.eos? if txt = mini_scanner.scan(CAST_SAFER) from_type, to_type, cast = mini_scanner[1], mini_scanner[2], mini_scanner[3] arg = @arghash[cast] to_type = to_type[1..-1] unless to_type.nil? or to_type.empty? to_type = (!(to_type.nil? or to_type.empty?) && to_type || (arg && arg.ctype || guess(cast))) retval << (Rubber::explicit_cast(cast, from_type, to_type)) elsif txt = mini_scanner.scan(CAST) warn("<TYPE:VALUE> is deprecated - please use <{TYPE>TO:VALUE}> instead.") name, cast = mini_scanner[1], mini_scanner[2] arg = @arghash[name] retval << (Rubber::explicit_cast(name, arg && arg.ctype || guess(name), cast)) else retval << (mini_scanner.get_byte) end end unless Rubber.native_type?(returntype) io << Rubber.explicit_cast(retval, returntype, 'VALUE') else io << retval end #end of internal scan io.write "; goto out; } while(0);" returned = true else parent.post_func(io, self) if parent.respond_to?(:post_func) io.write(";") end elsif c = sc.scan(/([a-z0-9A-Z_]+[ *]+)([a-zA-Z0-9]+)\s*([\[\]0-9]*)\s*([;=])/) io.write "#{[sc[1], sc[2], sc[3], sc[4]].join(' ')}\n" @vars ||= {} base = sc[1].split(/\s/).first p = 0 (sc[1]+sc[3]).each_byte { |i| p += 1 if i == ?[ or i == ?* } @vars[sc[2]] = base.strip + ( p > 0 && (' ' + ('*' * p)) || '' ) elsif c = sc.get_byte io.write(c) end end code = io.string io = oio io.puts " VALUE __p_retval OPTIONAL_ATTR = #{default()};" if returned io.puts setupvars io.puts "\n do {" if @vars and not @vars.empty? io.puts code io.puts "\n } while(0);\n\n" if @vars and not @vars.empty? io.puts "out:" if returned parent.post_func(io, self) if parent.respond_to?(:post_func) args.each { |arg| if arg.auto_convert? && $custom_frees[arg.ctype.gsub(/ /,'')] io.puts($custom_frees[arg.ctype].gsub(/%%/,"__orig_#{arg.name}")+";") end } if returned io.puts " return __p_retval;" else io.puts " return #{default()};" end io.puts "}" io.puts "" end |
#declare(io) ⇒ Object
66 67 68 69 70 |
# File 'lib/rubber/codegen/function.rb', line 66 def declare(io) check() prototype(io) io.puts ";" end |
#default ⇒ Object
243 244 245 246 247 248 249 250 251 |
# File 'lib/rubber/codegen/function.rb', line 243 def default if rname =~ /=\z/ and args.size == 1 args.first.cname elsif rname =~ /\Aset_/ "self" else "Qnil" end end |
#doc_rd(io) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/rubber/codegen/function.rb', line 48 def doc_rd(io) io << "--- #{fullname}" io << "(#{args.collect { |i| i.ruby_def }.join(', ')})\n" unless (fullname =~ /[!?]\z/ || !@singleton) and args.empty? io.puts doc io << "\n" end |
#fullname ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rubber/codegen/function.rb', line 38 def fullname() check() str = parent.fullname.dup if name == 'initialize' str << ".new" else str << ( @singleton ? (/^[a-zA-Z]/.match(rname) ? ".#{rname}" : rname) : "##{rname}") end str end |
#guess(name) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rubber/codegen/function.rb', line 71 def guess(name) if name == 'self' 'VALUE' elsif name == '_self' and parent.kind_of?(C_GObject) 'GObject*' elsif @vars[name] @vars[name] else if name =~ /^([A-Za-z0-9_]+)\[.*$/ if @vars[$1] @vars[$1] else VALUE; end else 'VALUE' end end end |
#prototype(io) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubber/codegen/function.rb', line 54 def prototype(io) io.puts "static VALUE" io.write "#{cname}(" if @multi io.write "int __p_argc, VALUE *__p_argv, VALUE self" else io.write "VALUE self OPTIONAL_ATTR " io.write(args.reject {|i| i.block }.collect { |i| ', VALUE ' + i.cname + " OPTIONAL_ATTR" }.join('')) #io.write ", " if args.size > 1 or (args.size > 0 and not @block) end io.write ")" end |
#register(io, already_defined = false) ⇒ Object
262 263 264 265 266 267 268 |
# File 'lib/rubber/codegen/function.rb', line 262 def register(io, already_defined=false) if alloc_func? io.puts " rb_define_alloc_func(#{parent.cname}, #{cname});" else io.puts " rb_define_#{@singleton ? "singleton_" : ""}method(#{parent.cname}, #{rname.inspect}, #{cname}, #{arity});" end end |
#rname ⇒ Object
31 32 33 |
# File 'lib/rubber/codegen/function.rb', line 31 def rname() name.split(/\./).last end |