diff --git a/Cargo.toml b/Cargo.toml index 69c4922..0048e0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "purrchance" -version = "0.1.0" +version = "0.2.0" authors = ["xenofem "] description = "An unofficial Rust implementation of the Perchance grammar engine" repository = "https://git.xeno.science/xenofem/purrchance" diff --git a/src/bin/purrchance.rs b/src/bin/purrchance.rs new file mode 100644 index 0000000..87d1664 --- /dev/null +++ b/src/bin/purrchance.rs @@ -0,0 +1,25 @@ +use purrchance::*; +use purrchance::parser::*; + +use std::env; +use std::fs::read_to_string; +use std::process::exit; + +fn main() { + let args: Vec = env::args().collect(); + + if args.len() < 2 { + eprintln!("too few arguments!"); + exit(1); + } + + let raw_grammar = read_to_string(&args[1]).unwrap(); + let grammar = load_grammar(&raw_grammar); + if args.len() < 3 { + eprintln!("{:?}", grammar); + exit(0); + } + + let sym = Symbol::NonTerminal(args[2].clone()); + println!("{}", sym.eval(&grammar).unwrap()); +} diff --git a/src/lib.rs b/src/lib.rs index b2c529a..038fea9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ pub trait Purrchance { fn eval(&self, g: &Grammar) -> Option; } +#[derive(Debug)] pub enum Symbol { Terminal(String), NonTerminal(String), @@ -24,6 +25,7 @@ impl Purrchance for Symbol { } } +#[derive(Debug)] pub struct Expr(Vec); impl Purrchance for Expr { @@ -32,6 +34,7 @@ impl Purrchance for Expr { } } +#[derive(Debug)] pub struct List(Vec<(Expr, f64)>); impl Purrchance for List { @@ -40,6 +43,7 @@ impl Purrchance for List { } } +#[derive(Debug)] pub struct Grammar(HashMap); #[cfg(test)] @@ -142,6 +146,19 @@ test test foo ^1000000 bar ^1/1000000 +"); + let nt = Symbol::NonTerminal(String::from("test")); + assert_eq!(nt.eval(&g), Some(String::from("foo"))); + } + + #[test] + fn eval_loaded_grammar_multiple_lists() { + let g = load_grammar(" +test + [test1] + +test1 + foo "); let nt = Symbol::NonTerminal(String::from("test")); assert_eq!(nt.eval(&g), Some(String::from("foo")));