Last active
          December 14, 2015 13:38 
        
      - 
      
- 
        Save FROGGS/5094474 to your computer and use it in GitHub Desktop. 
    working NFA example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | sub store_regex_alt_nfa($code_obj, $key, @alternatives) { | |
| my @saved; | |
| for @alternatives { | |
| @saved.push($_.save(:non_empty)); | |
| } | |
| $code_obj.SET_ALT_NFA($key, @saved); | |
| } | |
| my $code_obj := nqp::create(NQPRegex); | |
| my $ast := QAST::Regex.new( | |
| QAST::Regex.new( :rxtype<literal>, "xbc" ), | |
| QAST::Regex.new( :rxtype<literal>, "123" ), | |
| QAST::Regex.new( :rxtype<concat>, | |
| QAST::Regex.new( :rxtype<literal>, "a"), | |
| QAST::Regex.new( :rxtype<literal>, "b"), | |
| QAST::Regex.new( :rxtype<literal>, "c"), | |
| ), | |
| QAST::Regex.new( :rxtype<concat>, | |
| QAST::Regex.new( :rxtype<literal>, "x"), | |
| QAST::Regex.new( :rxtype<literal>, "b"), | |
| QAST::Regex.new( :rxtype<literal>, "c"), | |
| ), | |
| QAST::Regex.new( :rxtype<literal>, "456" ), | |
| QAST::Regex.new( :rxtype<literal>, "xb" ), | |
| ); | |
| my @alternatives; | |
| for $ast.list { | |
| nqp::push(@alternatives, QRegex::NFA.new.addnode($_)); | |
| } | |
| my $name := QAST::Node.unique('alt_nfa_') ~ '_' ~ ~nqp::time_n(); | |
| $ast.name($name); | |
| store_regex_alt_nfa($code_obj, $ast.name, @alternatives); | |
| my $nfa := QRegex::NFA.new; | |
| my @fates := $nfa.states[0]; | |
| my int $start := 1; | |
| my int $fate := 0; | |
| for $code_obj.ALT_NFA($name) { | |
| @fates[$fate] := $fate; | |
| $nfa.mergesubstates($start, 0, $fate, $_, self); | |
| $fate++; | |
| } | |
| say( $nfa.run( "xbc", 0 ) ); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | my @nodes := [ | |
| QAST::Regex.new( :rxtype<literal>, "xbc" ), | |
| QAST::Regex.new( :rxtype<literal>, "123" ), | |
| QAST::Regex.new( :rxtype<concat>, | |
| QAST::Regex.new( :rxtype<literal>, "a"), | |
| QAST::Regex.new( :rxtype<literal>, "b"), | |
| QAST::Regex.new( :rxtype<literal>, "c"), | |
| ), | |
| QAST::Regex.new( :rxtype<concat>, | |
| QAST::Regex.new( :rxtype<literal>, "x"), | |
| QAST::Regex.new( :rxtype<literal>, "b"), | |
| QAST::Regex.new( :rxtype<literal>, "c"), | |
| ), | |
| QAST::Regex.new( :rxtype<literal>, "456" ), | |
| QAST::Regex.new( :rxtype<literal>, "xb" ), | |
| ]; | |
| my $nfa := QRegex::NFA.new; | |
| my int $start := 1; | |
| my int $fate := 0; | |
| for @nodes { | |
| $nfa.mergesubstates($start, 0, $fate, QRegex::NFA.new.addnode( $_ ).save( :non_empty ), self); | |
| $fate++; | |
| } | |
| say( $nfa.run( "xbc", 0 ) ); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment