ref: c65c9894c643308c87de69de8c0be5c6c6e1c663
parent: 0083fb4ab61c40d2117948840c5be31bba758eb4
author: Ori Bernstein <[email protected]>
date: Wed Oct 23 06:18:54 EDT 2013
Refactor running the re out from initializing. This should make it easier to add regex.search(), which has different requirements from regex.exec()
--- a/interp.myr
+++ b/interp.myr
@@ -4,8 +4,10 @@
pkg regex =
const exec : (re : regex#, str : byte[:] -> bool)
+ const find : (re : regex#, str : byte[:] -> bool)
;;
+
const exec = {re, str
var i
@@ -12,6 +14,24 @@
re.debug = true
re.str = str
re.strp = 0
+ run(re)
+ match re.matched
+ `std.None:
+ -> false
+ ;;
+ `std.Some thr:
+ for i = 0; i < re.nmatch; i++
+ std.put("match %z:", i)
+ std.put("[%z..%z]\n", thr.mstart[i], thr.mend[i])
+ ;;
+ -> thr.mend[0] == str.len
+ ;;
+ ;;
+}
+
+const run = {re
+ var i
+
re.matched = `std.None
mkthread(re, 0)
re.thr[0].mstart = std.slalloc(re.nmatch)
@@ -22,21 +42,12 @@
;;
while re.nthr > 0
for i = 0; i < re.nthr; i++
- trace(re, "tid=%z, ip=%z, c=b\n", re.thr[i].uid, re.thr[i].ip, str[re.strp])
+ trace(re, "tid=%z, ip=%z, c=b\n", re.thr[i].uid, re.thr[i].ip, re.str[re.strp])
step(re, i)
;;
if re.nthr > 0
re.strp++
;;
- ;;
- match re.matched
- `std.Some thr:
- for i = 0; i < re.nmatch; i++
- std.put("match %z:", i)
- std.put("[%z..%z]\n", thr.mstart[i], thr.mend[i])
- ;;
- -> re.strp == str.len;;
- `std.None: -> false;;
;;
}
--- a/main.myr
+++ b/main.myr
@@ -3,7 +3,7 @@
const main = {
var found
- match regex.compile("a*b*")
+ match regex.compile("(a*b)*")
`std.Success re:
found = regex.exec(re, "a")
std.put("Found = %t: len = %z\n", found, re.strp)