diff --git a/README.md b/README.md index 718647e..53be8f3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,39 @@ # grassmudhorse.rs -Grass Mud Horse in Rust \ No newline at end of file +Grass Mud Horse in Rust + +help: +```shell +$ ./target/debug/grassmudhorse -h +grassmudhorse.rs v0.1 +``` + +execute from file: +```shell +$ ./target/debug/grassmudhorse print-110.gmh +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +execute from stdin: +```shell +$ cat print-110.gmh | ./target/debug/grassmudhorse +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` diff --git a/print-110.gmh b/print-110.gmh new file mode 100644 index 0000000..785970a --- /dev/null +++ b/print-110.gmh @@ -0,0 +1,16 @@ +草草草泥马 +马草草草泥草草草草泥泥马 +草马草 +泥马草泥 +草草草泥草泥草马 +泥马草草 +草草草泥马 +泥草草草 +草马草 +草草草泥草泥泥马 +泥草草泥 +马泥草草泥草草草泥草泥马 +马草马草泥草草草草泥泥马 +马草草草泥草草草泥草泥马 +草马马 +马马马 diff --git a/src/main.rs b/src/main.rs index 477ed44..90f1827 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,10 @@ mod parser; mod context; mod vm; +use std::{ + env, + fs::read_to_string, +}; use err::*; use parser::*; use context::*; @@ -13,16 +17,45 @@ use vm::*; // https://playsecurity.org/rawfile/grass_mud_horse_language_specification.md // https://p.rogram.me/grassmudhorse.js/grassmudhorse.js fn main() { - let input = "草草草泥马 马草草草泥草草草草泥泥马 草马草 泥马草泥 草草草泥草泥草马 泥马草草 草草草泥马 泥草草草 草马草 草草草泥草泥泥马 泥草草泥 马泥草草泥草草草泥草泥马 马草马草泥草草草草泥泥马 马草草草泥草草草泥草泥马 草马马 马马马"; + let is_help = env::args().any(|a| a == "-h" || a == "--help"); + if is_help { + println!("grassmudhorse.rs v0.1"); + return; + } + let is_debug = env::args().any(|a| a == "-d" || a == "--debug"); - let instructions = match parse_lang(input) { + let arg_file = env::args().filter(|a| !a.starts_with("-")).nth(1); + + let input = match arg_file { + Some(f) => match read_to_string(&f) { + Ok(s) => s, Err(err) => { + println!("[ERROR] Read file {}, error: {}", f, err); + return; + } + }, + None => { + let mut buff = String::new(); + use std::io::Read; + match std::io::stdin().lock().read_to_string(&mut buff) { + Ok(_) => buff, + Err(err) => { + println!("[ERROR] Read stdin, error: {}", err); + return; + } + } + }, + }; + + + + let instructions = match parse_lang(&input) { Ok(i) => i, Err(err) => { println!("Parse error: {}", err); return; }, }; - let mut context = Context::new(false); + let mut context = Context::new(is_debug); if context.is_debug() { println!("[DEBUG] parsed instructions:");