read from file or stdin, add readme

This commit is contained in:
2020-05-08 08:21:19 +08:00
parent 4b6c5119b1
commit 57bc12a692
3 changed files with 89 additions and 4 deletions

View File

@@ -1,3 +1,39 @@
# grassmudhorse.rs
Grass Mud Horse in Rust
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
```

16
print-110.gmh Normal file
View File

@@ -0,0 +1,16 @@
草草草泥马
马草草草泥草草草草泥泥马
草马草
泥马草泥
草草草泥草泥草马
泥马草草
草草草泥马
泥草草草
草马草
草草草泥草泥泥马
泥草草泥
马泥草草泥草草草泥草泥马
马草马草泥草草草草泥泥马
马草草草泥草草草泥草泥马
草马马
马马马

View File

@@ -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:");