Didn't I promise? 

////
for m in (r"\w+", "Hello world today is good") do 
  println$ "Word = " + m.0; 
done
///
~/felix>flx --test=build/release reit
Word = Hello
Word = world
Word = today
Word = is
Word = good
////

Perl eat your heart out!

The implementation is slightly tricky:

gen iterator (re2:string, target:string) () : opt[varray[string]] = {
  var l = len target;
  var s = StringPiece target;
  var p1 = s.data;  
  var p = 0;
  var r=RE2(re2);
  var n = NumberOfCapturingGroups(r)+1;
  var v1 = varray[StringPiece] (n.size,StringPiece "");
  var v2 = varray[string] (n.size,"");
  var start = 0;
again:>
  var result = Match(r, s, p, UNANCHORED,v1.stl_begin, n);
  if not result goto endoff;
  for var i in 0 upto n - 1 do set(v2, i.size, string(v1.i)); done
  var p2 = v1.0.data;
  assert(v1.0.len.int > 0); // prevent infinite loop
  p = (p2 - p1).int+v1.0.len.int;
  yield Some v2;
  goto again;
endoff:>
  return None[varray[string]];
}

There's a BUG in it. Can you see it?

Note the messing about with StringPiece and pointer maths,
this is a design mess in RE2. Blame Google :)

Note the goto. Why not a "if .. do ..else .. done"?
Because Felix complains I'm trying to return a value
from a procedure: it's a bit ugly, the procedure is 
synthesised AND inlined away but in between, its
there. I should fix this, but I'm not sure how.

Oh .. the bug? Well strings are objects not values.
And we're taking the address of the char array inside
the string. so we must say:

gen iterator (re2:string, var target:string) () : opt[varray[string]] = {

with a "var" to ensure "target" is stored in a variable, otherwise
the word "target" could be replaced with its argument
on a call (when inlining). Actually happened .. a debugging
print stopped that optimisation and so I had the fun situation
that it worked with the debug print but not without it.

I'm gradually understanding Felix semantics. This one
is a bit of a "gotcha" so watch out for it. Strings are particularly
prone to it since they're objects not values, but they look a bit
like values.
 
--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to