# Basic example of using an embedded regex compiler. Requires
# "regex-compiler.pbc", which can be generated by running
#
#  parrot -o regex-compiler.pbc regex-compiler.pir
#

.sub main :main
        .local pmc match
        .local pmc compile_regex
        .local pmc regex1
        .local pmc regex2
        load_bytecode "regex-compiler.pbc"
        compile_regex = compreg "regex"
        regex1 = compile_regex("a*")
        match = regex1("aaaargh")

        print "regex match \"aaaargh\" =~ /a*/: "
        if match goto goodness
        print "failed\n"

match2:
        regex2 = compile_regex("aa")
        .local pmc stack
        stack = new IntList
        match = regex2(1, "aaaargh", 0, stack)

        print "regex match \"aaaargh\" =~ /aa/: "
        if match goto goodness2
        print "failed\n"
        goto match3

goodness:
        print "succeeded!\n"
        print "Matching range: "
        $I0 = match["0";0]
        print $I0
        print ".."
        $I0 = match["0";1]
        print $I0
        print "\n"
        goto match2

goodness2:
        print "succeeded!\n"
        print "Matching range: "
        $I0 = match["0";0]
        print $I0
        print ".."
        $I0 = match["0";1]
        print $I0
        print "\n"

match3:
        match=regex2("blah")
        print "regex match \"blah\" =~ /aa/: "
        if match goto badness3
        print "Failed (as it should)\n"
        end

badness3:
        print "succeeded?! (should not have)\n"
        end
.end
