diff options
Diffstat (limited to 'src/vm.rs')
| -rw-r--r-- | src/vm.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/vm.rs b/src/vm.rs new file mode 100644 index 0000000..e780e96 --- /dev/null +++ b/src/vm.rs @@ -0,0 +1,36 @@ +use crate::syntax::ast::Regex; + +pub mod compile; +pub mod instr; +pub mod vm; + +pub fn is_match(re: Regex, s: &str) -> bool { + let instrs = compile::compile(&re); + vm::run(&instrs, s.as_bytes()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn re(p: &str) -> Regex { + use crate::syntax::parse::parse; + parse(p).unwrap() + } + + #[test] + fn matches() { + assert!(is_match(re("a"), "a")); + assert!(is_match(re("a*"), "")); + assert!(is_match(re("a*"), "a")); + assert!(is_match(re("a|b*"), "a")); + assert!(is_match(re("a|b*"), "bb")); + assert!(is_match(re("(a|b)*"), "abababaa")); + assert!(is_match(re("a*b*"), "abb")); + } + + #[test] + fn does_not_match() { + assert!(!is_match(re("a"), "b")); + } +} |
