Starting from this:
./backtrack_inline.rb:73: error: too few arguments to function 'backtrack'
If you look at the generated code, the backtrack function is defined on line 29:
static VALUE backtrack(VALUE self, VALUE _ss, VALUE _s, VALUE _p, VALUE _mm, VALUE _ins, VALUE _del) { ... }
It has seven arguments, the original six, plus VALUE self , since it was converted to a Scan class method.
The call to this function on line 67 is as follows:
end = backtrack(ss, s, p, mm, ins, del);
He has only six arguments. RubyInline does not convert this to a method call on an object; it simply copies it verbatim. There are also warnings about makes integer from pointer without a cast : the function definition has been converted to accept VALUE s, but you are calling the original types.
The error message indicates that the error is indicated on line 73 in backtrack_inline.rb due to the directive on line 54 of the generated code:
# line 61 "./backtrack_inline.rb"
which basically tells the compiler to "reset" its line and file values ββfor errors and treats the next line (55) as line 61 in the ./backtrack_inline.rb file. The actual line 67, 12 is ahead of 55, but the compiler reports that it 73, 12 is ahead of 61 (the value that was reset), and from the differnt file. This method does not actually work in this case, since it does not take into account the extra lines added by RubyInline. The actual string in the source is 69.
A simple fix for this is to change the definition of the backtrack function only as a C function, and not add it as an object method. Change builder.c to builder.prefix (on line 38 of your Ruby file). This will not work if you want to have backtrack as a method for an object in Ruby. If in this case you may need to create another function, which will be a method that then wraps the "real" backtracking function.
Next, looking at
./backtrack_inline.rb:67: error: lvalue required as unary '&' operand
This actually refers to line 61 of the generated code, which looks like this:
char* s = StringValuePtr(rb_iv_get(self, "@seq"));
StringValuePtr - a macro that is defined as :
#define StringValue(v) rb_string_value(&(v))
Here is the & in the lvalue required as unary '&' operand . You must add a local variable to the lvalue value:
VALUE seq = rb_iv_get(self, "@seq"); char* s = StringValuePtr(seq);
In my case (Mac OS X Snow Leopard, Ruby 1.9.3-p0, RubyInline 3.11.0) these two changes started the script without errors, but gave a warning:
backtrack_inline.rb:47: warning: implicit conversion shortens 64-bit value into a 32-bit value
This actually refers to line 46 of the ruby ββfile:
return (s - ss) - 1;
s and ss are char * , that is, 64-bit pointers (on this computer), and the return type of the int function is 32 bits. Adding explicit casts fixed:
return (int)((s - ss) - 1);
Now it works cleanly:
ruby-inline $ ruby backtrack_inline.rb 14 ruby-inline $
(I hope this is the correct answer!)
Here is the version of the script with these changes .